]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pretty-print: split out color tinting into a helper of its own
authorLennart Poettering <lennart@poettering.net>
Fri, 19 Jan 2024 22:59:54 +0000 (23:59 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 23 Jan 2024 15:45:37 +0000 (16:45 +0100)
src/run/run.c
src/shared/pretty-print.c
src/shared/pretty-print.h

index c3313c6c529fae92641a7f62abe51895c573b46f..a695fea0f07b1405d1e4103f34e0a9e6b2cf3c68 100644 (file)
@@ -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;
index cb1ac2bb768e6bf2b29930d3f7cc4048f73765e7..7dd31a099f5d6f722ef70c539b44a1c5ab790a29 100644 (file)
@@ -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;
+}
index c17e976580dab9331fc6a4c2f8c21dedccafe1e7..6069318be7778e17f5de6b398ebee9fe823c1cd2 100644 (file)
@@ -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);