Message ID | 20221012140256.70306-1-xdavidwuph@gmail.com (mailing list archive) |
---|---|
State | Accepted, archived |
Headers | show |
Series | [v4] client: handle wide chars for table rows | expand |
Context | Check | Description |
---|---|---|
tedd_an/pre-ci_am | success | Success |
prestwoj/iwd-alpine-ci-fetch | success | Fetch PR |
prestwoj/iwd-ci-gitlint | success | GitLint |
prestwoj/iwd-ci-fetch | success | Fetch PR |
prestwoj/iwd-ci-makedistcheck | success | Make Distcheck |
prestwoj/iwd-ci-incremental_build | success | Incremental build not run PASS |
prestwoj/iwd-ci-build | success | Build - Configure |
prestwoj/iwd-alpine-ci-makedistcheck | success | Make Distcheck |
prestwoj/iwd-alpine-ci-incremental_build | success | Incremental build not run PASS |
prestwoj/iwd-alpine-ci-build | success | Build - Configure |
prestwoj/iwd-ci-clang | success | clang PASS |
prestwoj/iwd-ci-makecheckvalgrind | success | Make Check w/Valgrind |
prestwoj/iwd-ci-makecheck | success | Make Check |
prestwoj/iwd-alpine-ci-makecheckvalgrind | success | Make Check w/Valgrind |
prestwoj/iwd-alpine-ci-makecheck | success | Make Check |
prestwoj/iwd-ci-testrunner | success | test-runner PASS |
Hi Pinghao, On 10/12/22 09:02, Pinghao Wu wrote: > Find out printing width of wide chars via wcwidth(). > --- > client/display.c | 35 ++++++++++++++++++++++++++--------- > client/main.c | 3 +++ > 2 files changed, 29 insertions(+), 9 deletions(-) > Applied, thanks. Regards, -Denis
diff --git a/client/display.c b/client/display.c index b729ad4c..ddd9551c 100644 --- a/client/display.c +++ b/client/display.c @@ -30,6 +30,7 @@ #include <sys/stat.h> #include <sys/ioctl.h> #include <unistd.h> +#include <wchar.h> #include <readline/history.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) { - unsigned int i; + unsigned int i = 0; int last_space = -1; int last_color = -1; + unsigned int s_len = strlen(s); /* Find the last space before 'max', as well as any color */ - for (i = 0; i <= *max && s[i] != '\0'; i++) { - if (s[i] == ' ') - last_space = i; - else if (s[i] == 0x1b) { + while (i <= *max && i != s_len) { + int sequence_len; + int sequence_columns; + wchar_t w; + + if (s[i] == 0x1b) { + sequence_len = color_end(s + i); /* color escape won't count for column width */ - *max += color_end(s + i); + sequence_columns = 0; last_color = i; - /* Add width for non-codepoint UTF-8 bytes */ - } else if (((uint8_t)s[i] >> 6) == 2) - *max += 1; + } else { + if (s[i] == ' ') + 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 */ diff --git a/client/main.c b/client/main.c index df5c0a61..7e8dead4 100644 --- a/client/main.c +++ b/client/main.c @@ -25,6 +25,7 @@ #endif #include <errno.h> +#include <locale.h> #include <signal.h> #include <ell/ell.h> @@ -50,6 +51,8 @@ int main(int argc, char *argv[]) { bool all_done; + setlocale(LC_CTYPE, ""); + if (!l_main_init()) return EXIT_FAILURE;