From 39030abbd0de2289fa0600f0395b006176ff4c52 Mon Sep 17 00:00:00 2001 From: 0xGabriel <0xGabriel.kim@gmail.com> Date: Mon, 30 Sep 2024 01:51:49 +0900 Subject: [PATCH] Fix TUI display issues in non-UTF-8 locales This commit addresses the issue #1870 where the TUI displays incorrect characters in non-UTF-8 locales. The fix includes: - Checking the current locale - Using ASCII characters for graph display in non-UTF-8 locales - Adjusting character widths accordingly Fixes: #1870 Signed-off-by: Gabriel <0xGabriel.kim@gmail.com> --- cmds/tui.c | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/cmds/tui.c b/cmds/tui.c index 7b02444ae..a80baeb7a 100644 --- a/cmds/tui.c +++ b/cmds/tui.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -127,6 +128,7 @@ static struct tui_graph partial_graph; static struct tui_list tui_info; static struct tui_list tui_session; static char *tui_search; +static bool is_utf8_locale; static const struct tui_window_ops graph_ops; static const struct tui_window_ops report_ops; @@ -212,6 +214,12 @@ static char *selected_report_sort_key[NUM_REPORT_FIELD]; static int curr_sort_key = 0; +static void check_utf8_locale(void) +{ + const char *lang = getenv("LANG"); + is_utf8_locale = (lang != NULL && strstr(lang, ".UTF-8") != NULL); +} + static void init_colors(void) { if (!has_colors()) @@ -1260,6 +1268,18 @@ static void print_graph_indent(struct tui_graph *graph, struct tui_graph_node *n { int i; struct tui_graph_node *parent = (void *)node->n.parent; + const char *vertical_line, *corner, *branch; + + if (is_utf8_locale) { + vertical_line = "│"; + corner = "└"; + branch = "├"; + } + else { + vertical_line = "| "; + corner = "`-"; + branch = "+-"; + } for (i = 0; i < depth; i++) { if (width < 3) { @@ -1273,12 +1293,13 @@ static void print_graph_indent(struct tui_graph *graph, struct tui_graph_node *n continue; } - if (i < depth - 1 || single_child) - printw(" │"); + if (i < depth - 1 || single_child) { + printw(" %s", vertical_line); + } else if (is_last_child(parent, node)) - printw(" └"); + printw(" %s", corner); else - printw(" ├"); + printw(" %s", branch); } } @@ -1299,7 +1320,12 @@ static void win_display_graph(struct tui_window *win, void *node) return; } - fold_sign = curr->folded ? "▶" : "─"; + if (is_utf8_locale) { + fold_sign = curr->folded ? "▶" : "─"; + } + else { + fold_sign = curr->folded ? ">" : ""; + } parent = win_parent_graph(win, node); if (parent == NULL) @@ -1327,10 +1353,16 @@ static void win_display_graph(struct tui_window *win, void *node) w = COLS - width; width += snprintf(buf, sizeof(buf), "%s(%d) ", fold_sign, curr->n.nr_calls); - /* handle UTF-8 character length */ + /* handle character length */ if (strcmp(fold_sign, " ")) { - width -= 2; - w += 2; + if (is_utf8_locale) { + width -= 2; + w += 2; + } + else { + width -= 1; + w += 1; + } } printw("%.*s", w, buf); } @@ -3029,6 +3061,8 @@ int command_tui(int argc, char *argv[], struct uftrace_opts *opts) struct uftrace_data handle; struct uftrace_task_reader *task; + check_utf8_locale(); + ret = open_data_file(opts, &handle); if (ret < 0) { pr_warn("cannot open record data: %s: %m\n", opts->dirname);