+struct job_info {
+ uint32_t id;
+ char *name, *type, *state;
+};
+
+static void list_jobs_print(struct job_info* jobs, size_t n) {
+ size_t i;
+ struct job_info *j;
+ const char *on, *off;
+ bool shorten = false;
+
+ assert(n == 0 || jobs);
+
+ if (n == 0) {
+ on = ansi_highlight_green(true);
+ off = ansi_highlight_green(false);
+
+ printf("%sNo jobs running.%s\n", on, off);
+ return;
+ }
+
+ pager_open_if_enabled();
+
+ {
+ /* JOB UNIT TYPE STATE */
+ unsigned l0 = 3, l1 = 4, l2 = 4, l3 = 5;
+
+ for (i = 0, j = jobs; i < n; i++, j++) {
+ assert(j->name && j->type && j->state);
+ l0 = MAX(l0, decimal_str_max(j->id));
+ l1 = MAX(l1, strlen(j->name));
+ l2 = MAX(l2, strlen(j->type));
+ l3 = MAX(l3, strlen(j->state));
+ }
+
+ if (!arg_full && l0 + 1 + l1 + l2 + 1 + l3 > columns()) {
+ l1 = MAX(33u, columns() - l0 - l2 - l3 - 3);
+ shorten = true;
+ }
+
+ if (on_tty())
+ printf("%*s %-*s %-*s %-*s\n",
+ l0, "JOB",
+ l1, "UNIT",
+ l2, "TYPE",
+ l3, "STATE");
+
+ for (i = 0, j = jobs; i < n; i++, j++) {
+ char _cleanup_free_ *e = NULL;
+
+ if (streq(j->state, "running")) {
+ on = ansi_highlight(true);
+ off = ansi_highlight(false);
+ } else
+ on = off = "";
+
+ e = shorten ? ellipsize(j->name, l1, 33) : NULL;
+ printf("%*u %s%-*s%s %-*s %s%-*s%s\n",
+ l0, j->id,
+ on, l1, e ? e : j->name, off,
+ l2, j->type,
+ on, l3, j->state, off);
+ }
+ }
+
+ on = ansi_highlight(true);
+ off = ansi_highlight(false);
+
+ if (on_tty())
+ printf("\n%s%zu jobs listed%s.\n", on, n, off);
+}
+