From 66bb00590faa093e1012aa2033304625be5e1208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 23 Jul 2020 10:13:58 +0200 Subject: [PATCH 1/3] basic/terminal-util: define all foreground colors We would add and remove definitions based on which colors were used by other code. Let's just define all of them to simplify tests and allow easy comparisons which colors look good. --- src/basic/terminal-util.h | 33 ++++++++++++++++ src/test/test-terminal-util.c | 74 +++++++++++++++++++++++------------ 2 files changed, 81 insertions(+), 26 deletions(-) diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index 67634b4d46..9e824b0eee 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -11,19 +11,36 @@ #include "time-util.h" /* Regular colors */ +#define ANSI_BLACK "\x1B[0;30m" /* Some type of grey usually. */ #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" /* This is actually rendered as light grey, legible even on a white + * background. See ANSI_HIGHLIGHT_WHITE for real white. */ + +#define ANSI_BRIGHT_BLACK "\x1B[0;90m" +#define ANSI_BRIGHT_RED "\x1B[0;91m" +#define ANSI_BRIGHT_GREEN "\x1B[0;92m" +#define ANSI_BRIGHT_YELLOW "\x1B[0;93m" +#define ANSI_BRIGHT_BLUE "\x1B[0;94m" +#define ANSI_BRIGHT_MAGENTA "\x1B[0;95m" +#define ANSI_BRIGHT_CYAN "\x1B[0;96m" +#define ANSI_BRIGHT_WHITE "\x1B[0;97m" + #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;38;5;185m" #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" #define ANSI_HIGHLIGHT_YELLOW4 "\x1B[0;1;38;5;100m" @@ -124,18 +141,34 @@ bool dev_console_colors_enabled(void); DEFINE_ANSI_FUNC(normal, NORMAL); DEFINE_ANSI_FUNC(highlight, HIGHLIGHT); +DEFINE_ANSI_FUNC(black, BLACK); 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(cyan, CYAN); +DEFINE_ANSI_FUNC(white, WHITE); DEFINE_ANSI_FUNC(grey, GREY); + +DEFINE_ANSI_FUNC(bright_black, BRIGHT_BLACK); +DEFINE_ANSI_FUNC(bright_red, BRIGHT_RED); +DEFINE_ANSI_FUNC(bright_green, BRIGHT_GREEN); +DEFINE_ANSI_FUNC(bright_yellow, BRIGHT_YELLOW); +DEFINE_ANSI_FUNC(bright_blue, BRIGHT_BLUE); +DEFINE_ANSI_FUNC(bright_magenta, BRIGHT_MAGENTA); +DEFINE_ANSI_FUNC(bright_cyan, BRIGHT_CYAN); +DEFINE_ANSI_FUNC(bright_white, BRIGHT_WHITE); + +DEFINE_ANSI_FUNC(highlight_black, HIGHLIGHT_BLACK); 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_cyan, HIGHLIGHT_CYAN); DEFINE_ANSI_FUNC(highlight_grey, HIGHLIGHT_GREY); +DEFINE_ANSI_FUNC(highlight_white, HIGHLIGHT_WHITE); DEFINE_ANSI_FUNC_UNDERLINE(underline, UNDERLINE, NORMAL); DEFINE_ANSI_FUNC_UNDERLINE(highlight_underline, HIGHLIGHT_UNDERLINE, HIGHLIGHT); diff --git a/src/test/test-terminal-util.c b/src/test/test-terminal-util.c index 52e651faef..565cd18f97 100644 --- a/src/test/test-terminal-util.c +++ b/src/test/test-terminal-util.c @@ -75,36 +75,58 @@ 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()); -} +typedef struct { + const char *name; + const char* (*func)(void); +} Color; + +static const Color colors[] = { + { "normal", ansi_normal }, + { "highlight", ansi_highlight }, + { "black", ansi_black }, + { "red", ansi_red }, + { "green", ansi_green }, + { "yellow", ansi_yellow }, + { "blue", ansi_blue }, + { "magenta", ansi_magenta }, + { "cyan", ansi_cyan }, + { "white", ansi_white }, + { "grey", ansi_grey }, + + { "bright-black", ansi_bright_black }, + { "bright-red", ansi_bright_red }, + { "bright-green", ansi_bright_green }, + { "bright-yellow", ansi_bright_yellow }, + { "bright-blue", ansi_bright_blue }, + { "bright-magenta", ansi_bright_magenta }, + { "bright-cyan", ansi_bright_cyan }, + { "bright-white", ansi_bright_white }, + + { "highlight-black", ansi_highlight_black }, + { "highlight-red", ansi_highlight_red }, + { "highlight-green", ansi_highlight_green }, + { "highlight-yellow", ansi_highlight_yellow }, + { "highlight-blue", ansi_highlight_blue }, + { "highlight-magenta", ansi_highlight_magenta }, + { "highlight-cyan", ansi_highlight_cyan }, + { "highlight-white", ansi_highlight_white }, + { "highlight-grey", ansi_highlight_grey }, + + { "underline", ansi_underline }, + { "highlight-underline", ansi_highlight_underline }, + { "highlight-red-underline", ansi_highlight_red_underline }, + { "highlight-green-underline", ansi_highlight_green_underline }, + { "highlight-yellow-underline", ansi_highlight_yellow_underline }, + { "highlight-blue-underline", ansi_highlight_blue_underline }, + { "highlight-magenta-underline", ansi_highlight_magenta_underline }, + { "highlight-grey-underline", ansi_highlight_grey_underline }, +}; 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("magenta", 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()); + for (size_t i = 0; i < ELEMENTSOF(colors); i++) + printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal()); } int main(int argc, char *argv[]) { From 37873ebfbc3e36dce877e92d211dfe2daaa6c9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 23 Jul 2020 10:14:20 +0200 Subject: [PATCH 2/3] test-terminal-util: print paragraphs of text in different colors --- src/test/test-terminal-util.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/test/test-terminal-util.c b/src/test/test-terminal-util.c index 565cd18f97..d7af369d33 100644 --- a/src/test/test-terminal-util.c +++ b/src/test/test-terminal-util.c @@ -15,6 +15,12 @@ #include "tmpfile-util.h" #include "util.h" +#define LOREM_IPSUM "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " \ + "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation " \ + "ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit " \ + "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " \ + "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." + static void test_default_term_for_tty(void) { log_info("/* %s */", __func__); @@ -129,6 +135,23 @@ static void test_colors(void) { printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal()); } +static void test_text(void) { + log_info("/* %s */", __func__); + + for (size_t i = 0; !streq(colors[i].name, "underline"); i++) { + bool blwh = strstr(colors[i].name, "black") + || strstr(colors[i].name, "white"); + + printf("\n" + "Testing color %s%s\n%s%s%s\n", + colors[i].name, + blwh ? "" : ", this text should be readable", + colors[i].func(), + LOREM_IPSUM, + ansi_normal()); + } +} + int main(int argc, char *argv[]) { test_setup_logging(LOG_INFO); @@ -136,6 +159,7 @@ int main(int argc, char *argv[]) { test_read_one_char(); test_getttyname_malloc(); test_colors(); + test_text(); return 0; } From 5ee27251cd77f4acf3a798b706d9074d37ef8f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 23 Jul 2020 10:53:48 +0200 Subject: [PATCH 3/3] basic/terminal-util: rename our replacement highlight-yellow and test both the original and replacement The fact that we cannot use the "real" yellow color and need to use some other color is essentially a bug. In this particular case the issue is in urxvt, see a9009769de43b7f29d05017f4219e18c1e42a273. While we work around this by using a different color, let's not forget that this is just a (hopefullly temporary) workaround. https://bugzilla.redhat.com/show_bug.cgi?id=1859886 https://bugzilla.redhat.com/show_bug.cgi?id=1859889 https://bugzilla.redhat.com/show_bug.cgi?id=1859894 --- src/basic/terminal-util.h | 27 +++++++++++++++++---------- src/test/test-terminal-util.c | 3 ++- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index 9e824b0eee..42a129c7ac 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -33,16 +33,19 @@ #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;38;5;185m" -#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" -#define ANSI_HIGHLIGHT_YELLOW4 "\x1B[0;1;38;5;100m" +#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" /* This yellow is currently not displayed well by some terminals */ +#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_YELLOW4 "\x1B[0;1;38;5;100m" +#define ANSI_HIGHLIGHT_KHAKI3 "\x1B[0;1;38;5;185m" +#define ANSI_HIGHLIGHT_GREY "\x1B[0;1;38;5;245m" + +#define ANSI_HIGHLIGHT_YELLOW ANSI_HIGHLIGHT_KHAKI3 /* Replacement yellow that is more legible */ /* Underlined */ #define ANSI_GREY_UNDERLINE "\x1B[0;4;38;5;245m" @@ -170,6 +173,10 @@ DEFINE_ANSI_FUNC(highlight_cyan, HIGHLIGHT_CYAN); DEFINE_ANSI_FUNC(highlight_grey, HIGHLIGHT_GREY); DEFINE_ANSI_FUNC(highlight_white, HIGHLIGHT_WHITE); +static inline const char* _ansi_highlight_yellow(void) { + return colors_enabled() ? _ANSI_HIGHLIGHT_YELLOW : ""; +} + DEFINE_ANSI_FUNC_UNDERLINE(underline, UNDERLINE, NORMAL); DEFINE_ANSI_FUNC_UNDERLINE(highlight_underline, HIGHLIGHT_UNDERLINE, HIGHLIGHT); DEFINE_ANSI_FUNC_UNDERLINE(grey_underline, GREY_UNDERLINE, GREY); diff --git a/src/test/test-terminal-util.c b/src/test/test-terminal-util.c index d7af369d33..003257111a 100644 --- a/src/test/test-terminal-util.c +++ b/src/test/test-terminal-util.c @@ -111,7 +111,8 @@ static const Color colors[] = { { "highlight-black", ansi_highlight_black }, { "highlight-red", ansi_highlight_red }, { "highlight-green", ansi_highlight_green }, - { "highlight-yellow", ansi_highlight_yellow }, + { "highlight-yellow (original)", _ansi_highlight_yellow }, + { "highlight-yellow (replacement)", ansi_highlight_yellow }, { "highlight-blue", ansi_highlight_blue }, { "highlight-magenta", ansi_highlight_magenta }, { "highlight-cyan", ansi_highlight_cyan },