Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 1 | #include "../util.h" |
| 2 | #include "../../perf.h" |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 3 | #include "libslang.h" |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 4 | #include <newt.h> |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 5 | #include "ui.h" |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 6 | #include <linux/compiler.h> |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 7 | #include <linux/list.h> |
| 8 | #include <linux/rbtree.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <sys/ttydefaults.h> |
| 11 | #include "browser.h" |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 12 | #include "helpline.h" |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 13 | #include "../color.h" |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 14 | |
| 15 | int newtGetKey(void); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 16 | |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 17 | static int ui_browser__percent_color(double percent, bool current) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 18 | { |
| 19 | if (current) |
| 20 | return HE_COLORSET_SELECTED; |
| 21 | if (percent >= MIN_RED) |
| 22 | return HE_COLORSET_TOP; |
| 23 | if (percent >= MIN_GREEN) |
| 24 | return HE_COLORSET_MEDIUM; |
| 25 | return HE_COLORSET_NORMAL; |
| 26 | } |
| 27 | |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 28 | void ui_browser__set_color(struct ui_browser *self __used, int color) |
| 29 | { |
| 30 | SLsmg_set_color(color); |
| 31 | } |
| 32 | |
| 33 | void ui_browser__set_percent_color(struct ui_browser *self, |
| 34 | double percent, bool current) |
| 35 | { |
| 36 | int color = ui_browser__percent_color(percent, current); |
| 37 | ui_browser__set_color(self, color); |
| 38 | } |
| 39 | |
| 40 | void ui_browser__gotorc(struct ui_browser *self, int y, int x) |
| 41 | { |
| 42 | SLsmg_gotorc(self->y + y, self->x + x); |
| 43 | } |
| 44 | |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame^] | 45 | static struct list_head * |
| 46 | ui_browser__list_head_filter_entries(struct ui_browser *browser, |
| 47 | struct list_head *pos) |
| 48 | { |
| 49 | do { |
| 50 | if (!browser->filter || !browser->filter(browser, pos)) |
| 51 | return pos; |
| 52 | pos = pos->next; |
| 53 | } while (pos != browser->entries); |
| 54 | |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | static struct list_head * |
| 59 | ui_browser__list_head_filter_prev_entries(struct ui_browser *browser, |
| 60 | struct list_head *pos) |
| 61 | { |
| 62 | do { |
| 63 | if (!browser->filter || !browser->filter(browser, pos)) |
| 64 | return pos; |
| 65 | pos = pos->prev; |
| 66 | } while (pos != browser->entries); |
| 67 | |
| 68 | return NULL; |
| 69 | } |
| 70 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 71 | void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence) |
| 72 | { |
| 73 | struct list_head *head = self->entries; |
| 74 | struct list_head *pos; |
| 75 | |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame^] | 76 | if (self->nr_entries == 0) |
| 77 | return; |
| 78 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 79 | switch (whence) { |
| 80 | case SEEK_SET: |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame^] | 81 | pos = ui_browser__list_head_filter_entries(self, head->next); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 82 | break; |
| 83 | case SEEK_CUR: |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 84 | pos = self->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 85 | break; |
| 86 | case SEEK_END: |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame^] | 87 | pos = ui_browser__list_head_filter_prev_entries(self, head->prev); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 88 | break; |
| 89 | default: |
| 90 | return; |
| 91 | } |
| 92 | |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame^] | 93 | assert(pos != NULL); |
| 94 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 95 | if (offset > 0) { |
| 96 | while (offset-- != 0) |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame^] | 97 | pos = ui_browser__list_head_filter_entries(self, pos->next); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 98 | } else { |
| 99 | while (offset++ != 0) |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame^] | 100 | pos = ui_browser__list_head_filter_prev_entries(self, pos->prev); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 101 | } |
| 102 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 103 | self->top = pos; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence) |
| 107 | { |
| 108 | struct rb_root *root = self->entries; |
| 109 | struct rb_node *nd; |
| 110 | |
| 111 | switch (whence) { |
| 112 | case SEEK_SET: |
| 113 | nd = rb_first(root); |
| 114 | break; |
| 115 | case SEEK_CUR: |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 116 | nd = self->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 117 | break; |
| 118 | case SEEK_END: |
| 119 | nd = rb_last(root); |
| 120 | break; |
| 121 | default: |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | if (offset > 0) { |
| 126 | while (offset-- != 0) |
| 127 | nd = rb_next(nd); |
| 128 | } else { |
| 129 | while (offset++ != 0) |
| 130 | nd = rb_prev(nd); |
| 131 | } |
| 132 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 133 | self->top = nd; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self) |
| 137 | { |
| 138 | struct rb_node *nd; |
| 139 | int row = 0; |
| 140 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 141 | if (self->top == NULL) |
| 142 | self->top = rb_first(self->entries); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 143 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 144 | nd = self->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 145 | |
| 146 | while (nd != NULL) { |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 147 | ui_browser__gotorc(self, row, 0); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 148 | self->write(self, nd, row); |
| 149 | if (++row == self->height) |
| 150 | break; |
| 151 | nd = rb_next(nd); |
| 152 | } |
| 153 | |
| 154 | return row; |
| 155 | } |
| 156 | |
| 157 | bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row) |
| 158 | { |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 159 | return self->top_idx + row == self->index; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void ui_browser__refresh_dimensions(struct ui_browser *self) |
| 163 | { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 164 | self->width = SLtt_Screen_Cols - 1; |
| 165 | self->height = SLtt_Screen_Rows - 2; |
Arnaldo Carvalho de Melo | 469917c | 2010-09-13 10:25:04 -0300 | [diff] [blame] | 166 | self->y = 1; |
| 167 | self->x = 0; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void ui_browser__reset_index(struct ui_browser *self) |
| 171 | { |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 172 | self->index = self->top_idx = 0; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 173 | self->seek(self, 0, SEEK_SET); |
| 174 | } |
| 175 | |
Arnaldo Carvalho de Melo | b210b3b | 2011-02-25 11:33:31 -0300 | [diff] [blame] | 176 | void __ui_browser__show_title(struct ui_browser *browser, const char *title) |
| 177 | { |
| 178 | SLsmg_gotorc(0, 0); |
| 179 | ui_browser__set_color(browser, NEWT_COLORSET_ROOT); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 180 | slsmg_write_nstring(title, browser->width + 1); |
Arnaldo Carvalho de Melo | b210b3b | 2011-02-25 11:33:31 -0300 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void ui_browser__show_title(struct ui_browser *browser, const char *title) |
| 184 | { |
| 185 | pthread_mutex_lock(&ui__lock); |
| 186 | __ui_browser__show_title(browser, title); |
| 187 | pthread_mutex_unlock(&ui__lock); |
| 188 | } |
| 189 | |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 190 | int ui_browser__show(struct ui_browser *self, const char *title, |
| 191 | const char *helpline, ...) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 192 | { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 193 | int err; |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 194 | va_list ap; |
| 195 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 196 | ui_browser__refresh_dimensions(self); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 197 | |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 198 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | b210b3b | 2011-02-25 11:33:31 -0300 | [diff] [blame] | 199 | __ui_browser__show_title(self, title); |
Arnaldo Carvalho de Melo | 469917c | 2010-09-13 10:25:04 -0300 | [diff] [blame] | 200 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 201 | self->title = title; |
| 202 | free(self->helpline); |
| 203 | self->helpline = NULL; |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 204 | |
| 205 | va_start(ap, helpline); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 206 | err = vasprintf(&self->helpline, helpline, ap); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 207 | va_end(ap); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 208 | if (err > 0) |
| 209 | ui_helpline__push(self->helpline); |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 210 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 211 | return err ? 0 : -1; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 212 | } |
| 213 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 214 | void ui_browser__hide(struct ui_browser *browser __used) |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 215 | { |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 216 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 217 | ui_helpline__pop(); |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 218 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 219 | } |
| 220 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 221 | static void ui_browser__scrollbar_set(struct ui_browser *browser) |
| 222 | { |
| 223 | int height = browser->height, h = 0, pct = 0, |
| 224 | col = browser->width, |
| 225 | row = browser->y - 1; |
| 226 | |
| 227 | if (browser->nr_entries > 1) { |
| 228 | pct = ((browser->index * (browser->height - 1)) / |
| 229 | (browser->nr_entries - 1)); |
| 230 | } |
| 231 | |
| 232 | while (h < height) { |
| 233 | ui_browser__gotorc(browser, row++, col); |
| 234 | SLsmg_set_char_set(1); |
| 235 | SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_BOARD_CHAR); |
| 236 | SLsmg_set_char_set(0); |
| 237 | ++h; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static int __ui_browser__refresh(struct ui_browser *browser) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 242 | { |
| 243 | int row; |
| 244 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 245 | row = browser->refresh(browser); |
| 246 | ui_browser__set_color(browser, HE_COLORSET_NORMAL); |
| 247 | SLsmg_fill_region(browser->y + row, browser->x, |
| 248 | browser->height - row, browser->width, ' '); |
| 249 | ui_browser__scrollbar_set(browser); |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | int ui_browser__refresh(struct ui_browser *browser) |
| 255 | { |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 256 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 257 | __ui_browser__refresh(browser); |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 258 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Arnaldo Carvalho de Melo | 900e14a | 2011-10-11 16:15:39 -0300 | [diff] [blame] | 263 | /* |
| 264 | * Here we're updating nr_entries _after_ we started browsing, i.e. we have to |
| 265 | * forget about any reference to any entry in the underlying data structure, |
| 266 | * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser |
| 267 | * after an output_resort and hist decay. |
| 268 | */ |
| 269 | void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries) |
| 270 | { |
| 271 | off_t offset = nr_entries - browser->nr_entries; |
| 272 | |
| 273 | browser->nr_entries = nr_entries; |
| 274 | |
| 275 | if (offset < 0) { |
| 276 | if (browser->top_idx < (u64)-offset) |
| 277 | offset = -browser->top_idx; |
| 278 | |
| 279 | browser->index += offset; |
| 280 | browser->top_idx += offset; |
| 281 | } |
| 282 | |
Arnaldo Carvalho de Melo | 437cfe7 | 2011-10-14 09:31:53 -0300 | [diff] [blame] | 283 | browser->top = NULL; |
Arnaldo Carvalho de Melo | 900e14a | 2011-10-11 16:15:39 -0300 | [diff] [blame] | 284 | browser->seek(browser, browser->top_idx, SEEK_SET); |
| 285 | } |
| 286 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 287 | int ui_browser__run(struct ui_browser *self, int delay_secs) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 288 | { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 289 | int err, key; |
| 290 | struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL; |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 291 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 292 | pthread__unblock_sigwinch(); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 293 | |
| 294 | while (1) { |
| 295 | off_t offset; |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 296 | fd_set read_set; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 297 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 298 | pthread_mutex_lock(&ui__lock); |
| 299 | err = __ui_browser__refresh(self); |
| 300 | SLsmg_refresh(); |
| 301 | pthread_mutex_unlock(&ui__lock); |
| 302 | if (err < 0) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 303 | break; |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 304 | |
| 305 | FD_ZERO(&read_set); |
| 306 | FD_SET(0, &read_set); |
| 307 | |
| 308 | if (delay_secs) { |
| 309 | timeout.tv_sec = delay_secs; |
| 310 | timeout.tv_usec = 0; |
| 311 | } |
| 312 | |
| 313 | err = select(1, &read_set, NULL, NULL, ptimeout); |
| 314 | if (err > 0 && FD_ISSET(0, &read_set)) |
| 315 | key = newtGetKey(); |
| 316 | else if (err == 0) |
| 317 | break; |
| 318 | else { |
| 319 | pthread_mutex_lock(&ui__lock); |
| 320 | SLtt_get_screen_size(); |
| 321 | SLsmg_reinit_smg(); |
| 322 | pthread_mutex_unlock(&ui__lock); |
| 323 | ui_browser__refresh_dimensions(self); |
| 324 | __ui_browser__show_title(self, self->title); |
| 325 | ui_helpline__puts(self->helpline); |
| 326 | continue; |
| 327 | } |
| 328 | |
| 329 | switch (key) { |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 330 | case NEWT_KEY_DOWN: |
| 331 | if (self->index == self->nr_entries - 1) |
| 332 | break; |
| 333 | ++self->index; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 334 | if (self->index == self->top_idx + self->height) { |
| 335 | ++self->top_idx; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 336 | self->seek(self, +1, SEEK_CUR); |
| 337 | } |
| 338 | break; |
| 339 | case NEWT_KEY_UP: |
| 340 | if (self->index == 0) |
| 341 | break; |
| 342 | --self->index; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 343 | if (self->index < self->top_idx) { |
| 344 | --self->top_idx; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 345 | self->seek(self, -1, SEEK_CUR); |
| 346 | } |
| 347 | break; |
| 348 | case NEWT_KEY_PGDN: |
| 349 | case ' ': |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 350 | if (self->top_idx + self->height > self->nr_entries - 1) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 351 | break; |
| 352 | |
| 353 | offset = self->height; |
| 354 | if (self->index + offset > self->nr_entries - 1) |
| 355 | offset = self->nr_entries - 1 - self->index; |
| 356 | self->index += offset; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 357 | self->top_idx += offset; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 358 | self->seek(self, +offset, SEEK_CUR); |
| 359 | break; |
| 360 | case NEWT_KEY_PGUP: |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 361 | if (self->top_idx == 0) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 362 | break; |
| 363 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 364 | if (self->top_idx < self->height) |
| 365 | offset = self->top_idx; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 366 | else |
| 367 | offset = self->height; |
| 368 | |
| 369 | self->index -= offset; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 370 | self->top_idx -= offset; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 371 | self->seek(self, -offset, SEEK_CUR); |
| 372 | break; |
| 373 | case NEWT_KEY_HOME: |
| 374 | ui_browser__reset_index(self); |
| 375 | break; |
| 376 | case NEWT_KEY_END: |
| 377 | offset = self->height - 1; |
| 378 | if (offset >= self->nr_entries) |
| 379 | offset = self->nr_entries - 1; |
| 380 | |
| 381 | self->index = self->nr_entries - 1; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 382 | self->top_idx = self->index - offset; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 383 | self->seek(self, -offset, SEEK_END); |
| 384 | break; |
| 385 | default: |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 386 | return key; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 387 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 388 | } |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 389 | return -1; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | unsigned int ui_browser__list_head_refresh(struct ui_browser *self) |
| 393 | { |
| 394 | struct list_head *pos; |
| 395 | struct list_head *head = self->entries; |
| 396 | int row = 0; |
| 397 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 398 | if (self->top == NULL || self->top == self->entries) |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame^] | 399 | self->top = ui_browser__list_head_filter_entries(self, head->next); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 400 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 401 | pos = self->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 402 | |
| 403 | list_for_each_from(pos, head) { |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame^] | 404 | if (!self->filter || !self->filter(self, pos)) { |
| 405 | ui_browser__gotorc(self, row, 0); |
| 406 | self->write(self, pos, row); |
| 407 | if (++row == self->height) |
| 408 | break; |
| 409 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | return row; |
| 413 | } |
| 414 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 415 | static struct ui_browser__colors { |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 416 | const char *topColorFg, *topColorBg; |
| 417 | const char *mediumColorFg, *mediumColorBg; |
| 418 | const char *normalColorFg, *normalColorBg; |
| 419 | const char *selColorFg, *selColorBg; |
| 420 | const char *codeColorFg, *codeColorBg; |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 421 | } ui_browser__default_colors = { |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 422 | "red", "lightgray", |
| 423 | "green", "lightgray", |
| 424 | "black", "lightgray", |
| 425 | "lightgray", "magenta", |
| 426 | "blue", "lightgray", |
| 427 | }; |
| 428 | |
| 429 | void ui_browser__init(void) |
| 430 | { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 431 | struct ui_browser__colors *c = &ui_browser__default_colors; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 432 | |
| 433 | sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg); |
| 434 | sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg); |
| 435 | sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg); |
| 436 | sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg); |
| 437 | sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg); |
| 438 | } |