]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
column: add --table-empty-lines
authorKarel Zak <kzak@redhat.com>
Thu, 23 Aug 2018 11:31:29 +0000 (13:31 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 23 Aug 2018 11:34:43 +0000 (13:34 +0200)
The option allows to add empty line to the table. The default behavior
is to ignore empty lines at all.

echo -e "A\nAA\nAAA\n\nAAAA" | ./column --table
A
AA
AAA
AAAA

$ echo -e "A\nAA\nAAA\n\nAAAA" | ./column --table --table-empty-lines
A
AA
AAA

AAAA

Addresses: https://github.com/karelzak/util-linux/issues/593
Signed-off-by: Karel Zak <kzak@redhat.com>
Documentation/TODO
text-utils/column.1
text-utils/column.c

index 22b28b00542451ddf96bbe1ad3c2d167c7027af6..cda52592b5054ba32537ee1974d0a986bc57c0a5 100644 (file)
@@ -8,11 +8,6 @@ cal
    - support another --reforms, see for example freebsd version
      https://github.com/freebsd/freebsd/blob/master/usr.bin/ncal/ncal.c#L72
 
-column
-------
-   - add option to NOT ignore empty lines
-     https://github.com/karelzak/util-linux/issues/593
-
 script
 ------
    - think about optional "event" records in timing file to save information
index e726151b6138149a2241dfddbc6ccc8764f45058..9ce5879e506296d70b2daa1ee6d992f8e9dfc833 100644 (file)
@@ -106,6 +106,9 @@ hide all unnamed columns (see --table-columns).
 Specify columns order on output.
 .IP "\fB\-n, \-\-table-name\fP \fIname\fP"
 Specify the table name used for JSON output. The default is "table".
+.IP "\fB\-L, \-\-table\-empty\-lines\fP"
+Insert empty line to the table for each empty line on input. The default
+is ignore empty lines at all.
 .IP "\fB\-r, \-\-tree\fP \fIcolumn\fP"
 Specify column to use tree-like output. Note that the circular dependencies and
 another anomalies in child and parent relation are silently ignored.
index 19581432849e2498ab9969dee8cd76789f614975..8961bd38b63e07808f7ec0b646e6bfff2e7f40e8 100644 (file)
@@ -95,6 +95,7 @@ struct column_control {
        unsigned int greedy :1,
                     json :1,
                     header_repeat :1,
+                    tab_empty_lines :1,        /* --table-empty-lines */
                     tab_noheadings :1;
 };
 
@@ -463,6 +464,17 @@ static int add_line_to_table(struct column_control *ctl, wchar_t *wcs)
        return 0;
 }
 
+static int add_emptyline_to_table(struct column_control *ctl)
+{
+       if (!ctl->tab)
+               init_table(ctl);
+
+       if (!scols_table_new_line(ctl->tab, NULL))
+               err(EXIT_FAILURE, _("failed to allocate output line"));
+
+       return 0;
+}
+
 static int read_input(struct column_control *ctl, FILE *fp)
 {
        char *buf = NULL;
@@ -487,8 +499,11 @@ static int read_input(struct column_control *ctl, FILE *fp)
                        if (p)
                                *p = '\0';
                }
-               if (!str || !*str)
+               if (!str || !*str) {
+                       if (ctl->mode == COLUMN_MODE_TABLE && ctl->tab_empty_lines)
+                               add_emptyline_to_table(ctl);
                        continue;
+               }
 
                wcs = mbs_to_wcs(buf);
                if (!wcs) {
@@ -621,6 +636,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -R, --table-right <columns>      right align text in these columns\n"), out);
        fputs(_(" -T, --table-truncate <columns>   truncate text in the columns when necessary\n"), out);
        fputs(_(" -W, --table-wrap <columns>       wrap text in the columns when necessary\n"), out);
+       fputs(_(" -L, --table-empty-lines          don't ignore empty lines\n"), out);
        fputs(_(" -J, --json                       use JSON output format for table\n"), out);
 
        fputs(USAGE_SEPARATOR, out);
@@ -672,6 +688,7 @@ int main(int argc, char **argv)
                { "table-right",         required_argument, NULL, 'R' },
                { "table-truncate",      required_argument, NULL, 'T' },
                { "table-wrap",          required_argument, NULL, 'W' },
+               { "table-empty-lines",   no_argument,       NULL, 'L' },
                { "table-header-repeat", no_argument,       NULL, 'e' },
                { "tree",                required_argument, NULL, 'r' },
                { "tree-id",             required_argument, NULL, 'i' },
@@ -694,7 +711,7 @@ int main(int argc, char **argv)
        ctl.output_separator = "  ";
        ctl.input_separator = mbs_to_wcs("\t ");
 
-       while ((c = getopt_long(argc, argv, "c:dE:eH:hi:JN:n:O:o:p:R:r:s:T:tVW:x", longopts, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "c:dE:eH:hi:JLN:n:O:o:p:R:r:s:T:tVW:x", longopts, NULL)) != -1) {
 
                err_exclusive_options(c, longopts, excl, excl_st);
 
@@ -724,6 +741,9 @@ int main(int argc, char **argv)
                        ctl.json = 1;
                        ctl.mode = COLUMN_MODE_TABLE;
                        break;
+               case 'L':
+                       ctl.tab_empty_lines = 1;
+                       break;
                case 'N':
                        ctl.tab_colnames = split_or_error(optarg, _("failed to parse column names"));
                        break;