flowtop: Use new UI table API for draw flows list
[netsniff-ng.git] / ui.c
blob5e52efdc61a075f12c2fc31b98f51d90df4275e1
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Subject to the GPL, version 2.
4 */
6 #include <curses.h>
8 #include "ui.h"
9 #include "xmalloc.h"
11 void ui_table_init(struct ui_table *tbl)
13 memset(tbl, 0, sizeof(*tbl));
15 getsyx(tbl->y, tbl->x);
17 tbl->rows_y = tbl->y;
18 tbl->width = COLS;
19 tbl->col_pad = 1;
21 INIT_LIST_HEAD(&tbl->cols);
24 void ui_table_uninit(struct ui_table *tbl)
26 struct ui_col *col, *tmp;
28 list_for_each_entry_safe(col, tmp, &tbl->cols, entry)
29 xfree(col);
32 void ui_table_pos_set(struct ui_table *tbl, int y, int x)
34 tbl->y = y;
35 tbl->x = x;
36 tbl->rows_y = y;
39 static struct ui_col *ui_table_col_get(struct ui_table *tbl, uint32_t id)
41 struct ui_col *col;
43 list_for_each_entry(col, &tbl->cols, entry) {
44 if (col->id == id)
45 return col;
48 bug();
51 static void __ui_table_pos_update(struct ui_table *tbl)
53 struct ui_col *col;
54 uint32_t pos = tbl->x;
56 list_for_each_entry(col, &tbl->cols, entry) {
57 col->pos = pos;
58 pos += col->len + tbl->col_pad;
62 void ui_table_col_add(struct ui_table *tbl, uint32_t id, char *name, uint32_t len)
64 struct ui_col *col = xzmalloc(sizeof(*col));
66 col->id = id;
67 col->name = name;
68 col->len = len;
69 col->align = UI_ALIGN_LEFT;
71 list_add_tail(&col->entry, &tbl->cols);
73 __ui_table_pos_update(tbl);
76 void ui_table_col_color_set(struct ui_table *tbl, int col_id, int color)
78 struct ui_col *col = ui_table_col_get(tbl, col_id);
80 col->color = color;
83 void ui_table_col_align_set(struct ui_table *tbl, int col_id, enum ui_align align)
85 struct ui_col *col = ui_table_col_get(tbl, col_id);
87 col->align = align;
90 void ui_table_row_add(struct ui_table *tbl)
92 tbl->rows_y++;
95 void ui_table_clear(struct ui_table *tbl)
97 tbl->rows_y = tbl->y;
100 #define UI_ALIGN_COL(col) (((col)->align == UI_ALIGN_LEFT) ? "%-*.*s" : "%*.*s")
102 static void __ui_table_row_print(struct ui_table *tbl, struct ui_col *col,
103 const char *str)
105 mvprintw(tbl->rows_y, col->pos, UI_ALIGN_COL(col), col->len, col->len, str);
106 mvprintw(tbl->rows_y, col->pos + col->len, "%*s", tbl->col_pad, " ");
109 void ui_table_row_print(struct ui_table *tbl, uint32_t col_id, const char *str)
111 struct ui_col *col = ui_table_col_get(tbl, col_id);
113 attron(col->color);
114 __ui_table_row_print(tbl, col, str);
115 attroff(col->color);
118 void ui_table_header_color_set(struct ui_table *tbl, int color)
120 tbl->hdr_color = color;
123 void ui_table_header_print(struct ui_table *tbl)
125 struct ui_col *col;
126 int max_width = tbl->width;
127 int width = 0;
129 attron(tbl->hdr_color);
131 mvprintw(tbl->y, tbl->x, "%-*.*s", max_width - tbl->x, max_width - tbl->x, "");
132 mvprintw(tbl->y, tbl->x, "");
134 list_for_each_entry(col, &tbl->cols, entry) {
135 __ui_table_row_print(tbl, col, col->name);
136 width += col->len + tbl->col_pad;
139 mvprintw(tbl->y, width, "%*s", max_width - width, " ");
140 attroff(tbl->hdr_color);