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>
This commit is contained in:
parent
2a750b4c4e
commit
2981e0fd0e
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -133,4 +133,5 @@ global:
|
|||
scols_cell_get_flags;
|
||||
scols_cell_set_flags;
|
||||
scols_table_print_range;
|
||||
scols_table_enable_nolinesep;
|
||||
} SMARTCOLS_2.27;
|
||||
|
|
|
@ -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 */
|
||||
};
|
||||
|
||||
|
|
|
@ -707,6 +707,27 @@ int scols_table_set_symbols(struct libscols_table *tb,
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue