From 309c76e4ee67d21c10e1724633d128d5a5c838c0 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 7 Dec 2018 12:26:58 +0100 Subject: libsmartcols: add grouping samples Signed-off-by: Karel Zak --- libsmartcols/samples/Makemodule.am | 9 ++ libsmartcols/samples/grouping-overlay.c | 137 ++++++++++++++++++++++++++++++ libsmartcols/samples/grouping-simple.c | 144 ++++++++++++++++++++++++++++++++ 3 files changed, 290 insertions(+) create mode 100644 libsmartcols/samples/grouping-overlay.c create mode 100644 libsmartcols/samples/grouping-simple.c (limited to 'libsmartcols') diff --git a/libsmartcols/samples/Makemodule.am b/libsmartcols/samples/Makemodule.am index 644ac129e..d6ab25c13 100644 --- a/libsmartcols/samples/Makemodule.am +++ b/libsmartcols/samples/Makemodule.am @@ -4,6 +4,8 @@ check_PROGRAMS += \ sample-scols-wrap \ sample-scols-continuous \ sample-scols-fromfile \ + sample-scols-grouping-simple \ + sample-scols-grouping-overlay \ sample-scols-maxout sample_scols_cflags = $(AM_CFLAGS) $(NO_UNUSED_WARN_CFLAGS) \ @@ -37,3 +39,10 @@ sample_scols_fromfile_SOURCES = libsmartcols/samples/fromfile.c sample_scols_fromfile_LDADD = $(sample_scols_ldadd) libcommon.la sample_scols_fromfile_CFLAGS = $(sample_scols_cflags) +sample_scols_grouping_simple_SOURCES = libsmartcols/samples/grouping-simple.c +sample_scols_grouping_simple_LDADD = $(sample_scols_ldadd) libcommon.la +sample_scols_grouping_simple_CFLAGS = $(sample_scols_cflags) + +sample_scols_grouping_overlay_SOURCES = libsmartcols/samples/grouping-overlay.c +sample_scols_grouping_overlay_LDADD = $(sample_scols_ldadd) libcommon.la +sample_scols_grouping_overlay_CFLAGS = $(sample_scols_cflags) diff --git a/libsmartcols/samples/grouping-overlay.c b/libsmartcols/samples/grouping-overlay.c new file mode 100644 index 000000000..ef125a660 --- /dev/null +++ b/libsmartcols/samples/grouping-overlay.c @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2018 Karel Zak + * + * This file may be redistributed under the terms of the + * GNU Lesser General Public License. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "c.h" +#include "nls.h" +#include "strutils.h" +#include "xalloc.h" + +#include "libsmartcols.h" + + +enum { COL_NAME, COL_DATA }; + +/* add columns to the @tb */ +static void setup_columns(struct libscols_table *tb) +{ + if (!scols_table_new_column(tb, "NAME", 0, SCOLS_FL_TREE)) + goto fail; + if (!scols_table_new_column(tb, "DATA", 0, 0)) + goto fail; + return; +fail: + scols_unref_table(tb); + err(EXIT_FAILURE, "failed to create output columns"); +} + +static struct libscols_line *add_line(struct libscols_table *tb, struct libscols_line *parent, const char *name, const char *data) +{ + struct libscols_line *ln = scols_table_new_line(tb, parent); + if (!ln) + err(EXIT_FAILURE, "failed to create output line"); + + if (scols_line_set_data(ln, COL_NAME, name)) + goto fail; + if (scols_line_set_data(ln, COL_DATA, data)) + goto fail; + return ln; +fail: + scols_unref_table(tb); + err(EXIT_FAILURE, "failed to create output line"); +} + +int main(int argc, char *argv[]) +{ + struct libscols_table *tb; + struct libscols_line *ln; /* any line */ + struct libscols_line *g1, *g2, *g3; /* groups */ + struct libscols_line *p1; /* parents */ + int c; + + static const struct option longopts[] = { + { "maxout", 0, NULL, 'm' }, + { "width", 1, NULL, 'w' }, + { "help", 1, NULL, 'h' }, + + { NULL, 0, NULL, 0 }, + }; + + setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */ + + scols_init_debug(0); + + tb = scols_new_table(); + if (!tb) + err(EXIT_FAILURE, "failed to create output table"); + + while((c = getopt_long(argc, argv, "hmw:", longopts, NULL)) != -1) { + switch(c) { + case 'h': + printf("%s [--help | --maxout | --width ]\n", program_invocation_short_name); + break; + case 'm': + scols_table_enable_maxout(tb, TRUE); + break; + case 'w': + scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS); + scols_table_set_termwidth(tb, strtou32_or_err(optarg, "failed to parse terminal width")); + break; + } + } + + scols_table_enable_colors(tb, isatty(STDOUT_FILENO)); + setup_columns(tb); + + add_line(tb, NULL, "Alone", "bla bla bla"); + + p1 = add_line(tb, NULL, "A", "bla bla bla"); + add_line(tb, p1, "A:B", "bla bla bla"); + add_line(tb, p1, "A:C", "bla bla bla"); + + g1 = add_line(tb, NULL, "B", "bla bla bla"); + + g2 = add_line(tb, NULL, "C", "bla bla bla"); + ln = add_line(tb, NULL, "D", "bla bla bla"); + scols_table_group_lines(tb, g2, ln, 0); + + ln = add_line(tb, NULL, "G2:A", "alb alb alb"); + scols_line_link_group(ln, g2, 0); + + ln = add_line(tb, NULL, "E", "bla bla bla"); + scols_table_group_lines(tb, g1, ln, 0); + + + ln = add_line(tb, NULL, "G1:A", "alb alb alb"); + scols_line_link_group(ln, g1, 0); + + add_line(tb, NULL, "G", "bla bla bla"); + + g3 = ln = add_line(tb, NULL, "G1:B", "alb alb alb"); + scols_line_link_group(ln, g1, 0); + + ln = add_line(tb, NULL, "F", "bla bla bla"); + scols_table_group_lines(tb, g3, ln, 0); + + ln = add_line(tb, NULL, "G3:A", "alb alb alb"); + scols_line_link_group(ln, g3, 0); + + add_line(tb, NULL, "foo", "bla bla bla"); + add_line(tb, NULL, "bar", "bla bla bla"); + + scols_print_table(tb); + + scols_unref_table(tb); + return EXIT_SUCCESS; +} diff --git a/libsmartcols/samples/grouping-simple.c b/libsmartcols/samples/grouping-simple.c new file mode 100644 index 000000000..8f363feea --- /dev/null +++ b/libsmartcols/samples/grouping-simple.c @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2010-2014 Karel Zak + * + * This file may be redistributed under the terms of the + * GNU Lesser General Public License. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "c.h" +#include "nls.h" +#include "strutils.h" +#include "xalloc.h" + +#include "libsmartcols.h" + + +enum { COL_NAME, COL_DATA }; + +/* add columns to the @tb */ +static void setup_columns(struct libscols_table *tb) +{ + if (!scols_table_new_column(tb, "NAME", 0, SCOLS_FL_TREE)) + goto fail; + if (!scols_table_new_column(tb, "DATA", 0, 0)) + goto fail; + return; +fail: + scols_unref_table(tb); + err(EXIT_FAILURE, "failed to create output columns"); +} + +static struct libscols_line *add_line(struct libscols_table *tb, struct libscols_line *parent, const char *name, const char *data) +{ + struct libscols_line *ln = scols_table_new_line(tb, parent); + if (!ln) + err(EXIT_FAILURE, "failed to create output line"); + + if (scols_line_set_data(ln, COL_NAME, name)) + goto fail; + if (scols_line_set_data(ln, COL_DATA, data)) + goto fail; + return ln; +fail: + scols_unref_table(tb); + err(EXIT_FAILURE, "failed to create output line"); +} + +int main(int argc, char *argv[]) +{ + struct libscols_table *tb; + struct libscols_line *ln; /* any line */ + struct libscols_line *g1; /* groups */ + struct libscols_line *p1, *p2; /* parents */ + int c; + + static const struct option longopts[] = { + { "maxout", 0, NULL, 'm' }, + { "width", 1, NULL, 'w' }, + { "help", 1, NULL, 'h' }, + + { NULL, 0, NULL, 0 }, + }; + + setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */ + + scols_init_debug(0); + + tb = scols_new_table(); + if (!tb) + err(EXIT_FAILURE, "failed to create output table"); + + while((c = getopt_long(argc, argv, "hmw:", longopts, NULL)) != -1) { + switch(c) { + case 'h': + printf("%s [--help | --maxout | --width ]\n", program_invocation_short_name); + break; + case 'm': + scols_table_enable_maxout(tb, TRUE); + break; + case 'w': + scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS); + scols_table_set_termwidth(tb, strtou32_or_err(optarg, "failed to parse terminal width")); + break; + } + } + + scols_table_enable_colors(tb, isatty(STDOUT_FILENO)); + setup_columns(tb); + + add_line(tb, NULL, "Alone", "bla bla bla"); + + p1 = add_line(tb, NULL, "A", "bla bla bla"); + add_line(tb, p1, "A:B", "bla bla bla"); + add_line(tb, p1, "A:C", "bla bla bla"); + + g1 = add_line(tb, NULL, "B", "bla bla bla"); + add_line(tb, NULL, "C", "bla bla bla"); + p1 = add_line(tb, NULL, "D", "bla bla bla"); + + p2 = add_line(tb, p1, "D:A", "bla bla bla"); + + ln = add_line(tb, p2, "D:A:A", "bla bla bla"); + scols_table_group_lines(tb, g1, ln, 0); + + add_line(tb, p1, "D:B", "bla bla bla"); + + ln = add_line(tb, NULL, "E", "bla bla bla"); + scols_table_group_lines(tb, g1, ln, 0); + + p1 = ln; + add_line(tb, p1, "E:A", "bla bla bla"); + add_line(tb, p1, "E:B", "bla bla bla"); + add_line(tb, p1, "E:C", "bla bla bla"); + + add_line(tb, NULL, "F", "bla bla bla"); + + ln = add_line(tb, NULL, "G1:A", "alb alb alb"); + scols_line_link_group(ln, g1, 0); + + p1 = ln; + add_line(tb, p1, "G1:A:A", "bla bla bla"); + add_line(tb, p1, "G1:A:B", "bla bla bla"); + add_line(tb, p1, "G1:A:C", "bla bla bla"); + + add_line(tb, NULL, "G", "bla bla bla"); + + ln = add_line(tb, NULL, "G1:B", "alb alb alb"); + scols_line_link_group(ln, g1, 0); + + add_line(tb, NULL, "foo", "bla bla bla"); + add_line(tb, NULL, "bar", "bla bla bla"); + + scols_print_table(tb); + + scols_unref_table(tb); + return EXIT_SUCCESS; +} -- cgit v1.2.3-55-g7522