vppinfra: format table improvements 47/33747/4
authorNathan Skrzypczak <nathan.skrzypczak@gmail.com>
Fri, 17 Sep 2021 12:32:03 +0000 (14:32 +0200)
committerDamjan Marion <dmarion@me.com>
Mon, 20 Sep 2021 11:26:03 +0000 (11:26 +0000)
This adds a way to define default fg, bg colors
and alignement for cell tables.
It also allows removing the table title by not
setting it.
It also removes the trailing newline, for usage
inside a format("%U", format_table, ...)

Type: improvement

Change-Id: I27d7a04c4a919b34d0170b04e24a56831f581ea1
Signed-off-by: Nathan Skrzypczak <nathan.skrzypczak@gmail.com>
src/vppinfra/format_table.c
src/vppinfra/format_table.h

index 5d83f7a..b698cfd 100644 (file)
@@ -126,17 +126,25 @@ format_table (u8 *s, va_list *args)
   for (int i = 0; i < vec_len (t->row_sizes); i++)
     table_width += t->row_sizes[i];
 
-  s = format_text_cell (t, s, &title_cell, &default_title, table_width);
-  s = format (s, "\n");
+  if (t->title)
+    {
+      table_text_attr_t *title_default;
+      title_default =
+       t->default_title.as_u32 ? &t->default_title : &default_title;
+      s = format_text_cell (t, s, &title_cell, title_default, table_width);
+      s = format (s, "\n");
+    }
 
   for (int c = 0; c < vec_len (t->cells); c++)
     {
       table_text_attr_t *col_default;
 
       if (c < t->n_header_cols)
-       col_default = &default_header_col;
+       col_default = t->default_header_col.as_u32 ? &t->default_header_col :
+                                                    &default_header_col;
       else
-       col_default = &default_body;
+       col_default =
+         t->default_body.as_u32 ? &t->default_body : &default_body;
 
       for (int r = 0; r < vec_len (t->cells[c]); r++)
        {
@@ -144,11 +152,14 @@ format_table (u8 *s, va_list *args)
          if (r)
            s = format (s, " ");
          if (r < t->n_header_rows && c >= t->n_header_cols)
-           row_default = &default_header_row;
+           row_default = t->default_header_row.as_u32 ?
+                           &t->default_header_row :
+                           &default_header_row;
          s = format_text_cell (t, s, &t->cells[c][r], row_default,
                                t->row_sizes[r]);
        }
-      s = format (s, "\n");
+      if (c + 1 < vec_len (t->cells))
+       s = format (s, "\n");
     }
 
   return s;
index f9b66a7..a659533 100644 (file)
@@ -58,10 +58,17 @@ typedef enum
 
 typedef struct
 {
-  table_text_attr_flags_t flags : 16;
-  table_text_attr_color_t fg_color : 4;
-  table_text_attr_color_t bg_color : 4;
-  table_text_attr_align_t align : 4;
+  union
+  {
+    struct
+    {
+      table_text_attr_flags_t flags : 16;
+      table_text_attr_color_t fg_color : 4;
+      table_text_attr_color_t bg_color : 4;
+      table_text_attr_align_t align : 4;
+    };
+    u32 as_u32;
+  };
 } table_text_attr_t;
 
 typedef struct
@@ -79,6 +86,10 @@ typedef struct
   int n_header_cols;
   int n_header_rows;
   int n_footer_cols;
+  table_text_attr_t default_title;
+  table_text_attr_t default_body;
+  table_text_attr_t default_header_col;
+  table_text_attr_t default_header_row;
 } table_t;
 
 __clib_export format_function_t format_table;