]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: use underlines to seperate unit types in listing
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 17 Oct 2016 15:37:41 +0000 (11:37 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 17 Oct 2016 15:37:41 +0000 (11:37 -0400)
(printf("%.*s", -1, "…") is the same as not specifying the precision at all.)

v2: also underline highlighted (failing) units

Fixes #4137.

src/basic/terminal-util.h
src/systemctl/systemctl.c

index 169ab772ffd97141398f354158a9b8f578cb5bad..6b47c17278fa32c7bcd5d1d55056a0379effbdb5 100644 (file)
 #define ANSI_HIGHLIGHT_YELLOW "\x1B[0;1;33m"
 #define ANSI_HIGHLIGHT_BLUE "\x1B[0;1;34m"
 #define ANSI_HIGHLIGHT_UNDERLINE "\x1B[0;1;4m"
+#define ANSI_HIGHLIGHT_RED_UNDERLINE "\x1B[0;1;4;31m"
+#define ANSI_HIGHLIGHT_GREEN_UNDERLINE "\x1B[0;1;4;32m"
+#define ANSI_HIGHLIGHT_YELLOW_UNDERLINE "\x1B[0;1;4;33m"
+#define ANSI_HIGHLIGHT_BLUE_UNDERLINE "\x1B[0;1;4;34m"
 #define ANSI_NORMAL "\x1B[0m"
 
 #define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
@@ -111,6 +115,22 @@ static inline const char *ansi_highlight_blue(void) {
         return colors_enabled() ? ANSI_HIGHLIGHT_BLUE : "";
 }
 
+static inline const char *ansi_highlight_red_underline(void) {
+        return colors_enabled() ? ANSI_HIGHLIGHT_RED_UNDERLINE : "";
+}
+
+static inline const char *ansi_highlight_green_underline(void) {
+        return colors_enabled() ? ANSI_HIGHLIGHT_GREEN_UNDERLINE : "";
+}
+
+static inline const char *ansi_highlight_yellow_underline(void) {
+        return colors_enabled() ? ANSI_HIGHLIGHT_YELLOW_UNDERLINE : "";
+}
+
+static inline const char *ansi_highlight_blue_underline(void) {
+        return colors_enabled() ? ANSI_HIGHLIGHT_BLUE_UNDERLINE : "";
+}
+
 static inline const char *ansi_normal(void) {
         return colors_enabled() ? ANSI_NORMAL : "";
 }
index 7ed60dbe87859b129d783454b3293ce90cdd54ff..8b08b1762f3481ba9ed8a6e91a908a4886dc1593 100644 (file)
@@ -362,22 +362,24 @@ static int compare_unit_info(const void *a, const void *b) {
         return strcasecmp(u->id, v->id);
 }
 
+static const char* unit_type_suffix(const char *name) {
+        const char *dot;
+
+        dot = strrchr(name, '.');
+        if (!dot)
+                return "";
+
+        return dot + 1;
+}
+
 static bool output_show_unit(const UnitInfo *u, char **patterns) {
         assert(u);
 
         if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE))
                 return false;
 
-        if (arg_types) {
-                const char *dot;
-
-                dot = strrchr(u->id, '.');
-                if (!dot)
-                        return false;
-
-                if (!strv_find(arg_types, dot+1))
-                        return false;
-        }
+        if (arg_types && !strv_find(arg_types, unit_type_suffix(u->id)))
+                return false;
 
         if (arg_all)
                 return true;
@@ -403,10 +405,10 @@ static bool output_show_unit(const UnitInfo *u, char **patterns) {
 }
 
 static int output_units_list(const UnitInfo *unit_infos, unsigned c) {
-        unsigned circle_len = 0, id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len;
+        unsigned circle_len = 0, id_len, max_id_len, load_len, active_len, sub_len, job_len;
         const UnitInfo *u;
         unsigned n_shown = 0;
-        int job_count = 0;
+        int job_count = 0, desc_len;
 
         max_id_len = strlen("UNIT");
         load_len = strlen("LOAD");
@@ -464,18 +466,20 @@ static int output_units_list(const UnitInfo *unit_infos, unsigned c) {
 
         for (u = unit_infos; u < unit_infos + c; u++) {
                 _cleanup_free_ char *e = NULL, *j = NULL;
+                const char *on_underline = "", *off_underline = "";
                 const char *on_loaded = "", *off_loaded = "";
                 const char *on_active = "", *off_active = "";
                 const char *on_circle = "", *off_circle = "";
                 const char *id;
-                bool circle = false;
+                bool circle = false, underline = false;
 
                 if (!n_shown && !arg_no_legend) {
 
                         if (circle_len > 0)
                                 fputs("  ", stdout);
 
-                        printf("%-*s %-*s %-*s %-*s ",
+                        printf("%s%-*s %-*s %-*s %-*s ",
+                               ansi_underline(),
                                id_len, "UNIT",
                                load_len, "LOAD",
                                active_len, "ACTIVE",
@@ -484,23 +488,33 @@ static int output_units_list(const UnitInfo *unit_infos, unsigned c) {
                         if (job_count)
                                 printf("%-*s ", job_len, "JOB");
 
-                        if (!arg_full && arg_no_pager)
-                                printf("%.*s\n", desc_len, "DESCRIPTION");
-                        else
-                                printf("%s\n", "DESCRIPTION");
+                        printf("%.*s%s\n",
+                               !arg_full && arg_no_pager ? desc_len : -1,
+                               "DESCRIPTION",
+                               ansi_normal());
                 }
 
                 n_shown++;
 
+                if (u + 1 < unit_infos + c &&
+                    !streq(unit_type_suffix(u->id), unit_type_suffix((u + 1)->id))) {
+                        on_underline = ansi_underline();
+                        off_underline = ansi_normal();
+                        underline = true;
+                }
+
                 if (STR_IN_SET(u->load_state, "error", "not-found", "masked") && !arg_plain) {
-                        on_loaded = ansi_highlight_red();
                         on_circle = ansi_highlight_yellow();
-                        off_loaded = off_circle = ansi_normal();
+                        off_circle = ansi_normal();
                         circle = true;
+                        on_loaded = underline ? ansi_highlight_red_underline() : ansi_highlight_red();
+                        off_loaded = on_underline;
                 } else if (streq(u->active_state, "failed") && !arg_plain) {
-                        on_circle = on_active = ansi_highlight_red();
-                        off_circle = off_active = ansi_normal();
+                        on_circle = ansi_highlight_red();
+                        off_circle = ansi_normal();
                         circle = true;
+                        on_active = underline ? ansi_highlight_red_underline() : ansi_highlight_red();
+                        off_active = on_underline;
                 }
 
                 if (u->machine) {
@@ -523,17 +537,18 @@ static int output_units_list(const UnitInfo *unit_infos, unsigned c) {
                 if (circle_len > 0)
                         printf("%s%s%s ", on_circle, circle ? special_glyph(BLACK_CIRCLE) : " ", off_circle);
 
-                printf("%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s",
+                printf("%s%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s",
+                       on_underline,
                        on_active, id_len, id, off_active,
                        on_loaded, load_len, u->load_state, off_loaded,
                        on_active, active_len, u->active_state,
                        sub_len, u->sub_state, off_active,
                        job_count ? job_len + 1 : 0, u->job_id ? u->job_type : "");
 
-                if (desc_len > 0)
-                        printf("%.*s\n", desc_len, u->description);
-                else
-                        printf("%s\n", u->description);
+                printf("%.*s%s\n",
+                       desc_len > 0 ? desc_len : -1,
+                       u->description,
+                       off_underline);
         }
 
         if (!arg_no_legend) {