5 * slang versions <= 2.0.6 have a "#if HAVE_LONG_LONG" that breaks
6 * the build if it isn't defined. Use the equivalent one that glibc
10 #ifndef HAVE_LONG_LONG
11 #define HAVE_LONG_LONG __GLIBC_HAVE_LONG_LONG
14 #include <linux/list.h>
15 #include <linux/rbtree.h>
17 #include <sys/ttydefaults.h>
22 #if SLANG_VERSION < 20104
23 #define sltt_set_color(obj, name, fg, bg) \
24 SLtt_set_color(obj,(char *)name, (char *)fg, (char *)bg)
26 #define sltt_set_color SLtt_set_color
29 newtComponent newt_form__new(void);
31 int ui_browser__percent_color(double percent, bool current)
34 return HE_COLORSET_SELECTED;
35 if (percent >= MIN_RED)
36 return HE_COLORSET_TOP;
37 if (percent >= MIN_GREEN)
38 return HE_COLORSET_MEDIUM;
39 return HE_COLORSET_NORMAL;
42 void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence)
44 struct list_head *head = self->entries;
45 struct list_head *pos;
72 void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
74 struct rb_root *root = self->entries;
102 unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
107 if (self->top == NULL)
108 self->top = rb_first(self->entries);
113 SLsmg_gotorc(self->y + row, self->x);
114 self->write(self, nd, row);
115 if (++row == self->height)
123 bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
125 return self->top_idx + row == self->index;
128 void ui_browser__refresh_dimensions(struct ui_browser *self)
131 newtGetScreenSize(&cols, &rows);
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;
138 self->y = (rows - self->height) / 2;
139 self->x = (cols - self->width) / 2;
142 void ui_browser__reset_index(struct ui_browser *self)
144 self->index = self->top_idx = 0;
145 self->seek(self, 0, SEEK_SET);
148 int ui_browser__show(struct ui_browser *self, const char *title)
150 if (self->form != NULL) {
151 newtFormDestroy(self->form);
154 ui_browser__refresh_dimensions(self);
155 newtCenteredWindow(self->width, self->height, title);
156 self->form = newt_form__new();
157 if (self->form == NULL)
160 self->sb = newtVerticalScrollbar(self->width, 0, self->height,
162 HE_COLORSET_SELECTED);
163 if (self->sb == NULL)
166 newtFormAddHotKey(self->form, NEWT_KEY_UP);
167 newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
168 newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
169 newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
170 newtFormAddHotKey(self->form, NEWT_KEY_HOME);
171 newtFormAddHotKey(self->form, NEWT_KEY_END);
172 newtFormAddHotKey(self->form, ' ');
173 newtFormAddComponent(self->form, self->sb);
177 int ui_browser__refresh(struct ui_browser *self)
181 newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
182 row = self->refresh(self);
183 SLsmg_set_color(HE_COLORSET_NORMAL);
184 SLsmg_fill_region(self->y + row, self->x,
185 self->height - row, self->width, ' ');
190 int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
192 if (ui_browser__refresh(self) < 0)
198 newtFormRun(self->form, es);
200 if (es->reason != NEWT_EXIT_HOTKEY)
202 if (is_exit_key(es->u.key))
206 if (self->index == self->nr_entries - 1)
209 if (self->index == self->top_idx + self->height) {
211 self->seek(self, +1, SEEK_CUR);
215 if (self->index == 0)
218 if (self->index < self->top_idx) {
220 self->seek(self, -1, SEEK_CUR);
225 if (self->top_idx + self->height > self->nr_entries - 1)
228 offset = self->height;
229 if (self->index + offset > self->nr_entries - 1)
230 offset = self->nr_entries - 1 - self->index;
231 self->index += offset;
232 self->top_idx += offset;
233 self->seek(self, +offset, SEEK_CUR);
236 if (self->top_idx == 0)
239 if (self->top_idx < self->height)
240 offset = self->top_idx;
242 offset = self->height;
244 self->index -= offset;
245 self->top_idx -= offset;
246 self->seek(self, -offset, SEEK_CUR);
249 ui_browser__reset_index(self);
252 offset = self->height - 1;
253 if (offset >= self->nr_entries)
254 offset = self->nr_entries - 1;
256 self->index = self->nr_entries - 1;
257 self->top_idx = self->index - offset;
258 self->seek(self, -offset, SEEK_END);
263 if (ui_browser__refresh(self) < 0)
269 unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
271 struct list_head *pos;
272 struct list_head *head = self->entries;
275 if (self->top == NULL || self->top == self->entries)
276 self->top = head->next;
280 list_for_each_from(pos, head) {
281 SLsmg_gotorc(self->y + row, self->x);
282 self->write(self, pos, row);
283 if (++row == self->height)
290 static struct newtPercentTreeColors {
291 const char *topColorFg, *topColorBg;
292 const char *mediumColorFg, *mediumColorBg;
293 const char *normalColorFg, *normalColorBg;
294 const char *selColorFg, *selColorBg;
295 const char *codeColorFg, *codeColorBg;
296 } defaultPercentTreeColors = {
298 "green", "lightgray",
299 "black", "lightgray",
300 "lightgray", "magenta",
304 void ui_browser__init(void)
306 struct newtPercentTreeColors *c = &defaultPercentTreeColors;
308 sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
309 sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
310 sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
311 sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
312 sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);