summaryrefslogtreecommitdiffstats
path: root/libsmartcols/src/print.c
diff options
context:
space:
mode:
authorKarel Zak2019-07-23 16:04:51 +0200
committerKarel Zak2019-07-23 16:24:42 +0200
commit94dbb322737524027a55e6ed5646dc69c5ed9e5b (patch)
treed4b325c8890240f984805abce6ace03fc16a2043 /libsmartcols/src/print.c
parentlibfdisk: improve partition copy on resize (diff)
downloadkernel-qcow2-util-linux-94dbb322737524027a55e6ed5646dc69c5ed9e5b.tar.gz
kernel-qcow2-util-linux-94dbb322737524027a55e6ed5646dc69c5ed9e5b.tar.xz
kernel-qcow2-util-linux-94dbb322737524027a55e6ed5646dc69c5ed9e5b.zip
libsmartcols: cleanup and extend padding functionality
LIBSMARTCOLS_DEBUG_PADDING=on in the next examples forces libsmartcols print '.' as a padding char. See line "ffff" in the exmaples. * default output is to fill all except last cell $ LIBSMARTCOLS_DEBUG_PADDING=on ./sample-scols-fromfile --nlines 10 --width 80 --column tests/ts/libsmartcols/files/col-name --column tests/ts/libsmartcols/files/col-number --column tests/ts/libsmartcols/files/col-string --column tests/ts/libsmartcols/files/col-string tests/ts/libsmartcols/files/data-string tests/ts/libsmartcols/files/data-number tests/ts/libsmartcols/files/data-string-empty tests/ts/libsmartcols/files/data-string-empty 2> /dev/null NAME.. ......NUM STRINGS STRINGS aaaa.. ........0 aaaa... aaaa bbb... ......100 bbb.... bbb ccccc. .......21 ccccc.. ccccc dddddd ........3 dddddd. dddddd ee.... ......411 ee..... ee ffff.. .....5111 ....... gggggg 678993321 gggggg. gggggg hhh... ..7666666 hhh.... hhh * scols_table_enable_minout() minimizes output for tailing empty cells, example: $ LIBSMARTCOLS_DEBUG_PADDING=on ./sample-scols-fromfile --nlines 10 --minout --width 80 --column tests/ts/libsmartcols/files/col-name --column tests/ts/libsmartcols/files/col-number --column tests/ts/libsmartcols/files/col-string --column tests/ts/libsmartcols/files/col-string tests/ts/libsmartcols/files/data-string tests/ts/libsmartcols/files/data-number tests/ts/libsmartcols/files/data-string-empty tests/ts/libsmartcols/files/data-string-empty 2> /dev/null NAME.. ......NUM STRINGS STRINGS aaaa.. ........0 aaaa... aaaa bbb... ......100 bbb.... bbb ccccc. .......21 ccccc.. ccccc dddddd ........3 dddddd. dddddd ee.... ......411 ee..... ee ffff.. .....5111 gggggg 678993321 gggggg. gggggg hhh... ..7666666 hhh.... hhh * cleanup up scols_table_enable_maxout() use, example: $ LIBSMARTCOLS_DEBUG_PADDING=on ./sample-scols-fromfile --nlines 10 --maxout --width 80 --column tests/ts/libsmartcols/files/col-name --column tests/ts/libsmartcols/files/col-number --column tests/ts/libsmartcols/files/col-string --column tests/ts/libsmartcols/files/col-string tests/ts/libsmartcols/files/data-string tests/ts/libsmartcols/files/data-number tests/ts/libsmartcols/files/data-string-empty tests/ts/libsmartcols/files/data-string-empty 2> /dev/null NAME.............. ..................NUM STRINGS............ STRINGS............ aaaa.............. ....................0 aaaa............... aaaa............... bbb............... ..................100 bbb................ bbb................ ccccc............. ...................21 ccccc.............. ccccc.............. dddddd............ ....................3 dddddd............. dddddd............. ee................ ..................411 ee................. ee................. ffff.............. .................5111 ................... ................... gggggg............ ............678993321 gggggg............. gggggg............. hhh............... ..............7666666 hhh................ hhh................ Note that we cannot make scols_table_enable_minout() default because for example "column --table" is pretty commonly used with non-blank columns separator and in this case all cells has to be filled. $ echo -e "aa,b,ccc\na,,\naaa,bbb,ccc" | column --table --separator ',' --output-separator '|' aa |b |ccc a | | aaa|bbb|ccc Addresses: https://github.com/karelzak/util-linux/issues/826 Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libsmartcols/src/print.c')
-rw-r--r--libsmartcols/src/print.c81
1 files changed, 70 insertions, 11 deletions
diff --git a/libsmartcols/src/print.c b/libsmartcols/src/print.c
index 986af153c..c92154219 100644
--- a/libsmartcols/src/print.c
+++ b/libsmartcols/src/print.c
@@ -50,6 +50,43 @@
#define want_repeat_header(tb) (!(tb)->header_repeat || (tb)->header_next <= (tb)->termlines_used)
+static int is_next_columns_empty(
+ struct libscols_table *tb,
+ struct libscols_column *cl,
+ struct libscols_line *ln)
+{
+ struct libscols_iter itr;
+
+ if (!tb || !cl)
+ return 0;
+ if (is_last_column(cl))
+ return 1;
+ if (!ln)
+ return 0;
+
+ scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
+ scols_table_set_columns_iter(tb, &itr, cl);
+
+ /* skip current column */
+ scols_table_next_column(tb, &itr, &cl);
+
+ while (scols_table_next_column(tb, &itr, &cl) == 0) {
+ struct libscols_cell *ce;
+ const char *data = NULL;
+
+ if (scols_column_is_hidden(cl))
+ continue;
+ if (scols_column_is_tree(cl))
+ return 0;
+
+ ce = scols_line_get_cell(ln, cl->seqnum);
+ if (ce)
+ data = scols_cell_get_data(ce);
+ if (data && *data)
+ return 0;
+ }
+ return 1;
+}
/* returns pointer to the end of used data */
static int tree_ascii_art_to_buffer(struct libscols_table *tb,
@@ -216,14 +253,20 @@ static void print_empty_cell(struct libscols_table *tb,
}
}
- if (is_last_column(cl))
+ /* minout -- don't fill */
+ if (scols_table_is_minout(tb) && is_next_columns_empty(tb, cl, ln))
+ return;
+
+ /* default -- fill except last column */
+ if (!scols_table_is_maxout(tb) && is_last_column(cl))
return;
/* fill rest of cell with space */
for(; len_pad < cl->width; ++len_pad)
fputs(cellpadding_symbol(tb), tb->out);
- fputs(colsep(tb), tb->out);
+ if (!is_last_column(cl))
+ fputs(colsep(tb), tb->out);
}
@@ -367,13 +410,21 @@ static int print_pending_data(
fputs(UL_COLOR_RESET, tb->out);
free(data);
- if (is_last_column(cl))
+ /* minout -- don't fill */
+ if (scols_table_is_minout(tb) && is_next_columns_empty(tb, cl, ln))
return 0;
- for (i = len; i < width; i++)
- fputs(cellpadding_symbol(tb), tb->out); /* padding */
+ /* default -- fill except last column */
+ if (!scols_table_is_maxout(tb) && is_last_column(cl))
+ return 0;
+
+ /* fill rest of cell with space */
+ for(i = len; i < width; i++)
+ fputs(cellpadding_symbol(tb), tb->out);
+
+ if (!is_last_column(cl))
+ fputs(colsep(tb), tb->out);
- fputs(colsep(tb), tb->out); /* columns separator */
return 0;
err:
free(data);
@@ -491,7 +542,7 @@ static int print_data(struct libscols_table *tb,
data = NULL;
}
- if (data) {
+ if (data && *data) {
if (scols_column_is_right(cl)) {
if (color)
fputs(color, tb->out);
@@ -518,16 +569,24 @@ static int print_data(struct libscols_table *tb,
} else
fputs(data, tb->out);
}
- for (i = len; i < width; i++)
- fputs(cellpadding_symbol(tb), tb->out); /* padding */
- if (is_last)
+ /* minout -- don't fill */
+ if (scols_table_is_minout(tb) && is_next_columns_empty(tb, cl, ln))
return 0;
+ /* default -- fill except last column */
+ if (!scols_table_is_maxout(tb) && is_last)
+ return 0;
+
+ /* fill rest of cell with space */
+ for(i = len; i < width; i++)
+ fputs(cellpadding_symbol(tb), tb->out);
+
if (len > width && !scols_column_is_trunc(cl)) {
DBG(COL, ul_debugobj(cl, "*** data len=%zu > column width=%zu", len, width));
print_newline_padding(tb, cl, ln, buffer_get_size(buf)); /* next column starts on next line */
- } else
+
+ } else if (!is_last)
fputs(colsep(tb), tb->out); /* columns separator */
return 0;