]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
column: add --table-truncate
authorKarel Zak <kzak@redhat.com>
Wed, 29 Mar 2017 10:26:35 +0000 (12:26 +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 fa1540b8369a2f68097ebf7636f00c6f00140bb8..ca33c1ef54fe23332078723d3ea01982793e7a90 100644 (file)
@@ -66,6 +66,11 @@ for the table header or to addres column in option arguments.
 .IP "\fB\-R, \-\-table-right\fP \fIcolumns\fP"
 Right align text in the specified columns.  The \fIcolumns\fP is comma separated
 list of the column names (see \fB\-\-table-columns\fP) or column number.
+.IP "\fB\-T, \-\-table-truncate\fP \fIcolumns\fP"
+Specify columns where is allowed to truncate text when necessary, otherwise
+very long table entries may be printed on multiple lines. 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 0db8fd5047d1476b77d4175cfefb243919407033..f9f191ec0d3f5241967b2f37a8837a743325516f 100644 (file)
@@ -73,6 +73,7 @@ struct column_control {
        char **tab_colnames;            /* array with column names */
        const char *tab_name;           /* table name */
        const char *tab_colright;       /* non-parsed --table-right */
+       const char *tab_coltrunc;       /* non-parsed --table-trunc */
 
        wchar_t *input_separator;
        const char *output_separator;
@@ -220,6 +221,7 @@ static struct libscols_column *string_to_column(struct column_control *ctl, cons
        return scols_table_get_column(ctl->tab, colnum);
 }
 
+
 static int column_set_flag(struct libscols_column *cl, int fl)
 {
        int cur = scols_column_get_flags(cl);
@@ -227,20 +229,33 @@ static int column_set_flag(struct libscols_column *cl, int fl)
        return scols_column_set_flags(cl, cur | fl);
 }
 
+static void apply_columnflag_from_list(struct column_control *ctl, const char *list,
+                                        int flag, const char *errmsg)
+{
+       char **all = split_or_error(list, errmsg);
+       char **one;
+
+       STRV_FOREACH(one, all) {
+               struct libscols_column *cl = string_to_column(ctl, *one);
+               if (cl)
+                       column_set_flag(cl, flag);
+       }
+       strv_free(all);
+}
+
 static void modify_table(struct column_control *ctl)
 {
+       scols_table_set_termwidth(ctl->tab, ctl->termwidth);
+
        /* align text in columns to right */
-       if (ctl->tab_colright) {
-               char **allright = split_or_error(ctl->tab_colright, _("failed to parse --table-colright list"));
-               char **right;
-
-               STRV_FOREACH(right, allright) {
-                       struct libscols_column *cl = string_to_column(ctl, *right);
-                       if (cl)
-                               column_set_flag(cl, SCOLS_FL_RIGHT);
-               }
-               strv_free(allright);
-       }
+       if (ctl->tab_colright)
+               apply_columnflag_from_list(ctl, ctl->tab_colright,
+                               SCOLS_FL_RIGHT, _("failed to parse --table-right list"));
+
+       /* truncate text in columns */
+       if (ctl->tab_coltrunc)
+               apply_columnflag_from_list(ctl, ctl->tab_coltrunc,
+                               SCOLS_FL_TRUNC , _("failed to parse --table-trunc list"));
 }
 
 static int add_line_to_table(struct column_control *ctl, wchar_t *wcs)
@@ -414,6 +429,7 @@ static void __attribute__((__noreturn__)) usage(int rc)
        fputs(_(" -t, --table                      create a table\n"), out);
        fputs(_(" -N, --table-columns <names>      comma separated columns names\n"), out);
        fputs(_(" -R, --table-right <columns>      right align text in these columns\n"), out);
+       fputs(_(" -T, --table-truncate <columns>   truncate text in these columns when necessary\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"
@@ -432,7 +448,6 @@ int main(int argc, char **argv)
 {
        struct column_control ctl = {
                .mode = COLUMN_MODE_FILLCOLS,
-               .termwidth = 80,
                .greedy = 1
        };
 
@@ -451,6 +466,7 @@ int main(int argc, char **argv)
                { "table",               no_argument,       NULL, 't' },
                { "table-columns",       required_argument, NULL, 'N' },
                { "table-right",         required_argument, NULL, 'R' },
+               { "table-truncate",      required_argument, NULL, 'T' },
                { "table-name",          required_argument, NULL, 'n' },
                { "version",             no_argument,       NULL, 'V' },
                { NULL, 0, NULL, 0 },
@@ -471,7 +487,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:", longopts, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "hVc:Jn:N:R:s:txo:T:", longopts, NULL)) != -1) {
 
                err_exclusive_options(c, longopts, excl, excl_st);
 
@@ -498,6 +514,9 @@ int main(int argc, char **argv)
                case 'R':
                        ctl.tab_colright = optarg;
                        break;
+               case 'T':
+                       ctl.tab_coltrunc = optarg;
+                       break;
                case 's':
                        free(ctl.input_separator);
                        ctl.input_separator = mbs_to_wcs(optarg);