From c339d9775d1df19fdbbafc57486f7cd51af6b7fb Mon Sep 17 00:00:00 2001 From: Michal Schmidt Date: Fri, 2 Nov 2012 17:35:30 +0100 Subject: util : fallback to plain ASCII drawing if locale is not UTF-8 When printing cgroup and sysfs hierarchies, avoid using UTF-8 box drawing characters if the locale is not UTF-8. https://bugzilla.redhat.com/show_bug.cgi?id=871153 --- src/shared/cgroup-show.c | 11 +++++++---- src/shared/util.c | 19 +++++++++++++++++++ src/shared/util.h | 9 +++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) (limited to 'src/shared') diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c index 2ffed8b739..69fe7fc96d 100644 --- a/src/shared/cgroup-show.c +++ b/src/shared/cgroup-show.c @@ -88,7 +88,8 @@ static void show_pid_array(int pids[], unsigned n_pids, const char *prefix, unsi printf("%s%s %*lu %s\n", prefix, - extra ? "\342\200\243" : ((more || i < n_pids-1) ? "\342\224\234" : "\342\224\224"), + draw_special_char(extra ? DRAW_TRIANGULAR_BULLET : + ((more || i < n_pids-1) ? DRAW_BOX_VERT_AND_RIGHT : DRAW_BOX_UP_AND_RIGHT)), pid_width, (unsigned long) pids[i], strna(t)); @@ -207,10 +208,11 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns } if (last) { - printf("%s\342\224\234 %s\n", prefix, path_get_file_name(last)); + printf("%s%s %s\n", prefix, draw_special_char(DRAW_BOX_VERT_AND_RIGHT), + path_get_file_name(last)); if (!p1) { - p1 = strappend(prefix, "\342\224\202 "); + p1 = strjoin(prefix, draw_special_char(DRAW_BOX_VERT), " ", NULL); if (!p1) { free(k); r = -ENOMEM; @@ -232,7 +234,8 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns show_cgroup_one_by_path(path, prefix, n_columns, !!last, kernel_threads); if (last) { - printf("%s\342\224\224 %s\n", prefix, path_get_file_name(last)); + printf("%s%s %s\n", prefix, draw_special_char(DRAW_BOX_UP_AND_RIGHT), + path_get_file_name(last)); if (!p2) { p2 = strappend(prefix, " "); diff --git a/src/shared/util.c b/src/shared/util.c index 3ac67505f0..2a8afae0ec 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -6140,3 +6140,22 @@ bool is_locale_utf8(void) { out: return (bool)cached_answer; } + +const char *draw_special_char(DrawSpecialChar ch) { + static const char *draw_table[2][_DRAW_SPECIAL_CHAR_MAX] = { + /* UTF-8 */ { + [DRAW_BOX_VERT] = "\342\224\202", /* │ */ + [DRAW_BOX_VERT_AND_RIGHT] = "\342\224\234", /* ├ */ + [DRAW_BOX_UP_AND_RIGHT] = "\342\224\224", /* └ */ + [DRAW_TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */ + }, + /* ASCII fallback */ { + [DRAW_BOX_VERT] = "|", + [DRAW_BOX_VERT_AND_RIGHT] = "+", + [DRAW_BOX_UP_AND_RIGHT] = "\\", + [DRAW_TRIANGULAR_BULLET] = ">", + } + }; + + return draw_table[!is_locale_utf8()][ch]; +} diff --git a/src/shared/util.h b/src/shared/util.h index b979b0e89f..e387b1268a 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -600,3 +600,12 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, void *arg); bool is_locale_utf8(void); + +typedef enum DrawSpecialChar { + DRAW_BOX_VERT, + DRAW_BOX_VERT_AND_RIGHT, + DRAW_BOX_UP_AND_RIGHT, + DRAW_TRIANGULAR_BULLET, + _DRAW_SPECIAL_CHAR_MAX +} DrawSpecialChar; +const char *draw_special_char(DrawSpecialChar ch); -- cgit v1.2.3-54-g00ecf