From 2698f9ba887cb7e4a204ada72016b1c1192b17c1 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 23 Aug 2018 13:31:29 +0200 Subject: column: add --table-empty-lines 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 --- text-utils/column.1 | 3 +++ text-utils/column.c | 24 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) (limited to 'text-utils') diff --git a/text-utils/column.1 b/text-utils/column.1 index e726151b6..9ce5879e5 100644 --- a/text-utils/column.1 +++ b/text-utils/column.1 @@ -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. diff --git a/text-utils/column.c b/text-utils/column.c index 195814328..8961bd38b 100644 --- a/text-utils/column.c +++ b/text-utils/column.c @@ -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 right align text in these columns\n"), out); fputs(_(" -T, --table-truncate truncate text in the columns when necessary\n"), out); fputs(_(" -W, --table-wrap 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; -- cgit v1.2.3-55-g7522