This commit is contained in:
Shylie 2024-04-14 17:03:56 -04:00
commit 7dd2afe186
3 changed files with 35 additions and 17 deletions

View File

@ -26,17 +26,8 @@ namespace
constexpr int THREE_BYTE_FILL = 0b111000001000000010000000; constexpr int THREE_BYTE_FILL = 0b111000001000000010000000;
constexpr int FOUR_BYTE_FILL = 0b11110000100000001000000010000000; constexpr int FOUR_BYTE_FILL = 0b11110000100000001000000010000000;
void print_cell(tcell cell, const tcell* last) void print_cell_impl(tcell cell)
{ {
if (!last || cell.foreground != last->foreground)
{
printf(FGP, (cell.foreground & 0xFF0000) >> 16, (cell.foreground & 0xFF00) >> 8, cell.foreground & 0xFF);
}
if (!last || cell.background != last->background)
{
printf(BGP, (cell.background & 0xFF0000) >> 16, (cell.background & 0xFF00) >> 8, cell.background & 0xFF);
}
// one-byte codepoints // one-byte codepoints
if (cell.codepoint < 0x80) if (cell.codepoint < 0x80)
{ {
@ -77,6 +68,28 @@ namespace
printf("%s", str); printf("%s", str);
} }
} }
void print_cell(tcell cell)
{
printf(FG(%d, %d, %d), (cell.foreground & 0xFF0000) >> 16, (cell.foreground & 0xFF00) >> 8, cell.foreground & 0xFF);
printf(BG(%d, %d, %d), (cell.background & 0xFF0000) >> 16, (cell.background & 0xFF00) >> 8, cell.background & 0xFF);
print_cell_impl(cell);
}
void print_cell(tcell cell, tcell last)
{
if (cell.foreground != last.foreground)
{
printf(FG(%d, %d, %d), (cell.foreground & 0xFF0000) >> 16, (cell.foreground & 0xFF00) >> 8, cell.foreground & 0xFF);
}
if (cell.background != last.background)
{
printf(BG(%d, %d, %d), (cell.background & 0xFF0000) >> 16, (cell.background & 0xFF00) >> 8, cell.background & 0xFF);
}
print_cell_impl(cell);
}
} }
terml::terml() : terml::terml() :
@ -110,13 +123,16 @@ void terml::set(unsigned int x, unsigned int y, tcell cell)
void terml::flush() const void terml::flush() const
{ {
printf(CUP(1, 1)); printf(CUP(1, 1));
fflush(stdout); print_cell(cells[0]);
print_cell(cells[0], nullptr);
for (int i = 1; i < width * height; i++) for (int i = 1; i < width * height; i++)
{ {
print_cell(cells[i], &cells[i - 1]); const unsigned int x = i % width;
const unsigned int y = i / width;
printf(CUP(%d, %d), y + 1, x + 1);
print_cell(cells[i], cells[i - 1]);
} }
fflush(stdout); fflush(stdout);
} }
@ -244,6 +260,7 @@ void terml::set_console_settings()
setvbuf(stdout, nullptr, _IOFBF, BUFSIZ * BUFSIZ); setvbuf(stdout, nullptr, _IOFBF, BUFSIZ * BUFSIZ);
printf(ALT_BUF() HIDE_CURSOR()); printf(ALT_BUF() HIDE_CURSOR());
fflush(stdout); fflush(stdout);
set_console_settings_impl(); set_console_settings_impl();
} }

View File

@ -16,6 +16,9 @@ void terml_linux::set_console_settings_impl()
t.c_lflag &= ~(ICANON | ECHO); t.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &t); tcsetattr(STDIN_FILENO, TCSANOW, &t);
printf(SELECT_UTF8());
fflush(stdout);
} }
void terml_linux::reset_console_settings_impl() void terml_linux::reset_console_settings_impl()

View File

@ -16,6 +16,7 @@
#define HIDE_CURSOR() CSI "?25l" #define HIDE_CURSOR() CSI "?25l"
#define SHOW_CURSOR() CSI "?25h" #define SHOW_CURSOR() CSI "?25h"
#define SELECT_UTF8() CSI "%G"
#define REPORT_CUSROR_POSITION() CSI "6n" #define REPORT_CUSROR_POSITION() CSI "6n"
#define CURSOR_POSITION_FORMAT() "%*1s[%u;%u" #define CURSOR_POSITION_FORMAT() "%*1s[%u;%u"
@ -25,9 +26,6 @@
#define FG(r, g, b) CSI "38;2;" STRINGIFY(r) ";" STRINGIFY(g) ";" STRINGIFY(b) "m" #define FG(r, g, b) CSI "38;2;" STRINGIFY(r) ";" STRINGIFY(g) ";" STRINGIFY(b) "m"
#define BG(r, g, b) CSI "48;2;" STRINGIFY(r) ";" STRINGIFY(g) ";" STRINGIFY(b) "m" #define BG(r, g, b) CSI "48;2;" STRINGIFY(r) ";" STRINGIFY(g) ";" STRINGIFY(b) "m"
#define FGP CSI "38;2;%d;%d;%dm"
#define BGP CSI "48;2;%d;%d;%dm"
class terml class terml
{ {
public: public: