summaryrefslogtreecommitdiffstats
path: root/libsmartcols/src/test.c
blob: 0899cea3112c6b5701c78720fa1f944c73dc66de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * TT - Table or Tree, features:
 * - column width could be defined as absolute or relative to the terminal width
 * - allows to truncate or wrap data in columns
 * - prints tree if parent->child relation is defined
 * - draws the tree by ASCII or UTF8 lines (depends on terminal setting)
 *
 * Copyright (C) 2010-2014 Karel Zak <kzak@redhat.com>
 *
 * This file may be redistributed under the terms of the
 * GNU Lesser General Public License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <ctype.h>
#include <errno.h>

#include "c.h"
#include "nls.h"
#include "widechar.h"
#include "mbsalign.h"
#include "ttyutils.h"
#include "colors.h"

#include "libsmartcols.h"

enum { MYCOL_NAME, MYCOL_FOO, MYCOL_BAR, MYCOL_PATH };

int main(int argc, char *argv[])
{
	struct libscols_table *tb;
	struct libscols_column *cl;
	int notree = 0, clone = 0, i, color = 0;

	tb = scols_new_table(NULL);
	if (!tb)
		err(EXIT_FAILURE, "table initialization failed");

	if (argc == 2 && !strcmp(argv[1], "--help")) {
		printf("%s [--max | --ascii | --raw | --export | --list | "
		       "--color | --colortree | --clone | --clonetree]\n",
				program_invocation_short_name);
		scols_unref_table(tb);
		return EXIT_SUCCESS;
	} else if (argc == 2 && !strcmp(argv[1], "--max")) {
		scols_table_set_max(tb, 1);
	} else if (argc == 2 && !strcmp(argv[1], "--ascii")) {
		scols_table_set_ascii(tb, 1);
	} else if (argc == 2 && !strcmp(argv[1], "--raw")) {
		scols_table_set_raw(tb, 1);
		notree = 1;
	} else if (argc == 2 && !strcmp(argv[1], "--export")) {
		scols_table_set_export(tb, 1);
		notree = 1;
	} else if (argc == 2 && !strcmp(argv[1], "--list")) {
		notree = 1;
	} else if (argc == 2 && !strcmp(argv[1], "--color")) {
		notree = 1;
		color = 1;
	} else if (argc == 2 && !strcmp(argv[1], "--colortree")) {
		notree = 0;
		color = 1;
	} else if (argc == 2 && !strcmp(argv[1], "--clone")) {
		notree = 1;
		clone = 1;
	} else if (argc == 2 && !strcmp(argv[1], "--clonetree")) {
		notree = 0;
		clone = 1;
	}

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	cl = scols_table_new_column(tb, "NAME", 0.3);
	scols_table_set_tree(tb, !notree);
	scols_table_enable_colors(tb, color);

	if (color)
		scols_column_set_color(cl, UL_COLOR_RED);

	cl = scols_table_new_column(tb, "FOO", 0.3);
	scols_column_set_trunc(cl, 1);
	if (color) {
		struct libscols_cell *h = scols_column_get_header(cl);

		scols_column_set_color(cl, UL_COLOR_BOLD_GREEN);
		scols_cell_set_color(h, "green"); /* a human-readable string is also legal */
	}
	scols_table_new_column(tb, "BAR", 0.3);
	scols_table_new_column(tb, "PATH", 0.3);

	for (i = 0; i < 2; i++) {
		struct libscols_line *ln = scols_table_new_line(tb, NULL);
		struct libscols_line *pr, *root = ln;

		scols_line_set_data(ln, MYCOL_NAME, "AAA");
		scols_line_set_data(ln, MYCOL_FOO, "a-foo-foo");
		scols_line_set_data(ln, MYCOL_BAR, "barBar-A");
		scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA");

		pr = ln = scols_table_new_line(tb, root);
		scols_line_set_data(ln, MYCOL_NAME, "AAA.A");
		scols_line_set_data(ln, MYCOL_FOO, "a.a-foo-foo");
		scols_line_set_data(ln, MYCOL_BAR, "barBar-A.A");
		scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA/A");
		if (color)
			scols_line_set_color(ln, UL_COLOR_BOLD_YELLOW);

		ln = scols_table_new_line(tb, pr);
		scols_line_set_data(ln, MYCOL_NAME, "AAA.A.AAA");
		scols_line_set_data(ln, MYCOL_FOO, "a.a.a-foo-foo");
		scols_line_set_data(ln, MYCOL_BAR, "barBar-A.A.A");
		scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA/A/AAA");

		ln = scols_table_new_line(tb, root);
		scols_line_set_data(ln, MYCOL_NAME, "AAA.B");
		scols_line_set_data(ln, MYCOL_FOO, "a.b-foo-foo");
		scols_line_set_data(ln, MYCOL_BAR, "barBar-A.B");
		scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA/B");
		if (color)
			scols_cell_set_color(scols_line_get_cell(ln, MYCOL_FOO),
					     UL_COLOR_MAGENTA);

		ln = scols_table_new_line(tb, pr);
		scols_line_set_data(ln, MYCOL_NAME, "AAA.A.BBB");
		scols_line_set_data(ln, MYCOL_FOO, "a.a.b-foo-foo");
		scols_line_set_data(ln, MYCOL_BAR, "barBar-A.A.BBB");
		scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA/A/BBB");

		ln = scols_table_new_line(tb, pr);
		scols_line_set_data(ln, MYCOL_NAME, "AAA.A.CCC");
		scols_line_set_data(ln, MYCOL_FOO, "a.a.c-foo-foo");
		scols_line_set_data(ln, MYCOL_BAR, "barBar-A.A.CCC");
		scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA/A/CCC");

		ln = scols_table_new_line(tb, root);
		scols_line_set_data(ln, MYCOL_NAME, "AAA.C");
		scols_line_set_data(ln, MYCOL_FOO, "a.c-foo-foo");
		scols_line_set_data(ln, MYCOL_BAR, "barBar-A.C");
		scols_line_set_data(ln, MYCOL_PATH, "/mnt/AAA/C");
	}

	printf("\nColumns: %d, Lines: %d\n\n",
			scols_table_get_ncols(tb),
			scols_table_get_nlines(tb));

	if (clone) {
		struct libscols_table *xtb = scols_copy_table(tb);
		scols_print_table(xtb);
		fputs("\n\n", stdout);
		scols_unref_table(xtb);
	}

	scols_print_table(tb);
	scols_unref_table(tb);

	return EXIT_SUCCESS;
}