From 7e11c02405e66da97dfaa38f3da880e3c54ebb58 Mon Sep 17 00:00:00 2001 From: cowmonk Date: Tue, 21 Jan 2025 13:39:21 -0700 Subject: [PATCH] Better handling of ansi escape code sequences for clearing Handling of ESC[0m especially --- cowterm.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/cowterm.c b/cowterm.c index 50b2e1a..42e65b2 100644 --- a/cowterm.c +++ b/cowterm.c @@ -920,6 +920,9 @@ static void parse_ansi_code(const char *buf, int *idx, int max_len, break; case 'J': // Clear screen if (params[0] == 2) { + current_color = XWhitePixel(display, DefaultScreen(display)); + current_bg_color = XBlackPixel(display, DefaultScreen(display)); + current_attr = 0; for (int y = 0; y < term_rows; y++) { memset(terminal_buffer[y], ' ', term_cols); for (int x = 0; x < term_cols; x++) { @@ -976,11 +979,25 @@ static void parse_ansi_code(const char *buf, int *idx, int max_len, cursor_y = 0; break; case 'm': // Handling of text - for (int i = 0; i < param_idx; i++) { + if (param_idx == 0 || (param_idx == 1 && params[0] == 0)) { // Handle ESC[0m + current_color = XWhitePixel(display, DefaultScreen(display)); + current_bg_color = XBlackPixel(display, DefaultScreen(display)); + current_attr = 0; + needs_refresh = 1; + break; + } + for (int i = 0; i < (int)param_idx; i++) { if (params[i] == 0) { // Reset Everything current_color = XWhitePixel(display, DefaultScreen(display)); current_bg_color = XBlackPixel(display, DefaultScreen(display)); current_attr = 0; + if (cursor_y >= 0 && cursor_y < term_rows) { + for (int x = cursor_x; x < term_cols; x++) { + color_buffer[cursor_y][x] = current_color; + bg_color_buffer[cursor_y][x] = current_bg_color; + attr_buffer[cursor_y][x] = current_attr; + } + } } else if (params[i] == 1) { // Apply ATTRS current_attr |= ATTR_BOLD; } else if (params[i] == 2) { @@ -1001,8 +1018,12 @@ static void parse_ansi_code(const char *buf, int *idx, int max_len, current_attr &= ~ATTR_REVERSE; } else if (params[i] >= 30 && params[i] <= 37) { // Foreground colors current_color = get_ansi_color(params[i] - 30); + } else if (params[i] == 39) { // Reset foreground color + current_color = XWhitePixel(display, DefaultScreen(display)); } else if (params[i] >= 40 && params[i] <= 47) { // Background colors current_bg_color = get_ansi_color(params[i] - 40); + } else if (params[i] == 49) { // Reset background color + current_bg_color = XBlackPixel(display, DefaultScreen(display)); } else if (params[i] >= 90 && params[i] <= 97) { // Bright foreground current_color = get_bright_ansi_color(params[i] - 90); } else if (params[i] >= 100 && params[i] <= 107) { // Bright background