3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-11-22 14:49:24 +01:00

client: handle wide chars for table rows

Find out printing width of wide chars via wcwidth().
This commit is contained in:
Pinghao Wu 2022-10-12 22:02:57 +08:00 committed by Denis Kenzior
parent ba6a48018c
commit d82869c346
2 changed files with 29 additions and 9 deletions

View File

@ -30,6 +30,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#include <wchar.h>
#include <readline/history.h> #include <readline/history.h>
#include <readline/readline.h> #include <readline/readline.h>
@ -398,21 +399,37 @@ static unsigned int color_end(char *s)
*/ */
static char* next_line(char *s, unsigned int *max, char **color_out) static char* next_line(char *s, unsigned int *max, char **color_out)
{ {
unsigned int i; unsigned int i = 0;
int last_space = -1; int last_space = -1;
int last_color = -1; int last_color = -1;
unsigned int s_len = strlen(s);
/* Find the last space before 'max', as well as any color */ /* Find the last space before 'max', as well as any color */
for (i = 0; i <= *max && s[i] != '\0'; i++) { while (i <= *max && i != s_len) {
if (s[i] == ' ') int sequence_len;
last_space = i; int sequence_columns;
else if (s[i] == 0x1b) { wchar_t w;
if (s[i] == 0x1b) {
sequence_len = color_end(s + i);
/* color escape won't count for column width */ /* color escape won't count for column width */
*max += color_end(s + i); sequence_columns = 0;
last_color = i; last_color = i;
/* Add width for non-codepoint UTF-8 bytes */ } else {
} else if (((uint8_t)s[i] >> 6) == 2) if (s[i] == ' ')
*max += 1; last_space = i;
sequence_len = l_utf8_get_codepoint(&s[i], s_len - i,
&w);
if (sequence_len < 0) {
sequence_len = 1;
sequence_columns = 1;
} else
sequence_columns = wcwidth(w);
}
/* Compensate max bytes */
*max += sequence_len - sequence_columns;
i += sequence_len;
} }
/* Reached the end of the string within the column bounds */ /* Reached the end of the string within the column bounds */

View File

@ -25,6 +25,7 @@
#endif #endif
#include <errno.h> #include <errno.h>
#include <locale.h>
#include <signal.h> #include <signal.h>
#include <ell/ell.h> #include <ell/ell.h>
@ -50,6 +51,8 @@ int main(int argc, char *argv[])
{ {
bool all_done; bool all_done;
setlocale(LC_CTYPE, "");
if (!l_main_init()) if (!l_main_init())
return EXIT_FAILURE; return EXIT_FAILURE;