libsmartcols: add scols_table_print_range()
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
a8b122f229
commit
2a750b4c4e
|
@ -143,6 +143,7 @@ scols_unref_table
|
||||||
<FILE>table_print</FILE>
|
<FILE>table_print</FILE>
|
||||||
scols_print_table
|
scols_print_table
|
||||||
scols_print_table_to_string
|
scols_print_table_to_string
|
||||||
|
scols_table_print_range
|
||||||
</SECTION>
|
</SECTION>
|
||||||
|
|
||||||
<SECTION>
|
<SECTION>
|
||||||
|
|
|
@ -142,13 +142,15 @@ static void add_lines(struct libscols_table *tb, const char *dirname)
|
||||||
static void __attribute__((__noreturn__)) usage(FILE *out)
|
static void __attribute__((__noreturn__)) usage(FILE *out)
|
||||||
{
|
{
|
||||||
fprintf(out, " %s [options] [<dir> ...]\n\n", program_invocation_short_name);
|
fprintf(out, " %s [options] [<dir> ...]\n\n", program_invocation_short_name);
|
||||||
fputs(" -c, --csv display a csv-like output\n", out);
|
fputs(" -c, --csv display a csv-like output\n", out);
|
||||||
fputs(" -i, --ascii use ascii characters only\n", out);
|
fputs(" -i, --ascii use ascii characters only\n", out);
|
||||||
fputs(" -l, --list use list format output\n", out);
|
fputs(" -l, --list use list format output\n", out);
|
||||||
fputs(" -n, --noheadings don't print headings\n", out);
|
fputs(" -n, --noheadings don't print headings\n", out);
|
||||||
fputs(" -p, --pairs use key=\"value\" output format\n", out);
|
fputs(" -p, --pairs use key=\"value\" output format\n", out);
|
||||||
fputs(" -J, --json use JSON output format\n", out);
|
fputs(" -J, --json use JSON output format\n", out);
|
||||||
fputs(" -r, --raw use raw output format\n", out);
|
fputs(" -r, --raw use raw output format\n", out);
|
||||||
|
fputs(" -S, --range-start <n> first line to print\n", out);
|
||||||
|
fputs(" -E, --range-end <n> last line to print\n", out);
|
||||||
|
|
||||||
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
@ -156,7 +158,8 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct libscols_table *tb;
|
struct libscols_table *tb;
|
||||||
int c, notree = 0;
|
int c, notree = 0, nstart = -1, nend = -1;
|
||||||
|
|
||||||
|
|
||||||
static const struct option longopts[] = {
|
static const struct option longopts[] = {
|
||||||
{ "ascii", 0, 0, 'i' },
|
{ "ascii", 0, 0, 'i' },
|
||||||
|
@ -166,7 +169,8 @@ int main(int argc, char *argv[])
|
||||||
{ "pairs", 0, 0, 'p' },
|
{ "pairs", 0, 0, 'p' },
|
||||||
{ "json", 0, 0, 'J' },
|
{ "json", 0, 0, 'J' },
|
||||||
{ "raw", 0, 0, 'r' },
|
{ "raw", 0, 0, 'r' },
|
||||||
|
{ "range-start",1, 0, 'S' },
|
||||||
|
{ "range-end", 1, 0, 'E' },
|
||||||
{ NULL, 0, 0, 0 },
|
{ NULL, 0, 0, 0 },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -178,7 +182,7 @@ int main(int argc, char *argv[])
|
||||||
if (!tb)
|
if (!tb)
|
||||||
err(EXIT_FAILURE, "faild to create output table");
|
err(EXIT_FAILURE, "faild to create output table");
|
||||||
|
|
||||||
while((c = getopt_long(argc, argv, "ciJlnpr", longopts, NULL)) != -1) {
|
while((c = getopt_long(argc, argv, "ciJlnprS:E:", longopts, NULL)) != -1) {
|
||||||
switch(c) {
|
switch(c) {
|
||||||
case 'c':
|
case 'c':
|
||||||
scols_table_set_column_separator(tb, ",");
|
scols_table_set_column_separator(tb, ",");
|
||||||
|
@ -206,6 +210,12 @@ int main(int argc, char *argv[])
|
||||||
scols_table_enable_raw(tb, 1);
|
scols_table_enable_raw(tb, 1);
|
||||||
notree = 1;
|
notree = 1;
|
||||||
break;
|
break;
|
||||||
|
case 'S':
|
||||||
|
nstart = strtos32_or_err(optarg, "failed to parse range start") - 1;
|
||||||
|
break;
|
||||||
|
case 'E':
|
||||||
|
nend = strtos32_or_err(optarg, "failed to parse range end") - 1;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
usage(stderr);
|
usage(stderr);
|
||||||
}
|
}
|
||||||
|
@ -219,8 +229,21 @@ int main(int argc, char *argv[])
|
||||||
else while (optind < argc)
|
else while (optind < argc)
|
||||||
add_lines(tb, argv[optind++]);
|
add_lines(tb, argv[optind++]);
|
||||||
|
|
||||||
scols_print_table(tb);
|
if (nstart >= 0 || nend >= 0) {
|
||||||
scols_unref_table(tb);
|
/* print subset */
|
||||||
|
struct libscols_line *start = NULL, *end = NULL;
|
||||||
|
|
||||||
|
if (nstart >= 0)
|
||||||
|
start = scols_table_get_line(tb, nstart);
|
||||||
|
if (nend >= 0)
|
||||||
|
end = scols_table_get_line(tb, nend);
|
||||||
|
|
||||||
|
if (start || end)
|
||||||
|
scols_table_print_range(tb, start, end);
|
||||||
|
} else
|
||||||
|
/* print all table */
|
||||||
|
scols_print_table(tb);
|
||||||
|
|
||||||
|
scols_unref_table(tb);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -246,6 +246,10 @@ extern int scols_sort_table(struct libscols_table *tb, struct libscols_column *c
|
||||||
extern int scols_print_table(struct libscols_table *tb);
|
extern int scols_print_table(struct libscols_table *tb);
|
||||||
extern int scols_print_table_to_string(struct libscols_table *tb, char **data);
|
extern int scols_print_table_to_string(struct libscols_table *tb, char **data);
|
||||||
|
|
||||||
|
extern int scols_table_print_range( struct libscols_table *tb,
|
||||||
|
struct libscols_line *start,
|
||||||
|
struct libscols_line *end);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -132,4 +132,5 @@ global:
|
||||||
scols_table_get_title;
|
scols_table_get_title;
|
||||||
scols_cell_get_flags;
|
scols_cell_get_flags;
|
||||||
scols_cell_set_flags;
|
scols_cell_set_flags;
|
||||||
|
scols_table_print_range;
|
||||||
} SMARTCOLS_2.27;
|
} SMARTCOLS_2.27;
|
||||||
|
|
|
@ -572,13 +572,7 @@ err:
|
||||||
* @tb: table
|
* @tb: table
|
||||||
* @n: column number (0..N)
|
* @n: column number (0..N)
|
||||||
*
|
*
|
||||||
* This is a shortcut for
|
* Returns: a line or NULL
|
||||||
*
|
|
||||||
* ln = scols_new_line();
|
|
||||||
* scols_line_set_....(cl, ...);
|
|
||||||
* scols_table_add_line(tb, ln);
|
|
||||||
*
|
|
||||||
* Returns: a newly allocate line
|
|
||||||
*/
|
*/
|
||||||
struct libscols_line *scols_table_get_line(struct libscols_table *tb,
|
struct libscols_line *scols_table_get_line(struct libscols_table *tb,
|
||||||
size_t n)
|
size_t n)
|
||||||
|
|
|
@ -801,24 +801,39 @@ static int print_header(struct libscols_table *tb, struct libscols_buffer *buf)
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int print_table(struct libscols_table *tb, struct libscols_buffer *buf)
|
static int print_range( struct libscols_table *tb,
|
||||||
|
struct libscols_buffer *buf,
|
||||||
|
struct libscols_iter *itr,
|
||||||
|
struct libscols_line *end)
|
||||||
{
|
{
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
struct libscols_line *ln;
|
struct libscols_line *ln;
|
||||||
struct libscols_iter itr;
|
|
||||||
|
|
||||||
assert(tb);
|
assert(tb);
|
||||||
|
|
||||||
scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
|
while (rc == 0 && scols_table_next_line(tb, itr, &ln) == 0) {
|
||||||
while (rc == 0 && scols_table_next_line(tb, &itr, &ln) == 0) {
|
|
||||||
fput_line_open(tb);
|
fput_line_open(tb);
|
||||||
rc = print_line(tb, ln, buf);
|
rc = print_line(tb, ln, buf);
|
||||||
fput_line_close(tb, scols_iter_is_last(&itr));
|
fput_line_close(tb, scols_iter_is_last(itr));
|
||||||
|
|
||||||
|
if (end && ln == end)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int print_table(struct libscols_table *tb, struct libscols_buffer *buf)
|
||||||
|
{
|
||||||
|
struct libscols_iter itr;
|
||||||
|
|
||||||
|
scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
|
||||||
|
return print_range(tb, buf, &itr, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int print_tree_line(struct libscols_table *tb,
|
static int print_tree_line(struct libscols_table *tb,
|
||||||
struct libscols_line *ln,
|
struct libscols_line *ln,
|
||||||
struct libscols_buffer *buf,
|
struct libscols_buffer *buf,
|
||||||
|
@ -1202,33 +1217,14 @@ static size_t strlen_line(struct libscols_line *ln)
|
||||||
return sz;
|
return sz;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int initialize_printting(struct libscols_table *tb, struct libscols_buffer **buf)
|
||||||
|
|
||||||
/**
|
|
||||||
* scols_print_table:
|
|
||||||
* @tb: table
|
|
||||||
*
|
|
||||||
* Prints the table to the output stream.
|
|
||||||
*
|
|
||||||
* Returns: 0, a negative value in case of an error.
|
|
||||||
*/
|
|
||||||
int scols_print_table(struct libscols_table *tb)
|
|
||||||
{
|
{
|
||||||
int rc = 0;
|
|
||||||
size_t bufsz, extra_bufsz = 0;
|
size_t bufsz, extra_bufsz = 0;
|
||||||
struct libscols_line *ln;
|
struct libscols_line *ln;
|
||||||
struct libscols_iter itr;
|
struct libscols_iter itr;
|
||||||
struct libscols_buffer *buf;
|
int rc;
|
||||||
|
|
||||||
if (!tb)
|
DBG(TAB, ul_debugobj(tb, "initialize printting"));
|
||||||
return -EINVAL;
|
|
||||||
|
|
||||||
DBG(TAB, ul_debugobj(tb, "printing"));
|
|
||||||
|
|
||||||
if (list_empty(&tb->tb_lines)) {
|
|
||||||
DBG(TAB, ul_debugobj(tb, "ignore -- epmty table"));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tb->symbols)
|
if (!tb->symbols)
|
||||||
scols_table_set_symbols(tb, NULL); /* use default */
|
scols_table_set_symbols(tb, NULL); /* use default */
|
||||||
|
@ -1262,7 +1258,9 @@ int scols_print_table(struct libscols_table *tb)
|
||||||
case SCOLS_FMT_EXPORT:
|
case SCOLS_FMT_EXPORT:
|
||||||
{
|
{
|
||||||
struct libscols_column *cl;
|
struct libscols_column *cl;
|
||||||
|
|
||||||
scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
|
scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
|
||||||
|
|
||||||
while (rc == 0 && scols_table_next_column(tb, &itr, &cl) == 0) {
|
while (rc == 0 && scols_table_next_column(tb, &itr, &cl) == 0) {
|
||||||
if (scols_column_is_hidden(cl))
|
if (scols_column_is_hidden(cl))
|
||||||
continue;
|
continue;
|
||||||
|
@ -1275,7 +1273,6 @@ int scols_print_table(struct libscols_table *tb)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Enlarge buffer if necessary, the buffer should be large enough to
|
* Enlarge buffer if necessary, the buffer should be large enough to
|
||||||
* store line data and tree ascii art (or another decoration).
|
* store line data and tree ascii art (or another decoration).
|
||||||
|
@ -1287,16 +1284,87 @@ int scols_print_table(struct libscols_table *tb)
|
||||||
bufsz = sz;
|
bufsz = sz;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = new_buffer(bufsz + 1); /* data + space for \0 */
|
*buf = new_buffer(bufsz + 1); /* data + space for \0 */
|
||||||
if (!buf)
|
if (!*buf)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
if (tb->format == SCOLS_FMT_HUMAN) {
|
if (tb->format == SCOLS_FMT_HUMAN) {
|
||||||
rc = recount_widths(tb, buf);
|
rc = recount_widths(tb, *buf);
|
||||||
if (rc != 0)
|
if (rc != 0)
|
||||||
goto done;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
err:
|
||||||
|
free_buffer(*buf);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* scola_table_print_range:
|
||||||
|
* @tb: table
|
||||||
|
* @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.
|
||||||
|
*
|
||||||
|
* Returns: 0, a negative value in case of an error.
|
||||||
|
*/
|
||||||
|
int scols_table_print_range( struct libscols_table *tb,
|
||||||
|
struct libscols_line *start,
|
||||||
|
struct libscols_line *end)
|
||||||
|
{
|
||||||
|
struct libscols_buffer *buf;
|
||||||
|
struct libscols_iter itr;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
if (scols_table_is_tree(tb))
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
DBG(TAB, ul_debugobj(tb, "printing range"));
|
||||||
|
|
||||||
|
rc = initialize_printting(tb, &buf);
|
||||||
|
if (rc)
|
||||||
|
return rc;
|
||||||
|
|
||||||
|
if (start) {
|
||||||
|
itr.direction = SCOLS_ITER_FORWARD;
|
||||||
|
itr.head = &tb->tb_lines;
|
||||||
|
itr.p = &start->ln_lines;
|
||||||
|
} else
|
||||||
|
scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
|
||||||
|
|
||||||
|
rc = print_range(tb, buf, &itr, end);
|
||||||
|
|
||||||
|
free_buffer(buf);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* scols_print_table:
|
||||||
|
* @tb: table
|
||||||
|
*
|
||||||
|
* Prints the table to the output stream.
|
||||||
|
*
|
||||||
|
* Returns: 0, a negative value in case of an error.
|
||||||
|
*/
|
||||||
|
int scols_print_table(struct libscols_table *tb)
|
||||||
|
{
|
||||||
|
int rc = 0;
|
||||||
|
struct libscols_buffer *buf;
|
||||||
|
|
||||||
|
if (!tb)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
DBG(TAB, ul_debugobj(tb, "printing"));
|
||||||
|
|
||||||
|
if (list_empty(&tb->tb_lines)) {
|
||||||
|
DBG(TAB, ul_debugobj(tb, "ignore -- epmty table"));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = initialize_printting(tb, &buf);
|
||||||
|
if (rc)
|
||||||
|
return rc;
|
||||||
|
|
||||||
fput_table_open(tb);
|
fput_table_open(tb);
|
||||||
|
|
||||||
if (tb->format == SCOLS_FMT_HUMAN)
|
if (tb->format == SCOLS_FMT_HUMAN)
|
||||||
|
|
Loading…
Reference in New Issue