]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
column: add --table-order
authorKarel Zak <kzak@redhat.com>
Wed, 29 Mar 2017 12:08:14 +0000 (14:08 +0200)
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 c11da134e9f41af10205ca05fbd13e08877207aa..4add28471988b210979e7705f879f6cdfbb59a00 100644 (file)
@@ -84,6 +84,9 @@ necessary.  The \fIcolumns\fP is comma separated list of the column names (see
 .IP "\fB\-H, \-\-table-hide\fP \fIcolumns\fP"
 Don't print specified columns.  The \fIcolumns\fP is comma separated list of
 the column names (see \fB\-\-table-columns\fP) or column number.
+.IP "\fB\-O, \-\-table-order\fP \fIcolumns\fP"
+Specify columns order on output.  The \fIcolumns\fP is comma separated list of
+the column names (see \fB\-\-table-columns\fP) or column number.
 .IP "\fB\-n, \-\-table-name\fP \fIname\fP"
 Specify the table name used for JSON output. The defaout is "table".
 .IP "\fB\-x, \-\-fillrows\fP"
index f323202ad2c26b4c2e83a5d4e85ab23afe573a7a..80f27307e996cd0a82e70a8ef218b97a5f71a9bd 100644 (file)
@@ -77,6 +77,7 @@ struct column_control {
        const char *tab_colnoextrem;    /* --table-noextreme */
        const char *tab_colwrap;        /* --table-wrap */
        const char *tab_colhide;        /* --table-hide */
+       const char *tab_order;  /* --table-order */
 
        wchar_t *input_separator;
        const char *output_separator;
@@ -246,6 +247,28 @@ static void apply_columnflag_from_list(struct column_control *ctl, const char *l
        strv_free(all);
 }
 
+static void reorder_table(struct column_control *ctl)
+{
+       struct libscols_column **wanted, *last = NULL;
+       size_t i, count = 0;
+       size_t ncols = scols_table_get_ncols(ctl->tab);
+       char **order = split_or_error(ctl->tab_order, _("failed to parse --table-order list"));
+       char **one;
+
+       wanted = xcalloc(ncols, sizeof(struct libscols_column *));
+
+       STRV_FOREACH(one, order) {
+               struct libscols_column *cl = string_to_column(ctl, *one);
+               if (cl)
+                       wanted[count++] = cl;
+       }
+
+       for (i = 0; i < count; i++) {
+               scols_table_move_column(ctl->tab, last, wanted[i]);
+               last = wanted[i];
+       }
+}
+
 static void modify_table(struct column_control *ctl)
 {
        scols_table_set_termwidth(ctl->tab, ctl->termwidth);
@@ -269,8 +292,14 @@ static void modify_table(struct column_control *ctl)
        if (ctl->tab_colhide)
                apply_columnflag_from_list(ctl, ctl->tab_colhide,
                                SCOLS_FL_HIDDEN , _("failed to parse --table-hide list"));
+
+
+       /* This must be the last step! */
+       if (ctl->tab_order)
+               reorder_table(ctl);
 }
 
+
 static int add_line_to_table(struct column_control *ctl, wchar_t *wcs)
 {
        wchar_t *wcdata, *sv = NULL;
@@ -446,6 +475,7 @@ static void __attribute__((__noreturn__)) usage(int rc)
        fputs(_(" -E, --table-noextreme <columns>  don't count long text from the columns to column width\n"), out);
        fputs(_(" -W, --table-wrap <columns>       wrap text in the columns when necessary\n"), out);
        fputs(_(" -H, --table-hide <columns>       don't print the columns\n"), out);
+       fputs(_(" -O, --table-order <columns>      specify order of output columns\n"), out);
        fputs(_(" -n, --table-name <name>          table name for JSON output\n"), out);
        fputs(_(" -s, --separator <string>         possible table delimiters\n"), out);
        fputs(_(" -o, --output-separator <string>  columns separator for table output\n"
@@ -486,6 +516,7 @@ int main(int argc, char **argv)
                { "table-noextreme",     required_argument, NULL, 'E' },
                { "table-wrap",          required_argument, NULL, 'W' },
                { "table-hide",          required_argument, NULL, 'H' },
+               { "table-order",         required_argument, NULL, 'O' },
                { "table-name",          required_argument, NULL, 'n' },
                { "version",             no_argument,       NULL, 'V' },
                { NULL, 0, NULL, 0 },
@@ -506,7 +537,7 @@ int main(int argc, char **argv)
        ctl.output_separator = "  ";
        ctl.input_separator = mbs_to_wcs("\t ");
 
-       while ((c = getopt_long(argc, argv, "hVc:Jn:N:R:s:txo:T:E:W:H:", longopts, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "hVc:Jn:N:R:s:txo:T:E:W:H:O:", longopts, NULL)) != -1) {
 
                err_exclusive_options(c, longopts, excl, excl_st);
 
@@ -547,6 +578,9 @@ int main(int argc, char **argv)
                        ctl.input_separator = mbs_to_wcs(optarg);
                        ctl.greedy = 0;
                        break;
+               case 'O':
+                       ctl.tab_order = optarg;
+                       break;
                case 'o':
                        ctl.output_separator = optarg;
                        break;