]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libsmartcols/samples/wrap.c
96cdff31cc635bcd882550d3274b8aebf296f745
2 * Copyright (C) 2010-2014 Karel Zak <kzak@redhat.com>
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
11 #include <sys/types.h>
21 #include "libsmartcols.h"
24 enum { COL_NAME
, COL_DESC
, COL_FOO
, COL_LIKE
, COL_TEXT
};
26 /* add columns to the @tb */
27 static void setup_columns(struct libscols_table
*tb
)
29 if (!scols_table_new_column(tb
, "NAME", 0, SCOLS_FL_TREE
))
31 if (!scols_table_new_column(tb
, "DESC", 0, 0))
33 if (!scols_table_new_column(tb
, "FOO", 0, SCOLS_FL_WRAP
))
35 if (!scols_table_new_column(tb
, "LIKE", 0, SCOLS_FL_RIGHT
))
37 if (!scols_table_new_column(tb
, "TEXT", 0, SCOLS_FL_WRAP
))
41 scols_unref_table(tb
);
42 err(EXIT_FAILURE
, "faild to create output columns");
45 static char *gen_text(const char *prefix
, const char *sub_prefix
, char *buf
, size_t sz
)
47 int x
= snprintf(buf
, sz
, "%s-%s-", prefix
, sub_prefix
);
49 for ( ; (size_t)x
< sz
- 1; x
++)
57 static struct libscols_line
* add_line( struct libscols_table
*tb
,
58 struct libscols_line
*parent
,
62 struct libscols_line
*ln
= scols_table_new_line(tb
, parent
);
64 err(EXIT_FAILURE
, "failed to create output line");
66 if (scols_line_set_data(ln
, COL_NAME
, gen_text(prefix
, "N", buf
, 15)))
68 if (scols_line_set_data(ln
, COL_DESC
, gen_text(prefix
, "D", buf
, 10)))
70 if (scols_line_set_data(ln
, COL_FOO
, gen_text(prefix
, "U", buf
, 55)))
72 if (scols_line_set_data(ln
, COL_LIKE
, "1"))
74 if (scols_line_set_data(ln
, COL_TEXT
, gen_text(prefix
, "T", buf
, 50)))
78 scols_unref_table(tb
);
79 err(EXIT_FAILURE
, "faild to create output line");
82 int main(int argc
, char *argv
[])
84 struct libscols_table
*tb
;
85 struct libscols_line
*ln
, *xln
;
87 setlocale(LC_ALL
, ""); /* just to have enable UTF8 chars */
91 tb
= scols_new_table();
93 err(EXIT_FAILURE
, "faild to create output table");
95 scols_table_enable_colors(tb
, 1);
98 ln
= add_line(tb
, NULL
, "A");
99 add_line(tb
, ln
, "aa");
100 add_line(tb
, ln
, "ab");
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");
108 scols_print_table(tb
);
109 scols_unref_table(tb
);