Skip to content

Commit

Permalink
Emit dedicated bright terminal color codes if supported
Browse files Browse the repository at this point in the history
Some terminals support having a bold normal foreground color which is
_not_ bright. kitty is one of them, as well as GNOME Terminal with the
fairly recent "Show bold text in bright colors" option turned off (or
when calling the underlying vte_terminal_set_bold_is_bright() function
with the appropriate value).

An easy test is to execute:

    $ echo -e "\033[31mTHIS\n\033[1mTHAT\033[0m"

and compare the colors of both lines: if they are the same, then the
terminal supports non-bright bold foreground colors.

For those terminals, the way to have a non-bold bright color is to emit
the dedicated SGR codes 90 to 97:

    $ echo -e "\033[91mHELLO\033[0m"

Some terminals can print non-bold bright colors, but cannot print
non-bright bold colors.

This patch makes the Babeltrace project emit different color codes
depending on the connected terminal and the new
`BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` environment variable's value:

kitty or `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` is `0`:
    Output bright colors using dedicated SGR codes 90 to 97.

Otherwise:
    Output bright colors with bold + SGR codes 30 to 37.

This patch effectively makes the Babeltrace modules emit correct bright
color codes for kitty.

To change as little as possible, I updated all the sites which emit a
bold code followed by a foreground color code to emit instead a bold
color code followed by a bright foreground color code. The only drawback
with the current approach is that, for non-kitty terminals, two bold
color codes are emitted for those cases (the real bold and the
brightening bold).

This means all bold colored text is also bright currently. Eventually we
can start decoupling the bold attribute from bright colors if need be,
although this means users must have a supporting terminal.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I3e0124942294fbe833d33aa59506c67e6ca85700
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2328
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
  • Loading branch information
eepp authored and jgalar committed Nov 14, 2019
1 parent 7c7324d commit 4253e1e
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 97 deletions.
6 changes: 6 additions & 0 deletions doc/man/common-common-env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ The available values are:
`ALWAYS`::
Always emit terminal color codes.
--

`BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD`=`0`::
Set to `0` to emit
https://en.wikipedia.org/wiki/ANSI_escape_code[SGR]
codes 90 to 97 for bright colors instead of
bold (SGR code~1) and standard color codes (SGR codes 30 to 37).
20 changes: 10 additions & 10 deletions src/cli/babeltrace2.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name,

fprintf(fh, "'%s%s%s%s",
bt_common_color_bold(),
bt_common_color_fg_cyan(),
bt_common_color_fg_bright_cyan(),
component_type_str(type),
bt_common_color_fg_default());

