2 * ASCII table generator.
3 * Copyright (C) 2017 Cumulus Networks
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)
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
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
36 struct ttable_border
{
48 /* cell style and cell */
49 struct ttable_cellstyle
{
52 enum ttable_align align
;
53 struct ttable_border border
;
58 struct ttable_cellstyle style
;
61 /* table style and table */
63 char corner
; /* intersection */
64 int indent
; /* left table indent */
65 bool rownums_on
; /* show row numbers; unimplemented */
67 struct ttable_border border
;
68 struct ttable_cellstyle cell
;
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 */
79 /* some predefined styles */
80 #define TTSTYLE_ASCII 0
81 #define TTSTYLE_BLANK 1
83 extern struct ttable_style ttable_styles
[2];
86 * Creates a new table with the default style, which looks like this:
88 * +----------+----------+
89 * | column 1 | column 2 |
90 * +----------+----------+
91 * | data... | data!! |
92 * +----------+----------+
94 * +----------+----------+
96 * @return the created table
98 struct ttable
*ttable_new(struct ttable_style
*tts
);
101 * Deletes a table and releases all associated resources.
103 * @param tt the table to destroy
105 void ttable_del(struct ttable
*tt
);
108 * Deletes an individual cell.
110 * @param cell the cell to destroy
112 void ttable_cell_del(struct ttable_cell
*cell
);
115 * Inserts a new row at the given index.
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,
122 * ttable_insert_row(<tt>, <n>, "%s|%s", "column 1", "column 2");
124 * All features of printf format strings are permissible here.
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
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
136 * @return pointer to the first cell in the created row, or NULL if not enough
137 * columns were specified
139 struct ttable_cell
*ttable_insert_row(struct ttable
*tt
, unsigned int row
,
140 const char *format
, ...) PRINTFRR(3, 4);
142 * Inserts a new row at the end of the table.
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,
149 * ttable_add_row(<tt>, "%s|%s", "column 1", "column 2");
151 * All features of printf format strings are permissible here.
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
158 * @param tt table to insert row into
159 * @param format column-separated format string
160 * @param ... arguments to format string
162 * @return pointer to the first cell in the created row, or NULL if not enough
163 * columns were specified
165 struct ttable_cell
*ttable_add_row(struct ttable
*tt
, const char *format
, ...)
169 * Removes a row from the table.
171 * @param tt table to delete row from
172 * @param row the row number (begins at 0)
174 void ttable_del_row(struct ttable
*tt
, unsigned int row
);
177 * Sets alignment for a range of cells.
179 * Available alignments are LEFT and RIGHT. Cell contents will be aligned
180 * accordingly, while respecting padding (if any). Suppose a cell has:
187 * The cell would look like:
189 * +-------------------+
191 * +-------------------+
200 * +-------------------+
202 * +-------------------+
204 * The default alignment is LEFT.
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
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
);
218 * Sets padding for a range of cells.
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:
229 * The cell padding, marked by '.', would look like:
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.
238 * +--------------------+
239 * | Data!!!11!~~~~~:-) |
240 * +--------------------+
242 * +--------------------+
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
252 void ttable_pad(struct ttable
*tt
, unsigned int srow
, unsigned int scol
,
253 unsigned int nrow
, unsigned int ncol
, enum ttable_align align
,
257 * Restyle all cells according to table.cell.style.
259 * @param tt table to restyle
261 void ttable_restyle(struct ttable
*tt
);
264 * Turn left/right column separators on or off for specified column.
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
272 void ttable_colseps(struct ttable
*tt
, unsigned int col
,
273 enum ttable_align align
, bool on
, char sep
);
276 * Turn bottom row separators on or off for specified row.
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
284 void ttable_rowseps(struct ttable
*tt
, unsigned int row
,
285 enum ttable_align align
, bool on
, char sep
);
288 * Dumps a table to a heap-allocated string.
290 * Caller must free this string after use with
292 * XFREE (MTYPE_TMP, str);
294 * @param tt the table to dump
295 * @param newline the desired newline sequence to use, null terminated.
296 * @return table in text form
298 char *ttable_dump(struct ttable
*tt
, const char *newline
);
304 #endif /* _TERMTABLE_H */