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
);
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
)
32 void ui_table_pos_set(struct ui_table
*tbl
, int y
, int x
)
39 static struct ui_col
*ui_table_col_get(struct ui_table
*tbl
, uint32_t id
)
43 list_for_each_entry(col
, &tbl
->cols
, entry
) {
51 static void __ui_table_pos_update(struct ui_table
*tbl
)
54 uint32_t pos
= tbl
->x
;
56 list_for_each_entry(col
, &tbl
->cols
, entry
) {
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
));
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
);
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
);
90 void ui_table_row_add(struct ui_table
*tbl
)
95 void ui_table_clear(struct ui_table
*tbl
)
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
,
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
);
114 __ui_table_row_print(tbl
, col
, str
);
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
)
126 int max_width
= tbl
->width
;
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
);