1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ASCII table generator.
4 * Copyright (C) 2017 Cumulus Networks
23 struct ttable_border
{
35 /* cell style and cell */
36 struct ttable_cellstyle
{
39 enum ttable_align align
;
40 struct ttable_border border
;
45 struct ttable_cellstyle style
;
48 /* table style and table */
50 char corner
; /* intersection */
51 int indent
; /* left table indent */
52 bool rownums_on
; /* show row numbers; unimplemented */
54 struct ttable_border border
;
55 struct ttable_cellstyle cell
;
59 int nrows
; /* number of rows */
60 int ncols
; /* number of cols */
61 struct ttable_cell
**table
; /* table, row x col */
62 size_t size
; /* size */
63 struct ttable_style style
; /* style */
66 /* some predefined styles */
67 #define TTSTYLE_ASCII 0
68 #define TTSTYLE_BLANK 1
70 extern const struct ttable_style ttable_styles
[2];
73 * Creates a new table with the default style, which looks like this:
75 * +----------+----------+
76 * | column 1 | column 2 |
77 * +----------+----------+
78 * | data... | data!! |
79 * +----------+----------+
81 * +----------+----------+
83 * @return the created table
85 struct ttable
*ttable_new(const struct ttable_style
*tts
);
88 * Deletes a table and releases all associated resources.
90 * @param tt the table to destroy
92 void ttable_del(struct ttable
*tt
);
95 * Inserts a new row at the given index.
97 * The row contents are determined by a format string. The format string has
98 * the same form as a regular printf format string, except that columns are
99 * delimited by '|'. For example, to make the first column of the table above,
102 * ttable_insert_row(<tt>, <n>, "%s|%s", "column 1", "column 2");
104 * All features of printf format strings are permissible here.
107 * - At present you cannot insert '|' into a cell's contents.
108 * - If there are N columns, '|' must appear n-1 times or the row will not be
111 * @param tt table to insert row into
112 * @param row the row number (begins at 0)
113 * @param format column-separated format string
114 * @param ... arguments to format string
116 * @return pointer to the first cell in the created row, or NULL if not enough
117 * columns were specified
119 struct ttable_cell
*ttable_insert_row(struct ttable
*tt
, unsigned int row
,
120 const char *format
, ...) PRINTFRR(3, 4);
122 * Inserts a new row at the end of the table.
124 * The row contents are determined by a format string. The format string has
125 * the same form as a regular printf format string, except that columns are
126 * delimited by '|'. For example, to make the first column of the table above,
129 * ttable_add_row(<tt>, "%s|%s", "column 1", "column 2");
131 * All features of printf format strings are permissible here.
134 * - At present you cannot insert '|' into a cell's contents.
135 * - If there are N columns, '|' must appear n-1 times or the row will not be
138 * @param tt table to insert row into
139 * @param format column-separated format string
140 * @param ... arguments to format string
142 * @return pointer to the first cell in the created row, or NULL if not enough
143 * columns were specified
145 struct ttable_cell
*ttable_add_row(struct ttable
*tt
, const char *format
, ...)
149 * Removes a row from the table.
151 * @param tt table to delete row from
152 * @param row the row number (begins at 0)
154 void ttable_del_row(struct ttable
*tt
, unsigned int row
);
157 * Sets alignment for a range of cells.
159 * Available alignments are LEFT and RIGHT. Cell contents will be aligned
160 * accordingly, while respecting padding (if any). Suppose a cell has:
167 * The cell would look like:
169 * +-------------------+
171 * +-------------------+
180 * +-------------------+
182 * +-------------------+
184 * The default alignment is LEFT.
186 * @param tt the table to set alignment on
187 * @param srow starting row index
188 * @param scol starting column index
189 * @param nrow # rows to align
190 * @param ncol # cols to align
191 * @param align the alignment to set
193 void ttable_align(struct ttable
*tt
, unsigned int srow
, unsigned int scol
,
194 unsigned int erow
, unsigned int ecol
,
195 enum ttable_align align
);
198 * Sets padding for a range of cells.
200 * Available padding options are LEFT and RIGHT (the alignment enum is reused).
201 * Both options may be set. Padding is treated as though it is stuck to the
202 * walls of the cell. Suppose a cell has:
209 * The cell padding, marked by '.', would look like:
215 * If the column is wider than the cell, the cell contents are aligned in an
216 * additional padded field according to the cell alignment.
218 * +--------------------+
219 * | Data!!!11!~~~~~:-) |
220 * +--------------------+
222 * +--------------------+
224 * @param tt the table to set padding on
225 * @param srow starting row index
226 * @param scol starting column index
227 * @param nrow # rows to pad
228 * @param ncol # cols to pad
229 * @param align LEFT or RIGHT
230 * @param pad # spaces to pad with
232 void ttable_pad(struct ttable
*tt
, unsigned int srow
, unsigned int scol
,
233 unsigned int nrow
, unsigned int ncol
, enum ttable_align align
,
237 * Restyle all cells according to table.cell.style.
239 * @param tt table to restyle
241 void ttable_restyle(struct ttable
*tt
);
244 * Turn left/right column separators on or off for specified column.
247 * @param col column index
248 * @param align left or right separators
249 * @param on true/false for on/off
250 * @param sep character to use
252 void ttable_colseps(struct ttable
*tt
, unsigned int col
,
253 enum ttable_align align
, bool on
, char sep
);
256 * Turn bottom row separators on or off for specified row.
259 * @param row row index
260 * @param align left or right separators
261 * @param on true/false for on/off
262 * @param sep character to use
264 void ttable_rowseps(struct ttable
*tt
, unsigned int row
,
265 enum ttable_align align
, bool on
, char sep
);
268 * Dumps a table to a heap-allocated string.
270 * Caller must free this string after use with
272 * XFREE (MTYPE_TMP, str);
274 * @param tt the table to dump
275 * @param newline the desired newline sequence to use, null terminated.
276 * @return table in text form
278 char *ttable_dump(struct ttable
*tt
, const char *newline
);
284 #endif /* _TERMTABLE_H */