From bb40c12569d017f75774ff9f4b6fb0ac64f9eee0 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 10 May 2019 14:52:05 -0400 Subject: [PATCH 1/5] terminal-util: define grey as 256color ANSI sequence Apparently all relevant terminals implement these sequences, including the Linux kernel, rxvt, xterm, and of course gnome terminal. Hence it should be OK to use them, and fixate the grey color in a way that maps to the same color in all terminals. Ideally we'd stick to the more symbolic colors that allow terminal emulators to implement color styles, but this apparently doesn#t work, since rxvt maps grey to something unreadable by default. Note that this change has negative effects besides the non-themability of the palette: the midrange grey this uses maps to regular white on the linux console. However, that's probably not too bad: allowing things to be unreadable on some terminals is probably worse than showing no color on some terminals. Fixes: #12482 --- src/basic/terminal-util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index f3e785e7ba..6a77e5ebb9 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -19,7 +19,7 @@ #define ANSI_MAGENTA "\x1B[0;35m" #define ANSI_CYAN "\x1B[0;36m" #define ANSI_WHITE "\x1B[0;37m" -#define ANSI_GREY "\x1B[0;2;37m" +#define ANSI_GREY "\x1B[0;38;5;245m" /* Bold/highlighted */ #define ANSI_HIGHLIGHT_BLACK "\x1B[0;1;30m" From 1530bf251a110878febeca4a21b31fb68a01b50b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 10 May 2019 14:55:59 -0400 Subject: [PATCH 2/5] terminal-util: reduce colors we define a bit three of these colors we never use, so let's drop them. Let's however add a highlight version of grey, so that at least for all colors we do define we have all possible styles defined. --- src/basic/terminal-util.h | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index 6a77e5ebb9..90de9a479e 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -11,35 +11,28 @@ #include "time-util.h" /* Regular colors */ -#define ANSI_BLACK "\x1B[0;30m" #define ANSI_RED "\x1B[0;31m" #define ANSI_GREEN "\x1B[0;32m" #define ANSI_YELLOW "\x1B[0;33m" #define ANSI_BLUE "\x1B[0;34m" #define ANSI_MAGENTA "\x1B[0;35m" -#define ANSI_CYAN "\x1B[0;36m" -#define ANSI_WHITE "\x1B[0;37m" #define ANSI_GREY "\x1B[0;38;5;245m" /* Bold/highlighted */ -#define ANSI_HIGHLIGHT_BLACK "\x1B[0;1;30m" #define ANSI_HIGHLIGHT_RED "\x1B[0;1;31m" #define ANSI_HIGHLIGHT_GREEN "\x1B[0;1;32m" #define ANSI_HIGHLIGHT_YELLOW "\x1B[0;1;33m" #define ANSI_HIGHLIGHT_BLUE "\x1B[0;1;34m" #define ANSI_HIGHLIGHT_MAGENTA "\x1B[0;1;35m" -#define ANSI_HIGHLIGHT_CYAN "\x1B[0;1;36m" -#define ANSI_HIGHLIGHT_WHITE "\x1B[0;1;37m" +#define ANSI_HIGHLIGHT_GREY "\x1B[0;1;38;5;245m" /* Underlined */ -#define ANSI_HIGHLIGHT_BLACK_UNDERLINE "\x1B[0;1;4;30m" #define ANSI_HIGHLIGHT_RED_UNDERLINE "\x1B[0;1;4;31m" #define ANSI_HIGHLIGHT_GREEN_UNDERLINE "\x1B[0;1;4;32m" #define ANSI_HIGHLIGHT_YELLOW_UNDERLINE "\x1B[0;1;4;33m" #define ANSI_HIGHLIGHT_BLUE_UNDERLINE "\x1B[0;1;4;34m" #define ANSI_HIGHLIGHT_MAGENTA_UNDERLINE "\x1B[0;1;4;35m" -#define ANSI_HIGHLIGHT_CYAN_UNDERLINE "\x1B[0;1;4;36m" -#define ANSI_HIGHLIGHT_WHITE_UNDERLINE "\x1B[0;1;4;37m" +#define ANSI_HIGHLIGHT_GREY_UNDERLINE "\x1B[0;1;4;38;5;245m" /* Other ANSI codes */ #define ANSI_UNDERLINE "\x1B[0;4m" From 003aa3e045d6fab051a9fd75fdf1da9ce98e554b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 10 May 2019 14:57:01 -0400 Subject: [PATCH 3/5] terminal-util: add complete set of inline functions for all colors we define We don#t actually use all combinations, but it's kinda nice if we can output all combinations in a test, and this is preparation so that we can do so nicely. --- src/basic/terminal-util.h | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index 90de9a479e..a31203c584 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -120,21 +120,29 @@ bool dev_console_colors_enabled(void); colors_enabled() ? ANSI_##REPLACEMENT : ""; \ } -DEFINE_ANSI_FUNC(highlight, HIGHLIGHT); -DEFINE_ANSI_FUNC(highlight_red, HIGHLIGHT_RED); -DEFINE_ANSI_FUNC(highlight_green, HIGHLIGHT_GREEN); -DEFINE_ANSI_FUNC(highlight_yellow, HIGHLIGHT_YELLOW); -DEFINE_ANSI_FUNC(highlight_blue, HIGHLIGHT_BLUE); -DEFINE_ANSI_FUNC(highlight_magenta, HIGHLIGHT_MAGENTA); -DEFINE_ANSI_FUNC(normal, NORMAL); -DEFINE_ANSI_FUNC(grey, GREY); +DEFINE_ANSI_FUNC(normal, NORMAL); +DEFINE_ANSI_FUNC(highlight, HIGHLIGHT); +DEFINE_ANSI_FUNC(red, RED); +DEFINE_ANSI_FUNC(green, GREEN); +DEFINE_ANSI_FUNC(yellow, YELLOW); +DEFINE_ANSI_FUNC(blue, BLUE); +DEFINE_ANSI_FUNC(magenta, MAGENTA); +DEFINE_ANSI_FUNC(grey, GREY); +DEFINE_ANSI_FUNC(highlight_red, HIGHLIGHT_RED); +DEFINE_ANSI_FUNC(highlight_green, HIGHLIGHT_GREEN); +DEFINE_ANSI_FUNC(highlight_yellow, HIGHLIGHT_YELLOW); +DEFINE_ANSI_FUNC(highlight_blue, HIGHLIGHT_BLUE); +DEFINE_ANSI_FUNC(highlight_magenta, HIGHLIGHT_MAGENTA); +DEFINE_ANSI_FUNC(highlight_grey, HIGHLIGHT_GREY); -DEFINE_ANSI_FUNC_UNDERLINE(underline, UNDERLINE, NORMAL); -DEFINE_ANSI_FUNC_UNDERLINE(highlight_underline, HIGHLIGHT_UNDERLINE, HIGHLIGHT); -DEFINE_ANSI_FUNC_UNDERLINE(highlight_red_underline, HIGHLIGHT_RED_UNDERLINE, HIGHLIGHT_RED); -DEFINE_ANSI_FUNC_UNDERLINE(highlight_green_underline, HIGHLIGHT_GREEN_UNDERLINE, HIGHLIGHT_GREEN); -DEFINE_ANSI_FUNC_UNDERLINE(highlight_yellow_underline, HIGHLIGHT_YELLOW_UNDERLINE, HIGHLIGHT_YELLOW); -DEFINE_ANSI_FUNC_UNDERLINE(highlight_blue_underline, HIGHLIGHT_BLUE_UNDERLINE, HIGHLIGHT_BLUE); +DEFINE_ANSI_FUNC_UNDERLINE(underline, UNDERLINE, NORMAL); +DEFINE_ANSI_FUNC_UNDERLINE(highlight_underline, HIGHLIGHT_UNDERLINE, HIGHLIGHT); +DEFINE_ANSI_FUNC_UNDERLINE(highlight_red_underline, HIGHLIGHT_RED_UNDERLINE, HIGHLIGHT_RED); +DEFINE_ANSI_FUNC_UNDERLINE(highlight_green_underline, HIGHLIGHT_GREEN_UNDERLINE, HIGHLIGHT_GREEN); +DEFINE_ANSI_FUNC_UNDERLINE(highlight_yellow_underline, HIGHLIGHT_YELLOW_UNDERLINE, HIGHLIGHT_YELLOW); +DEFINE_ANSI_FUNC_UNDERLINE(highlight_blue_underline, HIGHLIGHT_BLUE_UNDERLINE, HIGHLIGHT_BLUE); +DEFINE_ANSI_FUNC_UNDERLINE(highlight_magenta_underline, HIGHLIGHT_MAGENTA_UNDERLINE, HIGHLIGHT_MAGENTA); +DEFINE_ANSI_FUNC_UNDERLINE(highlight_grey_underline, HIGHLIGHT_GREY_UNDERLINE, HIGHLIGHT_GREY); int get_ctty_devnr(pid_t pid, dev_t *d); int get_ctty(pid_t, dev_t *_devnr, char **r); From 042f472eaa773b4d12872932397e9da8abf8067d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 10 May 2019 14:57:51 -0400 Subject: [PATCH 4/5] test: add test that shows all colors we define --- src/test/test-terminal-util.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/test/test-terminal-util.c b/src/test/test-terminal-util.c index 96d92279fb..0e563f5497 100644 --- a/src/test/test-terminal-util.c +++ b/src/test/test-terminal-util.c @@ -75,12 +75,45 @@ static void test_getttyname_malloc(void) { assert_se(PATH_IN_SET(ttyname, "ptmx", "pts/ptmx")); } +static void test_one_color(const char *name, const char *color) { + printf("<%s%s%s>\n", color, name, ansi_normal()); +} + +static void test_colors(void) { + log_info("/* %s */", __func__); + + test_one_color("normal", ansi_normal()); + test_one_color("highlight", ansi_highlight()); + test_one_color("red", ansi_red()); + test_one_color("green", ansi_green()); + test_one_color("yellow", ansi_yellow()); + test_one_color("blue", ansi_blue()); + test_one_color("megenta", ansi_magenta()); + test_one_color("grey", ansi_grey()); + test_one_color("highlight-red", ansi_highlight_red()); + test_one_color("highlight-green", ansi_highlight_green()); + test_one_color("highlight-yellow", ansi_highlight_yellow()); + test_one_color("highlight-blue", ansi_highlight_blue()); + test_one_color("highlight-magenta", ansi_highlight_magenta()); + test_one_color("highlight-grey", ansi_highlight_grey()); + + test_one_color("underline", ansi_underline()); + test_one_color("highlight-underline", ansi_highlight_underline()); + test_one_color("highlight-red-underline", ansi_highlight_red_underline()); + test_one_color("highlight-green-underline", ansi_highlight_green_underline()); + test_one_color("highlight-yellow-underline", ansi_highlight_yellow_underline()); + test_one_color("highlight-blue-underline", ansi_highlight_blue_underline()); + test_one_color("highlight-magenta-underline", ansi_highlight_magenta_underline()); + test_one_color("highlight-grey-underline", ansi_highlight_grey_underline()); +} + int main(int argc, char *argv[]) { test_setup_logging(LOG_INFO); test_default_term_for_tty(); test_read_one_char(); test_getttyname_malloc(); + test_colors(); return 0; } From af1060e1abf9eb958444bde2b06aa3279e1c81cb Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 10 May 2019 14:58:23 -0400 Subject: [PATCH 5/5] journalctl: don't output ANSI sequences if disabled --- src/journal/journal-verify.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/journal/journal-verify.c b/src/journal/journal-verify.c index 0a06ce7b84..344b7b019a 100644 --- a/src/journal/journal-verify.c +++ b/src/journal/journal-verify.c @@ -46,7 +46,7 @@ static void draw_progress(uint64_t p, usec_t *last_usec) { for (i = 0; i < j; i++) fputs("\xe2\x96\x88", stdout); - fputs(ANSI_NORMAL, stdout); + fputs(ansi_normal(), stdout); for (i = 0; i < k; i++) fputs("\xe2\x96\x91", stdout);