diff options
-rw-r--r-- | src/gui/curses/gui-curses-chat.c | 106 | ||||
-rw-r--r-- | src/gui/gui-chat.c | 18 | ||||
-rw-r--r-- | src/gui/gui-chat.h | 3 | ||||
-rw-r--r-- | tests/unit/gui/test-gui-chat.cpp | 53 |
4 files changed, 92 insertions, 88 deletions
diff --git a/src/gui/curses/gui-curses-chat.c b/src/gui/curses/gui-curses-chat.c index 915009d67..b4fa4cc92 100644 --- a/src/gui/curses/gui-curses-chat.c +++ b/src/gui/curses/gui-curses-chat.c @@ -1385,7 +1385,7 @@ gui_chat_display_line (struct t_gui_window *window, struct t_gui_line *line, int num_lines, x, y, pre_lines_displayed, lines_displayed, line_align; int read_marker_x, read_marker_y; int word_start_offset, word_end_offset; - int word_length_with_spaces, word_length, word_is_newlines; + int word_length_with_spaces, word_length; char *message_with_tags, *message_with_search; const char *ptr_data, *ptr_end_offset, *ptr_style, *next_char; struct t_gui_line *ptr_prev_line, *ptr_next_line; @@ -1521,11 +1521,24 @@ gui_chat_display_line (struct t_gui_window *window, struct t_gui_line *line, { while (ptr_data && ptr_data[0]) { + if (ptr_data[0] == '\n') + { + gui_chat_display_new_line (window, num_lines, count, + &lines_displayed, simulate); + ptr_data++; + gui_chat_display_prefix_suffix(window, line, + ptr_data, + pre_lines_displayed, &lines_displayed, + simulate, + CONFIG_BOOLEAN(config_look_color_inactive_message), + 0); + } + gui_chat_get_word_info (window, ptr_data, &word_start_offset, &word_end_offset, - &word_length_with_spaces, &word_length, &word_is_newlines); + &word_length_with_spaces, &word_length); ptr_end_offset = ptr_data + word_end_offset; @@ -1538,69 +1551,44 @@ gui_chat_display_line (struct t_gui_window *window, struct t_gui_line *line, if (word_length >= 0) { - if (word_is_newlines) + line_align = gui_line_get_align (window->buffer, line, 1, + (lines_displayed == 0) ? 1 : 0); + if ((window->win_chat_cursor_x + word_length_with_spaces > gui_chat_get_real_width (window)) + && (word_length <= gui_chat_get_real_width (window) - line_align)) { - /* jump to start of word */ - ptr_data += word_start_offset; - - while (ptr_data && ptr_data[0] == '\n') + /* spaces + word too long for current line but OK for next line */ + gui_chat_display_new_line (window, num_lines, count, + &lines_displayed, simulate); + /* apply styles before jumping to start of word */ + if (!simulate && (word_start_offset > 0)) { - gui_chat_display_new_line (window, num_lines, count, - &lines_displayed, simulate); - ptr_data++; - gui_chat_display_prefix_suffix(window, line, - ptr_data, - pre_lines_displayed, &lines_displayed, - simulate, - CONFIG_BOOLEAN(config_look_color_inactive_message), - 0); - } - - if (!ptr_data[0]) - gui_chat_display_new_line (window, num_lines, count, - &lines_displayed, simulate); - } - else - { - line_align = gui_line_get_align (window->buffer, line, 1, - (lines_displayed == 0) ? 1 : 0); - if ((window->win_chat_cursor_x + word_length_with_spaces > gui_chat_get_real_width (window)) - && (word_length <= gui_chat_get_real_width (window) - line_align)) - { - /* spaces + word too long for current line but OK for next line */ - gui_chat_display_new_line (window, num_lines, count, - &lines_displayed, simulate); - /* apply styles before jumping to start of word */ - if (!simulate && (word_start_offset > 0)) + ptr_style = ptr_data; + while (ptr_style < ptr_data + word_start_offset) { - ptr_style = ptr_data; - while (ptr_style < ptr_data + word_start_offset) - { - /* loop until no style/char available */ - ptr_style = gui_chat_string_next_char (window, line, - (unsigned char *)ptr_style, - 1, - CONFIG_BOOLEAN(config_look_color_inactive_message), - 0); - if (!ptr_style) - break; - ptr_style = utf8_next_char (ptr_style); - } + /* loop until no style/char available */ + ptr_style = gui_chat_string_next_char (window, line, + (unsigned char *)ptr_style, + 1, + CONFIG_BOOLEAN(config_look_color_inactive_message), + 0); + if (!ptr_style) + break; + ptr_style = utf8_next_char (ptr_style); } - /* jump to start of word */ - ptr_data += word_start_offset; } - - /* display word */ - gui_chat_display_word (window, line, ptr_data, - ptr_end_offset + 1, - 0, num_lines, count, - pre_lines_displayed, &lines_displayed, - simulate, - CONFIG_BOOLEAN(config_look_color_inactive_message), - 0); + /* jump to start of word */ + ptr_data += word_start_offset; } + /* display word */ + gui_chat_display_word (window, line, ptr_data, + ptr_end_offset + 1, + 0, num_lines, count, + pre_lines_displayed, &lines_displayed, + simulate, + CONFIG_BOOLEAN(config_look_color_inactive_message), + 0); + if ((!simulate) && (window->win_chat_cursor_y >= window->win_chat_height)) ptr_data = NULL; else @@ -1610,7 +1598,7 @@ gui_chat_display_line (struct t_gui_window *window, struct t_gui_line *line, if (*(ptr_data - 1) == '\0') ptr_data = NULL; - if (window->win_chat_cursor_x == 0 && !word_is_newlines) + if (window->win_chat_cursor_x == 0) { while (ptr_data && (ptr_data[0] == ' ')) { diff --git a/src/gui/gui-chat.c b/src/gui/gui-chat.c index 30af928da..59c1e339f 100644 --- a/src/gui/gui-chat.c +++ b/src/gui/gui-chat.c @@ -304,6 +304,8 @@ gui_chat_string_pos (const char *string, int real_pos) /* * Returns info about next word: beginning, end, length. * + * Stops before the first newline character, even if no characters or only spaces and color codes precede it. + * * Note: the word_{start|end}_offset are in bytes, but word_length(_with_spaces) * are in number of chars on screen. */ @@ -312,8 +314,7 @@ void gui_chat_get_word_info (struct t_gui_window *window, const char *data, int *word_start_offset, int *word_end_offset, - int *word_length_with_spaces, int *word_length, - int *word_is_newlines) + int *word_length_with_spaces, int *word_length) { const char *start_data, *next_char, *next_char2; int leading_spaces, char_size_screen; @@ -322,7 +323,6 @@ gui_chat_get_word_info (struct t_gui_window *window, *word_end_offset = 0; *word_length_with_spaces = 0; *word_length = -1; - *word_is_newlines = 0; start_data = data; @@ -336,11 +336,15 @@ gui_chat_get_word_info (struct t_gui_window *window, next_char2 = utf8_next_char (next_char); if (next_char2) { - if (next_char[0] != ' ' && - (leading_spaces || (next_char[0] == '\n') == *word_is_newlines)) + if (next_char[0] == '\n') + { + *word_end_offset = next_char - start_data - 1; + if (*word_length < 0) + *word_length = 0; + return; + } + else if (next_char[0] != ' ') { - if (next_char[0] == '\n') - *word_is_newlines = 1; if (leading_spaces) *word_start_offset = next_char - start_data; leading_spaces = 0; diff --git a/src/gui/gui-chat.h b/src/gui/gui-chat.h index 28c222a69..0971b93f9 100644 --- a/src/gui/gui-chat.h +++ b/src/gui/gui-chat.h @@ -83,8 +83,7 @@ extern void gui_chat_get_word_info (struct t_gui_window *window, const char *data, int *word_start_offset, int *word_end_offset, int *word_length_with_spaces, - int *word_length, - int *word_is_newlines); + int *word_length); extern char *gui_chat_get_time_string (time_t date); extern int gui_chat_get_time_length (); extern void gui_chat_change_time_format (); diff --git a/tests/unit/gui/test-gui-chat.cpp b/tests/unit/gui/test-gui-chat.cpp index 99152886b..a9a46569c 100644 --- a/tests/unit/gui/test-gui-chat.cpp +++ b/tests/unit/gui/test-gui-chat.cpp @@ -34,23 +34,19 @@ extern "C" #define WEE_GET_WORD_INFO(__result_word_start_offset, \ __result_word_end_offset, \ __result_word_length_with_spaces, \ - __result_word_length, \ - __result_word_is_newlines, __string) \ + __result_word_length, __string) \ word_start_offset = -2; \ word_end_offset = -2; \ word_length_with_spaces = -2; \ word_length = -2; \ - word_is_newlines = -2; \ gui_chat_get_word_info (gui_windows, __string, \ &word_start_offset, &word_end_offset, \ - &word_length_with_spaces, &word_length, \ - &word_is_newlines); \ + &word_length_with_spaces, &word_length); \ LONGS_EQUAL(__result_word_start_offset, word_start_offset); \ LONGS_EQUAL(__result_word_end_offset, word_end_offset); \ LONGS_EQUAL(__result_word_length_with_spaces, \ word_length_with_spaces); \ - LONGS_EQUAL(__result_word_length, word_length); \ - LONGS_EQUAL(__result_word_is_newlines, word_is_newlines); + LONGS_EQUAL(__result_word_length, word_length); TEST_GROUP(GuiChat) { @@ -338,19 +334,36 @@ TEST(GuiChat, StringPos) TEST(GuiChat, GetWordInfo) { int word_start_offset, word_end_offset, word_length_with_spaces; - int word_length, word_is_newlines; - - WEE_GET_WORD_INFO (0, 0, 0, -1, 0, NULL); - WEE_GET_WORD_INFO (0, 0, 0, -1, 0, ""); - WEE_GET_WORD_INFO (0, 0, 1, 1, 0, "a"); - WEE_GET_WORD_INFO (0, 2, 3, 3, 0, "abc"); - WEE_GET_WORD_INFO (2, 4, 5, 3, 0, " abc"); - WEE_GET_WORD_INFO (2, 4, 5, 3, 0, " abc "); - WEE_GET_WORD_INFO (0, 4, 5, 5, 0, "first second"); - WEE_GET_WORD_INFO (1, 5, 6, 5, 0, " first second"); - WEE_GET_WORD_INFO (0, 0, 1, 1, 1, "\nabc"); - WEE_GET_WORD_INFO (2, 2, 3, 1, 1, " \nabc"); - WEE_GET_WORD_INFO (2, 3, 4, 2, 1, " \n\nabc"); + int word_length; + char string[32]; + + WEE_GET_WORD_INFO (0, 0, 0, -1, NULL); + WEE_GET_WORD_INFO (0, 0, 0, -1, ""); + WEE_GET_WORD_INFO (0, 0, 1, 1, "a"); + WEE_GET_WORD_INFO (0, 2, 3, 3, "abc"); + WEE_GET_WORD_INFO (2, 4, 5, 3, " abc"); + WEE_GET_WORD_INFO (2, 4, 5, 3, " abc "); + WEE_GET_WORD_INFO (0, 4, 5, 5, "first second"); + WEE_GET_WORD_INFO (1, 5, 6, 5, " first second"); + + WEE_GET_WORD_INFO (0, -1, 0, 0, "\nabc"); + WEE_GET_WORD_INFO (0, 0, 1, 0, " \nabc"); + WEE_GET_WORD_INFO (0, 1, 2, 0, " \nabc"); + WEE_GET_WORD_INFO (0, 4, 5, 5, "first\nsecond"); + + snprintf (string, sizeof (string), "%c%c01abc", GUI_COLOR_COLOR_CHAR, GUI_COLOR_FG_CHAR); + WEE_GET_WORD_INFO (4, 6, 3, 3, string); + snprintf (string, sizeof (string), "abc%c%c01", GUI_COLOR_COLOR_CHAR, GUI_COLOR_FG_CHAR); + WEE_GET_WORD_INFO (0, 6, 3, 3, string); + snprintf (string, sizeof (string), " %c%c01 abc", GUI_COLOR_COLOR_CHAR, GUI_COLOR_FG_CHAR); + WEE_GET_WORD_INFO (6, 8, 5, 3, string); + + snprintf (string, sizeof (string), "\n%c%c01abc", GUI_COLOR_COLOR_CHAR, GUI_COLOR_FG_CHAR); + WEE_GET_WORD_INFO (0, -1, 0, 0, string); + snprintf (string, sizeof (string), "%c%c01\nabc", GUI_COLOR_COLOR_CHAR, GUI_COLOR_FG_CHAR); + WEE_GET_WORD_INFO (0, 3, 0, 0, string); + snprintf (string, sizeof (string), " %c%c01 \nabc", GUI_COLOR_COLOR_CHAR, GUI_COLOR_FG_CHAR); + WEE_GET_WORD_INFO (0, 5, 2, 0, string); } /* |