Compare commits

..

4 Commits

Author SHA1 Message Date
Lennart Poettering 1efe30ded3
Merge pull request #16556 from keszybz/test-terminal-colors
Test terminal colors
2020-08-05 09:35:16 +02:00
Zbigniew Jędrzejewski-Szmek 5ee27251cd 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
a9009769de. 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
2020-07-30 14:43:02 +02:00
Zbigniew Jędrzejewski-Szmek 37873ebfbc test-terminal-util: print paragraphs of text in different colors 2020-07-30 14:41:15 +02:00
Zbigniew Jędrzejewski-Szmek 66bb00590f 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.
2020-07-23 15:04:15 +02:00
2 changed files with 119 additions and 32 deletions

View File

@ -11,21 +11,41 @@
#include "time-util.h" #include "time-util.h"
/* Regular colors */ /* Regular colors */
#define ANSI_BLACK "\x1B[0;30m" /* Some type of grey usually. */
#define ANSI_RED "\x1B[0;31m" #define ANSI_RED "\x1B[0;31m"
#define ANSI_GREEN "\x1B[0;32m" #define ANSI_GREEN "\x1B[0;32m"
#define ANSI_YELLOW "\x1B[0;33m" #define ANSI_YELLOW "\x1B[0;33m"
#define ANSI_BLUE "\x1B[0;34m" #define ANSI_BLUE "\x1B[0;34m"
#define ANSI_MAGENTA "\x1B[0;35m" #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" #define ANSI_GREY "\x1B[0;38;5;245m"
/* Bold/highlighted */ /* Bold/highlighted */
#define ANSI_HIGHLIGHT_BLACK "\x1B[0;1;30m"
#define ANSI_HIGHLIGHT_RED "\x1B[0;1;31m" #define ANSI_HIGHLIGHT_RED "\x1B[0;1;31m"
#define ANSI_HIGHLIGHT_GREEN "\x1B[0;1;32m" #define ANSI_HIGHLIGHT_GREEN "\x1B[0;1;32m"
#define ANSI_HIGHLIGHT_YELLOW "\x1B[0;1;38;5;185m" #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_BLUE "\x1B[0;1;34m"
#define ANSI_HIGHLIGHT_MAGENTA "\x1B[0;1;35m" #define ANSI_HIGHLIGHT_MAGENTA "\x1B[0;1;35m"
#define ANSI_HIGHLIGHT_GREY "\x1B[0;1;38;5;245m" #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_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 */ /* Underlined */
#define ANSI_GREY_UNDERLINE "\x1B[0;4;38;5;245m" #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(normal, NORMAL);
DEFINE_ANSI_FUNC(highlight, HIGHLIGHT); DEFINE_ANSI_FUNC(highlight, HIGHLIGHT);
DEFINE_ANSI_FUNC(black, BLACK);
DEFINE_ANSI_FUNC(red, RED); DEFINE_ANSI_FUNC(red, RED);
DEFINE_ANSI_FUNC(green, GREEN); DEFINE_ANSI_FUNC(green, GREEN);
DEFINE_ANSI_FUNC(yellow, YELLOW); DEFINE_ANSI_FUNC(yellow, YELLOW);
DEFINE_ANSI_FUNC(blue, BLUE); DEFINE_ANSI_FUNC(blue, BLUE);
DEFINE_ANSI_FUNC(magenta, MAGENTA); DEFINE_ANSI_FUNC(magenta, MAGENTA);
DEFINE_ANSI_FUNC(cyan, CYAN);
DEFINE_ANSI_FUNC(white, WHITE);
DEFINE_ANSI_FUNC(grey, GREY); 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_red, HIGHLIGHT_RED);
DEFINE_ANSI_FUNC(highlight_green, HIGHLIGHT_GREEN); DEFINE_ANSI_FUNC(highlight_green, HIGHLIGHT_GREEN);
DEFINE_ANSI_FUNC(highlight_yellow, HIGHLIGHT_YELLOW); DEFINE_ANSI_FUNC(highlight_yellow, HIGHLIGHT_YELLOW);
DEFINE_ANSI_FUNC(highlight_blue, HIGHLIGHT_BLUE); DEFINE_ANSI_FUNC(highlight_blue, HIGHLIGHT_BLUE);
DEFINE_ANSI_FUNC(highlight_magenta, HIGHLIGHT_MAGENTA); 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_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(underline, UNDERLINE, NORMAL);
DEFINE_ANSI_FUNC_UNDERLINE(highlight_underline, HIGHLIGHT_UNDERLINE, HIGHLIGHT); DEFINE_ANSI_FUNC_UNDERLINE(highlight_underline, HIGHLIGHT_UNDERLINE, HIGHLIGHT);

View File

@ -15,6 +15,12 @@
#include "tmpfile-util.h" #include "tmpfile-util.h"
#include "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) { static void test_default_term_for_tty(void) {
log_info("/* %s */", __func__); log_info("/* %s */", __func__);
@ -75,36 +81,76 @@ static void test_getttyname_malloc(void) {
assert_se(PATH_IN_SET(ttyname, "ptmx", "pts/ptmx")); assert_se(PATH_IN_SET(ttyname, "ptmx", "pts/ptmx"));
} }
static void test_one_color(const char *name, const char *color) { typedef struct {
printf("<%s%s%s>\n", color, name, ansi_normal()); 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) { static void test_colors(void) {
log_info("/* %s */", __func__); log_info("/* %s */", __func__);
test_one_color("normal", ansi_normal()); for (size_t i = 0; i < ELEMENTSOF(colors); i++)
test_one_color("highlight", ansi_highlight()); printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal());
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()); static void test_text(void) {
test_one_color("highlight-underline", ansi_highlight_underline()); log_info("/* %s */", __func__);
test_one_color("highlight-red-underline", ansi_highlight_red_underline());
test_one_color("highlight-green-underline", ansi_highlight_green_underline()); for (size_t i = 0; !streq(colors[i].name, "underline"); i++) {
test_one_color("highlight-yellow-underline", ansi_highlight_yellow_underline()); bool blwh = strstr(colors[i].name, "black")
test_one_color("highlight-blue-underline", ansi_highlight_blue_underline()); || strstr(colors[i].name, "white");
test_one_color("highlight-magenta-underline", ansi_highlight_magenta_underline());
test_one_color("highlight-grey-underline", ansi_highlight_grey_underline()); 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[]) { int main(int argc, char *argv[]) {
@ -114,6 +160,7 @@ int main(int argc, char *argv[]) {
test_read_one_char(); test_read_one_char();
test_getttyname_malloc(); test_getttyname_malloc();
test_colors(); test_colors();
test_text();
return 0; return 0;
} }