diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index 67634b4d46..42a129c7ac 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -11,21 +11,41 @@ #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_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_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" @@ -124,18 +144,38 @@ 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); + +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); diff --git a/src/test/test-terminal-util.c b/src/test/test-terminal-util.c index 52e651faef..003257111a 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__); @@ -75,36 +81,76 @@ 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 (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 }, + { "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()); + for (size_t i = 0; i < ELEMENTSOF(colors); i++) + printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal()); +} - 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()); +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[]) { @@ -114,6 +160,7 @@ int main(int argc, char *argv[]) { test_read_one_char(); test_getttyname_malloc(); test_colors(); + test_text(); return 0; }