From 3a29e909f7862855d814b76c9e369403cf21242f Mon Sep 17 00:00:00 2001 From: Ondrej Oprala Date: Wed, 19 Mar 2014 16:48:22 +0100 Subject: [PATCH] libsmartcols: add test [kzak@redhat.com: - refresh according to previous API changes] Signed-off-by: Karel Zak --- libsmartcols/src/Makemodule.am | 12 +++ libsmartcols/src/test.c | 155 +++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 libsmartcols/src/test.c diff --git a/libsmartcols/src/Makemodule.am b/libsmartcols/src/Makemodule.am index bf218f8f33..358078257a 100644 --- a/libsmartcols/src/Makemodule.am +++ b/libsmartcols/src/Makemodule.am @@ -41,6 +41,18 @@ EXTRA_DIST += \ libsmartcols/src/libsmartcols.h.in +if BUILD_LIBSMARTCOLS_TESTS +check_PROGRAMS += test_smartcols + +libsmartcols_tests_cflags = $(libsmartcols_la_CFLAGS) +libsmartcols_tests_ldadd = libsmartcols.la libcommon.la + +test_smartcols_SOURCES = libsmartcols/src/test.c +test_smartcols_CFLAGS = $(libsmartcols_tests_cflags) +test_smartcols_LDADD = $(libsmartcols_tests_ldadd) +endif # BUILD_LIBSMARTCOLS_TESTS + + # move lib from $(usrlib_execdir) to $(libdir) if needed install-exec-hook-libsmartcols: if test "$(usrlib_execdir)" != "$(libdir)"; then \ diff --git a/libsmartcols/src/test.c b/libsmartcols/src/test.c new file mode 100644 index 0000000000..2d0087e88c --- /dev/null +++ b/libsmartcols/src/test.c @@ -0,0 +1,155 @@ +/* + * TT - Table or Tree, features: + * - column width could be defined as absolute or relative to the terminal width + * - allows to truncate or wrap data in columns + * - prints tree if parent->child relation is defined + * - draws the tree by ASCII or UTF8 lines (depends on terminal setting) + * + * 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 "c.h" +#include "nls.h" +#include "widechar.h" +#include "mbsalign.h" +#include "ttyutils.h" +#include "colors.h" + +#include "libsmartcols.h" + +enum { MYCOL_NAME, MYCOL_FOO, MYCOL_BAR, MYCOL_PATH }; + +int main(int argc, char *argv[]) +{ + struct libscols_table *tb; + struct libscols_column *cl; + int flags = 0, notree = 0, clone = 0, i, color = 0; + + if (argc == 2 && !strcmp(argv[1], "--help")) { + printf("%s [--ascii | --raw | --list | --clone | --clonetree]\n", + program_invocation_short_name); + return EXIT_SUCCESS; + } else if (argc == 2 && !strcmp(argv[1], "--ascii")) { + flags |= SCOLS_FL_ASCII; + } else if (argc == 2 && !strcmp(argv[1], "--raw")) { + flags |= SCOLS_FL_RAW; + notree = 1; + } else if (argc == 2 && !strcmp(argv[1], "--export")) { + flags |= SCOLS_FL_EXPORT; + notree = 1; + } else if (argc == 2 && !strcmp(argv[1], "--list")) { + notree = 1; + } else if (argc == 2 && !strcmp(argv[1], "--color")) { + notree = 1; + color = 1; + } else if (argc == 2 && !strcmp(argv[1], "--colortree")) { + notree = 0; + color = 1; + } else if (argc == 2 && !strcmp(argv[1], "--clone")) { + notree = 1; + clone = 1; + } else if (argc == 2 && !strcmp(argv[1], "--clonetree")) { + notree = 0; + clone = 1; + } + + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + + tb = scols_new_table(flags, NULL); + if (!tb) + err(EXIT_FAILURE, "table initialization failed"); + + cl = scols_table_new_column(tb, "NAME", 0.3, notree ? 0 : SCOLS_FL_TREE); + if (color) + scols_column_set_color(cl, UL_COLOR_RED); + + cl = scols_table_new_column(tb, "FOO", 0.3, SCOLS_FL_TRUNC); + if (color) { + struct libscols_cell *h = scols_column_get_header(cl); + + scols_column_set_color(cl, UL_COLOR_BOLD_GREEN); + scols_cell_set_color(h, UL_COLOR_GREEN); + } + scols_table_new_column(tb, "BAR", 0.3, 0); + scols_table_new_column(tb, "PATH", 0.3, 0); + + for (i = 0; i < 2; i++) { + struct libscols_line *ln = scols_table_new_line(tb, NULL); + struct libscols_line *pr, *root = ln; + + scols_line_set_data(ln, MYCOL_NAME, "AAA"); + scols_line_set_data(ln, MYCOL_FOO, "a-foo-foo"); + scols_line_set_data(ln, MYCOL_BAR, "barBar-A"); + scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA"); + + pr = ln = scols_table_new_line(tb, root); + scols_line_set_data(ln, MYCOL_NAME, "AAA.A"); + scols_line_set_data(ln, MYCOL_FOO, "a.a-foo-foo"); + scols_line_set_data(ln, MYCOL_BAR, "barBar-A.A"); + scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA/A"); + if (color) + scols_line_set_color(ln, UL_COLOR_BOLD_YELLOW); + + ln = scols_table_new_line(tb, pr); + scols_line_set_data(ln, MYCOL_NAME, "AAA.A.AAA"); + scols_line_set_data(ln, MYCOL_FOO, "a.a.a-foo-foo"); + scols_line_set_data(ln, MYCOL_BAR, "barBar-A.A.A"); + scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA/A/AAA"); + + ln = scols_table_new_line(tb, root); + scols_line_set_data(ln, MYCOL_NAME, "AAA.B"); + scols_line_set_data(ln, MYCOL_FOO, "a.b-foo-foo"); + scols_line_set_data(ln, MYCOL_BAR, "barBar-A.B"); + scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA/B"); + if (color) + scols_cell_set_color(scols_line_get_cell(ln, MYCOL_FOO), + UL_COLOR_MAGENTA); + + ln = scols_table_new_line(tb, pr); + scols_line_set_data(ln, MYCOL_NAME, "AAA.A.BBB"); + scols_line_set_data(ln, MYCOL_FOO, "a.a.b-foo-foo"); + scols_line_set_data(ln, MYCOL_BAR, "barBar-A.A.BBB"); + scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA/A/BBB"); + + ln = scols_table_new_line(tb, pr); + scols_line_set_data(ln, MYCOL_NAME, "AAA.A.CCC"); + scols_line_set_data(ln, MYCOL_FOO, "a.a.c-foo-foo"); + scols_line_set_data(ln, MYCOL_BAR, "barBar-A.A.CCC"); + scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA/A/CCC"); + + ln = scols_table_new_line(tb, root); + scols_line_set_data(ln, MYCOL_NAME, "AAA.C"); + scols_line_set_data(ln, MYCOL_FOO, "a.c-foo-foo"); + scols_line_set_data(ln, MYCOL_BAR, "barBar-A.C"); + scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA/C"); + } + + printf("\nColumns: %d, Lines: %d\n\n", + scols_table_get_ncols(tb), + scols_table_get_nlines(tb)); + + if (clone) { + struct libscols_table *xtb = scols_copy_table(tb); + scols_print_table(xtb); + fputs("\n\n", stdout); + scols_unref_table(xtb); + } + + scols_print_table(tb); + scols_unref_table(tb); + + return EXIT_SUCCESS; +} -- 2.47.2