diff options
author | Karel Zak | 2016-02-19 16:43:28 +0100 |
---|---|---|
committer | Karel Zak | 2016-02-19 16:43:28 +0100 |
commit | 2981e0fd0e238f120811e1955e4f1bfddf93a326 (patch) | |
tree | 2f5fd9ceb4121ea4e35f096bc4575c9fc5291ea8 | |
parent | libsmartcols: add scols_table_print_range() (diff) | |
download | kernel-qcow2-util-linux-2981e0fd0e238f120811e1955e4f1bfddf93a326.tar.gz kernel-qcow2-util-linux-2981e0fd0e238f120811e1955e4f1bfddf93a326.tar.xz kernel-qcow2-util-linux-2981e0fd0e238f120811e1955e4f1bfddf93a326.zip |
libsmartcols: support continuous printing
This patch allows to disable line-breaks. This feature is usable when
you want to re-print the same line more than once -- move terminal
cursor to the begin of the line and print again and again (aka
progress bar).
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r-- | libsmartcols/docs/libsmartcols-sections.txt | 1 | ||||
-rw-r--r-- | libsmartcols/src/cell.c | 2 | ||||
-rw-r--r-- | libsmartcols/src/libsmartcols.h.in | 1 | ||||
-rw-r--r-- | libsmartcols/src/libsmartcols.sym | 1 | ||||
-rw-r--r-- | libsmartcols/src/smartcolsP.h | 2 | ||||
-rw-r--r-- | libsmartcols/src/table.c | 21 | ||||
-rw-r--r-- | libsmartcols/src/table_print.c | 22 |
7 files changed, 45 insertions, 5 deletions
diff --git a/libsmartcols/docs/libsmartcols-sections.txt b/libsmartcols/docs/libsmartcols-sections.txt index 4ac129141..c9bbec45b 100644 --- a/libsmartcols/docs/libsmartcols-sections.txt +++ b/libsmartcols/docs/libsmartcols-sections.txt @@ -103,6 +103,7 @@ scols_table_enable_export scols_table_enable_json scols_table_enable_maxout scols_table_enable_noheadings +scols_table_enable_nolinesep scols_table_enable_nowrap scols_table_enable_raw scols_table_get_column diff --git a/libsmartcols/src/cell.c b/libsmartcols/src/cell.c index c7aab353e..a9e69c0b8 100644 --- a/libsmartcols/src/cell.c +++ b/libsmartcols/src/cell.c @@ -56,7 +56,7 @@ int scols_reset_cell(struct libscols_cell *ce) * @ce: a pointer to a struct libscols_cell instance * @str: data (used for scols_print_table()) * - * Stores a copy of the @str in @ce. + * Stores a copy of the @str in @ce, the old data are deallocated by free(). * * Returns: 0, a negative value in case of an error. */ diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 5f87d965f..664822e77 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -210,6 +210,7 @@ extern int scols_table_enable_noheadings(struct libscols_table *tb, int enable); extern int scols_table_enable_export(struct libscols_table *tb, int enable); extern int scols_table_enable_maxout(struct libscols_table *tb, int enable); extern int scols_table_enable_nowrap(struct libscols_table *tb, int enable); +extern int scols_table_enable_nolinesep(struct libscols_table *tb, int enable); extern int scols_table_set_column_separator(struct libscols_table *tb, const char *sep); extern int scols_table_set_line_separator(struct libscols_table *tb, const char *sep); diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index 250c06d6d..8f64c09c9 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -133,4 +133,5 @@ global: scols_cell_get_flags; scols_cell_set_flags; scols_table_print_range; + scols_table_enable_nolinesep; } SMARTCOLS_2.27; diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 5add21849..377ab8045 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -156,7 +156,9 @@ struct libscols_table { colors_wanted :1, /* enable colors */ is_term :1, /* isatty() */ maxout :1, /* maximalize output */ + header_printed :1, /* header already printed */ no_headings :1, /* don't print header */ + no_linesep :1, /* don't print line separator */ no_wrap :1; /* never wrap lines */ }; diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index fdbc7cd3f..cc19e4ab2 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -708,6 +708,27 @@ int scols_table_set_symbols(struct libscols_table *tb, } /** + * scols_table_enable_nolinesep + * @tb: table + * @enable: 1 or 0 + * + * Enable/disable line separator printing. This is usefull if you want to + * re-printing the same line more than once (e.g. progress bar). Don't use it + * if you're not sure. + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_table_enable_nolinesep(struct libscols_table *tb, int enable) +{ + if (!tb) + return -EINVAL; + + DBG(TAB, ul_debugobj(tb, "nolinesep: %s", enable ? "ENABLE" : "DISABLE")); + tb->no_linesep = enable; + return 0; +} + +/** * scols_table_enable_colors: * @tb: table * @enable: 1 or 0 diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index d8965726e..75e828edd 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -645,7 +645,8 @@ static void fput_line_close(struct libscols_table *tb, int last) fput_indent(tb); fputs(last ? "}" : "},", tb->out); } - fputs(linesep(tb), tb->out); + if (!tb->no_linesep) + fputs(linesep(tb), tb->out); tb->indent_last_sep = 1; } @@ -778,7 +779,8 @@ static int print_header(struct libscols_table *tb, struct libscols_buffer *buf) assert(tb); - if (scols_table_is_noheadings(tb) || + if (tb->header_printed == 1 || + scols_table_is_noheadings(tb) || scols_table_is_export(tb) || scols_table_is_json(tb) || list_empty(&tb->tb_lines)) @@ -798,6 +800,8 @@ static int print_header(struct libscols_table *tb, struct libscols_buffer *buf) if (rc == 0) fputs(linesep(tb), tb->out); + + tb->header_printed = 1; return rc; } @@ -1306,6 +1310,9 @@ err: * @start: first printed line or NULL to print from the beggin of the table * @end: last printed line or NULL to print all from start. * + * If the start is the first line in the table than prints table header too. + * The header is printed only once. + * * Returns: 0, a negative value in case of an error. */ int scols_table_print_range( struct libscols_table *tb, @@ -1332,10 +1339,16 @@ int scols_table_print_range( struct libscols_table *tb, } else scols_reset_iter(&itr, SCOLS_ITER_FORWARD); - rc = print_range(tb, buf, &itr, end); + if (itr.p == tb->tb_lines.next) { + rc = print_header(tb, buf); + if (rc) + goto done; + } + rc = print_range(tb, buf, &itr, end); +done: free_buffer(buf); - return 0; + return rc; } /** @@ -1361,6 +1374,7 @@ int scols_print_table(struct libscols_table *tb) return 0; } + tb->header_printed = 0; rc = initialize_printting(tb, &buf); if (rc) return rc; |