2 Copyright (c) 2020 Damjan Marion
4 Permission is hereby granted, free of charge, to any person obtaining
5 a copy of this software and associated documentation files (the
6 "Software"), to deal in the Software without restriction, including
7 without limitation the rights to use, copy, modify, merge, publish,
8 distribute, sublicense, and/or sell copies of the Software, and to
9 permit persons to whom the Software is furnished to do so, subject to
10 the following conditions:
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include <vppinfra/format.h>
25 #include <vppinfra/format_table.h>
27 static table_text_attr_t default_title = {
28 .flags = TTAF_FG_COLOR_SET | TTAF_BOLD,
29 .fg_color = TTAC_YELLOW,
33 static table_text_attr_t default_body = {
37 static table_text_attr_t default_header_col = {
38 .flags = TTAF_FG_COLOR_SET,
39 .fg_color = TTAC_YELLOW,
43 static table_text_attr_t default_header_row = {
44 .flags = TTAF_FG_COLOR_SET | TTAF_BOLD,
45 .fg_color = TTAC_GREEN,
50 format_text_cell (table_t *t, u8 *s, table_cell_t *c, table_text_attr_t *def,
53 table_text_attr_t _a = {}, *a = &_a;
56 return format (s, t->no_ansi ? "" : "\x1b[0m");
58 clib_memcpy (a, def, sizeof (table_text_attr_t));
63 if (c->attr.flags & TTAF_FG_COLOR_SET)
65 a->fg_color = c->attr.fg_color;
66 a->flags |= TTAF_FG_COLOR_SET;
69 if (c->attr.flags & TTAF_BG_COLOR_SET)
71 a->bg_color = c->attr.bg_color;
72 a->flags |= TTAF_BG_COLOR_SET;
75 if (a->flags & TTAF_RESET)
78 if (a->flags & TTAF_BOLD)
81 if (a->flags & TTAF_DIM)
84 if (a->flags & TTAF_UNDERLINE)
87 if (a->flags & TTAF_FG_COLOR_SET)
89 (a->flags & TTAF_FG_COLOR_BRIGHT ? 90 : 30) + a->fg_color);
91 if (a->flags & TTAF_BG_COLOR_SET)
93 (a->flags & TTAF_BG_COLOR_BRIGHT ? 100 : 40) + a->bg_color);
97 s = format (s, "\x1b[");
98 for (int i = 0; i < vec_len (codes); i++)
99 s = format (s, "%s%u", i ? ";" : "", codes[i]);
106 table_text_attr_align_t align = c->attr.align;
107 if (align == TTAA_DEFAULT)
109 if (align == TTAA_LEFT)
110 fmt = format (fmt, "%%-%uv%c", size, 0);
111 else if (align == TTAA_CENTER)
112 fmt = format (fmt, "%%=%uv%c", size, 0);
114 fmt = format (fmt, "%%%uv%c", size, 0);
115 s = format (s, (char *) fmt, c->text);
117 return format (s, t->no_ansi ? "" : "\x1b[0m");
121 format_table (u8 *s, va_list *args)
123 table_t *t = va_arg (*args, table_t *);
124 table_cell_t title_cell = { .text = t->title };
126 for (int i = 0; i < vec_len (t->row_sizes); i++)
127 table_width += t->row_sizes[i];
131 table_text_attr_t *title_default;
133 t->default_title.as_u32 ? &t->default_title : &default_title;
134 s = format_text_cell (t, s, &title_cell, title_default, table_width);
135 s = format (s, "\n");
138 for (int c = 0; c < vec_len (t->cells); c++)
140 table_text_attr_t *col_default;
142 if (c < t->n_header_cols)
143 col_default = t->default_header_col.as_u32 ? &t->default_header_col :
147 t->default_body.as_u32 ? &t->default_body : &default_body;
149 for (int r = 0; r < vec_len (t->cells[c]); r++)
151 table_text_attr_t *row_default = col_default;
154 if (r < t->n_header_rows && c >= t->n_header_cols)
155 row_default = t->default_header_row.as_u32 ?
156 &t->default_header_row :
158 s = format_text_cell (t, s, &t->cells[c][r], row_default,
161 if (c + 1 < vec_len (t->cells))
162 s = format (s, "\n");
169 table_format_title (table_t *t, char *fmt, ...)
174 t->title = va_format (t->title, fmt, &va);
178 static table_cell_t *
179 table_get_cell (table_t *t, int c, int r)
181 c += t->n_header_cols;
182 r += t->n_header_rows;
184 /* grow table if needed */
185 vec_validate (t->cells, c);
186 for (int i = 0; i < vec_len (t->cells); i++)
187 vec_validate (t->cells[i], r);
188 return &t->cells[c][r];
192 table_format_cell (table_t *t, int c, int r, char *fmt, ...)
194 table_cell_t *cell = table_get_cell (t, c, r);
197 c += t->n_header_cols;
198 r += t->n_header_rows;
201 cell->text = va_format (t->cells[c][r].text, fmt, &va);
204 vec_validate (t->row_sizes, r);
205 t->row_sizes[r] = clib_max (t->row_sizes[r], vec_len (t->cells[c][r].text));
209 table_set_cell_align (table_t *t, int c, int r, table_text_attr_align_t a)
211 table_cell_t *cell = table_get_cell (t, c, r);
212 cell->attr.align = a;
216 table_set_cell_fg_color (table_t *t, int c, int r, table_text_attr_color_t v)
218 table_cell_t *cell = table_get_cell (t, c, r);
219 cell->attr.fg_color = v;
220 cell->attr.flags |= TTAF_FG_COLOR_SET;
224 table_set_cell_bg_color (table_t *t, int c, int r, table_text_attr_color_t v)
226 table_cell_t *cell = table_get_cell (t, c, r);
227 cell->attr.bg_color = v;
228 cell->attr.flags |= TTAF_BG_COLOR_SET;
232 table_free (table_t *t)
234 for (int c = 0; c < vec_len (t->cells); c++)
236 for (int r = 0; r < vec_len (t->cells[c]); r++)
237 vec_free (t->cells[c][r].text);
238 vec_free (t->cells[c]);
241 vec_free (t->row_sizes);
243 clib_memset (t, 0, sizeof (table_t));
247 table_add_header_col (table_t *t, int n_strings, ...)
250 int r, c = t->n_header_cols++;
253 vec_insert (t->cells, 1, c);
254 n_rows = clib_max (n_strings, 1);
255 n_rows = clib_max (vec_len (t->row_sizes), n_rows);
256 vec_validate (t->cells[c], n_rows - 1);
258 va_start (arg, n_strings);
259 for (r = 0; r < n_rows; r++)
262 table_format_cell (t, -1, r - t->n_header_rows, "%s",
263 va_arg (arg, char *));
269 table_add_header_row (table_t *t, int n_strings, ...)
272 int c, r = t->n_header_rows++;
274 vec_validate (t->cells, n_strings + t->n_header_cols - 1);
276 va_start (arg, n_strings);
277 for (c = t->n_header_cols; c < vec_len (t->cells); c++)
279 vec_insert (t->cells[c + t->n_header_cols], 1, r);
281 table_format_cell (t, c, -1, "%s", va_arg (arg, char *));