From 520f6c662534355c0a496f2323e50dd30b21be8a Mon Sep 17 00:00:00 2001 From: CPunch Date: Sat, 9 Mar 2024 16:23:41 -0600 Subject: [PATCH 1/5] linux: set console codepage to UTF8 --- source/terml.cpp | 1 + source/terml_private.h | 1 + 2 files changed, 2 insertions(+) diff --git a/source/terml.cpp b/source/terml.cpp index 5dffd2e..772476b 100644 --- a/source/terml.cpp +++ b/source/terml.cpp @@ -243,6 +243,7 @@ void terml::set_console_settings() { setvbuf(stdout, nullptr, _IOFBF, BUFSIZ * BUFSIZ); printf(ALT_BUF() HIDE_CURSOR()); + printf(SELECT_UTF8()); fflush(stdout); set_console_settings_impl(); } diff --git a/source/terml_private.h b/source/terml_private.h index 679ab0c..3e8fbc6 100644 --- a/source/terml_private.h +++ b/source/terml_private.h @@ -16,6 +16,7 @@ #define HIDE_CURSOR() CSI "?25l" #define SHOW_CURSOR() CSI "?25h" +#define SELECT_UTF8() CSI "%G" #define REPORT_CUSROR_POSITION() CSI "6n" #define CURSOR_POSITION_FORMAT() "%*1s[%u;%u" From 17b275b09827cbc95936c5700c8745c02a898783 Mon Sep 17 00:00:00 2001 From: CPunch Date: Sat, 9 Mar 2024 16:27:43 -0600 Subject: [PATCH 2/5] moved SELECT_UTF8() to the same line --- source/terml.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/terml.cpp b/source/terml.cpp index 772476b..ad420c3 100644 --- a/source/terml.cpp +++ b/source/terml.cpp @@ -242,8 +242,7 @@ void terml::setup_buffer() void terml::set_console_settings() { setvbuf(stdout, nullptr, _IOFBF, BUFSIZ * BUFSIZ); - printf(ALT_BUF() HIDE_CURSOR()); - printf(SELECT_UTF8()); + printf(ALT_BUF() HIDE_CURSOR() SELECT_UTF8()); fflush(stdout); set_console_settings_impl(); } From ea1e1cee821745c226fa19e52fad068c389309cd Mon Sep 17 00:00:00 2001 From: Shylie Date: Tue, 19 Mar 2024 14:59:05 -0400 Subject: [PATCH 3/5] 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: From a66ff4e82bfcfbcce57cdf5df2f10bb63571435b Mon Sep 17 00:00:00 2001 From: Shylie Date: Tue, 19 Mar 2024 16:02:39 -0400 Subject: [PATCH 4/5] Fix color change between last cell and first cell; Fix cursor positions --- source/terml.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/source/terml.cpp b/source/terml.cpp index 8a7d99a..10e06ce 100644 --- a/source/terml.cpp +++ b/source/terml.cpp @@ -26,7 +26,7 @@ namespace constexpr int THREE_BYTE_FILL = 0b111000001000000010000000; constexpr int FOUR_BYTE_FILL = 0b11110000100000001000000010000000; - void print_cell(tcell cell) + void print_cell_impl(tcell cell) { // one-byte codepoints if (cell.codepoint < 0x80) @@ -69,18 +69,26 @@ namespace } } + 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); + 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); + printf(BG(% d, % d, % d), (cell.background & 0xFF0000) >> 16, (cell.background & 0xFF00) >> 8, cell.background & 0xFF); } - print_cell(cell); + print_cell_impl(cell); } } @@ -114,14 +122,14 @@ void terml::set(unsigned int x, unsigned int y, tcell cell) void terml::flush() const { - printf(CUP(1, 0)); + printf(CUP(1, 1)); print_cell(cells[0]); for (int i = 1; i < width * height; i++) { const unsigned int x = i % width; const unsigned int y = i / width; - printf(CUP(%d, %d), y, x + 1); + printf(CUP(%d, %d), y + 1, x + 1); print_cell(cells[i], cells[i - 1]); } From 8abd908497105c8c8b366a30225ab994ae23db7d Mon Sep 17 00:00:00 2001 From: Shylie Date: Tue, 19 Mar 2024 16:03:08 -0400 Subject: [PATCH 5/5] Fix incorrect auto-format --- source/terml.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/terml.cpp b/source/terml.cpp index 10e06ce..b644d68 100644 --- a/source/terml.cpp +++ b/source/terml.cpp @@ -81,11 +81,11 @@ namespace { if (cell.foreground != last.foreground) { - printf(FG(% d, % d, % d), (cell.foreground & 0xFF0000) >> 16, (cell.foreground & 0xFF00) >> 8, cell.foreground & 0xFF); + 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); + printf(BG(%d, %d, %d), (cell.background & 0xFF0000) >> 16, (cell.background & 0xFF00) >> 8, cell.background & 0xFF); } print_cell_impl(cell);