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