chiark / gitweb /
systemctl: split job list writing to a seperate function
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 7 Sep 2010 08:30:51 +0000 (10:30 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 14 Sep 2010 20:15:41 +0000 (22:15 +0200)
src/systemctl.c

index ff6f86a..fe55eac 100644 (file)
@@ -251,12 +251,88 @@ static int compare_unit_info(const void *a, const void *b) {
         return strcasecmp(u->id, v->id);
 }
 
         return strcasecmp(u->id, v->id);
 }
 
+static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
+        if (on_tty()) {
+                if (columns() >= 80+12 || arg_full)
+                        printf("%-25s %-6s %-12s %-18s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
+                else
+                        printf("%-25s %-6s %-12s %-18s %-15s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB");
+        }
+
+        for (const struct unit_info *u = unit_infos; u < unit_infos + c; u++) {
+                const char *dot;
+
+                if ((!arg_type || ((dot = strrchr(u->id, '.')) &&
+                                   streq(dot+1, arg_type))) &&
+                    (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0)) {
+                        char *e;
+                        int a = 0, b = 0;
+                        const char *on_loaded, *off_loaded;
+                        const char *on_active, *off_active;
+
+                        if (!streq(u->load_state, "loaded")) {
+                                on_loaded = ansi_highlight(true);
+                                off_loaded = ansi_highlight(false);
+                        } else
+                                on_loaded = off_loaded = "";
+
+                        if (streq(u->active_state, "failed")) {
+                                on_active = ansi_highlight(true);
+                                off_active = ansi_highlight(false);
+                        } else
+                                on_active = off_active = "";
+
+                        e = arg_full ? NULL : ellipsize(u->id, 25, 33);
+
+                        printf("%-25s %s%-6s%s %s%-12s %-18s%s%n",
+                               e ? e : u->id,
+                               on_loaded, u->load_state, off_loaded,
+                               on_active, u->active_state, u->sub_state, off_active,
+                               &a);
+
+                        free(e);
+
+                        a -= strlen(on_loaded) + strlen(off_loaded);
+                        a -= strlen(on_active) + strlen(off_active);
+
+                        if (u->job_id != 0)
+                                printf(" %-15s%n", u->job_type, &b);
+                        else
+                                b = 1 + 15;
+
+                        if (a + b + 1 < columns()) {
+                                if (u->job_id == 0)
+                                        printf("                ");
+
+                                if (arg_full)
+                                        printf(" %s", u->description);
+                                else
+                                        printf(" %.*s", columns() - a - b - 1, u->description);
+                        }
+
+                        fputs("\n", stdout);
+                }
+        }
+
+        if (!on_tty()) {
+                printf("\nLOAD   = Reflects whether the unit definition was properly loaded.\n"
+                       "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
+                       "SUB    = The low-level unit activation state, values depend on unit type.\n"
+                       "JOB    = Pending job for the unit.\n");
+
+                if (arg_all)
+                        printf("\n%u units listed.\n", c);
+                else
+                        printf("\n%u units listed. Pass --all to see inactive units, too.\n", c);
+        }
+}
+
 static int list_units(DBusConnection *bus, char **args, unsigned n) {
         DBusMessage *m = NULL, *reply = NULL;
         DBusError error;
         int r;
         DBusMessageIter iter, sub, sub2;
 static int list_units(DBusConnection *bus, char **args, unsigned n) {
         DBusMessage *m = NULL, *reply = NULL;
         DBusError error;
         int r;
         DBusMessageIter iter, sub, sub2;
-        unsigned c = 0, k, n_units = 0;
+        unsigned c = 0, n_units = 0;
         struct unit_info *unit_infos = NULL;
 
         dbus_error_init(&error);
         struct unit_info *unit_infos = NULL;
 
         dbus_error_init(&error);
@@ -336,82 +412,7 @@ static int list_units(DBusConnection *bus, char **args, unsigned n) {
         }
 
         qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
         }
 
         qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
-
-        if (on_tty()) {
-                if (columns() >= 80+12 || arg_full)
-                        printf("%-25s %-6s %-12s %-18s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
-                else
-                        printf("%-25s %-6s %-12s %-18s %-15s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB");
-        }
-
-        for (k = 0; k < c; k++) {
-                const char *dot;
-                struct unit_info *u = unit_infos+k;
-
-                if ((!arg_type || ((dot = strrchr(u->id, '.')) &&
-                                   streq(dot+1, arg_type))) &&
-                    (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0)) {
-                        char *e;
-                        int a = 0, b = 0;
-                        const char *on_loaded, *off_loaded;
-                        const char *on_active, *off_active;
-
-                        if (!streq(u->load_state, "loaded")) {
-                                on_loaded = ansi_highlight(true);
-                                off_loaded = ansi_highlight(false);
-                        } else
-                                on_loaded = off_loaded = "";
-
-                        if (streq(u->active_state, "failed")) {
-                                on_active = ansi_highlight(true);
-                                off_active = ansi_highlight(false);
-                        } else
-                                on_active = off_active = "";
-
-                        e = arg_full ? NULL : ellipsize(u->id, 25, 33);
-
-                        printf("%-25s %s%-6s%s %s%-12s %-18s%s%n",
-                               e ? e : u->id,
-                               on_loaded, u->load_state, off_loaded,
-                               on_active, u->active_state, u->sub_state, off_active,
-                               &a);
-
-                        free(e);
-
-                        a -= strlen(on_loaded) + strlen(off_loaded);
-                        a -= strlen(on_active) + strlen(off_active);
-
-                        if (u->job_id != 0)
-                                printf(" %-15s%n", u->job_type, &b);
-                        else
-                                b = 1 + 15;
-
-                        if (a + b + 1 < columns()) {
-                                if (u->job_id == 0)
-                                        printf("                ");
-
-                                if (arg_full)
-                                        printf(" %s", u->description);
-                                else
-                                        printf(" %.*s", columns() - a - b - 1, u->description);
-                        }
-
-                        fputs("\n", stdout);
-                }
-        }
-
-        if (on_tty()) {
-
-                printf("\nLOAD   = Reflects whether the unit definition was properly loaded.\n"
-                       "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
-                       "SUB    = The low-level unit activation state, values depend on unit type.\n"
-                       "JOB    = Pending job for the unit.\n");
-
-                if (arg_all)
-                        printf("\n%u units listed.\n", c);
-                else
-                        printf("\n%u units listed. Pass --all to see inactive units, too.\n", c);
-        }
+        output_units_list(unit_infos, c);
 
         r = 0;
 
 
         r = 0;