]> git.ipfire.org Git - thirdparty/util-linux.git/blame - libsmartcols/samples/title.c
misc: Fix various typos
[thirdparty/util-linux.git] / libsmartcols / samples / title.c
CommitLineData
107ca34a
KZ
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
24enum { COL_NAME, COL_DATA };
25
26/* add columns to the @tb */
27static 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;
34fail:
35 scols_unref_table(tb);
9e930041 36 err(EXIT_FAILURE, "failed to create output columns");
107ca34a
KZ
37}
38
39static 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;
4a51e682 47 if (scols_line_set_data(ln, COL_DATA, data))
107ca34a
KZ
48 goto fail;
49 return;
50fail:
51 scols_unref_table(tb);
9e930041 52 err(EXIT_FAILURE, "failed to create output line");
107ca34a
KZ
53}
54
55int main(int argc, char *argv[])
56{
57 struct libscols_table *tb;
58 struct libscols_symbols *sy;
e865838d 59 struct libscols_cell *title;
107ca34a
KZ
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)
9e930041 67 err(EXIT_FAILURE, "failed to create output table");
107ca34a
KZ
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
e865838d
KZ
74 title = scols_table_get_title(tb);
75
107ca34a 76 /* right */
e865838d
KZ
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);
107ca34a
KZ
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, "=");
e865838d
KZ
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);
107ca34a
KZ
92 scols_print_table(tb);
93
94 /* left */
95 scols_symbols_set_title_padding(sy, "-");
e865838d
KZ
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);
107ca34a
KZ
99 scols_print_table(tb);
100
101 scols_unref_table(tb);
102 return EXIT_SUCCESS;
103}