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;
52 pos = self->first_visible_entry;
69 self->first_visible_entry = pos;
72 void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
74 struct rb_root *root = self->entries;
82 nd = self->first_visible_entry;
99 self->first_visible_entry = nd;
102 unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
107 if (self->first_visible_entry == NULL)
108 self->first_visible_entry = rb_first(self->entries);
110 nd = self->first_visible_entry;
113 SLsmg_gotorc(self->top + row, self->left);
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->first_visible_entry_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->top = (rows - self->height) / 2;
139 self->left = (cols - self->width) / 2;
142 void ui_browser__reset_index(struct ui_browser *self)
144 self->index = self->first_visible_entry_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 newtFormAddComponent(self->form, self->sb);
176 int ui_browser__refresh(struct ui_browser *self)
180 newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
181 row = self->refresh(self);
182 SLsmg_set_color(HE_COLORSET_NORMAL);
183 SLsmg_fill_region(self->top + row, self->left,
184 self->height - row, self->width, ' ');
189 int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
191 if (ui_browser__refresh(self) < 0)
197 newtFormRun(self->form, es);
199 if (es->reason != NEWT_EXIT_HOTKEY)
201 if (is_exit_key(es->u.key))
205 if (self->index == self->nr_entries - 1)
208 if (self->index == self->first_visible_entry_idx + self->height) {
209 ++self->first_visible_entry_idx;
210 self->seek(self, +1, SEEK_CUR);
214 if (self->index == 0)
217 if (self->index < self->first_visible_entry_idx) {
218 --self->first_visible_entry_idx;
219 self->seek(self, -1, SEEK_CUR);
224 if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
227 offset = self->height;
228 if (self->index + offset > self->nr_entries - 1)
229 offset = self->nr_entries - 1 - self->index;
230 self->index += offset;
231 self->first_visible_entry_idx += offset;
232 self->seek(self, +offset, SEEK_CUR);
235 if (self->first_visible_entry_idx == 0)
238 if (self->first_visible_entry_idx < self->height)
239 offset = self->first_visible_entry_idx;
241 offset = self->height;
243 self->index -= offset;
244 self->first_visible_entry_idx -= offset;
245 self->seek(self, -offset, SEEK_CUR);
248 ui_browser__reset_index(self);
251 offset = self->height - 1;
252 if (offset >= self->nr_entries)
253 offset = self->nr_entries - 1;
255 self->index = self->nr_entries - 1;
256 self->first_visible_entry_idx = self->index - offset;
257 self->seek(self, -offset, SEEK_END);
262 if (ui_browser__refresh(self) < 0)
268 unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
270 struct list_head *pos;
271 struct list_head *head = self->entries;
274 if (self->first_visible_entry == NULL ||
275 self->first_visible_entry == self->entries)
276 self->first_visible_entry = head->next;
278 pos = self->first_visible_entry;
280 list_for_each_from(pos, head) {
281 SLsmg_gotorc(self->top + row, self->left);
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);