2 * netsniff-ng - the packet sniffing beast
3 * Subject to the GPL, version 2.
12 static struct ui_text
*ui_text_alloc(size_t len
)
14 struct ui_text
*text
= xzmalloc(sizeof(*text
));
16 text
->str
= xzmalloc(sizeof(chtype
) * len
+ 1);
22 static void ui_text_len_set(struct ui_text
*text
, size_t len
)
27 if (text
->slen
+ len
> text
->len
) {
28 text
->str
= xrealloc(text
->str
, sizeof(chtype
) * len
+ 1);
32 text
->slen
= min(len
, text
->slen
);
33 text
->str
[text
->slen
] = 0;
36 static void ui_text_attr_insert(struct ui_text
*text
, int idx
, int attr
, const char *str
)
38 size_t slen
= strlen(str
);
41 if (idx
+ slen
> text
->len
)
42 ui_text_len_set(text
, idx
+ slen
);
44 for (j
= 0, i
= idx
; i
< idx
+ slen
; i
++, j
++)
45 text
->str
[i
] = str
[j
] | attr
;
48 static void ui_text_free(struct ui_text
*text
)
54 void ui_table_init(struct ui_table
*tbl
)
56 memset(tbl
, 0, sizeof(*tbl
));
58 getsyx(tbl
->y
, tbl
->x
);
62 tbl
->height
= LINES
- 2;
64 tbl
->row
= ui_text_alloc(tbl
->width
);
66 INIT_LIST_HEAD(&tbl
->cols
);
69 void ui_table_uninit(struct ui_table
*tbl
)
71 struct ui_col
*col
, *tmp
;
73 list_for_each_entry_safe(col
, tmp
, &tbl
->cols
, entry
)
76 ui_text_free(tbl
->row
);
79 void ui_table_pos_set(struct ui_table
*tbl
, int y
, int x
)
86 static struct ui_col
*ui_table_col_get(struct ui_table
*tbl
, uint32_t id
)
90 list_for_each_entry(col
, &tbl
->cols
, entry
) {
98 static void __ui_table_pos_update(struct ui_table
*tbl
)
103 list_for_each_entry(col
, &tbl
->cols
, entry
) {
105 pos
+= col
->len
+ tbl
->col_pad
;
109 void ui_table_col_add(struct ui_table
*tbl
, uint32_t id
, char *name
, uint32_t len
)
111 struct ui_col
*col
= xzmalloc(sizeof(*col
));
116 col
->align
= UI_ALIGN_LEFT
;
118 list_add_tail(&col
->entry
, &tbl
->cols
);
120 __ui_table_pos_update(tbl
);
123 void ui_table_col_color_set(struct ui_table
*tbl
, int col_id
, int color
)
125 struct ui_col
*col
= ui_table_col_get(tbl
, col_id
);
130 void ui_table_col_align_set(struct ui_table
*tbl
, int col_id
, enum ui_align align
)
132 struct ui_col
*col
= ui_table_col_get(tbl
, col_id
);
137 void ui_table_row_add(struct ui_table
*tbl
)
142 void ui_table_clear(struct ui_table
*tbl
)
146 tbl
->rows_y
= tbl
->y
;
148 for (y
= tbl
->y
+ 1; y
< tbl
->y
+ tbl
->height
; y
++) {
149 mvprintw(y
, tbl
->x
, "%*s", tbl
->width
, " ");
153 #define UI_ALIGN_COL(col) (((col)->align == UI_ALIGN_LEFT) ? "%-*.*s" : "%*.*s")
155 void ui_table_row_show(struct ui_table
*tbl
)
157 mvaddchstr(tbl
->rows_y
, tbl
->x
, tbl
->row
->str
+ tbl
->scroll_x
);
158 ui_text_len_set(tbl
->row
, 0);
161 static void __ui_table_row_print(struct ui_table
*tbl
, struct ui_col
*col
,
162 int color
, const char *str
)
166 slprintf(tmp
, sizeof(tmp
), UI_ALIGN_COL(col
), col
->len
, col
->len
, str
);
167 ui_text_attr_insert(tbl
->row
, col
->pos
, color
, tmp
);
169 slprintf(tmp
, sizeof(tmp
), "%*s", tbl
->col_pad
, " ");
170 ui_text_attr_insert(tbl
->row
, col
->pos
+ col
->len
, color
, tmp
);
173 void ui_table_row_col_set(struct ui_table
*tbl
, uint32_t col_id
, const char *str
)
175 struct ui_col
*col
= ui_table_col_get(tbl
, col_id
);
177 __ui_table_row_print(tbl
, col
, col
->color
, str
);
180 void ui_table_header_color_set(struct ui_table
*tbl
, int color
)
182 tbl
->hdr_color
= color
;
185 void ui_table_height_set(struct ui_table
*tbl
, int height
)
187 tbl
->height
= height
;
190 void ui_table_header_print(struct ui_table
*tbl
)
194 attron(tbl
->hdr_color
);
195 mvprintw(tbl
->y
, tbl
->x
, "%*s", tbl
->width
, " ");
196 attroff(tbl
->hdr_color
);
198 list_for_each_entry(col
, &tbl
->cols
, entry
) {
199 __ui_table_row_print(tbl
, col
, tbl
->hdr_color
, col
->name
);
203 ui_table_row_show(tbl
);
206 #define SCROLL_X_STEP 10
208 void ui_table_event_send(struct ui_table
*tbl
, enum ui_event_id evt_id
)
210 if (evt_id
== UI_EVT_SCROLL_RIGHT
) {
211 tbl
->scroll_x
+= SCROLL_X_STEP
;
212 } else if (evt_id
== UI_EVT_SCROLL_LEFT
) {
213 tbl
->scroll_x
-= SCROLL_X_STEP
;
214 if (tbl
->scroll_x
< 0)