1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/*
* 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->height = LINES - 2;
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)
{
int y;
tbl->rows_y = tbl->y;
for (y = tbl->y + 1; y < tbl->y + tbl->height; y++) {
mvprintw(y, tbl->x, "%*s", tbl->width, " ");
}
}
#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_height_set(struct ui_table *tbl, int height)
{
tbl->height = height;
}
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);
}
|