]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
column: add --table-colnames
authorKarel Zak <kzak@redhat.com>
Mon, 6 Mar 2017 10:37:01 +0000 (11:37 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 2 May 2017 10:18:00 +0000 (12:18 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
text-utils/column.1
text-utils/column.c

index e861e0d297388ce8b8901e4627d7d5964ae4293b..df097c37f0b7e916e4b4e27819a9928466414982 100644 (file)
@@ -57,6 +57,9 @@ Determine the number of columns the input contains and create a table.
 Columns are delimited with whitespace, by default, or with the characters
 supplied using the \fB\-\-output\-separator\fP option.
 Table output is useful for pretty-printing.
+.IP "\fB\-N, \-\-table-colnames\fP \fInames\fP"
+Specify the columns names by comma separated list of names. The names are used
+for the table header.
 .IP "\fB\-x, \-\-fillrows\fP"
 Fill rows before filling columns.
 .IP "\fB\-V\fR, \fB\-\-version\fR"
index b05bb3c6c2abcb499c10d8622ed631cfa9514b69..1d560fb72457d6403a175e197b9a358c0ed6ba37 100644 (file)
@@ -50,6 +50,7 @@
 #include "strutils.h"
 #include "closestream.h"
 #include "ttyutils.h"
+#include "strv.h"
 
 #include "libsmartcols.h"
 
@@ -68,6 +69,8 @@ struct column_control {
 
        struct libscols_table *tab;
 
+       char **tab_colnames;    /* parsed -H option */
+
        wchar_t *input_separator;
        const char *output_separator;
 
@@ -159,6 +162,17 @@ static wchar_t *local_wcstok(wchar_t *p, const wchar_t *separator, int greedy, w
        return result;
 }
 
+static int parse_table_colnames(struct column_control *ctl, const char *names)
+{
+       ctl->tab_colnames = strv_split(names, ",");
+       if (!ctl->tab_colnames) {
+               if (errno == ENOMEM)
+                       err_oom();
+               errx(EXIT_FAILURE, _("failed to parse column names"));
+       }
+       return 0;
+}
+
 static void init_table(struct column_control *ctl)
 {
        scols_init_debug(0);
@@ -168,7 +182,14 @@ static void init_table(struct column_control *ctl)
                err(EXIT_FAILURE, _("failed to allocate output table"));
 
        scols_table_set_column_separator(ctl->tab, ctl->output_separator);
-       scols_table_enable_noheadings(ctl->tab, 1);
+
+       if (ctl->tab_colnames) {
+               char **name;
+
+               STRV_FOREACH(name, ctl->tab_colnames)
+                       scols_table_new_column(ctl->tab, *name, 0, 0);
+       } else
+               scols_table_enable_noheadings(ctl->tab, 1);
 }
 
 static int add_line_to_table(struct column_control *ctl, wchar_t *wcs)
@@ -339,6 +360,7 @@ static void __attribute__((__noreturn__)) usage(int rc)
 
        fputs(USAGE_OPTIONS, out);
        fputs(_(" -t, --table                      create a table\n"), out);
+       fputs(_(" -N, --table-colnames <names>     comma separated columns names\n"), out);
        fputs(_(" -s, --separator <string>         possible table delimiters\n"), out);
        fputs(_(" -o, --output-separator <string>  columns separator for table output\n"
                "                                    (default is two spaces)\n"), out);
@@ -364,14 +386,15 @@ int main(int argc, char **argv)
 
        static const struct option longopts[] =
        {
-               { "columns",          required_argument, NULL, 'c' }, /* deprecated */
-               { "fillrows",         no_argument,       NULL, 'x' },
-               { "help",             no_argument,       NULL, 'h' },
-               { "output-separator", required_argument, NULL, 'o' },
-               { "output-width",     required_argument, NULL, 'c' },
-               { "separator",        required_argument, NULL, 's' },
-               { "table",            no_argument,       NULL, 't' },
-               { "version",          no_argument,       NULL, 'V' },
+               { "columns",             required_argument, NULL, 'c' }, /* deprecated */
+               { "fillrows",            no_argument,       NULL, 'x' },
+               { "help",                no_argument,       NULL, 'h' },
+               { "output-separator",    required_argument, NULL, 'o' },
+               { "output-width",        required_argument, NULL, 'c' },
+               { "separator",           required_argument, NULL, 's' },
+               { "table",               no_argument,       NULL, 't' },
+               { "table-colnames",      required_argument, NULL, 'N' },
+               { "version",             no_argument,       NULL, 'V' },
                { NULL, 0, NULL, 0 },
        };
 
@@ -384,8 +407,11 @@ int main(int argc, char **argv)
        ctl.output_separator = "  ";
        ctl.input_separator = mbs_to_wcs("\t ");
 
-       while ((ch = getopt_long(argc, argv, "hVc:s:txo:", longopts, NULL)) != -1)
+       while ((ch = getopt_long(argc, argv, "hVc:N:s:txo:", longopts, NULL)) != -1)
                switch(ch) {
+               case 'N':
+                       parse_table_colnames(&ctl, optarg);
+                       break;
                case 'h':
                        usage(EXIT_SUCCESS);
                        break;