]> git.ipfire.org Git - thirdparty/util-linux.git/blame - libsmartcols/samples/wrap.c
bash-completion: update options before release
[thirdparty/util-linux.git] / libsmartcols / samples / wrap.c
CommitLineData
e90f4b67
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
20759a42 24enum { COL_NAME, COL_DESC, COL_FOO, COL_LIKE, COL_TEXT };
e90f4b67
KZ
25
26/* add columns to the @tb */
27static void setup_columns(struct libscols_table *tb)
28{
e85c8c98 29 if (!scols_table_new_column(tb, "NAME", 0, SCOLS_FL_TREE))
e90f4b67 30 goto fail;
20759a42
KZ
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))
e90f4b67
KZ
38 goto fail;
39 return;
40fail:
41 scols_unref_table(tb);
9e930041 42 err(EXIT_FAILURE, "failed to create output columns");
e90f4b67
KZ
43}
44
20759a42
KZ
45static 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
5fde1d9f 49 for ( ; (size_t)x < sz - 1; x++)
20759a42
KZ
50 buf[x] = *prefix;
51
52 buf[x++] = 'x';
53 buf[x] = '\0';
54 return buf;
55}
56
e85c8c98
KZ
57static struct libscols_line * add_line( struct libscols_table *tb,
58 struct libscols_line *parent,
20759a42 59 const char *prefix)
e90f4b67 60{
20759a42 61 char buf[BUFSIZ];
e85c8c98 62 struct libscols_line *ln = scols_table_new_line(tb, parent);
e90f4b67
KZ
63 if (!ln)
64 err(EXIT_FAILURE, "failed to create output line");
65
20759a42
KZ
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"))
e90f4b67 73 goto fail;
20759a42 74 if (scols_line_set_data(ln, COL_TEXT, gen_text(prefix, "T", buf, 50)))
e90f4b67 75 goto fail;
e85c8c98 76 return ln;
e90f4b67
KZ
77fail:
78 scols_unref_table(tb);
9e930041 79 err(EXIT_FAILURE, "failed to create output line");
e90f4b67
KZ
80}
81
82int main(int argc, char *argv[])
83{
84 struct libscols_table *tb;
20759a42 85 struct libscols_line *ln, *xln;
e90f4b67
KZ
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)
9e930041 93 err(EXIT_FAILURE, "failed to create output table");
e90f4b67 94
066779c6 95 scols_table_enable_colors(tb, isatty(STDOUT_FILENO));
e90f4b67
KZ
96 setup_columns(tb);
97
20759a42
KZ
98 ln = add_line(tb, NULL, "A");
99 add_line(tb, ln, "aa");
100 add_line(tb, ln, "ab");
e85c8c98 101
20759a42
KZ
102 ln = add_line(tb, NULL, "B");
103 xln = add_line(tb, ln, "ba");
104 add_line(tb, xln, "baa");
105 add_line(tb, xln, "bab");
106 add_line(tb, ln, "bb");
e90f4b67
KZ
107
108 scols_print_table(tb);
109 scols_unref_table(tb);
110 return EXIT_SUCCESS;
111}