]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: port to the new underline cell attribute
authorLennart Poettering <lennart@poettering.net>
Mon, 20 Nov 2023 11:06:44 +0000 (12:06 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 20 Nov 2023 12:05:50 +0000 (13:05 +0100)
And be more careful when adding color to log lines:

1. Apply color derived from load state only to load state (and in
   special cases to the circle at the beginning of the line)

2. Apply color derived from active/sub states only to active/sub states
   (and in special cases to the circle at the beginning of the line)

3. Special case the "exited" state of services, i.e. make the substate
   grey, as in this case the service is active, yet consumes no
   resources.

4. Cover the "inactive" state properly, by greying it our too

5. Downgrade the load state of "not-found" from red to yellow, since
   it's not really a case one needs to be come active on.

src/systemctl/systemctl-list-units.c

index e48cf45333ba8f51a1abf43a8b13b4b23b68e8ea..be074663e2e602ef399f1bfd8e208e2c30094b40 100644 (file)
@@ -127,49 +127,70 @@ static int output_units_list(const UnitInfo *unit_infos, size_t c) {
         table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
 
         FOREACH_ARRAY(u, unit_infos, c) {
+                const char *on_loaded = NULL, *on_active = NULL, *on_sub = NULL, *on_circle = NULL;
                 _cleanup_free_ char *id = NULL;
-                const char *on_underline = "", *on_loaded = "", *on_active = "", *on_circle = "";
-                bool circle = false, underline = false;
+                bool circle = false, underline;
 
-                if (u + 1 < unit_infos + c &&
-                    !streq(unit_type_suffix(u->id), unit_type_suffix((u + 1)->id))) {
-                        on_underline = ansi_underline();
-                        underline = true;
-                }
+                underline = u + 1 < unit_infos + c && !streq(unit_type_suffix(u->id), unit_type_suffix((u + 1)->id));
 
-                if (STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked") && !arg_plain) {
-                        on_circle = underline ? ansi_highlight_yellow_underline() : ansi_highlight_yellow();
+                if (streq(u->load_state, "not-found")) {
+                        on_circle = on_loaded = ansi_highlight_yellow();
+                        on_circle = ansi_highlight_yellow();
                         circle = true;
-                        on_loaded = underline ? ansi_highlight_red_underline() : ansi_highlight_red();
-                } else if (streq(u->active_state, "failed") && !arg_plain) {
-                        on_circle = underline ? ansi_highlight_red_underline() : ansi_highlight_red();
+                } else if (STR_IN_SET(u->load_state, "bad-setting", "error", "masked")) {
+                        on_loaded = ansi_highlight_red();
+                        on_circle = ansi_highlight_yellow();
                         circle = true;
-                        on_active = underline ? ansi_highlight_red_underline() : ansi_highlight_red();
-                } else {
-                        on_circle = on_underline;
-                        on_active = on_underline;
-                        on_loaded = on_underline;
                 }
 
+                if (streq(u->active_state, "failed")) {
+                        on_sub = on_active = ansi_highlight_red();
+
+                        /* Here override any load_state highlighting */
+                        on_circle = ansi_highlight_red();
+                        circle = true;
+                } else if (STR_IN_SET(u->active_state, "reloading", "activating", "maintenance", "deactivating")) {
+                        on_sub = on_active = ansi_highlight();
+
+                        if (!circle) { /* Here we let load_state highlighting win */
+                                on_circle = ansi_highlight();
+                                circle = true;
+                        }
+                } else if (streq(u->active_state, "inactive"))
+                        on_sub = on_active = ansi_grey();
+
+                /* As a special case, when this is a service which has not process running, let's grey out
+                 * its state, to highlight that a bit */
+                if (!on_sub && endswith(u->id, ".service") && streq(u->sub_state, "exited"))
+                        on_sub = ansi_grey();
+
+                if (arg_plain)
+                        circle = false;
+
                 id = format_unit_id(u->id, u->machine);
                 if (!id)
                         return log_oom();
 
                 r = table_add_many(table,
                                    TABLE_STRING, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ",
-                                   TABLE_SET_BOTH_COLORS, on_circle,
+                                   TABLE_SET_COLOR, on_circle,
+                                   TABLE_SET_BOTH_UNDERLINES, underline,
                                    TABLE_STRING, id,
-                                   TABLE_SET_BOTH_COLORS, on_active,
+                                   TABLE_SET_COLOR, on_active,
+                                   TABLE_SET_BOTH_UNDERLINES, underline,
                                    TABLE_STRING, u->load_state,
-                                   TABLE_SET_BOTH_COLORS, on_loaded,
+                                   TABLE_SET_COLOR, on_loaded,
+                                   TABLE_SET_BOTH_UNDERLINES, underline,
                                    TABLE_STRING, u->active_state,
-                                   TABLE_SET_BOTH_COLORS, on_active,
+                                   TABLE_SET_COLOR, on_active,
+                                   TABLE_SET_BOTH_UNDERLINES, underline,
                                    TABLE_STRING, u->sub_state,
-                                   TABLE_SET_BOTH_COLORS, on_active,
+                                   TABLE_SET_COLOR, on_sub,
+                                   TABLE_SET_BOTH_UNDERLINES, underline,
                                    TABLE_STRING, u->job_id ? u->job_type: "",
-                                   TABLE_SET_BOTH_COLORS, on_underline,
+                                   TABLE_SET_BOTH_UNDERLINES, underline,
                                    TABLE_STRING, u->description,
-                                   TABLE_SET_BOTH_COLORS, on_underline);
+                                   TABLE_SET_BOTH_UNDERLINES, underline);
                 if (r < 0)
                         return table_log_add_error(r);