common: introduce struct bt_common_color_codes and function bt_common_color_get_codes
The behavior of the color functions is currently initialized at startup.
If we detect that colors should be enabled, we set the
bt_common_color_code_* variables once and for all.
The following patches will introduce string formatting functions that
optionally use color, based on a parameter with three values:
- always: always use colors, regardless of what we detected at startup
- never: never use colors, regardless of what we detected at startup
- auto: use colors based on what we detected at startup
One option would be to add some color functions that take a parameter,
so you could do:
bt_common_color_bg_cyan_opt(BT_COMMON_COLOR_WHEN_ALWAYS)
bt_common_color_bg_cyan_opt(BT_COMMON_COLOR_WHEN_NEVER)
bt_common_color_bg_cyan_opt(BT_COMMON_COLOR_WHEN_AUTO)
However, I find this very verbose, so not very practical.
This patch introduces a new structure type bt_common_color_codes that
holds the color codes for the current terminal, or empty strings. It
also introduces a function bt_common_color_get_codes that accepts an
always/never/auto parameter. It initializes a bt_common_color_codes
structure with the color codes (or empty strings) to use.
This will allow the string formatting function to conditionally use
colors without being too verbose:
struct bt_common_color_codes codes;
bt_common_color_get_codes(&codes, use_colors);
printf("%sHello%s\n", codes.fg_cyan, codes.reset);
Change-Id: I887e736291e9e1fe54156a81ce4011b3906d6f3c
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2431
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
This page took 0.024445 seconds and 4 git commands to generate.