]> git.ipfire.org Git - thirdparty/util-linux.git/blame - libsmartcols/samples/title.c
bash-completion: update options before release
[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;
36eb51eb
KZ
60 int c;
61
62 static const struct option longopts[] = {
71f08e97
SK
63 { "maxout", 0, NULL, 'm' },
64 { "width", 1, NULL, 'w' },
8aeda56b
KZ
65 { "help", 1, NULL, 'h' },
66
71f08e97 67 { NULL, 0, NULL, 0 },
36eb51eb 68 };
107ca34a
KZ
69
70 setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */
71
72 scols_init_debug(0);
73
74 tb = scols_new_table();
75 if (!tb)
9e930041 76 err(EXIT_FAILURE, "failed to create output table");
107ca34a 77
8aeda56b 78 while((c = getopt_long(argc, argv, "hmw:", longopts, NULL)) != -1) {
36eb51eb 79 switch(c) {
8aeda56b
KZ
80 case 'h':
81 printf("%s [--help | --maxout | --width <num>]\n", program_invocation_short_name);
82 break;
36eb51eb
KZ
83 case 'm':
84 scols_table_enable_maxout(tb, TRUE);
85 break;
86 case 'w':
87 scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS);
88 scols_table_set_termwidth(tb, strtou32_or_err(optarg, "failed to parse terminal width"));
89 break;
90 }
91 }
92
066779c6 93 scols_table_enable_colors(tb, isatty(STDOUT_FILENO));
107ca34a
KZ
94 setup_columns(tb);
95 add_line(tb, "foo", "bla bla bla");
96 add_line(tb, "bar", "alb alb alb");
97
e865838d
KZ
98 title = scols_table_get_title(tb);
99
107ca34a 100 /* right */
e865838d
KZ
101 scols_cell_set_data(title, "This is right title");
102 scols_cell_set_color(title, "red");
103 scols_cell_set_flags(title, SCOLS_CELL_FL_RIGHT);
107ca34a
KZ
104 scols_print_table(tb);
105
8aeda56b
KZ
106 /* left without padding */
107 scols_cell_set_data(title, "This is left title (without padding)");
108 scols_cell_set_color(title, "yellow");
109 scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT);
110 scols_print_table(tb);
111
107ca34a
KZ
112 /* center */
113 sy = scols_new_symbols();
114 if (!sy)
115 err_oom();
116 scols_table_set_symbols(tb, sy);
d1bf0ce5 117 scols_unref_symbols(sy);
107ca34a
KZ
118
119 scols_symbols_set_title_padding(sy, "=");
e865838d
KZ
120 scols_cell_set_data(title, "This is center title (with padding)");
121 scols_cell_set_color(title, "green");
122 scols_cell_set_flags(title, SCOLS_CELL_FL_CENTER);
107ca34a
KZ
123 scols_print_table(tb);
124
8aeda56b 125 /* left with padding */
107ca34a 126 scols_symbols_set_title_padding(sy, "-");
e865838d
KZ
127 scols_cell_set_data(title, "This is left title (with padding)");
128 scols_cell_set_color(title, "blue");
129 scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT);
107ca34a
KZ
130 scols_print_table(tb);
131
8aeda56b 132
107ca34a
KZ
133 scols_unref_table(tb);
134 return EXIT_SUCCESS;
135}