blob: 930c4acaf56ac1616a7bb98c6c3da3eaf8b2fcf6 [file] [log] [blame]
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -03001#include <slang.h>
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -03002#include "libslang.h"
3#include <linux/compiler.h>
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -03004#include <linux/list.h>
5#include <linux/rbtree.h>
6#include <stdlib.h>
7#include <sys/ttydefaults.h>
8#include "browser.h"
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -03009#include "helpline.h"
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030010#include "../color.h"
11#include "../util.h"
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030012#include <stdio.h>
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030013
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030014static int ui_browser__percent_color(double percent, bool current)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030015{
16 if (current)
17 return HE_COLORSET_SELECTED;
18 if (percent >= MIN_RED)
19 return HE_COLORSET_TOP;
20 if (percent >= MIN_GREEN)
21 return HE_COLORSET_MEDIUM;
22 return HE_COLORSET_NORMAL;
23}
24
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030025void ui_browser__set_color(struct ui_browser *self __used, int color)
26{
27 SLsmg_set_color(color);
28}
29
30void ui_browser__set_percent_color(struct ui_browser *self,
31 double percent, bool current)
32{
33 int color = ui_browser__percent_color(percent, current);
34 ui_browser__set_color(self, color);
35}
36
37void ui_browser__gotorc(struct ui_browser *self, int y, int x)
38{
39 SLsmg_gotorc(self->y + y, self->x + x);
40}
41
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030042void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence)
43{
44 struct list_head *head = self->entries;
45 struct list_head *pos;
46
47 switch (whence) {
48 case SEEK_SET:
49 pos = head->next;
50 break;
51 case SEEK_CUR:
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -030052 pos = self->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030053 break;
54 case SEEK_END:
55 pos = head->prev;
56 break;
57 default:
58 return;
59 }
60
61 if (offset > 0) {
62 while (offset-- != 0)
63 pos = pos->next;
64 } else {
65 while (offset++ != 0)
66 pos = pos->prev;
67 }
68
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -030069 self->top = pos;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030070}
71
72void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
73{
74 struct rb_root *root = self->entries;
75 struct rb_node *nd;
76
77 switch (whence) {
78 case SEEK_SET:
79 nd = rb_first(root);
80 break;
81 case SEEK_CUR:
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -030082 nd = self->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030083 break;
84 case SEEK_END:
85 nd = rb_last(root);
86 break;
87 default:
88 return;
89 }
90
91 if (offset > 0) {
92 while (offset-- != 0)
93 nd = rb_next(nd);
94 } else {
95 while (offset++ != 0)
96 nd = rb_prev(nd);
97 }
98
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -030099 self->top = nd;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300100}
101
102unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
103{
104 struct rb_node *nd;
105 int row = 0;
106
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300107 if (self->top == NULL)
108 self->top = rb_first(self->entries);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300109
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300110 nd = self->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300111
112 while (nd != NULL) {
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300113 ui_browser__gotorc(self, row, 0);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300114 self->write(self, nd, row);
115 if (++row == self->height)
116 break;
117 nd = rb_next(nd);
118 }
119
120 return row;
121}
122
123bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
124{
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300125 return self->top_idx + row == self->index;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300126}
127
128void ui_browser__refresh_dimensions(struct ui_browser *self)
129{
130 int cols, rows;
131 newtGetScreenSize(&cols, &rows);
132
133 if (self->width > cols - 4)
134 self->width = cols - 4;
135 self->height = rows - 5;
136 if (self->height > self->nr_entries)
137 self->height = self->nr_entries;
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300138 self->y = (rows - self->height) / 2;
139 self->x = (cols - self->width) / 2;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300140}
141
142void ui_browser__reset_index(struct ui_browser *self)
143{
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300144 self->index = self->top_idx = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300145 self->seek(self, 0, SEEK_SET);
146}
147
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -0300148void ui_browser__add_exit_key(struct ui_browser *self, int key)
149{
150 newtFormAddHotKey(self->form, key);
151}
152
153void ui_browser__add_exit_keys(struct ui_browser *self, int keys[])
154{
155 int i = 0;
156
157 while (keys[i] && i < 64) {
158 ui_browser__add_exit_key(self, keys[i]);
159 ++i;
160 }
161}
162
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300163int ui_browser__show(struct ui_browser *self, const char *title,
164 const char *helpline, ...)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300165{
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300166 va_list ap;
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -0300167 int keys[] = { NEWT_KEY_UP, NEWT_KEY_DOWN, NEWT_KEY_PGUP,
168 NEWT_KEY_PGDN, NEWT_KEY_HOME, NEWT_KEY_END, ' ',
169 NEWT_KEY_LEFT, NEWT_KEY_ESCAPE, 'q', CTRL('c'), 0 };
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300170
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300171 if (self->form != NULL) {
172 newtFormDestroy(self->form);
173 newtPopWindow();
174 }
175 ui_browser__refresh_dimensions(self);
176 newtCenteredWindow(self->width, self->height, title);
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -0300177 self->form = newtForm(NULL, NULL, 0);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300178 if (self->form == NULL)
179 return -1;
180
181 self->sb = newtVerticalScrollbar(self->width, 0, self->height,
182 HE_COLORSET_NORMAL,
183 HE_COLORSET_SELECTED);
184 if (self->sb == NULL)
185 return -1;
186
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -0300187 ui_browser__add_exit_keys(self, keys);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300188 newtFormAddComponent(self->form, self->sb);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300189
190 va_start(ap, helpline);
191 ui_helpline__vpush(helpline, ap);
192 va_end(ap);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300193 return 0;
194}
195
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300196void ui_browser__hide(struct ui_browser *self)
197{
198 newtFormDestroy(self->form);
199 newtPopWindow();
200 self->form = NULL;
201 ui_helpline__pop();
202}
203
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300204int ui_browser__refresh(struct ui_browser *self)
205{
206 int row;
207
208 newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
209 row = self->refresh(self);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300210 ui_browser__set_color(self, HE_COLORSET_NORMAL);
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300211 SLsmg_fill_region(self->y + row, self->x,
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300212 self->height - row, self->width, ' ');
213
214 return 0;
215}
216
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300217int ui_browser__run(struct ui_browser *self)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300218{
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300219 struct newtExitStruct es;
220
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300221 if (ui_browser__refresh(self) < 0)
222 return -1;
223
224 while (1) {
225 off_t offset;
226
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300227 newtFormRun(self->form, &es);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300228
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300229 if (es.reason != NEWT_EXIT_HOTKEY)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300230 break;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300231 switch (es.u.key) {
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300232 case NEWT_KEY_DOWN:
233 if (self->index == self->nr_entries - 1)
234 break;
235 ++self->index;
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300236 if (self->index == self->top_idx + self->height) {
237 ++self->top_idx;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300238 self->seek(self, +1, SEEK_CUR);
239 }
240 break;
241 case NEWT_KEY_UP:
242 if (self->index == 0)
243 break;
244 --self->index;
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300245 if (self->index < self->top_idx) {
246 --self->top_idx;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300247 self->seek(self, -1, SEEK_CUR);
248 }
249 break;
250 case NEWT_KEY_PGDN:
251 case ' ':
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300252 if (self->top_idx + self->height > self->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300253 break;
254
255 offset = self->height;
256 if (self->index + offset > self->nr_entries - 1)
257 offset = self->nr_entries - 1 - self->index;
258 self->index += offset;
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300259 self->top_idx += offset;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300260 self->seek(self, +offset, SEEK_CUR);
261 break;
262 case NEWT_KEY_PGUP:
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300263 if (self->top_idx == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300264 break;
265
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300266 if (self->top_idx < self->height)
267 offset = self->top_idx;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300268 else
269 offset = self->height;
270
271 self->index -= offset;
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300272 self->top_idx -= offset;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300273 self->seek(self, -offset, SEEK_CUR);
274 break;
275 case NEWT_KEY_HOME:
276 ui_browser__reset_index(self);
277 break;
278 case NEWT_KEY_END:
279 offset = self->height - 1;
280 if (offset >= self->nr_entries)
281 offset = self->nr_entries - 1;
282
283 self->index = self->nr_entries - 1;
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300284 self->top_idx = self->index - offset;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300285 self->seek(self, -offset, SEEK_END);
286 break;
287 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300288 return es.u.key;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300289 }
290 if (ui_browser__refresh(self) < 0)
291 return -1;
292 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300293 return -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300294}
295
296unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
297{
298 struct list_head *pos;
299 struct list_head *head = self->entries;
300 int row = 0;
301
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300302 if (self->top == NULL || self->top == self->entries)
303 self->top = head->next;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300304
Arnaldo Carvalho de Melod247eb62010-08-07 13:56:04 -0300305 pos = self->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300306
307 list_for_each_from(pos, head) {
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300308 ui_browser__gotorc(self, row, 0);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300309 self->write(self, pos, row);
310 if (++row == self->height)
311 break;
312 }
313
314 return row;
315}
316
317static struct newtPercentTreeColors {
318 const char *topColorFg, *topColorBg;
319 const char *mediumColorFg, *mediumColorBg;
320 const char *normalColorFg, *normalColorBg;
321 const char *selColorFg, *selColorBg;
322 const char *codeColorFg, *codeColorBg;
323} defaultPercentTreeColors = {
324 "red", "lightgray",
325 "green", "lightgray",
326 "black", "lightgray",
327 "lightgray", "magenta",
328 "blue", "lightgray",
329};
330
331void ui_browser__init(void)
332{
333 struct newtPercentTreeColors *c = &defaultPercentTreeColors;
334
335 sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
336 sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
337 sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
338 sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
339 sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
340}