3 #include <linux/compiler.h>
4 #include <linux/list.h>
5 #include <linux/rbtree.h>
7 #include <sys/ttydefaults.h>
14 newtComponent newt_form__new(void);
16 static int ui_browser__percent_color(double percent, bool current)
19 return HE_COLORSET_SELECTED;
20 if (percent >= MIN_RED)
21 return HE_COLORSET_TOP;
22 if (percent >= MIN_GREEN)
23 return HE_COLORSET_MEDIUM;
24 return HE_COLORSET_NORMAL;
27 void ui_browser__set_color(struct ui_browser *self __used, int color)
29 SLsmg_set_color(color);
32 void ui_browser__set_percent_color(struct ui_browser *self,
33 double percent, bool current)
35 int color = ui_browser__percent_color(percent, current);
36 ui_browser__set_color(self, color);
39 void ui_browser__gotorc(struct ui_browser *self, int y, int x)
41 SLsmg_gotorc(self->y + y, self->x + x);
44 void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence)
46 struct list_head *head = self->entries;
47 struct list_head *pos;
74 void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
76 struct rb_root *root = self->entries;
104 unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
109 if (self->top == NULL)
110 self->top = rb_first(self->entries);
115 ui_browser__gotorc(self, row, 0);
116 self->write(self, nd, row);
117 if (++row == self->height)
125 bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
127 return self->top_idx + row == self->index;
130 void ui_browser__refresh_dimensions(struct ui_browser *self)
133 newtGetScreenSize(&cols, &rows);
135 if (self->width > cols - 4)
136 self->width = cols - 4;
137 self->height = rows - 5;
138 if (self->height > self->nr_entries)
139 self->height = self->nr_entries;
140 self->y = (rows - self->height) / 2;
141 self->x = (cols - self->width) / 2;
144 void ui_browser__reset_index(struct ui_browser *self)
146 self->index = self->top_idx = 0;
147 self->seek(self, 0, SEEK_SET);
150 int ui_browser__show(struct ui_browser *self, const char *title,
151 const char *helpline, ...)
155 if (self->form != NULL) {
156 newtFormDestroy(self->form);
159 ui_browser__refresh_dimensions(self);
160 newtCenteredWindow(self->width, self->height, title);
161 self->form = newt_form__new();
162 if (self->form == NULL)
165 self->sb = newtVerticalScrollbar(self->width, 0, self->height,
167 HE_COLORSET_SELECTED);
168 if (self->sb == NULL)
171 newtFormAddHotKey(self->form, NEWT_KEY_UP);
172 newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
173 newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
174 newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
175 newtFormAddHotKey(self->form, NEWT_KEY_HOME);
176 newtFormAddHotKey(self->form, NEWT_KEY_END);
177 newtFormAddHotKey(self->form, ' ');
178 newtFormAddComponent(self->form, self->sb);
180 va_start(ap, helpline);
181 ui_helpline__vpush(helpline, ap);
186 void ui_browser__hide(struct ui_browser *self)
188 newtFormDestroy(self->form);
194 int ui_browser__refresh(struct ui_browser *self)
198 newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
199 row = self->refresh(self);
200 ui_browser__set_color(self, HE_COLORSET_NORMAL);
201 SLsmg_fill_region(self->y + row, self->x,
202 self->height - row, self->width, ' ');
207 int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
209 if (ui_browser__refresh(self) < 0)
215 newtFormRun(self->form, es);
217 if (es->reason != NEWT_EXIT_HOTKEY)
219 if (is_exit_key(es->u.key))
223 if (self->index == self->nr_entries - 1)
226 if (self->index == self->top_idx + self->height) {
228 self->seek(self, +1, SEEK_CUR);
232 if (self->index == 0)
235 if (self->index < self->top_idx) {
237 self->seek(self, -1, SEEK_CUR);
242 if (self->top_idx + self->height > self->nr_entries - 1)
245 offset = self->height;
246 if (self->index + offset > self->nr_entries - 1)
247 offset = self->nr_entries - 1 - self->index;
248 self->index += offset;
249 self->top_idx += offset;
250 self->seek(self, +offset, SEEK_CUR);
253 if (self->top_idx == 0)
256 if (self->top_idx < self->height)
257 offset = self->top_idx;
259 offset = self->height;
261 self->index -= offset;
262 self->top_idx -= offset;
263 self->seek(self, -offset, SEEK_CUR);
266 ui_browser__reset_index(self);
269 offset = self->height - 1;
270 if (offset >= self->nr_entries)
271 offset = self->nr_entries - 1;
273 self->index = self->nr_entries - 1;
274 self->top_idx = self->index - offset;
275 self->seek(self, -offset, SEEK_END);
280 if (ui_browser__refresh(self) < 0)
286 unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
288 struct list_head *pos;
289 struct list_head *head = self->entries;
292 if (self->top == NULL || self->top == self->entries)
293 self->top = head->next;
297 list_for_each_from(pos, head) {
298 ui_browser__gotorc(self, row, 0);
299 self->write(self, pos, row);
300 if (++row == self->height)
307 static struct newtPercentTreeColors {
308 const char *topColorFg, *topColorBg;
309 const char *mediumColorFg, *mediumColorBg;
310 const char *normalColorFg, *normalColorBg;
311 const char *selColorFg, *selColorBg;
312 const char *codeColorFg, *codeColorBg;
313 } defaultPercentTreeColors = {
315 "green", "lightgray",
316 "black", "lightgray",
317 "lightgray", "magenta",
321 void ui_browser__init(void)
323 struct newtPercentTreeColors *c = &defaultPercentTreeColors;
325 sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
326 sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
327 sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
328 sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
329 sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);