From 608f56020d2c4faeb1e32b637e9a9c4e96008fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sun, 30 Jan 2022 13:33:21 +0100 Subject: [PATCH] core: search in message tags when tags are displayed with /debug tags --- ChangeLog.adoc | 1 + src/gui/curses/gui-curses-chat.c | 34 +++++++++++----------- src/gui/gui-line.c | 48 +++++++++++++++++++++++++++----- src/gui/gui-line.h | 3 +- tests/unit/gui/test-gui-line.cpp | 39 ++++++++++++++++++++------ 5 files changed, 92 insertions(+), 33 deletions(-) diff --git a/ChangeLog.adoc b/ChangeLog.adoc index 04b955873..08f326a42 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -20,6 +20,7 @@ https://weechat.org/files/releasenotes/ReleaseNotes-devel.html[release notes] New features:: + * core: search in message tags when tags are displayed with /debug tags * core: add support of date and tags in messages displayed in buffers with free content, add function printf_y_date_tags (issue #1746) * irc: add IRC message tags in messages displayed (issue #1680) * relay: add `zstd` (https://facebook.github.io/zstd/[Zstandard]) compression in weechat protocol, remove option `compression` from `init` command, rename option relay.network.compression_level to relay.network.compression diff --git a/src/gui/curses/gui-curses-chat.c b/src/gui/curses/gui-curses-chat.c index d05204ab1..a7776e491 100644 --- a/src/gui/curses/gui-curses-chat.c +++ b/src/gui/curses/gui-curses-chat.c @@ -1423,7 +1423,8 @@ gui_chat_display_line (struct t_gui_window *window, struct t_gui_line *line, message_with_tags = (gui_chat_display_tags) ? gui_line_build_string_message_tags (line->data->message, line->data->tags_count, - line->data->tags_array) : NULL; + line->data->tags_array, + 1) : NULL; ptr_data = (message_with_tags) ? message_with_tags : line->data->message; message_with_search = NULL; @@ -1641,10 +1642,10 @@ void gui_chat_display_line_y (struct t_gui_window *window, struct t_gui_line *line, int y) { - char *ptr_data, *message_with_search, *message_with_tags; + char *ptr_data, *message_with_tags, *message_with_search; - message_with_search = NULL; message_with_tags = NULL; + message_with_search = NULL; /* reset color & style for a new line */ gui_chat_reset_style (window, line, 0, 1, @@ -1660,6 +1661,18 @@ gui_chat_display_line_y (struct t_gui_window *window, struct t_gui_line *line, ptr_data = line->data->message; + /* add tags if debug of tags is enabled */ + if (gui_chat_display_tags) + { + message_with_tags = gui_line_build_string_message_tags ( + ptr_data, + line->data->tags_count, + line->data->tags_array, + 1); + if (message_with_tags) + ptr_data = message_with_tags; + } + /* emphasize text (if searching text) */ if ((window->buffer->text_search != GUI_TEXT_SEARCH_DISABLED) && (window->buffer->text_search_where & GUI_TEXT_SEARCH_IN_MESSAGE) @@ -1674,17 +1687,6 @@ gui_chat_display_line_y (struct t_gui_window *window, struct t_gui_line *line, ptr_data = message_with_search; } - /* add tags if debug of tags is enabled */ - if (gui_chat_display_tags) - { - message_with_tags = gui_line_build_string_message_tags ( - ptr_data, - line->data->tags_count, - line->data->tags_array); - if (message_with_tags) - ptr_data = message_with_tags; - } - /* display the line */ if (gui_chat_display_word_raw (window, line, ptr_data, window->win_chat_width, 0, @@ -1694,10 +1696,10 @@ gui_chat_display_line_y (struct t_gui_window *window, struct t_gui_line *line, gui_window_clrtoeol (GUI_WINDOW_OBJECTS(window)->win_chat); } - if (message_with_search) - free (message_with_search); if (message_with_tags) free (message_with_tags); + if (message_with_search) + free (message_with_search); } /* diff --git a/src/gui/gui-line.c b/src/gui/gui-line.c index 3dafe90eb..cc91bed06 100644 --- a/src/gui/gui-line.c +++ b/src/gui/gui-line.c @@ -403,15 +403,21 @@ gui_line_build_string_prefix_message (const char *prefix, const char *message) /* * Builds a string with message and tags. * + * If colors == 1, keep colors in message and use color for delimiters around + * tags. + * If colors == 0, strip colors from message and do not use color for + * delimiters around tags. + * * Note: result must be freed after use. */ char * gui_line_build_string_message_tags (const char *message, - int tags_count, char **tags_array) + int tags_count, char **tags_array, + int colors) { int i; - char **string, *result; + char **string, *message_no_colors, *result; if ((tags_count < 0) || ((tags_count > 0) && !tags_array)) return NULL; @@ -421,17 +427,34 @@ gui_line_build_string_message_tags (const char *message, return NULL; if (message) - string_dyn_concat (string, message, -1); - string_dyn_concat (string, GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), -1); + { + if (colors) + { + string_dyn_concat (string, message, -1); + } + else + { + message_no_colors = gui_color_decode (message, NULL); + if (message_no_colors) + { + string_dyn_concat (string, message_no_colors, -1); + free (message_no_colors); + } + } + } + if (colors) + string_dyn_concat (string, GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), -1); string_dyn_concat (string, " [", -1); - string_dyn_concat (string, GUI_COLOR(GUI_COLOR_CHAT_TAGS), -1); + if (colors) + string_dyn_concat (string, GUI_COLOR(GUI_COLOR_CHAT_TAGS), -1); for (i = 0; i < tags_count; i++) { string_dyn_concat (string, tags_array[i], -1); if (i < tags_count - 1) string_dyn_concat (string, ",", -1); } - string_dyn_concat (string, GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), -1); + if (colors) + string_dyn_concat (string, GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), -1); string_dyn_concat (string, "]", -1); result = strdup (*string); @@ -595,7 +618,18 @@ gui_line_search_text (struct t_gui_buffer *buffer, struct t_gui_line *line) if (!rc && (buffer->text_search_where & GUI_TEXT_SEARCH_IN_MESSAGE)) { - message = gui_color_decode (line->data->message, NULL); + if (gui_chat_display_tags) + { + message = gui_line_build_string_message_tags ( + line->data->message, + line->data->tags_count, + line->data->tags_array, + 0); + } + else + { + message = gui_color_decode (line->data->message, NULL); + } if (message) { if (buffer->text_search_regex) diff --git a/src/gui/gui-line.h b/src/gui/gui-line.h index 11c2f18b9..9bb7ea95a 100644 --- a/src/gui/gui-line.h +++ b/src/gui/gui-line.h @@ -84,7 +84,8 @@ extern char *gui_line_build_string_prefix_message (const char *prefix, const char *message); extern char *gui_line_build_string_message_tags (const char *message, int tags_count, - char **tags_array); + char **tags_array, + int colors); extern int gui_line_is_displayed (struct t_gui_line *line); extern struct t_gui_line *gui_line_get_first_displayed (struct t_gui_buffer *buffer); extern struct t_gui_line *gui_line_get_last_displayed (struct t_gui_buffer *buffer); diff --git a/tests/unit/gui/test-gui-line.cpp b/tests/unit/gui/test-gui-line.cpp index 14e37a03f..3e2e8b24c 100644 --- a/tests/unit/gui/test-gui-line.cpp +++ b/tests/unit/gui/test-gui-line.cpp @@ -43,12 +43,13 @@ extern "C" gui_line_free_data (line); \ free (line); -#define WEE_BUILD_STR_MSG_TAGS(__tags, __message) \ +#define WEE_BUILD_STR_MSG_TAGS(__tags, __message, __colors) \ line = gui_line_new (gui_buffers, -1, 0, 0, __tags, \ NULL, __message); \ str = gui_line_build_string_message_tags (line->data->message, \ line->data->tags_count, \ - line->data->tags_array); \ + line->data->tags_array, \ + __colors); \ STRCMP_EQUAL(str_result, str); \ free (str); \ gui_line_free_data (line); \ @@ -214,17 +215,19 @@ TEST(GuiLine, BuildStringPrefixMessage) TEST(GuiLine, BuildStringMessageTags) { struct t_gui_line *line; - char *str, str_result[256]; + char *str, str_message[256], str_result[256]; line = gui_line_new (gui_buffers, -1, 0, 0, "tag1,tag2", NULL, "test"); POINTERS_EQUAL(NULL, gui_line_build_string_message_tags (line->data->message, -1, - line->data->tags_array)); + line->data->tags_array, + 1)); POINTERS_EQUAL(NULL, gui_line_build_string_message_tags (line->data->message, 1, - NULL)); + NULL, + 1)); gui_line_free_data (line); free (line); @@ -233,28 +236,46 @@ TEST(GuiLine, BuildStringMessageTags) GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), GUI_COLOR(GUI_COLOR_CHAT_TAGS), GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS)); - WEE_BUILD_STR_MSG_TAGS(NULL, "message"); + WEE_BUILD_STR_MSG_TAGS(NULL, "message", 1); snprintf (str_result, sizeof (str_result), "message%s [%s%s]", GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), GUI_COLOR(GUI_COLOR_CHAT_TAGS), GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS)); - WEE_BUILD_STR_MSG_TAGS("", "message"); + WEE_BUILD_STR_MSG_TAGS("", "message", 1); snprintf (str_result, sizeof (str_result), "message%s [%stag1%s]", GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), GUI_COLOR(GUI_COLOR_CHAT_TAGS), GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS)); - WEE_BUILD_STR_MSG_TAGS("tag1", "message"); + WEE_BUILD_STR_MSG_TAGS("tag1", "message", 1); snprintf (str_result, sizeof (str_result), "message%s [%stag1,tag2,tag3%s]", GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), GUI_COLOR(GUI_COLOR_CHAT_TAGS), GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS)); - WEE_BUILD_STR_MSG_TAGS("tag1,tag2,tag3", "message"); + WEE_BUILD_STR_MSG_TAGS("tag1,tag2,tag3", "message", 1); + + snprintf (str_message, sizeof (str_message), + "message %sin red", + gui_color_get_custom ("red")); + snprintf (str_result, sizeof (str_result), + "message %sin red%s [%stag1,tag2,tag3%s]", + gui_color_get_custom ("red"), + GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), + GUI_COLOR(GUI_COLOR_CHAT_TAGS), + GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS)); + WEE_BUILD_STR_MSG_TAGS("tag1,tag2,tag3", str_message, 1); + + snprintf (str_message, sizeof (str_message), + "message %sin red", + gui_color_get_custom ("red")); + snprintf (str_result, sizeof (str_result), + "message in red [tag1,tag2,tag3]"); + WEE_BUILD_STR_MSG_TAGS("tag1,tag2,tag3", str_message, 0); } /*