3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-11-25 09:39:25 +01:00

client: fix not accounting for color escapes after line break

Each color escape is tracked and the new_width is adjusted
accordingly. But if the color escape comes after a space which breaks
the line, the adjusted width ends up being too long since that escape
sequence isn't appearing on the current line. This causes the next
column to be shifted over.
This commit is contained in:
James Prestwood 2022-10-12 11:35:21 -07:00 committed by Denis Kenzior
parent ea82616b6b
commit 9d042ca321

View File

@ -404,8 +404,10 @@ static char* next_line(char *s, unsigned int width, unsigned int *new_width,
int last_space = -1; int last_space = -1;
int last_color = -1; int last_color = -1;
unsigned int s_len = strlen(s); unsigned int s_len = strlen(s);
unsigned int color_adjust = 0;
*new_width = width; *new_width = width;
*color_out = NULL;
/* Find the last space before 'max', as well as any color */ /* Find the last space before 'max', as well as any color */
while (i <= *new_width && i < s_len) { while (i <= *new_width && i < s_len) {
@ -418,9 +420,22 @@ static char* next_line(char *s, unsigned int width, unsigned int *new_width,
/* color escape won't count for column width */ /* color escape won't count for column width */
sequence_columns = 0; sequence_columns = 0;
last_color = i; last_color = i;
/*
* Color after a space. If the line gets broken this
* will need to be removed off new_width since it will
* appear on the next line.
*/
if (last_space != -1)
color_adjust += sequence_len;
} else { } else {
if (s[i] == ' ') if (s[i] == ' ') {
last_space = i; last_space = i;
/* Any past colors will appear on this line */
color_adjust = 0;
}
sequence_len = l_utf8_get_codepoint(&s[i], s_len - i, sequence_len = l_utf8_get_codepoint(&s[i], s_len - i,
&w); &w);
sequence_columns = wcwidth(w); sequence_columns = wcwidth(w);
@ -446,8 +461,8 @@ static char* next_line(char *s, unsigned int width, unsigned int *new_width,
if (last_color != -1 && last_space >= last_color) if (last_color != -1 && last_space >= last_color)
*color_out = l_strndup(s + last_color, *color_out = l_strndup(s + last_color,
color_end(s + last_color)); color_end(s + last_color));
else else if (last_color != -1 && last_space < last_color)
*color_out = NULL; *new_width -= color_adjust;
s[last_space] = '\0'; s[last_space] = '\0';