From 74597b6361d1c64fe5cd4ed36909668ae0e52c40 Mon Sep 17 00:00:00 2001 From: night0721 Date: Sat, 21 Sep 2024 22:49:29 +0100 Subject: [PATCH] Make color render more robust --- src/client/client.c | 1 - src/client/ui.c | 31 ++++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/client/client.c b/src/client/client.c index 8b71662..457f581 100644 --- a/src/client/client.c +++ b/src/client/client.c @@ -108,7 +108,6 @@ void *receive_worker(void *arg) nonce, shared_key) != 0) { free(decrypted); error(0, "Cannot decrypt data"); - return NULL; } else { /* Terminate decrypted data so we don't print random bytes */ decrypted[data_len] = '\0'; diff --git a/src/client/ui.c b/src/client/ui.c index 6d54e24..a4fabf0 100644 --- a/src/client/ui.c +++ b/src/client/ui.c @@ -173,10 +173,8 @@ void highlight_current_line() long range = users->length; /* not highlight if no files in directory */ if (range == 0 && errno == 0) { - #if DRAW_PREVIEW - wprintw(chat_content, "No users. Start a converstation."); - wrefresh(chat_content); - #endif + wprintw(chat_content, "No users. Start a converstation."); + wrefresh(chat_content); return; } @@ -278,6 +276,7 @@ void print_message(int flag, message_t *msg, int user_color) int i = 0; int n = strlen(msg->content); int in_bold = 0, in_italic = 0, in_underline = 0, in_block = 0; + int last_active_color = -1; while (i < n) { /* Bold */ @@ -382,11 +381,22 @@ void print_message(int flag, message_t *msg, int user_color) /* Handle color codes \1 to \8 */ if (msg->content[i] >= '1' && msg->content[i] <= '8') { /* Convert char to int */ - int color_pair = msg->content[i] - '0'; - wattron(chat_content, COLOR_PAIR(color_pair)); - /* Skip the color code character */ - i++; - + int new_color = msg->content[i] - '0'; + if (new_color == last_active_color) { + /* Turn off current color */ + wattroff(chat_content, COLOR_PAIR(last_active_color)); + /* Reset last active color */ + last_active_color = -1; + } else { + if (last_active_color != -1) { + /* Turn off previous color */ + wattroff(chat_content, COLOR_PAIR(last_active_color)); + } + last_active_color = new_color; + /* Turn on new color */ + wattron(chat_content, COLOR_PAIR(new_color)); + } + i++; /* Handle new line */ } else if (msg->content[i] == 'n') { waddch(chat_content, '\n'); @@ -410,6 +420,9 @@ void print_message(int flag, message_t *msg, int user_color) wattroff(chat_content, A_ITALIC); wattroff(chat_content, A_UNDERLINE); wattroff(chat_content, A_STANDOUT); + for (int i = 1; i < 8; i++) { + wattroff(chat_content, COLOR_PAIR(i)); + } } /*