1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-26 04:46:37 +02:00

core: fix chat colors at certain positions not being applied

The new rendering of multiline lines had some issues with colors at
certain positions not being applied. The color would not be applied if
the color code was at either of these positions:

  - At the start of a line after a newline character
  - At the end of a line after a space and before a newline character
  - At a line by itself before a newline character

The way I had done it by considering newline characters as a word in
gui_chat_get_word_info with a variable specifying that it's newline
characters became messy and didn't really make sense, so rather than
doing this, I changed gui_chat_get_word_info to stop before the first
newline character. That way, we can just check if we are at a newline
character at the start of the loop, and don't need any more special
handling.

Fixes #1928
This commit is contained in:
Trygve Aaberge
2023-05-28 14:34:38 +02:00
committed by Sébastien Helleu
parent f1a826a116
commit 96f41ce4bf
4 changed files with 92 additions and 88 deletions
+48 -60
View File
@@ -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,68 +1551,43 @@ 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))
{
/* 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)
{
/* 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;
while (ptr_data && 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);
}
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)
{
/* 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);
}
/* 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;
@@ -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] == ' '))
{
+11 -7
View File
@@ -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;
+1 -2
View File
@@ -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 ();
+32 -19
View File
@@ -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;
int word_length;
char string[32];
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");
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);
}
/*