Expand Down Expand Up @@ -367,31 +367,31 @@ void print_value_rec(FILE *fp, const bt_value *value, size_t indent)
case BT_VALUE_TYPE_BOOL:
bool_val = bt_value_bool_get(value);
fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
bt_common_color_fg_cyan(), bool_val ? "yes" : "no",
bt_common_color_fg_bright_cyan(), bool_val ? "yes" : "no",
bt_common_color_reset());
break;
case BT_VALUE_TYPE_UNSIGNED_INTEGER:
uint_val = bt_value_integer_unsigned_get(value);
fprintf(fp, "%s%s%" PRIu64 "%s\n", bt_common_color_bold(),
bt_common_color_fg_red(), uint_val,
bt_common_color_fg_bright_red(), uint_val,
bt_common_color_reset());
break;
case BT_VALUE_TYPE_SIGNED_INTEGER:
int_val = bt_value_integer_signed_get(value);
fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(),
bt_common_color_fg_red(), int_val,
bt_common_color_fg_bright_red(), int_val,
bt_common_color_reset());
break;
case BT_VALUE_TYPE_REAL:
dbl_val = bt_value_real_get(value);
fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(),
bt_common_color_fg_red(), dbl_val,
bt_common_color_fg_bright_red(), dbl_val,
bt_common_color_reset());
break;
case BT_VALUE_TYPE_STRING:
str_val = bt_value_string_get(value);
fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
bt_common_color_fg_green(), str_val,
bt_common_color_fg_bright_green(), str_val,
bt_common_color_reset());
break;
case BT_VALUE_TYPE_ARRAY:
Expand Down Expand Up @@ -659,7 +659,7 @@ void print_plugin_info(const bt_plugin *plugin)
version_avail = bt_plugin_get_version(plugin, &major, &minor,
&patch, &extra);
printf("%s%s%s%s:\n", bt_common_color_bold(),
bt_common_color_fg_blue(), plugin_name,
bt_common_color_fg_bright_blue(), plugin_name,
bt_common_color_reset());
if (path) {
printf(" %sPath%s: %s\n", bt_common_color_bold(),
Expand Down Expand Up @@ -2642,7 +2642,7 @@ void print_error_causes(void)

if (!error || bt_error_get_cause_count(error) == 0) {
fprintf(stderr, "%s%sUnknown command-line error.%s\n",
bt_common_color_bold(), bt_common_color_fg_red(),
bt_common_color_bold(), bt_common_color_fg_bright_red(),
bt_common_color_reset());
goto end;
}
Expand All @@ -2669,7 +2669,7 @@ void print_error_causes(void)

/* Print prefix */
fprintf(stderr, prefix_fmt,
bt_common_color_bold(), bt_common_color_fg_red(),
bt_common_color_bold(), bt_common_color_fg_bright_red(),
bt_common_color_reset());

/* Print actor name */
Expand Down Expand Up @@ -2717,7 +2717,7 @@ void print_error_causes(void)
/* Print file name and line number */
fprintf(stderr, "] (%s%s%s%s:%s%" PRIu64 "%s)\n",
bt_common_color_bold(),
bt_common_color_fg_magenta(),
bt_common_color_fg_bright_magenta(),
bt_error_cause_get_file_name(cause),
bt_common_color_reset(),
bt_common_color_fg_green(),
Expand Down
7 changes: 4 additions & 3 deletions src/common/assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ void bt_common_assert_failed(const char *file, int line, const char *func,
{
fprintf(stderr,
"%s\n%s%s%s (╯°□°)╯︵ ┻━┻ %s %s%s%s%s:%s%d%s: %s%s()%s: "
"%sAssertion %s`%s`%s%s failed.%s\n",
"%sAssertion %s%s`%s`%s%s failed.%s\n",
bt_common_color_reset(),
bt_common_color_bold(),
bt_common_color_bg_yellow(),
bt_common_color_fg_red(),
bt_common_color_fg_bright_red(),
bt_common_color_reset(),
bt_common_color_bold(),
bt_common_color_fg_magenta(),
bt_common_color_fg_bright_magenta(),
file,
bt_common_color_reset(),
bt_common_color_fg_green(),
Expand All @@ -48,6 +48,7 @@ void bt_common_assert_failed(const char *file, int line, const char *func,
bt_common_color_reset(),
bt_common_color_fg_red(),
bt_common_color_bold(),
bt_common_color_fg_bright_red(),
assertion,
bt_common_color_reset(),
bt_common_color_fg_red(),
Expand Down
125 changes: 125 additions & 0 deletions src/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ static const char *bt_common_color_code_fg_blue = "";
static const char *bt_common_color_code_fg_magenta = "";
static const char *bt_common_color_code_fg_cyan = "";
static const char *bt_common_color_code_fg_light_gray = "";
static const char *bt_common_color_code_fg_bright_red = "";
static const char *bt_common_color_code_fg_bright_green = "";
static const char *bt_common_color_code_fg_bright_yellow = "";
static const char *bt_common_color_code_fg_bright_blue = "";
static const char *bt_common_color_code_fg_bright_magenta = "";
static const char *bt_common_color_code_fg_bright_cyan = "";
static const char *bt_common_color_code_fg_bright_light_gray = "";
static const char *bt_common_color_code_bg_default = "";
static const char *bt_common_color_code_bg_red = "";
static const char *bt_common_color_code_bg_green = "";
Expand All @@ -76,6 +83,10 @@ static
void __attribute__((constructor)) bt_common_color_ctor(void)
{
if (bt_common_colors_supported()) {
const char *term_env_var;
const char *bright_means_bold_env_var;
bool bright_means_bold = true;

bt_common_color_code_reset = BT_COMMON_COLOR_RESET;
bt_common_color_code_bold = BT_COMMON_COLOR_BOLD;
bt_common_color_code_fg_default = BT_COMMON_COLOR_FG_DEFAULT;
Expand All @@ -86,6 +97,78 @@ void __attribute__((constructor)) bt_common_color_ctor(void)
bt_common_color_code_fg_magenta = BT_COMMON_COLOR_FG_MAGENTA;
bt_common_color_code_fg_cyan = BT_COMMON_COLOR_FG_CYAN;
bt_common_color_code_fg_light_gray = BT_COMMON_COLOR_FG_LIGHT_GRAY;

/*
* Check whether or not the terminal supports having
* bold foreground colors which do _not_ become bright
* colors, that is, the lines
*
* $ echo -e "\033[31mTHIS\n\033[1mTHAT\033[0m"
*
* have the _same_ color, but `THAT` uses a bold font.
*
* This is the case of the kitty terminal emulator.
*
* It's also possible with GNOME Terminal since 3.27.2
* and xfce4-terminal since 0.8.7 (and GNOME VTE since
* 0.51.2), but it's user-configurable. Since we don't
* have this configuration value here, assume it's not
* the case to support old versions of GNOME Terminal.
*
* Any user can set the
* `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` environment
* variable to `0` to use the bright foreground color
* codes instead of making the normal foreground color
* codes bold.
*
* Summary:
*
* With kitty or when
* `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` is `0`:
* Output bright colors using dedicated SGR codes
* 90 to 97.
*
* Otherwise:
* Output bright colors with bold + SGR codes 30 to
* 37.
*/
term_env_var = getenv("TERM");
BT_ASSERT(term_env_var);

if (strcmp(term_env_var, "xterm-kitty") == 0) {
/*
* The kitty terminal emulator supports
* non-bright bold foreground colors.
*/
bright_means_bold = false;
}

bright_means_bold_env_var =
getenv("BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD");

if (bright_means_bold_env_var) {
bright_means_bold =
!(strcmp(bright_means_bold_env_var, "0") == 0);
}

if (bright_means_bold) {
bt_common_color_code_fg_bright_red = BT_COMMON_COLOR_FG_BOLD_RED;
bt_common_color_code_fg_bright_green = BT_COMMON_COLOR_FG_BOLD_GREEN;
bt_common_color_code_fg_bright_yellow = BT_COMMON_COLOR_FG_BOLD_YELLOW;
bt_common_color_code_fg_bright_blue = BT_COMMON_COLOR_FG_BOLD_BLUE;
bt_common_color_code_fg_bright_magenta = BT_COMMON_COLOR_FG_BOLD_MAGENTA;
bt_common_color_code_fg_bright_cyan = BT_COMMON_COLOR_FG_BOLD_CYAN;
bt_common_color_code_fg_bright_light_gray = BT_COMMON_COLOR_FG_BOLD_LIGHT_GRAY;
} else {
bt_common_color_code_fg_bright_red = BT_COMMON_COLOR_FG_BRIGHT_RED;
bt_common_color_code_fg_bright_green = BT_COMMON_COLOR_FG_BRIGHT_GREEN;
bt_common_color_code_fg_bright_yellow = BT_COMMON_COLOR_FG_BRIGHT_YELLOW;
bt_common_color_code_fg_bright_blue = BT_COMMON_COLOR_FG_BRIGHT_BLUE;
bt_common_color_code_fg_bright_magenta = BT_COMMON_COLOR_FG_BRIGHT_MAGENTA;
bt_common_color_code_fg_bright_cyan = BT_COMMON_COLOR_FG_BRIGHT_CYAN;
bt_common_color_code_fg_bright_light_gray = BT_COMMON_COLOR_FG_BRIGHT_LIGHT_GRAY;
}

bt_common_color_code_bg_default = BT_COMMON_COLOR_BG_DEFAULT;
bt_common_color_code_bg_red = BT_COMMON_COLOR_BG_RED;
bt_common_color_code_bg_green = BT_COMMON_COLOR_BG_GREEN;
Expand Down Expand Up @@ -388,6 +471,48 @@ const char *bt_common_color_fg_light_gray(void)
return bt_common_color_code_fg_light_gray;
}

BT_HIDDEN
const char *bt_common_color_fg_bright_red(void)
{
return bt_common_color_code_fg_bright_red;
}

BT_HIDDEN
const char *bt_common_color_fg_bright_green(void)
{
return bt_common_color_code_fg_bright_green;
}

BT_HIDDEN
const char *bt_common_color_fg_bright_yellow(void)
{
return bt_common_color_code_fg_bright_yellow;
}

BT_HIDDEN
const char *bt_common_color_fg_bright_blue(void)
{
return bt_common_color_code_fg_bright_blue;
}

BT_HIDDEN
const char *bt_common_color_fg_bright_magenta(void)
{
return bt_common_color_code_fg_bright_magenta;
}

BT_HIDDEN
const char *bt_common_color_fg_bright_cyan(void)
{
return bt_common_color_code_fg_bright_cyan;
}

BT_HIDDEN
const char *bt_common_color_fg_bright_light_gray(void)
{
return bt_common_color_code_fg_bright_light_gray;
}

BT_HIDDEN
const char *bt_common_color_bg_default(void)
{
Expand Down
71 changes: 53 additions & 18 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,38 @@
#include "common/macros.h"
#include "common/safe.h"

#define BT_COMMON_COLOR_RESET "\033[0m"
#define BT_COMMON_COLOR_BOLD "\033[1m"
#define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
#define BT_COMMON_COLOR_FG_RED "\033[31m"
#define BT_COMMON_COLOR_FG_GREEN "\033[32m"
#define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
#define BT_COMMON_COLOR_FG_BLUE "\033[34m"
#define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
#define BT_COMMON_COLOR_FG_CYAN "\033[36m"
#define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
#define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
#define BT_COMMON_COLOR_BG_RED "\033[41m"
#define BT_COMMON_COLOR_BG_GREEN "\033[42m"
#define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
#define BT_COMMON_COLOR_BG_BLUE "\033[44m"
#define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
#define BT_COMMON_COLOR_BG_CYAN "\033[46m"
#define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
#define BT_COMMON_COLOR_RESET "\033[0m"
#define BT_COMMON_COLOR_BOLD "\033[1m"
#define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
#define BT_COMMON_COLOR_FG_RED "\033[31m"
#define BT_COMMON_COLOR_FG_GREEN "\033[32m"
#define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
#define BT_COMMON_COLOR_FG_BLUE "\033[34m"
#define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
#define BT_COMMON_COLOR_FG_CYAN "\033[36m"
#define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
#define BT_COMMON_COLOR_FG_BOLD_RED "\033[1m\033[31m"
#define BT_COMMON_COLOR_FG_BOLD_GREEN "\033[1m\033[32m"
#define BT_COMMON_COLOR_FG_BOLD_YELLOW "\033[1m\033[33m"
#define BT_COMMON_COLOR_FG_BOLD_BLUE "\033[1m\033[34m"
#define BT_COMMON_COLOR_FG_BOLD_MAGENTA "\033[1m\033[35m"
#define BT_COMMON_COLOR_FG_BOLD_CYAN "\033[1m\033[36m"
#define BT_COMMON_COLOR_FG_BOLD_LIGHT_GRAY "\033[1m\033[37m"
#define BT_COMMON_COLOR_FG_BRIGHT_RED "\033[91m"
#define BT_COMMON_COLOR_FG_BRIGHT_GREEN "\033[92m"
#define BT_COMMON_COLOR_FG_BRIGHT_YELLOW "\033[93m"
#define BT_COMMON_COLOR_FG_BRIGHT_BLUE "\033[94m"
#define BT_COMMON_COLOR_FG_BRIGHT_MAGENTA "\033[95m"
#define BT_COMMON_COLOR_FG_BRIGHT_CYAN "\033[96m"
#define BT_COMMON_COLOR_FG_BRIGHT_LIGHT_GRAY "\033[97m"
#define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
#define BT_COMMON_COLOR_BG_RED "\033[41m"
#define BT_COMMON_COLOR_BG_GREEN "\033[42m"
#define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
#define BT_COMMON_COLOR_BG_BLUE "\033[44m"
#define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
#define BT_COMMON_COLOR_BG_CYAN "\033[46m"
#define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"

struct bt_common_lttng_live_url_parts {
GString *proto;
Expand Down Expand Up @@ -140,6 +154,27 @@ const char *bt_common_color_fg_cyan(void);
BT_HIDDEN
const char *bt_common_color_fg_light_gray(void);

BT_HIDDEN
const char *bt_common_color_fg_bright_red(void);

BT_HIDDEN
const char *bt_common_color_fg_bright_green(void);

BT_HIDDEN
const char *bt_common_color_fg_bright_yellow(void);

BT_HIDDEN
const char *bt_common_color_fg_bright_blue(void);

BT_HIDDEN
const char *bt_common_color_fg_bright_magenta(void);

BT_HIDDEN
const char *bt_common_color_fg_bright_cyan(void);

BT_HIDDEN
const char *bt_common_color_fg_bright_light_gray(void);

BT_HIDDEN
const char *bt_common_color_bg_default(void);

Expand Down
Loading

0 comments on commit 4253e1e

Please sign in to comment.