Damjan Marion | 8b60fb0 | 2020-11-27 20:15:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2020 Damjan Marion |
| 3 | |
| 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: |
| 11 | |
| 12 | The above copyright notice and this permission notice shall be |
| 13 | included in all copies or substantial portions of the Software. |
| 14 | |
| 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. |
| 22 | */ |
| 23 | |
Nathan Skrzypczak | 0e65840 | 2021-09-17 11:51:46 +0200 | [diff] [blame] | 24 | #ifndef __format_table_h__ |
| 25 | #define __format_table_h__ |
Damjan Marion | 8b60fb0 | 2020-11-27 20:15:17 +0100 | [diff] [blame] | 26 | |
| 27 | typedef enum |
| 28 | { |
| 29 | TTAF_RESET = (1 << 0), |
| 30 | TTAF_BOLD = (1 << 1), |
| 31 | TTAF_DIM = (1 << 2), |
| 32 | TTAF_UNDERLINE = (1 << 3), |
| 33 | TTAF_FG_COLOR_SET = (1 << 4), |
| 34 | TTAF_BG_COLOR_SET = (1 << 5), |
| 35 | TTAF_FG_COLOR_BRIGHT = (1 << 6), |
| 36 | TTAF_BG_COLOR_BRIGHT = (1 << 7), |
| 37 | } table_text_attr_flags_t; |
| 38 | |
| 39 | typedef enum |
| 40 | { |
| 41 | TTAC_BLACK = 0, |
| 42 | TTAC_RED = 1, |
| 43 | TTAC_GREEN = 2, |
| 44 | TTAC_YELLOW = 3, |
| 45 | TTAC_BLUE = 4, |
| 46 | TTAC_MAGENTA = 5, |
| 47 | TTAC_CYAN = 6, |
| 48 | TTAC_WHITE = 7, |
Damjan Marion | 853530b | 2022-04-08 13:42:41 +0200 | [diff] [blame] | 49 | TTAC_BRIGHT_BLACK = 8, |
| 50 | TTAC_BRIGHT_RED = 9, |
| 51 | TTAC_BRIGHT_GREEN = 10, |
| 52 | TTAC_BRIGHT_YELLOW = 11, |
| 53 | TTAC_BRIGHT_BLUE = 12, |
| 54 | TTAC_BRIGHT_MAGENTA = 13, |
| 55 | TTAC_BRIGHT_CYAN = 14, |
| 56 | TTAC_BRIGHT_WHITE = 15, |
Damjan Marion | 8b60fb0 | 2020-11-27 20:15:17 +0100 | [diff] [blame] | 57 | } table_text_attr_color_t; |
| 58 | |
| 59 | typedef enum |
| 60 | { |
| 61 | TTAA_DEFAULT = 0, |
| 62 | TTAA_LEFT = 1, |
| 63 | TTAA_RIGHT = 2, |
| 64 | TTAA_CENTER = 3, |
| 65 | } table_text_attr_align_t; |
| 66 | |
| 67 | typedef struct |
| 68 | { |
Nathan Skrzypczak | 8430b8d | 2021-09-17 14:32:03 +0200 | [diff] [blame] | 69 | union |
| 70 | { |
| 71 | struct |
| 72 | { |
| 73 | table_text_attr_flags_t flags : 16; |
| 74 | table_text_attr_color_t fg_color : 4; |
| 75 | table_text_attr_color_t bg_color : 4; |
| 76 | table_text_attr_align_t align : 4; |
| 77 | }; |
| 78 | u32 as_u32; |
| 79 | }; |
Damjan Marion | 8b60fb0 | 2020-11-27 20:15:17 +0100 | [diff] [blame] | 80 | } table_text_attr_t; |
| 81 | |
| 82 | typedef struct |
| 83 | { |
| 84 | table_text_attr_t attr; |
| 85 | u8 *text; |
| 86 | } table_cell_t; |
| 87 | |
| 88 | typedef struct |
| 89 | { |
| 90 | u8 no_ansi : 1; |
| 91 | u8 *title; |
| 92 | table_cell_t **cells; |
| 93 | int *row_sizes; |
| 94 | int n_header_cols; |
| 95 | int n_header_rows; |
| 96 | int n_footer_cols; |
Nathan Skrzypczak | 8430b8d | 2021-09-17 14:32:03 +0200 | [diff] [blame] | 97 | table_text_attr_t default_title; |
| 98 | table_text_attr_t default_body; |
| 99 | table_text_attr_t default_header_col; |
| 100 | table_text_attr_t default_header_row; |
Damjan Marion | 8b60fb0 | 2020-11-27 20:15:17 +0100 | [diff] [blame] | 101 | } table_t; |
| 102 | |
Nathan Skrzypczak | 0e65840 | 2021-09-17 11:51:46 +0200 | [diff] [blame] | 103 | __clib_export format_function_t format_table; |
Damjan Marion | 8b60fb0 | 2020-11-27 20:15:17 +0100 | [diff] [blame] | 104 | |
Nathan Skrzypczak | 0e65840 | 2021-09-17 11:51:46 +0200 | [diff] [blame] | 105 | __clib_export void table_format_title (table_t *t, char *fmt, ...); |
| 106 | __clib_export void table_format_cell (table_t *t, int c, int r, char *fmt, |
| 107 | ...); |
| 108 | __clib_export void table_set_cell_align (table_t *t, int c, int r, |
| 109 | table_text_attr_align_t a); |
| 110 | __clib_export void table_set_cell_fg_color (table_t *t, int c, int r, |
| 111 | table_text_attr_color_t v); |
| 112 | __clib_export void table_set_cell_bg_color (table_t *t, int c, int r, |
| 113 | table_text_attr_color_t v); |
| 114 | __clib_export void table_free (table_t *t); |
| 115 | __clib_export void table_add_header_col (table_t *t, int n_strings, ...); |
| 116 | __clib_export void table_add_header_row (table_t *t, int n_strings, ...); |
Damjan Marion | 8b60fb0 | 2020-11-27 20:15:17 +0100 | [diff] [blame] | 117 | |
| 118 | #endif |