summaryrefslogtreecommitdiffstats
path: root/libsmartcols/samples
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--libsmartcols/samples/Makemodule.am39
-rw-r--r--libsmartcols/samples/continuous.c138
-rw-r--r--libsmartcols/samples/fromfile.c344
-rw-r--r--libsmartcols/samples/maxout.c56
-rw-r--r--libsmartcols/samples/title.c135
-rw-r--r--libsmartcols/samples/tree.c249
-rw-r--r--libsmartcols/samples/wrap.c111
7 files changed, 1072 insertions, 0 deletions
diff --git a/libsmartcols/samples/Makemodule.am b/libsmartcols/samples/Makemodule.am
new file mode 100644
index 0000000..644ac12
--- /dev/null
+++ b/libsmartcols/samples/Makemodule.am
@@ -0,0 +1,39 @@
+
+check_PROGRAMS += \
+ sample-scols-title \
+ sample-scols-wrap \
+ sample-scols-continuous \
+ sample-scols-fromfile \
+ sample-scols-maxout
+
+sample_scols_cflags = $(AM_CFLAGS) $(NO_UNUSED_WARN_CFLAGS) \
+ -I$(ul_libsmartcols_incdir)
+sample_scols_ldadd = libsmartcols.la $(LDADD)
+
+if HAVE_OPENAT
+check_PROGRAMS += sample-scols-tree
+sample_scols_tree_SOURCES = libsmartcols/samples/tree.c
+sample_scols_tree_LDADD = $(sample_scols_ldadd) libcommon.la
+sample_scols_tree_CFLAGS = $(sample_scols_cflags)
+endif
+
+sample_scols_title_SOURCES = libsmartcols/samples/title.c
+sample_scols_title_LDADD = $(sample_scols_ldadd) libcommon.la
+sample_scols_title_CFLAGS = $(sample_scols_cflags)
+
+sample_scols_wrap_SOURCES = libsmartcols/samples/wrap.c
+sample_scols_wrap_LDADD = $(sample_scols_ldadd)
+sample_scols_wrap_CFLAGS = $(sample_scols_cflags)
+
+sample_scols_continuous_SOURCES = libsmartcols/samples/continuous.c
+sample_scols_continuous_LDADD = $(sample_scols_ldadd) libcommon.la
+sample_scols_continuous_CFLAGS = $(sample_scols_cflags)
+
+sample_scols_maxout_SOURCES = libsmartcols/samples/maxout.c
+sample_scols_maxout_LDADD = $(sample_scols_ldadd)
+sample_scols_maxout_CFLAGS = $(sample_scols_cflags)
+
+sample_scols_fromfile_SOURCES = libsmartcols/samples/fromfile.c
+sample_scols_fromfile_LDADD = $(sample_scols_ldadd) libcommon.la
+sample_scols_fromfile_CFLAGS = $(sample_scols_cflags)
+
diff --git a/libsmartcols/samples/continuous.c b/libsmartcols/samples/continuous.c
new file mode 100644
index 0000000..dfe54b6
--- /dev/null
+++ b/libsmartcols/samples/continuous.c
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2016 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/time.h>
+
+#include "c.h"
+#include "nls.h"
+#include "strutils.h"
+#include "xalloc.h"
+
+#include "libsmartcols.h"
+
+#define TIME_PERIOD 3.0 /* seconds */
+
+enum { COL_NUM, COL_DATA, COL_TIME };
+
+static double time_diff(struct timeval *a, struct timeval *b)
+{
+ return (a->tv_sec - b->tv_sec) + (a->tv_usec - b->tv_usec) / 1E6;
+}
+
+/* add columns to the @tb */
+static void setup_columns(struct libscols_table *tb)
+{
+ scols_table_enable_maxout(tb, 1);
+ if (!scols_table_new_column(tb, "#NUM", 0.1, SCOLS_FL_RIGHT))
+ goto fail;
+ if (!scols_table_new_column(tb, "DATA", 0.7, 0))
+ goto fail;
+ if (!scols_table_new_column(tb, "TIME", 0.2, 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, size_t i)
+{
+ char *p;
+ struct libscols_line *ln = scols_table_new_line(tb, NULL);
+
+ if (!ln)
+ err(EXIT_FAILURE, "failed to create output line");
+
+ xasprintf(&p, "%zu", i);
+ if (scols_line_refer_data(ln, COL_NUM, p))
+ goto fail;
+
+ xasprintf(&p, "data-%02zu-%02zu-%02zu-end", i + 1, i + 2, i + 3);
+ if (scols_line_refer_data(ln, COL_DATA, p))
+ 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;
+ size_t i;
+ struct timeval last;
+
+ scols_init_debug(0);
+
+ tb = scols_new_table();
+ if (!tb)
+ err(EXIT_FAILURE, "failed to create output table");
+
+ setup_columns(tb);
+ gettimeofday(&last, NULL);
+
+ for (i = 0; i < 10; i++) {
+ struct libscols_line *line;
+ struct timeval now;
+ int done = 0;
+ char *timecell = xmalloc( sizeof(stringify_value(UINT_MAX)) );
+
+ line = add_line(tb, i);
+
+ /* Make a reference from cell data to the buffer, then we can
+ * update cell data without any interaction with libsmartcols
+ */
+ scols_line_refer_data(line, COL_TIME, timecell);
+
+ do {
+ double diff;
+
+ gettimeofday(&now, NULL);
+ diff = time_diff(&now, &last);
+
+ if (now.tv_sec == last.tv_sec + (long) TIME_PERIOD)
+ done = 1;
+ else
+ xusleep(100000);
+
+ /* update "TIME" cell data */
+ sprintf(timecell, "%f [%3d%%]", diff,
+ done ? 100 : (int)(diff / (TIME_PERIOD / 100.0)));
+
+ /* Note that libsmartcols don't print \n for last line
+ * in the table, but if you print a line somewhere in
+ * the midle of the table you need
+ *
+ * scols_table_enable_nolinesep(tb, !done);
+ *
+ * to disable line breaks. In this example it's
+ * unnecessary as we print the latest line only.
+ */
+
+ /* print the line */
+ scols_table_print_range(tb, line, NULL);
+
+ if (!done) {
+ /* terminal is waiting for \n, fflush() to force output */
+ fflush(scols_table_get_stream(tb));
+ /* move to the begin of the line */
+ fputc('\r', scols_table_get_stream(tb));
+ } else
+ fputc('\n', scols_table_get_stream(tb));
+ } while (!done);
+
+ last = now;
+ }
+
+ scols_unref_table(tb);
+ return EXIT_SUCCESS;
+}
diff --git a/libsmartcols/samples/fromfile.c b/libsmartcols/samples/fromfile.c
new file mode 100644
index 0000000..c1ab728
--- /dev/null
+++ b/libsmartcols/samples/fromfile.c
@@ -0,0 +1,344 @@
+/*
+ * Copyright (C) 2016 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <getopt.h>
+
+#include "c.h"
+#include "nls.h"
+#include "strutils.h"
+#include "xalloc.h"
+#include "optutils.h"
+
+#include "libsmartcols.h"
+
+struct column_flag {
+ const char *name;
+ int mask;
+};
+
+static const struct column_flag flags[] = {
+ { "trunc", SCOLS_FL_TRUNC },
+ { "tree", SCOLS_FL_TREE },
+ { "right", SCOLS_FL_RIGHT },
+ { "strictwidth",SCOLS_FL_STRICTWIDTH },
+ { "noextremes", SCOLS_FL_NOEXTREMES },
+ { "hidden", SCOLS_FL_HIDDEN },
+ { "wrap", SCOLS_FL_WRAP },
+ { "wrapnl", SCOLS_FL_WRAP },
+ { "none", 0 }
+};
+
+static long name_to_flag(const char *name, size_t namesz)
+{
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(flags); i++) {
+ const char *cn = flags[i].name;
+
+ if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
+ return flags[i].mask;
+ }
+ warnx("unknown flag: %s", name);
+ return -1;
+}
+
+static int parse_column_flags(char *str)
+{
+ unsigned long num_flags = 0;
+
+ if (string_to_bitmask(str, &num_flags, name_to_flag))
+ err(EXIT_FAILURE, "failed to parse column flags");
+
+ return num_flags;
+}
+
+static struct libscols_column *parse_column(FILE *f)
+{
+ size_t len = 0;
+ char *line = NULL;
+ int nlines = 0;
+
+ struct libscols_column *cl = NULL;
+
+ while (getline(&line, &len, f) != -1) {
+
+ char *p = strrchr(line, '\n');
+ if (p)
+ *p = '\0';
+
+ switch (nlines) {
+ case 0: /* NAME */
+ {
+ struct libscols_cell *hr;
+
+ cl = scols_new_column();
+ if (!cl)
+ goto fail;
+ hr = scols_column_get_header(cl);
+ if (!hr || scols_cell_set_data(hr, line))
+ goto fail;
+ break;
+ }
+ case 1: /* WIDTH-HINT */
+ {
+ double whint = strtod_or_err(line, "failed to parse column whint");
+ if (scols_column_set_whint(cl, whint))
+ goto fail;
+ break;
+ }
+ case 2: /* FLAGS */
+ {
+ int num_flags = parse_column_flags(line);
+ if (scols_column_set_flags(cl, num_flags))
+ goto fail;
+ if (strcmp(line, "wrapnl") == 0) {
+ scols_column_set_wrapfunc(cl,
+ scols_wrapnl_chunksize,
+ scols_wrapnl_nextchunk,
+ NULL);
+ scols_column_set_safechars(cl, "\n");
+ }
+ break;
+ }
+ case 3: /* COLOR */
+ if (scols_column_set_color(cl, line))
+ goto fail;
+ break;
+ default:
+ break;
+ }
+
+ nlines++;
+ }
+
+ free(line);
+ return cl;
+fail:
+ free(line);
+ scols_unref_column(cl);
+ return NULL;
+}
+
+static int parse_column_data(FILE *f, struct libscols_table *tb, int col)
+{
+ size_t len = 0, nlines = 0;
+ int i;
+ char *str = NULL;
+
+ while ((i = getline(&str, &len, f)) != -1) {
+
+ struct libscols_line *ln;
+ char *p = strrchr(str, '\n');
+ if (p)
+ *p = '\0';
+
+ while ((p = strrchr(str, '\\')) && *(p + 1) == 'n') {
+ *p = '\n';
+ memmove(p + 1, p + 2, i - (p + 2 - str));
+ }
+
+ ln = scols_table_get_line(tb, nlines++);
+ if (!ln)
+ break;
+
+ scols_line_set_data(ln, col, str);
+ }
+
+ free(str);
+ return 0;
+
+}
+
+static struct libscols_line *get_line_with_id(struct libscols_table *tb,
+ int col_id, const char *id)
+{
+ struct libscols_line *ln;
+ struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
+
+ while (scols_table_next_line(tb, itr, &ln) == 0) {
+ struct libscols_cell *ce = scols_line_get_cell(ln, col_id);
+ const char *data = ce ? scols_cell_get_data(ce) : NULL;
+
+ if (data && strcmp(data, id) == 0)
+ break;
+ }
+
+ scols_free_iter(itr);
+ return ln;
+}
+
+static void compose_tree(struct libscols_table *tb, int parent_col, int id_col)
+{
+ struct libscols_line *ln;
+ struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
+
+ while (scols_table_next_line(tb, itr, &ln) == 0) {
+ struct libscols_line *parent = NULL;
+ struct libscols_cell *ce = scols_line_get_cell(ln, parent_col);
+ const char *data = ce ? scols_cell_get_data(ce) : NULL;
+
+ if (data)
+ parent = get_line_with_id(tb, id_col, data);
+ if (parent)
+ scols_line_add_child(parent, ln);
+ }
+
+ scols_free_iter(itr);
+}
+
+
+static void __attribute__((__noreturn__)) usage(void)
+{
+ FILE *out = stdout;
+ fprintf(out,
+ "\n %s [options] <column-data-file> ...\n\n", program_invocation_short_name);
+
+ fputs(" -m, --maxout fill all terminal width\n", out);
+ fputs(" -c, --column <file> column definition\n", out);
+ fputs(" -n, --nlines <num> number of lines\n", out);
+ fputs(" -J, --json JSON output format\n", out);
+ fputs(" -r, --raw RAW output format\n", out);
+ fputs(" -E, --export use key=\"value\" output format\n", out);
+ fputs(" -C, --colsep <str> set columns separator\n", out);
+ fputs(" -w, --width <num> hardcode terminal width\n", out);
+ fputs(" -p, --tree-parent-column <n> parent column\n", out);
+ fputs(" -i, --tree-id-column <n> id column\n", out);
+ fputs(" -h, --help this help\n", out);
+ fputs("\n", out);
+
+ exit(EXIT_SUCCESS);
+}
+
+int main(int argc, char *argv[])
+{
+ struct libscols_table *tb;
+ int c, n, nlines = 0;
+ int parent_col = -1, id_col = -1;
+
+ static const struct option longopts[] = {
+ { "maxout", 0, NULL, 'm' },
+ { "column", 1, NULL, 'c' },
+ { "nlines", 1, NULL, 'n' },
+ { "width", 1, NULL, 'w' },
+ { "tree-parent-column", 1, NULL, 'p' },
+ { "tree-id-column", 1, NULL, 'i' },
+ { "json", 0, NULL, 'J' },
+ { "raw", 0, NULL, 'r' },
+ { "export", 0, NULL, 'E' },
+ { "colsep", 1, NULL, 'C' },
+ { "help", 0, NULL, 'h' },
+ { NULL, 0, NULL, 0 },
+ };
+
+ static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
+ { 'E', 'J', 'r' },
+ { 0 }
+ };
+ int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
+
+ 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, "hCc:Ei:Jmn:p:rw:", longopts, NULL)) != -1) {
+
+ err_exclusive_options(c, longopts, excl, excl_st);
+
+ switch(c) {
+ case 'c': /* add column from file */
+ {
+ struct libscols_column *cl;
+ FILE *f = fopen(optarg, "r");
+
+ if (!f)
+ err(EXIT_FAILURE, "%s: open failed", optarg);
+ cl = parse_column(f);
+ if (cl && scols_table_add_column(tb, cl))
+ err(EXIT_FAILURE, "%s: failed to add column", optarg);
+ scols_unref_column(cl);
+ fclose(f);
+ break;
+ }
+ case 'p':
+ parent_col = strtou32_or_err(optarg, "failed to parse tree PARENT column");
+ break;
+ case 'i':
+ id_col = strtou32_or_err(optarg, "failed to parse tree ID column");
+ break;
+ case 'J':
+ scols_table_enable_json(tb, 1);
+ scols_table_set_name(tb, "testtable");
+ break;
+ case 'm':
+ scols_table_enable_maxout(tb, TRUE);
+ break;
+ case 'r':
+ scols_table_enable_raw(tb, TRUE);
+ break;
+ case 'E':
+ scols_table_enable_export(tb, TRUE);
+ break;
+ case 'C':
+ scols_table_set_column_separator(tb, optarg);
+ break;
+ case 'n':
+ nlines = strtou32_or_err(optarg, "failed to parse number of lines");
+ 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;
+ case 'h':
+ usage();
+ default:
+ errtryhelp(EXIT_FAILURE);
+ }
+ }
+
+ if (nlines <= 0)
+ errx(EXIT_FAILURE, "--nlines not set");
+
+ for (n = 0; n < nlines; n++) {
+ struct libscols_line *ln = scols_new_line();
+
+ if (!ln || scols_table_add_line(tb, ln))
+ err(EXIT_FAILURE, "failed to add a new line");
+
+ scols_unref_line(ln);
+ }
+
+ n = 0;
+
+ while (optind < argc) {
+ FILE *f = fopen(argv[optind], "r");
+
+ if (!f)
+ err(EXIT_FAILURE, "%s: open failed", argv[optind]);
+
+ parse_column_data(f, tb, n);
+ optind++;
+ n++;
+ }
+
+ if (scols_table_is_tree(tb) && parent_col >= 0 && id_col >= 0)
+ compose_tree(tb, parent_col, id_col);
+
+ scols_table_enable_colors(tb, isatty(STDOUT_FILENO));
+
+ scols_print_table(tb);
+ scols_unref_table(tb);
+ return EXIT_SUCCESS;
+}
diff --git a/libsmartcols/samples/maxout.c b/libsmartcols/samples/maxout.c
new file mode 100644
index 0000000..07a05e1
--- /dev/null
+++ b/libsmartcols/samples/maxout.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2016 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <getopt.h>
+
+#include "c.h"
+#include "nls.h"
+#include "libsmartcols.h"
+
+enum { COL_LEFT, COL_FOO, COL_RIGHT };
+
+int main(int argc, char *argv[])
+{
+ struct libscols_table *tb;
+ int rc = -1, nlines = 3;
+
+ 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");
+
+ scols_table_enable_maxout(tb, TRUE);
+ if (!scols_table_new_column(tb, "LEFT", 0, 0))
+ goto done;
+ if (!scols_table_new_column(tb, "FOO", 0, 0))
+ goto done;
+ if (!scols_table_new_column(tb, "RIGHT", 0, SCOLS_FL_RIGHT))
+ goto done;
+
+ while (nlines--) {
+ struct libscols_line *ln = scols_table_new_line(tb, NULL);
+
+ scols_line_set_data(ln, COL_LEFT, "A");
+ scols_line_set_data(ln, COL_FOO, "B");
+ scols_line_set_data(ln, COL_RIGHT, "C");
+ }
+
+ scols_print_table(tb);
+ rc = 0;
+done:
+ scols_unref_table(tb);
+ return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+}
diff --git a/libsmartcols/samples/title.c b/libsmartcols/samples/title.c
new file mode 100644
index 0000000..131400d
--- /dev/null
+++ b/libsmartcols/samples/title.c
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2010-2014 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <getopt.h>
+
+#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, 0))
+ 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 void add_line(struct libscols_table *tb, const char *name, const char *data)
+{
+ struct libscols_line *ln = scols_table_new_line(tb, NULL);
+ 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;
+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_symbols *sy;
+ struct libscols_cell *title;
+ 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 <num>]\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, "foo", "bla bla bla");
+ add_line(tb, "bar", "alb alb alb");
+
+ title = scols_table_get_title(tb);
+
+ /* right */
+ scols_cell_set_data(title, "This is right title");
+ scols_cell_set_color(title, "red");
+ scols_cell_set_flags(title, SCOLS_CELL_FL_RIGHT);
+ scols_print_table(tb);
+
+ /* left without padding */
+ scols_cell_set_data(title, "This is left title (without padding)");
+ scols_cell_set_color(title, "yellow");
+ scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT);
+ scols_print_table(tb);
+
+ /* center */
+ sy = scols_new_symbols();
+ if (!sy)
+ err_oom();
+ scols_table_set_symbols(tb, sy);
+ scols_unref_symbols(sy);
+
+ scols_symbols_set_title_padding(sy, "=");
+ scols_cell_set_data(title, "This is center title (with padding)");
+ scols_cell_set_color(title, "green");
+ scols_cell_set_flags(title, SCOLS_CELL_FL_CENTER);
+ scols_print_table(tb);
+
+ /* left with padding */
+ scols_symbols_set_title_padding(sy, "-");
+ scols_cell_set_data(title, "This is left title (with padding)");
+ scols_cell_set_color(title, "blue");
+ scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT);
+ scols_print_table(tb);
+
+
+ scols_unref_table(tb);
+ return EXIT_SUCCESS;
+}
diff --git a/libsmartcols/samples/tree.c b/libsmartcols/samples/tree.c
new file mode 100644
index 0000000..fcb1751
--- /dev/null
+++ b/libsmartcols/samples/tree.c
@@ -0,0 +1,249 @@
+/*
+ * Copyright (C) 2010-2014 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <getopt.h>
+
+#include "c.h"
+#include "nls.h"
+#include "strutils.h"
+
+#include "libsmartcols.h"
+
+static int add_children(struct libscols_table *tb,
+ struct libscols_line *ln, int fd);
+
+
+enum { COL_MODE, COL_SIZE, COL_NAME };
+
+/* add columns to the @tb */
+static void setup_columns(struct libscols_table *tb, int notree)
+{
+ if (!scols_table_new_column(tb, "MODE", 0.3, 0))
+ goto fail;
+ if (!scols_table_new_column(tb, "SIZE", 5, SCOLS_FL_RIGHT))
+ goto fail;
+ if (!scols_table_new_column(tb, "NAME", 0.5,
+ (notree ? 0 : SCOLS_FL_TREE) | SCOLS_FL_NOEXTREMES))
+ goto fail;
+
+ return;
+fail:
+ scols_unref_table(tb);
+ err(EXIT_FAILURE, "failed to create output columns");
+}
+
+/* add a new line to @tb, the content is based on @st */
+static int add_line_from_stat(struct libscols_table *tb,
+ struct libscols_line *parent,
+ int parent_fd,
+ struct stat *st,
+ const char *name)
+{
+ struct libscols_line *ln;
+ char modbuf[11], *p;
+ mode_t mode = st->st_mode;
+ int rc = 0;
+
+ ln = scols_table_new_line(tb, parent);
+ if (!ln)
+ err(EXIT_FAILURE, "failed to create output line");
+
+ /* MODE; local buffer, use scols_line_set_data() that calls strdup() */
+ xstrmode(mode, modbuf);
+ if (scols_line_set_data(ln, COL_MODE, modbuf))
+ goto fail;
+
+ /* SIZE; already allocated string, use scols_line_refer_data() */
+ p = size_to_human_string(0, st->st_size);
+ if (!p || scols_line_refer_data(ln, COL_SIZE, p))
+ goto fail;
+
+ /* NAME */
+ if (scols_line_set_data(ln, COL_NAME, name))
+ goto fail;
+
+ /* colors */
+ if (scols_table_colors_wanted(tb)) {
+ struct libscols_cell *ce = scols_line_get_cell(ln, COL_NAME);
+
+ if (S_ISDIR(mode))
+ scols_cell_set_color(ce, "blue");
+ else if (S_ISLNK(mode))
+ scols_cell_set_color(ce, "cyan");
+ else if (S_ISBLK(mode))
+ scols_cell_set_color(ce, "magenta");
+ else if ((mode & S_IXOTH) || (mode & S_IXGRP) || (mode & S_IXUSR))
+ scols_cell_set_color(ce, "green");
+ }
+
+ if (S_ISDIR(st->st_mode)) {
+ int fd;
+
+ if (parent_fd >= 0)
+ fd = openat(parent_fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC);
+ else
+ fd = open(name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC);
+ if (fd >= 0) {
+ rc = add_children(tb, ln, fd);
+ close(fd);
+ }
+ }
+ return rc;
+fail:
+ err(EXIT_FAILURE, "failed to create cell data");
+ return -1;
+}
+
+/* read all entries from directory addressed by @fd */
+static int add_children(struct libscols_table *tb,
+ struct libscols_line *ln,
+ int fd)
+{
+ DIR *dir;
+ struct dirent *d;
+
+ dir = fdopendir(fd);
+ if (!dir)
+ return -errno;
+
+ while ((d = readdir(dir))) {
+ struct stat st;
+
+ if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
+ continue;
+ if (fstatat(fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0)
+ continue;
+ add_line_from_stat(tb, ln, fd, &st, d->d_name);
+ }
+ closedir(dir);
+ return 0;
+}
+
+static void add_lines(struct libscols_table *tb, const char *dirname)
+{
+ struct stat st;
+
+ if (lstat(dirname, &st))
+ err(EXIT_FAILURE, "%s", dirname);
+
+ add_line_from_stat(tb, NULL, -1, &st, 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(" -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);
+}
+
+int main(int argc, char *argv[])
+{
+ struct libscols_table *tb;
+ int c, notree = 0, nstart = -1, nend = -1;
+
+
+ static const struct option longopts[] = {
+ { "ascii", 0, NULL, 'i' },
+ { "csv", 0, NULL, 'c' },
+ { "list", 0, NULL, 'l' },
+ { "noheadings", 0, NULL, 'n' },
+ { "pairs", 0, NULL, 'p' },
+ { "json", 0, NULL, 'J' },
+ { "raw", 0, NULL, 'r' },
+ { "range-start",1, NULL, 'S' },
+ { "range-end", 1, NULL, 'E' },
+ { 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, "ciJlnprS:E:", longopts, NULL)) != -1) {
+ switch(c) {
+ case 'c':
+ scols_table_set_column_separator(tb, ",");
+ scols_table_enable_raw(tb, 1);
+ notree = 1;
+ break;
+ case 'i':
+ scols_table_enable_ascii(tb, 1);
+ break;
+ case 'J':
+ scols_table_set_name(tb, "scolstest");
+ scols_table_enable_json(tb, 1);
+ break;
+ case 'l':
+ notree = 1;
+ break;
+ case 'n':
+ scols_table_enable_noheadings(tb, 1);
+ break;
+ case 'p':
+ scols_table_enable_export(tb, 1);
+ notree = 1;
+ break;
+ case 'r':
+ 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);
+ }
+ }
+
+ scols_table_enable_colors(tb, isatty(STDOUT_FILENO));
+ setup_columns(tb, notree);
+
+ if (optind == argc)
+ add_lines(tb, ".");
+ else while (optind < argc)
+ add_lines(tb, argv[optind++]);
+
+ 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;
+}
diff --git a/libsmartcols/samples/wrap.c b/libsmartcols/samples/wrap.c
new file mode 100644
index 0000000..795bef7
--- /dev/null
+++ b/libsmartcols/samples/wrap.c
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2010-2014 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <getopt.h>
+
+#include "c.h"
+#include "nls.h"
+#include "strutils.h"
+#include "xalloc.h"
+
+#include "libsmartcols.h"
+
+
+enum { COL_NAME, COL_DESC, COL_FOO, COL_LIKE, COL_TEXT };
+
+/* 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, "DESC", 0, 0))
+ goto fail;
+ if (!scols_table_new_column(tb, "FOO", 0, SCOLS_FL_WRAP))
+ goto fail;
+ if (!scols_table_new_column(tb, "LIKE", 0, SCOLS_FL_RIGHT))
+ goto fail;
+ if (!scols_table_new_column(tb, "TEXT", 0, SCOLS_FL_WRAP))
+ goto fail;
+ return;
+fail:
+ scols_unref_table(tb);
+ err(EXIT_FAILURE, "failed to create output columns");
+}
+
+static char *gen_text(const char *prefix, const char *sub_prefix, char *buf, size_t sz)
+{
+ int x = snprintf(buf, sz, "%s-%s-", prefix, sub_prefix);
+
+ for ( ; (size_t)x < sz - 1; x++)
+ buf[x] = *prefix;
+
+ buf[x++] = 'x';
+ buf[x] = '\0';
+ return buf;
+}
+
+static struct libscols_line * add_line( struct libscols_table *tb,
+ struct libscols_line *parent,
+ const char *prefix)
+{
+ char buf[BUFSIZ];
+ 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, gen_text(prefix, "N", buf, 15)))
+ goto fail;
+ if (scols_line_set_data(ln, COL_DESC, gen_text(prefix, "D", buf, 10)))
+ goto fail;
+ if (scols_line_set_data(ln, COL_FOO, gen_text(prefix, "U", buf, 55)))
+ goto fail;
+ if (scols_line_set_data(ln, COL_LIKE, "1"))
+ goto fail;
+ if (scols_line_set_data(ln, COL_TEXT, gen_text(prefix, "T", buf, 50)))
+ 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, *xln;
+
+ 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");
+
+ scols_table_enable_colors(tb, isatty(STDOUT_FILENO));
+ setup_columns(tb);
+
+ ln = add_line(tb, NULL, "A");
+ add_line(tb, ln, "aa");
+ add_line(tb, ln, "ab");
+
+ ln = add_line(tb, NULL, "B");
+ xln = add_line(tb, ln, "ba");
+ add_line(tb, xln, "baa");
+ add_line(tb, xln, "bab");
+ add_line(tb, ln, "bb");
+
+ scols_print_table(tb);
+ scols_unref_table(tb);
+ return EXIT_SUCCESS;
+}