pretty-print: split out color tinting into a helper of its own
This commit is contained in:
+7
-26
@@ -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;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user