int ui_browser__show(struct ui_browser *browser, const char *title, const char *helpline, ...) { int err; va_list ap; if (browser->refresh_dimensions == NULL) browser->refresh_dimensions = ui_browser__refresh_dimensions; browser->refresh_dimensions(browser); pthread_mutex_lock(&ui__lock); __ui_browser__show_title(browser, title); browser->title = title; zfree(&browser->helpline); va_start(ap, helpline); err = vasprintf(&browser->helpline, helpline, ap); va_end(ap); if (err > 0) ui_helpline__push(browser->helpline); pthread_mutex_unlock(&ui__lock); return err ? 0 : -1; }
int ui_browser__show(struct ui_browser *self, const char *title, const char *helpline, ...) { int err; va_list ap; ui_browser__refresh_dimensions(self); pthread_mutex_lock(&ui__lock); __ui_browser__show_title(self, title); self->title = title; free(self->helpline); self->helpline = NULL; va_start(ap, helpline); err = vasprintf(&self->helpline, helpline, ap); va_end(ap); if (err > 0) ui_helpline__push(self->helpline); pthread_mutex_unlock(&ui__lock); return err ? 0 : -1; }
void ui_browser__show_title(struct ui_browser *browser, const char *title) { pthread_mutex_lock(&ui__lock); __ui_browser__show_title(browser, title); pthread_mutex_unlock(&ui__lock); }
int ui_browser__run(struct ui_browser *browser, int delay_secs) { int err, key; while (1) { off_t offset; pthread_mutex_lock(&ui__lock); err = __ui_browser__refresh(browser); SLsmg_refresh(); pthread_mutex_unlock(&ui__lock); if (err < 0) break; key = ui__getch(delay_secs); if (key == K_RESIZE) { ui__refresh_dimensions(false); browser->refresh_dimensions(browser); __ui_browser__show_title(browser, browser->title); ui_helpline__puts(browser->helpline); continue; } if (browser->use_navkeypressed && !browser->navkeypressed) { if (key == K_DOWN || key == K_UP || key == K_PGDN || key == K_PGUP || key == K_HOME || key == K_END || key == ' ') { browser->navkeypressed = true; continue; } else return key; } switch (key) { case K_DOWN: if (browser->index == browser->nr_entries - 1) break; ++browser->index; if (browser->index == browser->top_idx + browser->rows) { ++browser->top_idx; browser->seek(browser, +1, SEEK_CUR); } break; case K_UP: if (browser->index == 0) break; --browser->index; if (browser->index < browser->top_idx) { --browser->top_idx; browser->seek(browser, -1, SEEK_CUR); } break; case K_PGDN: case ' ': if (browser->top_idx + browser->rows > browser->nr_entries - 1) break; offset = browser->rows; if (browser->index + offset > browser->nr_entries - 1) offset = browser->nr_entries - 1 - browser->index; browser->index += offset; browser->top_idx += offset; browser->seek(browser, +offset, SEEK_CUR); break; case K_PGUP: if (browser->top_idx == 0) break; if (browser->top_idx < browser->rows) offset = browser->top_idx; else offset = browser->rows; browser->index -= offset; browser->top_idx -= offset; browser->seek(browser, -offset, SEEK_CUR); break; case K_HOME: ui_browser__reset_index(browser); break; case K_END: offset = browser->rows - 1; if (offset >= browser->nr_entries) offset = browser->nr_entries - 1; browser->index = browser->nr_entries - 1; browser->top_idx = browser->index - offset; browser->seek(browser, -offset, SEEK_END); break; default: return key; } } return -1; }