diff options
author | Vadim Kochan <vadim4j@gmail.com> | 2016-04-21 21:47:39 +0300 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2016-04-22 15:29:03 +0200 |
commit | aef19410e1346cea962d838894785eef96182312 (patch) | |
tree | 6e52cbf4dd5ae027afd701dcd299cbb3cf236f7c | |
parent | 627b1d172d19372dc43248cfe680521743efd826 (diff) |
ui: Implement UI table for flows printing
Add new module ui.c which is responsible to render
different kinds of UI widgets - tables, etc.
Implemented generic API for print table-like list of elements.
This table API might be used for print flows in curses or text mode.
Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
[tk: change bug_on(true) to bug(), whitespace fix]
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r-- | ui.c | 141 | ||||
-rw-r--r-- | ui.h | 51 |
2 files changed, 192 insertions, 0 deletions
@@ -0,0 +1,141 @@ +/* + * netsniff-ng - the packet sniffing beast + * Subject to the GPL, version 2. + */ + +#include <curses.h> + +#include "ui.h" +#include "xmalloc.h" + +void ui_table_init(struct ui_table *tbl) +{ + memset(tbl, 0, sizeof(*tbl)); + + getsyx(tbl->y, tbl->x); + + tbl->rows_y = tbl->y; + tbl->width = COLS; + tbl->col_pad = 1; + + INIT_LIST_HEAD(&tbl->cols); +} + +void ui_table_uninit(struct ui_table *tbl) +{ + struct ui_col *col, *tmp; + + list_for_each_entry_safe(col, tmp, &tbl->cols, entry) + xfree(col); +} + +void ui_table_pos_set(struct ui_table *tbl, int y, int x) +{ + tbl->y = y; + tbl->x = x; + tbl->rows_y = y; +} + +static struct ui_col *ui_table_col_get(struct ui_table *tbl, uint32_t id) +{ + struct ui_col *col; + + list_for_each_entry(col, &tbl->cols, entry) { + if (col->id == id) + return col; + } + + bug(); +} + +static void __ui_table_pos_update(struct ui_table *tbl) +{ + struct ui_col *col; + uint32_t pos = tbl->x; + + list_for_each_entry(col, &tbl->cols, entry) { + col->pos = pos; + pos += col->len + tbl->col_pad; + } +} + +void ui_table_col_add(struct ui_table *tbl, uint32_t id, char *name, uint32_t len) +{ + struct ui_col *col = xzmalloc(sizeof(*col)); + + col->id = id; + col->name = name; + col->len = len; + col->align = UI_ALIGN_LEFT; + + list_add_tail(&col->entry, &tbl->cols); + + __ui_table_pos_update(tbl); +} + +void ui_table_col_color_set(struct ui_table *tbl, int col_id, int color) +{ + struct ui_col *col = ui_table_col_get(tbl, col_id); + + col->color = color; +} + +void ui_table_col_align_set(struct ui_table *tbl, int col_id, enum ui_align align) +{ + struct ui_col *col = ui_table_col_get(tbl, col_id); + + col->align = align; +} + +void ui_table_row_add(struct ui_table *tbl) +{ + tbl->rows_y++; +} + +void ui_table_clear(struct ui_table *tbl) +{ + tbl->rows_y = tbl->y; +} + +#define UI_ALIGN_COL(col) (((col)->align == UI_ALIGN_LEFT) ? "%-*.*s" : "%*.*s") + +static void __ui_table_row_print(struct ui_table *tbl, struct ui_col *col, + const char *str) +{ + mvprintw(tbl->rows_y, col->pos, UI_ALIGN_COL(col), col->len, col->len, str); + mvprintw(tbl->rows_y, col->pos + col->len, "%*s", tbl->col_pad, " "); +} + +void ui_table_row_print(struct ui_table *tbl, uint32_t col_id, const char *str) +{ + struct ui_col *col = ui_table_col_get(tbl, col_id); + + attron(col->color); + __ui_table_row_print(tbl, col, str); + attroff(col->color); +} + +void ui_table_header_color_set(struct ui_table *tbl, int color) +{ + tbl->hdr_color = color; +} + +void ui_table_header_print(struct ui_table *tbl) +{ + struct ui_col *col; + int max_width = tbl->width; + int width = 0; + + attron(tbl->hdr_color); + + mvprintw(tbl->y, tbl->x, "%-*.*s", max_width - tbl->x, max_width - tbl->x, ""); + mvprintw(tbl->y, tbl->x, ""); + + list_for_each_entry(col, &tbl->cols, entry) { + __ui_table_row_print(tbl, col, col->name); + width += col->len + tbl->col_pad; + } + + mvprintw(tbl->y, width, "%*s", max_width - width, " "); + attroff(tbl->hdr_color); +} @@ -0,0 +1,51 @@ +#ifndef UI_H +#define UI_H + +#include <stdbool.h> +#include <inttypes.h> + +#include "list.h" + +enum ui_align { + UI_ALIGN_LEFT, + UI_ALIGN_RIGHT, +}; + +struct ui_col { + struct list_head entry; + uint32_t id; + char *name; + uint32_t len; + int pos; + int color; + enum ui_align align; +}; + +struct ui_table { + int y; + int x; + int rows_y; + struct list_head cols; + int hdr_color; + int col_pad; + int width; +}; + +extern void ui_table_init(struct ui_table *tbl); +extern void ui_table_uninit(struct ui_table *tbl); +extern void ui_table_pos_set(struct ui_table *tbl, int y, int x); +extern void ui_table_clear(struct ui_table *tbl); + +extern void ui_table_col_add(struct ui_table *tbl, uint32_t id, char *name, + uint32_t len); +extern void ui_table_col_color_set(struct ui_table *tbl, int col_id, int color); +extern void ui_table_col_align_set(struct ui_table *tbl, int col_id, enum ui_align align); + +extern void ui_table_row_add(struct ui_table *tbl); +extern void ui_table_row_print(struct ui_table *tbl, uint32_t col_id, + const char *str); + +extern void ui_table_header_color_set(struct ui_table *tbl, int color); +extern void ui_table_header_print(struct ui_table *tbl); + +#endif /* UI_H */ |