From 905dde8fa5d41b10b16a6fe707aa44f1610ef2e1 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 8 Aug 2023 12:19:26 +0200 Subject: [PATCH] libsmartcols: add filter sample Signed-off-by: Karel Zak --- Makefile.am | 4 +- libsmartcols/samples/Makemodule.am | 5 + libsmartcols/samples/filter.c | 142 +++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 libsmartcols/samples/filter.c diff --git a/Makefile.am b/Makefile.am index 0335f58937..2df89ff6df 100644 --- a/Makefile.am +++ b/Makefile.am @@ -63,6 +63,8 @@ dist_bashcompletion_DATA = check_PROGRAMS = dist_check_SCRIPTS = +BUILT_SOURCES = + PATHFILES = ADOCFILES_COMMON = MANPAGES = @@ -347,7 +349,7 @@ DISTCHECK_CONFIGURE_FLAGS = \ --with-systemdsystemunitdir=$$dc_install_base/$(systemdsystemunitdir) -BUILT_SOURCES = .version +BUILT_SOURCES += .version .version: echo $(VERSION) > $@-t && mv $@-t $@ diff --git a/libsmartcols/samples/Makemodule.am b/libsmartcols/samples/Makemodule.am index c0130b9e0a..fc4bce9b2c 100644 --- a/libsmartcols/samples/Makemodule.am +++ b/libsmartcols/samples/Makemodule.am @@ -1,6 +1,7 @@ check_PROGRAMS += \ sample-scols-colors \ + sample-scols-filter \ sample-scols-title \ sample-scols-wrap \ sample-scols-continuous \ @@ -24,6 +25,10 @@ sample_scols_colors_SOURCES = libsmartcols/samples/colors.c sample_scols_colors_LDADD = $(sample_scols_ldadd) libcommon.la sample_scols_colors_CFLAGS = $(sample_scols_cflags) +sample_scols_filter_SOURCES = libsmartcols/samples/filter.c +sample_scols_filter_LDADD = $(sample_scols_ldadd) libcommon.la +sample_scols_filter_CFLAGS = $(sample_scols_cflags) + sample_scols_title_SOURCES = libsmartcols/samples/title.c sample_scols_title_LDADD = $(sample_scols_ldadd) libcommon.la sample_scols_title_CFLAGS = $(sample_scols_cflags) diff --git a/libsmartcols/samples/filter.c b/libsmartcols/samples/filter.c new file mode 100644 index 0000000000..e043e46acf --- /dev/null +++ b/libsmartcols/samples/filter.c @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2023 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_NUM, COL_FLOAT, COL_STRING }; + +/* add columns to the @tb */ +static void setup_columns(struct libscols_table *tb) +{ + struct libscols_column *col; + + col = scols_table_new_column(tb, "NAME", 0, 0); + if (!col) + goto fail; + scols_column_set_json_type(col, SCOLS_JSON_STRING); + + col = scols_table_new_column(tb, "NUM", 0, 0); + if (!col) + goto fail; + scols_column_set_json_type(col, SCOLS_JSON_NUMBER); + + col = scols_table_new_column(tb, "FLOAT", 0, 0); + if (!col) + goto fail; + scols_column_set_json_type(col, SCOLS_JSON_NUMBER); + + col = scols_table_new_column(tb, "STRING", 0, 0); + if (!col) + goto fail; + scols_column_set_json_type(col, SCOLS_JSON_STRING); + + return; +fail: + scols_unref_table(tb); + err(EXIT_FAILURE, "failed to create output columns"); +} + +static struct libscols_line *add_line(struct libscols_table *tb, int n) +{ + char *data = NULL; + struct libscols_line *ln = scols_table_new_line(tb, NULL); + if (!ln) + err(EXIT_FAILURE, "failed to create output line"); + + xasprintf(&data, "#%d", n); + if (scols_line_refer_data(ln, COL_NAME, data)) + goto fail; + + xasprintf(&data, "%d", n); + if (scols_line_refer_data(ln, COL_NUM, data)) + goto fail; + + xasprintf(&data, "%d.%d", n, n); + if (scols_line_refer_data(ln, COL_FLOAT, data)) + goto fail; + + xasprintf(&data, "str%dstr", n); + if (scols_line_refer_data(ln, COL_STRING, 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_filter *fltr = NULL; + int c, i, json = 0, dump = 0; + + static const struct option longopts[] = { + { "json", 0, NULL, 'J' }, + { "dump", 0, NULL, 'D' }, + { "filter", 1, NULL, 'Q' }, + { "help", 0, 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, "DhJQ:", longopts, NULL)) != -1) { + switch(c) { + case 'h': + printf("%s --help | --filter | --dump | --json]\n", program_invocation_short_name); + break; + case 'D': + dump = 1; + break; + case 'J': + json = 1; + break; + case 'Q': + fltr = scols_new_filter(NULL); + if (!fltr) + err(EXIT_FAILURE, "failed to allocate filter"); + if (scols_filter_parse_string(fltr, optarg) != 0) + errx(EXIT_FAILURE, "failed to parse filter: %s", + scols_filter_get_errmsg(fltr)); + break; + } + } + + if (dump && fltr) + scols_dump_filter(fltr, stdout); + + scols_table_enable_json(tb, json); + setup_columns(tb); + + for (i = 0; i < 10; i++) + add_line(tb, i + 1); + + scols_print_table(tb); + scols_unref_table(tb); + scols_unref_filter(fltr); + return EXIT_SUCCESS; +} -- 2.47.3