From ea1e1cee821745c226fa19e52fad068c389309cd Mon Sep 17 00:00:00 2001 From: Shylie Date: Tue, 19 Mar 2024 14:59:05 -0400 Subject: [PATCH] Fix windows build and issue with printing codepoints that don't move the cursor --- source/terml.cpp | 41 +++++++++++++++++++++++++---------------- source/terml_linux.cpp | 3 +++ source/terml_private.h | 3 --- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/source/terml.cpp b/source/terml.cpp index ad420c3..8a7d99a 100644 --- a/source/terml.cpp +++ b/source/terml.cpp @@ -26,17 +26,8 @@ namespace constexpr int THREE_BYTE_FILL = 0b111000001000000010000000; constexpr int FOUR_BYTE_FILL = 0b11110000100000001000000010000000; - void print_cell(tcell cell, const tcell* last) + void print_cell(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 if (cell.codepoint < 0x80) { @@ -77,6 +68,20 @@ namespace printf("%s", str); } } + + 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(cell); + } } terml::terml() : @@ -109,14 +114,17 @@ void terml::set(unsigned int x, unsigned int y, tcell cell) void terml::flush() const { - printf(CUP(1, 1)); - fflush(stdout); - - print_cell(cells[0], nullptr); + printf(CUP(1, 0)); + print_cell(cells[0]); 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, x + 1); + print_cell(cells[i], cells[i - 1]); } + fflush(stdout); } @@ -242,8 +250,9 @@ void terml::setup_buffer() void terml::set_console_settings() { setvbuf(stdout, nullptr, _IOFBF, BUFSIZ * BUFSIZ); - printf(ALT_BUF() HIDE_CURSOR() SELECT_UTF8()); + printf(ALT_BUF() HIDE_CURSOR()); fflush(stdout); + set_console_settings_impl(); } diff --git a/source/terml_linux.cpp b/source/terml_linux.cpp index 4776392..db295af 100644 --- a/source/terml_linux.cpp +++ b/source/terml_linux.cpp @@ -16,6 +16,9 @@ void terml_linux::set_console_settings_impl() t.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &t); + + printf(SELECT_UTF8()); + fflush(stdout); } void terml_linux::reset_console_settings_impl() diff --git a/source/terml_private.h b/source/terml_private.h index 3e8fbc6..e402dd1 100644 --- a/source/terml_private.h +++ b/source/terml_private.h @@ -26,9 +26,6 @@ #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 FGP CSI "38;2;%d;%d;%dm" -#define BGP CSI "48;2;%d;%d;%dm" - class terml { public: