Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::console::ConsoleStream Class Reference

A stream that can be used to write to a console. More...

#include <console.h>

Inheritance diagram for MaxOS::console::ConsoleStream:

Public Member Functions

 ConsoleStream (Console *)
 Construct a new Console Stream object.
 
void write_char (char c) override
 write a character to the console stream
 
void set_cursor (uint16_t x, uint16_t y)
 Set the m_position of the cursor.
 

Detailed Description

A stream that can be used to write to a console.

Definition at line 92 of file console.h.

Constructor & Destructor Documentation

◆ ConsoleStream()

ConsoleStream::ConsoleStream ( Console *  console)
explicit

Construct a new Console Stream object.

Parameters
consoleThe console to create the stream on

Definition at line 423 of file console.cpp.

424 : m_console(console) {
425
426}

Member Function Documentation

◆ set_cursor()

void ConsoleStream::set_cursor ( uint16_t  x,
uint16_t  y 
)

Set the m_position of the cursor.

Parameters
xThe x coordinate of the cursor
yThe y coordinate of the cursor

Definition at line 509 of file console.cpp.

509 {
510
511 // Set the x and y coordinates
512 m_cursor_x = x;
513 m_cursor_y = y;
514}

Referenced by MaxOS::console::FramebufferConsole::finish().

◆ write_char()

void ConsoleStream::write_char ( char  c)
override

write a character to the console stream

Parameters
cThe character to write

Definition at line 435 of file console.cpp.

435 {
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}
void write_char(char c) override
write a character to the console stream
Definition console.cpp:435
virtual void scroll_up()
Scroll the entire console up by 1 line.
Definition console.cpp:141
virtual uint16_t height()
Get the height of the console in characters.
Definition console.cpp:33
virtual void put_character(uint16_t x, uint16_t y, char c)
Put a character on the console.
Definition console.cpp:44
virtual uint16_t width()
Get the width of the console in characters.
Definition console.cpp:24

References MaxOS::console::Console::height(), MaxOS::console::Console::put_character(), MaxOS::console::Console::scroll_up(), MaxOS::console::Console::width(), and write_char().

Referenced by write_char().


The documentation for this class was generated from the following files: