Max OS 0.3
Loading...
Searching...
No Matches
console.cpp
Go to the documentation of this file.
1
10
11using namespace MaxOS;
12using namespace MaxOS::common;
13using namespace MaxOS::drivers;
14using namespace MaxOS::drivers::console;
15
16Console::Console() = default;
17
18Console::~Console() = default;
19
26 return 0;
27}
28
35 return 0;
36}
37
47
58
69
78 return ' ';
79}
80
89 return ConsoleColour::Green;
90}
91
100 return ConsoleColour::Green;
101}
102
113
114 // Set the colors of the character
117
118 // Put the character on the console
119 put_character(x, y, c);
120
121}
122
133
134 // Print each character on the screen
135 for(size_t i = 0; i < string.length(); i++)
136 put_character(x + i, y, string[i], foreground, background);
137}
138
143
144 scroll_up(0, 0, width(), height());
145}
146
159
160 // Shift everything but the last line by getting what is below it
161 for(int y = top; y < top + height - 1; y++)
162 for(uint32_t x = left; x < left + width; x++)
163 put_character(x, y, get_character(x, y + 1), get_foreground_color(x, y + 1),
164 get_background_color(x, y + 1));
165
166 // Fill the last line
167 for(uint16_t x = left; x < left + width; x++)
169
170}
171
176
177 clear(0, 0, width(), height());
178
179}
180
193
194 // Check bounds
195 if(left > this->width() || top > this->height() || width > this->width() || height > this->height())
196 return;
197
198 // Fill the area with the character
199 for(uint16_t y = top; y < top + height; y++)
200 for(uint16_t x = left; x < left + width; x++)
202
203}
204
212
213 // Get the colors of the character
216
217 // Set the colors of the character
220}
221
232 : m_console(console),
233 m_left(left),
234 m_top(top),
235 m_width(width),
236 m_height(height) {
237
238}
239
252 : m_console(console),
253 m_left(left),
254 m_top(top),
255 m_width(width),
256 m_height(height) {
257
258 // Store the colours of the console
259 for(uint16_t y = top; y < top + height; y++)
260 for(uint16_t x = left; x < left + width; x++) {
261 console->set_foreground_color(x, y, foreground);
262 console->set_background_color(x, y, background);
263 }
264
265}
266
267ConsoleArea::~ConsoleArea() = default;
268
275 return m_width;
276}
277
284 return m_height;
285}
286
295
296 // Check bounds
297 if(x >= m_width || y >= m_height)
298 return;
299
300 // Write to the console
301 m_console->put_character(m_left + x, m_top + y, c);
302}
303
304
313
314 // Make sure the coordinates are within the console area
315 if(x >= m_width || y >= m_height)
316 return;
317
318 // Set the foreground color of the character
319 m_console->set_foreground_color(m_left + x, m_top + y, foreground);
320
321}
322
331
332 // Make sure the coordinates are within the console area
333 if(x >= m_width || y >= m_height)
334 return;
335
336 // Set the background color of the character
337 m_console->set_background_color(m_left + x, m_top + y, background);
338
339}
340
349
350 // Make sure the coordinates are within the console area
351 if(x >= m_width || y >= m_height)
352 return ' ';
353
354 // Return the character at the given coordinates
355 return m_console->get_character(m_left + x, m_top + y);
356}
357
366
367 // Make sure the coordinates are within the console area
368 if(x >= m_width || y >= m_height)
369 return ConsoleColour::LightGrey;
370
371 // Return the foreground color of the character at the given coordinates
372 return m_console->get_foreground_color(m_left + x, m_top + y);
373
374}
375
384
385 // Make sure the coordinates are within the console area
386 if(x >= m_width || y >= m_height)
387 return ConsoleColour::Black;
388
389 // Return the background color of the character at the given coordinates
390 return m_console->get_background_color(m_left + x, m_top + y);
391}
392
397
398 m_console->scroll_up(m_left, m_top, m_width, m_height);
399}
400
415
416 m_console->scroll_up(m_left + left, m_top + top, width, height, foreground, background, fill);
417
418}
419
425 : m_console(console) {
426
427}
428
429ConsoleStream::~ConsoleStream() = default;
430
437
438 uint16_t spaces = 0;
439
440 // If the character placement is more than the width of the console go on a new line
441 if(m_cursor_x >= m_console->width() && c != '\n') {
442 line_feed();
443 write(" ");
444 }
445
446 // Check if the character is an ANSI escape code
447 if(c == '\033') is_ansi = true;
448
449 // Handle the character
450 switch(c) {
451 // New line
452 case '\n':
453 // Go to the next line, if it is more than what the console can fit then scroll
454 if(++m_cursor_y >= m_console->height()) {
455 m_console->scroll_up();
456
457 // Ensure there is space at the bottom to write
458 m_cursor_y = m_console->height() - 1;
459 }
460
461 // don't break here, we want to go to the next case because of the \r
462 [[fallthrough]];
463
464 // Carriage return
465 case '\r':
466 m_cursor_x = 0;
467 break;
468
469 // End of string
470 case '\0':
471 break;
472
473 // Backspace
474 case '\b':
475 m_cursor_x--;
476 break;
477
478 // Tab
479 case '\t':
480
481 // Pad with spaces until a tab is reached
482 spaces = 8 - (m_cursor_x % 8);
483 for(uint16_t i = 0; i < spaces; i++)
484 write_char(' '); //TODO: Recursive, dont
485 break;
486
487 default:
488 // Put the character on the console
489 m_console->put_character(m_cursor_x, m_cursor_y, c);
490
491 // Increment the x coordinate (ANSI aren't rendered)
492 if(!is_ansi)
493 m_cursor_x++;
494
495 break;
496
497 }
498
499 // Check if the ANSI code is complete
500 if(c == 'm') is_ansi = false;
501
502}
503
511
512 // Set the x and y coordinates
513 m_cursor_x = x;
514 m_cursor_y = y;
515}
void write(string string_to_write) override
Writes a string to the output stream.
virtual void line_feed()
Writes a newline to the output stream.
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
char get_character(uint16_t x, uint16_t y) override
Return the character at the given coordinates if the coordinates are within the console area.
Definition console.cpp:348
ConsoleArea(Console *console, uint16_t left, uint16_t top, uint16_t width, uint16_t height)
Construct a new Console Area object.
Definition console.cpp:231
common::ConsoleColour get_foreground_color(uint16_t x, uint16_t y) override
Return the foreground color of the character at the given coordinates if the coordinates are within t...
Definition console.cpp:365
void set_background_color(uint16_t x, uint16_t y, common::ConsoleColour background) override
Change the background color of a character on the console area if the coordinates are within the area...
Definition console.cpp:330
uint16_t height() override
Return the height of the console area.
Definition console.cpp:283
void scroll_up() override
Scroll the console area up by 1 line.
Definition console.cpp:396
void put_character(uint16_t x, uint16_t y, char c) override
Place a character on the console area if the coordinates are within the area.
Definition console.cpp:294
void set_foreground_color(uint16_t x, uint16_t y, common::ConsoleColour foreground) override
Change the foreground color of a character on the console area if the coordinates are within the area...
Definition console.cpp:312
common::ConsoleColour get_background_color(uint16_t x, uint16_t y) override
Return the background color of the character at the given coordinates if the coordinates are within t...
Definition console.cpp:383
uint16_t width() override
Return the width of the console area.
Definition console.cpp:274
ConsoleStream(Console *)
Construct a new Console Stream object.
Definition console.cpp:424
void write_char(char c) override
write a character to the console stream
Definition console.cpp:436
void set_cursor(uint16_t x, uint16_t y)
Set the m_position of the cursor.
Definition console.cpp:510
Abstract class for a console, allows for the printing of characters and strings with colours.
Definition console.h:24
virtual void set_foreground_color(uint16_t x, uint16_t y, common::ConsoleColour foreground)
Set the foreground color of a character on the console.
Definition console.cpp:55
virtual common::ConsoleColour get_background_color(uint16_t x, uint16_t y)
Get the background color of a character on the console.
Definition console.cpp:99
virtual void scroll_up()
Scroll the entire console up by 1 line.
Definition console.cpp:142
virtual char get_character(uint16_t x, uint16_t y)
Get the character at a given coordinate on the console.
Definition console.cpp:77
virtual uint16_t height()
Get the height of the console in characters.
Definition console.cpp:34
virtual void put_string(uint16_t x, uint16_t y, string s, common::ConsoleColour foreground=common::ConsoleColour::LightGrey, common::ConsoleColour background=common::ConsoleColour::Black)
Put a string on the console.
Definition console.cpp:132
virtual void put_character(uint16_t x, uint16_t y, char c)
Put a character on the console.
Definition console.cpp:45
virtual void invert_colors(uint16_t x, uint16_t y)
Invert the colors of a character on the console.
Definition console.cpp:211
virtual uint16_t width()
Get the width of the console in characters.
Definition console.cpp:25
virtual common::ConsoleColour get_foreground_color(uint16_t x, uint16_t y)
Get the background color of a character on the console.
Definition console.cpp:88
virtual void set_background_color(uint16_t x, uint16_t y, common::ConsoleColour background)
Set the background color of a character on the console.
Definition console.cpp:66
ConsoleColour
Enumeration of console colour codes.
Definition colour.h:67
Defines a Console class for handling text-based console output with colour support.