1 /*
2 * Copyright (C) 2010-2014 Karel Zak <kzak@redhat.com>
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_FOO, COL_BAR };
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, 0))
30 goto fail;
31 if (!scols_table_new_column(tb, "BAR", 0, 0))
32 goto fail;
33 if (!scols_table_new_column(tb, "FOO", 0, 0))
34 goto fail;
35 return;
36 fail:
37 scols_unref_table(tb);
38 err(EXIT_FAILURE, "failed to create output columns");
39 }
40
41 static struct libscols_line *add_line(struct libscols_table *tb, const char *name, const char *data)
42 {
43 struct libscols_line *ln = scols_table_new_line(tb, NULL);
44 if (!ln)
45 err(EXIT_FAILURE, "failed to create output line");
46
47 if (scols_line_set_data(ln, COL_NAME, name))
48 goto fail;
49 if (scols_line_set_data(ln, COL_FOO, data))
50 goto fail;
51 if (scols_line_set_data(ln, COL_BAR, data))
52 goto fail;
53 return ln;
54 fail:
55 scols_unref_table(tb);
56 err(EXIT_FAILURE, "failed to create output line");
57 }
58
59 int main(int argc, char *argv[])
60 {
61 struct libscols_table *tb;
62 struct libscols_column *cl;
63 struct libscols_line *ln;
64 struct libscols_cell *ce;
65 int c;
66
67 static const struct option longopts[] = {
68 { "maxout", 0, NULL, 'm' },
69 { "width", 1, NULL, 'w' },
70 { "help", 1, NULL, 'h' },
71
72 { NULL, 0, NULL, 0 },
73 };
74
75 setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */
76
77 scols_init_debug(0);
78
79 tb = scols_new_table();
80 if (!tb)
81 err(EXIT_FAILURE, "failed to create output table");
82
83 while((c = getopt_long(argc, argv, "hmw:", longopts, NULL)) != -1) {
84 switch(c) {
85 case 'h':
86 printf("%s [--help | --maxout | --width <num>]\n", program_invocation_short_name);
87 break;
88 case 'm':
89 scols_table_enable_maxout(tb, TRUE);
90 break;
91 case 'w':
92 scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS);
93 scols_table_set_termwidth(tb, strtou32_or_err(optarg, "failed to parse terminal width"));
94 break;
95 }
96 }
97
98 scols_table_enable_colors(tb, isatty(STDOUT_FILENO));
99 setup_columns(tb);
100 add_line(tb, "AAA", "bla bla bla");
101 add_line(tb, "BB", "b");
102 add_line(tb, "CCCC", "fooo");
103 add_line(tb, "D", "baaar");
104 add_line(tb, "EE", "eee");
105
106 cl = scols_table_get_column(tb, 1);
107 scols_column_set_color(cl, "red"); /* red column */
108
109 cl = scols_table_get_column(tb, 2);
110 scols_column_set_color(cl, "reverse"); /* reverse column */
111
112 ln = scols_table_get_line(tb, 0);
113 scols_line_set_color(ln, "\033[37;41m"); /* line with red bg */
114 ce = scols_line_get_cell(ln, 0);
115 scols_cell_set_color(ce, "\033[37;45m"); /* cell with purple bg */
116
117 ln = scols_table_get_line(tb, 3);
118 scols_line_set_color(ln, "\033[37;41m"); /* line with red bg */
119 ce = scols_line_get_cell(ln, 2);
120 scols_cell_set_color(ce, "\033[37;44m"); /* cell with blue bg */
121
122 scols_print_table(tb);
123 scols_unref_table(tb);
124 return EXIT_SUCCESS;
125 }