2 * netsniff-ng - the packet sniffing beast
3 * Subject to the GPL, version 2.
11 void ui_table_init(struct ui_table
*tbl
)
13 memset(tbl
, 0, sizeof(*tbl
));
15 getsyx(tbl
->y
, tbl
->x
);
19 tbl
->height
= LINES
- 2;
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
)
33 void ui_table_pos_set(struct ui_table
*tbl
, int y
, int x
)
40 static struct ui_col
*ui_table_col_get(struct ui_table
*tbl
, uint32_t id
)
44 list_for_each_entry(col
, &tbl
->cols
, entry
) {
52 static void __ui_table_pos_update(struct ui_table
*tbl
)
55 uint32_t pos
= tbl
->x
;
57 list_for_each_entry(col
, &tbl
->cols
, entry
) {
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
));
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
);
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
);
91 void ui_table_row_add(struct ui_table
*tbl
)
96 void ui_table_clear(struct ui_table
*tbl
)
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
,
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
);
121 __ui_table_row_print(tbl
, col
, str
);
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
)
138 int max_width
= tbl
->width
;
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
);