blob: 603844f668c134e6e85a737be233a99d7d2058e1 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
2 * Prints table or tree. See lib/table.c for more details and example.
3 *
4 * Copyright (C) 2010 Karel Zak <kzak@redhat.com>
5 *
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
8 */
9#ifndef UTIL_LINUX_TT_H
10#define UTIL_LINUX_TT_H
11
12#include "list.h"
13
14enum {
15 /*
16 * Global flags
17 */
18 TT_FL_RAW = (1 << 1),
19 TT_FL_ASCII = (1 << 2),
20 TT_FL_NOHEADINGS = (1 << 3),
21 TT_FL_EXPORT = (1 << 4),
22
23 /*
24 * Column flags
25 */
26 TT_FL_TRUNC = (1 << 5), /* truncate fields data if necessary */
27 TT_FL_TREE = (1 << 6), /* use tree "ascii art" */
28 TT_FL_RIGHT = (1 << 7), /* align to the right */
29 TT_FL_STRICTWIDTH = (1 << 8), /* don't reduce width if column is empty */
30 TT_FL_NOEXTREMES = (1 << 9) /* ignore extreme fields when count column width*/
31};
32
33struct tt {
34 size_t ncols; /* number of columns */
35 size_t termwidth; /* terminal width */
36 int is_term; /* is a tty? */
37 int flags;
38 int first_run;
39
40 struct list_head tb_columns;
41 struct list_head tb_lines;
42
43 const struct tt_symbols *symbols;
44};
45
46struct tt_column {
47 const char *name; /* header */
48 size_t seqnum;
49
50 size_t width; /* real column width */
51 size_t width_min; /* minimal width (usually header width) */
52 size_t width_max; /* maximal width */
53 size_t width_avg; /* average width, used to detect extreme fields */
54 double width_hint; /* hint (N < 1 is in percent of termwidth) */
55
56 int flags;
57 int is_extreme;
58
59 struct list_head cl_columns;
60};
61
62struct tt_line {
63 struct tt *table;
64 char const **data;
65 void *userdata;
66 size_t data_sz; /* strlen of all data */
67
68 struct list_head ln_lines; /* table lines */
69
70 struct list_head ln_branch; /* begin of branch (head of ln_children) */
71 struct list_head ln_children;
72
73 struct tt_line *parent;
74};
75
76extern struct tt *tt_new_table(int flags);
77extern void tt_free_table(struct tt *tb);
78extern void tt_remove_lines(struct tt *tb);
79extern int tt_print_table(struct tt *tb);
80
81extern struct tt_column *tt_define_column(struct tt *tb, const char *name,
82 double whint, int flags);
83
84extern struct tt_column *tt_get_column(struct tt *tb, size_t colnum);
85
86extern struct tt_line *tt_add_line(struct tt *tb, struct tt_line *parent);
87
88extern int tt_line_set_data(struct tt_line *ln, int colnum, const char *data);
89extern int tt_line_set_userdata(struct tt_line *ln, void *data);
90
91extern void tt_fputs_quoted(const char *data, FILE *out);
92extern void tt_fputs_nonblank(const char *data, FILE *out);
93
94#endif /* UTIL_LINUX_TT_H */