[systemd-devel] [PATCH] Move path to the right to mitigate alignment issues with untruncated paths. Add --full option (like systemctl) to skip all truncation.

david at davidstrauss.net david at davidstrauss.net
Wed Jul 25 17:54:50 PDT 2012


From: David Strauss <david at davidstrauss.net>

---
 src/cgtop/cgtop.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index b4454c54..c3d2a28 100644
--- a/src/cgtop/cgtop.c
+++ b/src/cgtop/cgtop.c
@@ -67,6 +67,7 @@ static enum {
 } arg_order = ORDER_CPU;
 
 bool arg_batch = false;
+bool arg_full = false;
 
 static void group_free(Group *g) {
         assert(g);
@@ -447,13 +448,13 @@ static int display(Hashmap *a) {
         if (rows <= 0)
                 rows = 25;
 
-        printf("%s%-37s%s %s%7s%s %s%6s%s %s%8s%s %s%8s%s %s%8s%s\n\n",
-               arg_order == ORDER_PATH   ? ANSI_HIGHLIGHT_ON : "", "Path",     arg_order == ORDER_PATH   ? ANSI_HIGHLIGHT_OFF : "",
+        printf("%s%7s%s %s%6s%s %s%8s%s %s%8s%s %s%8s%s %s%-37s%s\n\n",
                arg_order == ORDER_TASKS  ? ANSI_HIGHLIGHT_ON : "", "Tasks",    arg_order == ORDER_TASKS  ? ANSI_HIGHLIGHT_OFF : "",
                arg_order == ORDER_CPU    ? ANSI_HIGHLIGHT_ON : "", "%CPU",     arg_order == ORDER_CPU    ? ANSI_HIGHLIGHT_OFF : "",
                arg_order == ORDER_MEMORY ? ANSI_HIGHLIGHT_ON : "", "Memory",   arg_order == ORDER_MEMORY ? ANSI_HIGHLIGHT_OFF : "",
                arg_order == ORDER_IO     ? ANSI_HIGHLIGHT_ON : "", "Input/s",  arg_order == ORDER_IO     ? ANSI_HIGHLIGHT_OFF : "",
-               arg_order == ORDER_IO     ? ANSI_HIGHLIGHT_ON : "", "Output/s", arg_order == ORDER_IO     ? ANSI_HIGHLIGHT_OFF : "");
+               arg_order == ORDER_IO     ? ANSI_HIGHLIGHT_ON : "", "Output/s", arg_order == ORDER_IO     ? ANSI_HIGHLIGHT_OFF : "",
+               arg_order == ORDER_PATH   ? ANSI_HIGHLIGHT_ON : "", "Path",     arg_order == ORDER_PATH   ? ANSI_HIGHLIGHT_OFF : "");
 
         for (j = 0; j < n; j++) {
                 char *p;
@@ -464,14 +465,10 @@ static int display(Hashmap *a) {
 
                 g = array[j];
 
-                p = ellipsize(g->path, 37, 33);
-                printf("%-37s", p ? p : g->path);
-                free(p);
-
                 if (g->n_tasks_valid)
-                        printf(" %7u", g->n_tasks);
+                        printf("%7u", g->n_tasks);
                 else
-                        fputs("       -", stdout);
+                        fputs("      -", stdout);
 
                 if (g->cpu_valid)
                         printf(" %6.1f", g->cpu_fraction*100);
@@ -491,6 +488,15 @@ static int display(Hashmap *a) {
                 } else
                         fputs("        -        -", stdout);
 
+                if (arg_full) {
+                        printf(" %s", g->path);
+                }
+                else {
+                        p = ellipsize(g->path, 37, 33);
+                        printf(" %-37s", p ? p : g->path);
+                        free(p);
+                }
+
                 putchar('\n');
         }
 
@@ -510,14 +516,16 @@ static void help(void) {
                "  -d --delay=DELAY    Specify delay\n"
                "  -n --iterations=N   Run for N iterations before exiting\n"
                "  -b --batch          Run in batch mode, accepting no input\n"
-               "     --depth=DEPTH    Maximum traversal depth (default: 2)\n",
+               "     --depth=DEPTH    Maximum traversal depth (default: 2)\n"
+               "     --full           Don't ellipsize paths on output\n",
                program_invocation_short_name);
 }
 
 static int parse_argv(int argc, char *argv[]) {
 
         enum {
-                ARG_DEPTH = 0x100
+                ARG_DEPTH = 0x100,
+                ARG_FULL  = 0x101
         };
 
         static const struct option options[] = {
@@ -525,6 +533,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "delay",      required_argument, NULL, 'd'       },
                 { "iterations", required_argument, NULL, 'n'       },
                 { "batch",      no_argument,       NULL, 'b'       },
+                { "full",       no_argument,       NULL, ARG_FULL  },
                 { "depth",      required_argument, NULL, ARG_DEPTH },
                 { NULL,         0,                 NULL, 0         }
         };
@@ -570,6 +579,10 @@ static int parse_argv(int argc, char *argv[]) {
 
                         break;
 
+                case ARG_FULL:
+                        arg_full = true;
+                        break;
+
                 case 'b':
                         arg_batch = true;
                         break;
-- 
1.7.11.2



More information about the systemd-devel mailing list