From: Lennart Poettering Date: Fri, 19 Jan 2024 22:59:54 +0000 (+0100) Subject: pretty-print: split out color tinting into a helper of its own X-Git-Tag: v256-rc1~1061^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3ef072ee268f817730f81fe8f81a3072fa990b4f;p=thirdparty%2Fsystemd.git pretty-print: split out color tinting into a helper of its own --- diff --git a/src/run/run.c b/src/run/run.c index c3313c6c529..a695fea0f07 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -17,7 +17,6 @@ #include "bus-unit-util.h" #include "bus-wait-for-jobs.h" #include "calendarspec.h" -#include "color-util.h" #include "env-util.h" #include "escape.h" #include "exit-status.h" @@ -940,34 +939,16 @@ static int parse_argv_sudo_mode(int argc, char *argv[]) { return log_oom(); if (!arg_background && arg_stdio == ARG_STDIO_PTY) { - double red, green, blue; + double hue; - r = get_default_background_color(&red, &green, &blue); + if (!arg_exec_user || STR_IN_SET(arg_exec_user, "root", "0")) + hue = 0; /* red */ + else + hue = 60 /* yellow */; + + r = terminal_tint_color(hue, &arg_background); if (r < 0) log_debug_errno(r, "Unable to get terminal background color, not tinting background: %m"); - else { - double h, s, v; - - rgb_to_hsv(red, green, blue, &h, &s, &v); - - if (!arg_exec_user || STR_IN_SET(arg_exec_user, "root", "0")) - h = 0; /* red */ - else - h = 60 /* yellow */; - - if (v > 50) /* If the background is bright, then pull down saturation */ - s = 25; - else /* otherwise pump it up */ - s = 75; - - v = MAX(30, v); /* Make sure we don't hide the color in black */ - - uint8_t r8, g8, b8; - hsv_to_rgb(h, s, v, &r8, &g8, &b8); - - if (asprintf(&arg_background, "48;2;%u;%u;%u", r8, g8, b8) < 0) - return log_oom(); - } } return 1; diff --git a/src/shared/pretty-print.c b/src/shared/pretty-print.c index cb1ac2bb768..7dd31a099f5 100644 --- a/src/shared/pretty-print.c +++ b/src/shared/pretty-print.c @@ -5,6 +5,7 @@ #include #include "alloc-util.h" +#include "color-util.h" #include "conf-files.h" #include "constants.h" #include "env-util.h" @@ -441,3 +442,32 @@ int conf_files_cat(const char *root, const char *name, CatFlags flags) { return cat_files(path, files, flags); } + +int terminal_tint_color(double hue, char **ret) { + double red, green, blue; + int r; + + assert(ret); + + r = get_default_background_color(&red, &green, &blue); + if (r < 0) + return log_debug_errno(r, "Unable to get terminal background color: %m"); + + double s, v; + rgb_to_hsv(red, green, blue, /* h= */ NULL, &s, &v); + + if (v > 50) /* If the background is bright, then pull down saturation */ + s = 25; + else /* otherwise pump it up */ + s = 75; + + v = MAX(30, v); /* Make sure we don't hide the color in black */ + + uint8_t r8, g8, b8; + hsv_to_rgb(hue, s, v, &r8, &g8, &b8); + + if (asprintf(ret, "48;2;%u;%u;%u", r8, g8, b8) < 0) + return -ENOMEM; + + return 0; +} diff --git a/src/shared/pretty-print.h b/src/shared/pretty-print.h index c17e976580d..6069318be77 100644 --- a/src/shared/pretty-print.h +++ b/src/shared/pretty-print.h @@ -47,3 +47,5 @@ static inline const char *green_check_mark_internal(char buffer[static GREEN_CHE #define GREEN_CHECK_MARK() green_check_mark_internal((char[GREEN_CHECK_MARK_MAX]) {}) #define COLOR_MARK_BOOL(b) ((b) ? GREEN_CHECK_MARK() : RED_CROSS_MARK()) + +int terminal_tint_color(double hue, char **ret);