]> git.proxmox.com Git - mirror_frr.git/blob - lib/termtable.h
Merge pull request #5410 from ton31337/feature/bgp_default-route_with_route-map_set
[mirror_frr.git] / lib / termtable.h
1 /*
2 * ASCII table generator.
3 * Copyright (C) 2017 Cumulus Networks
4 * Quentin Young
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #ifndef _TERMTABLE_H_
21 #define _TERMTABLE_H_
22
23 #include <zebra.h>
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 enum ttable_align {
30 LEFT,
31 RIGHT,
32 TOP,
33 BOTTOM,
34 };
35
36 struct ttable_border {
37 char top;
38 char bottom;
39 char left;
40 char right;
41
42 bool top_on;
43 bool bottom_on;
44 bool left_on;
45 bool right_on;
46 };
47
48 /* cell style and cell */
49 struct ttable_cellstyle {
50 short lpad;
51 short rpad;
52 enum ttable_align align;
53 struct ttable_border border;
54 };
55
56 struct ttable_cell {
57 char *text;
58 struct ttable_cellstyle style;
59 };
60
61 /* table style and table */
62 struct ttable_style {
63 char corner; /* intersection */
64 int indent; /* left table indent */
65 bool rownums_on; /* show row numbers; unimplemented */
66
67 struct ttable_border border;
68 struct ttable_cellstyle cell;
69 };
70
71 struct ttable {
72 int nrows; /* number of rows */
73 int ncols; /* number of cols */
74 struct ttable_cell **table; /* table, row x col */
75 size_t size; /* size */
76 struct ttable_style style; /* style */
77 };
78
79 /* some predefined styles */
80 #define TTSTYLE_ASCII 0
81 #define TTSTYLE_BLANK 1
82
83 extern const struct ttable_style ttable_styles[2];
84
85 /**
86 * Creates a new table with the default style, which looks like this:
87 *
88 * +----------+----------+
89 * | column 1 | column 2 |
90 * +----------+----------+
91 * | data... | data!! |
92 * +----------+----------+
93 * | datums | 12345 |
94 * +----------+----------+
95 *
96 * @return the created table
97 */
98 struct ttable *ttable_new(const struct ttable_style *tts);
99
100 /**
101 * Deletes a table and releases all associated resources.
102 *
103 * @param tt the table to destroy
104 */
105 void ttable_del(struct ttable *tt);
106
107 /**
108 * Deletes an individual cell.
109 *
110 * @param cell the cell to destroy
111 */
112 void ttable_cell_del(struct ttable_cell *cell);
113
114 /**
115 * Inserts a new row at the given index.
116 *
117 * The row contents are determined by a format string. The format string has
118 * the same form as a regular printf format string, except that columns are
119 * delimited by '|'. For example, to make the first column of the table above,
120 * the call is:
121 *
122 * ttable_insert_row(<tt>, <n>, "%s|%s", "column 1", "column 2");
123 *
124 * All features of printf format strings are permissible here.
125 *
126 * Caveats:
127 * - At present you cannot insert '|' into a cell's contents.
128 * - If there are N columns, '|' must appear n-1 times or the row will not be
129 * created
130 *
131 * @param tt table to insert row into
132 * @param row the row number (begins at 0)
133 * @param format column-separated format string
134 * @param ... arguments to format string
135 *
136 * @return pointer to the first cell in the created row, or NULL if not enough
137 * columns were specified
138 */
139 struct ttable_cell *ttable_insert_row(struct ttable *tt, unsigned int row,
140 const char *format, ...) PRINTFRR(3, 4);
141 /**
142 * Inserts a new row at the end of the table.
143 *
144 * The row contents are determined by a format string. The format string has
145 * the same form as a regular printf format string, except that columns are
146 * delimited by '|'. For example, to make the first column of the table above,
147 * the call is:
148 *
149 * ttable_add_row(<tt>, "%s|%s", "column 1", "column 2");
150 *
151 * All features of printf format strings are permissible here.
152 *
153 * Caveats:
154 * - At present you cannot insert '|' into a cell's contents.
155 * - If there are N columns, '|' must appear n-1 times or the row will not be
156 * created
157 *
158 * @param tt table to insert row into
159 * @param format column-separated format string
160 * @param ... arguments to format string
161 *
162 * @return pointer to the first cell in the created row, or NULL if not enough
163 * columns were specified
164 */
165 struct ttable_cell *ttable_add_row(struct ttable *tt, const char *format, ...)
166 PRINTFRR(2, 3);
167
168 /**
169 * Removes a row from the table.
170 *
171 * @param tt table to delete row from
172 * @param row the row number (begins at 0)
173 */
174 void ttable_del_row(struct ttable *tt, unsigned int row);
175
176 /**
177 * Sets alignment for a range of cells.
178 *
179 * Available alignments are LEFT and RIGHT. Cell contents will be aligned
180 * accordingly, while respecting padding (if any). Suppose a cell has:
181 *
182 * lpad = 1
183 * rpad = 1
184 * align = RIGHT
185 * text = 'datums'
186 *
187 * The cell would look like:
188 *
189 * +-------------------+
190 * | datums |
191 * +-------------------+
192 *
193 * On the other hand:
194 *
195 * lpad = 1
196 * rpad = 10
197 * align = RIGHT
198 * text = 'datums'
199 *
200 * +-------------------+
201 * | datums |
202 * +-------------------+
203 *
204 * The default alignment is LEFT.
205 *
206 * @param tt the table to set alignment on
207 * @param srow starting row index
208 * @param scol starting column index
209 * @param nrow # rows to align
210 * @param ncol # cols to align
211 * @param align the alignment to set
212 */
213 void ttable_align(struct ttable *tt, unsigned int srow, unsigned int scol,
214 unsigned int erow, unsigned int ecol,
215 enum ttable_align align);
216
217 /**
218 * Sets padding for a range of cells.
219 *
220 * Available padding options are LEFT and RIGHT (the alignment enum is reused).
221 * Both options may be set. Padding is treated as though it is stuck to the
222 * walls of the cell. Suppose a cell has:
223 *
224 * lpad = 4
225 * rpad = 2
226 * align = RIGHT
227 * text = 'datums'
228 *
229 * The cell padding, marked by '.', would look like:
230 *
231 * +--------------+
232 * | .datums. |
233 * +--------------+
234 *
235 * If the column is wider than the cell, the cell contents are aligned in an
236 * additional padded field according to the cell alignment.
237 *
238 * +--------------------+
239 * | Data!!!11!~~~~~:-) |
240 * +--------------------+
241 * | . datums. |
242 * +--------------------+
243 *
244 * @param tt the table to set padding on
245 * @param srow starting row index
246 * @param scol starting column index
247 * @param nrow # rows to pad
248 * @param ncol # cols to pad
249 * @param align LEFT or RIGHT
250 * @param pad # spaces to pad with
251 */
252 void ttable_pad(struct ttable *tt, unsigned int srow, unsigned int scol,
253 unsigned int nrow, unsigned int ncol, enum ttable_align align,
254 short pad);
255
256 /**
257 * Restyle all cells according to table.cell.style.
258 *
259 * @param tt table to restyle
260 */
261 void ttable_restyle(struct ttable *tt);
262
263 /**
264 * Turn left/right column separators on or off for specified column.
265 *
266 * @param tt table
267 * @param col column index
268 * @param align left or right separators
269 * @param on true/false for on/off
270 * @param sep character to use
271 */
272 void ttable_colseps(struct ttable *tt, unsigned int col,
273 enum ttable_align align, bool on, char sep);
274
275 /**
276 * Turn bottom row separators on or off for specified row.
277 *
278 * @param tt table
279 * @param row row index
280 * @param align left or right separators
281 * @param on true/false for on/off
282 * @param sep character to use
283 */
284 void ttable_rowseps(struct ttable *tt, unsigned int row,
285 enum ttable_align align, bool on, char sep);
286
287 /**
288 * Dumps a table to a heap-allocated string.
289 *
290 * Caller must free this string after use with
291 *
292 * XFREE (MTYPE_TMP, str);
293 *
294 * @param tt the table to dump
295 * @param newline the desired newline sequence to use, null terminated.
296 * @return table in text form
297 */
298 char *ttable_dump(struct ttable *tt, const char *newline);
299
300 #ifdef __cplusplus
301 }
302 #endif
303
304 #endif /* _TERMTABLE_H */