diff options
Diffstat (limited to 'libsmartcols/samples/tree.c')
-rw-r--r-- | libsmartcols/samples/tree.c | 47 |
1 files changed, 35 insertions, 12 deletions
diff --git a/libsmartcols/samples/tree.c b/libsmartcols/samples/tree.c index 52be7fbd5..d2afc4717 100644 --- a/libsmartcols/samples/tree.c +++ b/libsmartcols/samples/tree.c @@ -142,13 +142,15 @@ static void add_lines(struct libscols_table *tb, const char *dirname) static void __attribute__((__noreturn__)) usage(FILE *out) { fprintf(out, " %s [options] [<dir> ...]\n\n", program_invocation_short_name); - fputs(" -c, --csv display a csv-like output\n", out); - fputs(" -i, --ascii use ascii characters only\n", out); - fputs(" -l, --list use list format output\n", out); - fputs(" -n, --noheadings don't print headings\n", out); - fputs(" -p, --pairs use key=\"value\" output format\n", out); - fputs(" -J, --json use JSON output format\n", out); - fputs(" -r, --raw use raw output format\n", out); + fputs(" -c, --csv display a csv-like output\n", out); + fputs(" -i, --ascii use ascii characters only\n", out); + fputs(" -l, --list use list format output\n", out); + fputs(" -n, --noheadings don't print headings\n", out); + fputs(" -p, --pairs use key=\"value\" output format\n", out); + fputs(" -J, --json use JSON 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); } @@ -156,7 +158,8 @@ static void __attribute__((__noreturn__)) usage(FILE *out) int main(int argc, char *argv[]) { struct libscols_table *tb; - int c, notree = 0; + int c, notree = 0, nstart = -1, nend = -1; + static const struct option longopts[] = { { "ascii", 0, 0, 'i' }, @@ -166,7 +169,8 @@ int main(int argc, char *argv[]) { "pairs", 0, 0, 'p' }, { "json", 0, 0, 'J' }, { "raw", 0, 0, 'r' }, - + { "range-start",1, 0, 'S' }, + { "range-end", 1, 0, 'E' }, { NULL, 0, 0, 0 }, }; @@ -178,7 +182,7 @@ int main(int argc, char *argv[]) if (!tb) 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) { case 'c': scols_table_set_column_separator(tb, ","); @@ -206,6 +210,12 @@ int main(int argc, char *argv[]) scols_table_enable_raw(tb, 1); notree = 1; 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: usage(stderr); } @@ -219,8 +229,21 @@ int main(int argc, char *argv[]) else while (optind < argc) add_lines(tb, argv[optind++]); - scols_print_table(tb); - scols_unref_table(tb); + if (nstart >= 0 || nend >= 0) { + /* 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; } |