]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libsmartcols/samples/title.c
ec72093cc952286cabc1ffbde9ac10825ede6418
[thirdparty/util-linux.git] / libsmartcols / samples / title.c
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_DATA };
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, "DATA", 0, 0))
32 goto fail;
33 return;
34 fail:
35 scols_unref_table(tb);
36 err(EXIT_FAILURE, "faild to create output columns");
37 }
38
39 static void add_line(struct libscols_table *tb, const char *name, const char *data)
40 {
41 struct libscols_line *ln = scols_table_new_line(tb, NULL);
42 if (!ln)
43 err(EXIT_FAILURE, "failed to create output line");
44
45 if (scols_line_set_data(ln, COL_NAME, name))
46 goto fail;
47 if (scols_line_set_data(ln, COL_DATA, data))
48 goto fail;
49 return;
50 fail:
51 scols_unref_table(tb);
52 err(EXIT_FAILURE, "faild to create output line");
53 }
54
55 int main(int argc, char *argv[])
56 {
57 struct libscols_table *tb;
58 struct libscols_symbols *sy;
59 struct libscols_cell *title;
60
61 setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */
62
63 scols_init_debug(0);
64
65 tb = scols_new_table();
66 if (!tb)
67 err(EXIT_FAILURE, "faild to create output table");
68
69 scols_table_enable_colors(tb, 1);
70 setup_columns(tb);
71 add_line(tb, "foo", "bla bla bla");
72 add_line(tb, "bar", "alb alb alb");
73
74 title = scols_table_get_title(tb);
75
76 /* right */
77 scols_cell_set_data(title, "This is right title");
78 scols_cell_set_color(title, "red");
79 scols_cell_set_flags(title, SCOLS_CELL_FL_RIGHT);
80 scols_print_table(tb);
81
82 /* center */
83 sy = scols_new_symbols();
84 if (!sy)
85 err_oom();
86 scols_table_set_symbols(tb, sy);
87
88 scols_symbols_set_title_padding(sy, "=");
89 scols_cell_set_data(title, "This is center title (with padding)");
90 scols_cell_set_color(title, "green");
91 scols_cell_set_flags(title, SCOLS_CELL_FL_CENTER);
92 scols_print_table(tb);
93
94 /* left */
95 scols_symbols_set_title_padding(sy, "-");
96 scols_cell_set_data(title, "This is left title (with padding)");
97 scols_cell_set_color(title, "blue");
98 scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT);
99 scols_print_table(tb);
100
101 scols_unref_table(tb);
102 return EXIT_SUCCESS;
103 }