trafgen: parser: Replace 'mtype' by 'type'
[netsniff-ng-new.git] / ui.c
blob46062d4ca65d0b172dde42e00d531d050d37a9d2
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->height = LINES - 2;
20 tbl->col_pad = 1;
22 INIT_LIST_HEAD(&tbl->cols);
25 void ui_table_uninit(struct ui_table *tbl)
27 struct ui_col *col, *tmp;
29 list_for_each_entry_safe(col, tmp, &tbl->cols, entry)
30 xfree(col);
33 void ui_table_pos_set(struct ui_table *tbl, int y, int x)
35 tbl->y = y;
36 tbl->x = x;
37 tbl->rows_y = y;
40 static struct ui_col *ui_table_col_get(struct ui_table *tbl, uint32_t id)
42 struct ui_col *col;
44 list_for_each_entry(col, &tbl->cols, entry) {
45 if (col->id == id)
46 return col;
49 bug();
52 static void __ui_table_pos_update(struct ui_table *tbl)
54 struct ui_col *col;
55 uint32_t pos = tbl->x;
57 list_for_each_entry(col, &tbl->cols, entry) {
58 col->pos = pos;
59 pos += col->len + tbl->col_pad;
63 void ui_table_col_add(struct ui_table *tbl, uint32_t id, char *name, uint32_t len)
65 struct ui_col *col = xzmalloc(sizeof(*col));
67 col->id = id;
68 col->name = name;
69 col->len = len;
70 col->align = UI_ALIGN_LEFT;
72 list_add_tail(&col->entry, &tbl->cols);
74 __ui_table_pos_update(tbl);
77 void ui_table_col_color_set(struct ui_table *tbl, int col_id, int color)
79 struct ui_col *col = ui_table_col_get(tbl, col_id);
81 col->color = color;
84 void ui_table_col_align_set(struct ui_table *tbl, int col_id, enum ui_align align)
86 struct ui_col *col = ui_table_col_get(tbl, col_id);
88 col->align = align;
91 void ui_table_row_add(struct ui_table *tbl)
93 tbl->rows_y++;
96 void ui_table_clear(struct ui_table *tbl)
98 int y;
100 tbl->rows_y = tbl->y;
102 for (y = tbl->y + 1; y < tbl->y + tbl->height; y++) {
103 mvprintw(y, tbl->x, "%*s", tbl->width, " ");
107 #define UI_ALIGN_COL(col) (((col)->align == UI_ALIGN_LEFT) ? "%-*.*s" : "%*.*s")
109 static void __ui_table_row_print(struct ui_table *tbl, struct ui_col *col,
110 const char *str)
112 mvprintw(tbl->rows_y, col->pos, UI_ALIGN_COL(col), col->len, col->len, str);
113 mvprintw(tbl->rows_y, col->pos + col->len, "%*s", tbl->col_pad, " ");
116 void ui_table_row_print(struct ui_table *tbl, uint32_t col_id, const char *str)
118 struct ui_col *col = ui_table_col_get(tbl, col_id);
120 attron(col->color);
121 __ui_table_row_print(tbl, col, str);
122 attroff(col->color);
125 void ui_table_header_color_set(struct ui_table *tbl, int color)
127 tbl->hdr_color = color;
130 void ui_table_height_set(struct ui_table *tbl, int height)
132 tbl->height = height;
135 void ui_table_header_print(struct ui_table *tbl)
137 struct ui_col *col;
138 int max_width = tbl->width;
139 int width = 0;
141 attron(tbl->hdr_color);
143 mvprintw(tbl->y, tbl->x, "%-*.*s", max_width - tbl->x, max_width - tbl->x, "");
144 mvprintw(tbl->y, tbl->x, "");
146 list_for_each_entry(col, &tbl->cols, entry) {
147 __ui_table_row_print(tbl, col, col->name);
148 width += col->len + tbl->col_pad;
151 mvprintw(tbl->y, width, "%*s", max_width - width, " ");
152 attroff(tbl->hdr_color);