Skip to content

Commit

Permalink
Fix TUI display issues in non-UTF-8 locales
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
GabrielKimm committed Oct 1, 2024
1 parent 4afc58f commit 39030ab
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions cmds/tui.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 39030ab

Please sign in to comment.