Debian Codegraph @

libsmartcols/samples/wrap.c

raw
1 /*
2 * Copyright (C) 2010-2014 Karel Zak <[email protected]>
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
6 */
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <dirent.h>
14 #include <getopt.h>
15
16 #include "c.h"
17 #include "nls.h"
18 #include "strutils.h"
19 #include "xalloc.h"
20
21 #include "libsmartcols.h"
22
23
24 enum { COL_NAME, COL_DESC, COL_FOO, COL_LIKE, COL_TEXT };
25
26 /* add columns to the @tb */
27 static void setup_columns(struct libscols_table *tb)
28 {
29 if (!scols_table_new_column(tb, "NAME", 0, SCOLS_FL_TREE))
30 goto fail;
31 if (!scols_table_new_column(tb, "DESC", 0, 0))
32 goto fail;
33 if (!scols_table_new_column(tb, "FOO", 0, SCOLS_FL_WRAP))
34 goto fail;
35 if (!scols_table_new_column(tb, "LIKE", 0, SCOLS_FL_RIGHT))
36 goto fail;
37 if (!scols_table_new_column(tb, "TEXT", 0, SCOLS_FL_WRAP))
38 goto fail;
39 return;
40 fail:
41 scols_unref_table(tb);
42 err(EXIT_FAILURE, "failed to create output columns");
43 }
44
45 static char *gen_text(const char *prefix, const char *sub_prefix, char *buf, size_t sz)
46 {
47 int x = snprintf(buf, sz, "%s-%s-", prefix, sub_prefix);
48
49 for ( ; (size_t)x < sz - 1; x++)
50 buf[x] = *prefix;
51
52 buf[x++] = 'x';
53 buf[x] = '\0';
54 return buf;
55 }
56
57 static struct libscols_line * add_line( struct libscols_table *tb,
58 struct libscols_line *parent,
59 const char *prefix)
60 {
61 char buf[BUFSIZ];
62 struct libscols_line *ln = scols_table_new_line(tb, parent);
63 if (!ln)
64 err(EXIT_FAILURE, "failed to create output line");
65
66 if (scols_line_set_data(ln, COL_NAME, gen_text(prefix, "N", buf, 15)))
67 goto fail;
68 if (scols_line_set_data(ln, COL_DESC, gen_text(prefix, "D", buf, 10)))
69 goto fail;
70 if (scols_line_set_data(ln, COL_FOO, gen_text(prefix, "U", buf, 55)))
71 goto fail;
72 if (scols_line_set_data(ln, COL_LIKE, "1"))
73 goto fail;
74 if (scols_line_set_data(ln, COL_TEXT, gen_text(prefix, "T", buf, 50)))
75 goto fail;
76 return ln;
77 fail:
78 scols_unref_table(tb);
79 err(EXIT_FAILURE, "failed to create output line");
80 }
81
82 int main(int argc, char *argv[])
83 {
84 struct libscols_table *tb;
85 struct libscols_line *ln, *xln;
86
87 setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */
88
89 scols_init_debug(0);
90
91 tb = scols_new_table();
92 if (!tb)
93 err(EXIT_FAILURE, "failed to create output table");
94
95 if (argc > 1 && strcmp(argv[1], "--export") == 0)
96 scols_table_enable_export(tb, 1);
97 else if (argc > 1 && strcmp(argv[1], "--raw") == 0)
98 scols_table_enable_raw(tb, 1);
99 else if (argc > 1 && strcmp(argv[1], "--json") == 0)
100 scols_table_enable_json(tb, 1);
101 else
102 scols_table_enable_colors(tb, isatty(STDOUT_FILENO));
103
104 setup_columns(tb);
105
106 ln = add_line(tb, NULL, "A");
107 add_line(tb, ln, "aa");
108 add_line(tb, ln, "ab");
109
110 ln = add_line(tb, NULL, "B");
111 xln = add_line(tb, ln, "ba");
112 add_line(tb, xln, "baa");
113 add_line(tb, xln, "bab");
114 add_line(tb, ln, "bb");
115
116 scols_print_table(tb);
117 scols_unref_table(tb);
118 return EXIT_SUCCESS;
119 }