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; }
void ui_helpline__vpush(const char *fmt, va_list ap) { char *s; if (vasprintf(&s, fmt, ap) < 0) vfprintf(stderr, fmt, ap); else { ui_helpline__push(s); free(s); } }
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_helpline__puts(const char *msg) { ui_helpline__pop(); ui_helpline__push(msg); }
int ui__popup_menu(int argc, char * const argv[]) { struct ui_browser menu = { .entries = (void *)argv, .refresh = ui_browser__argv_refresh, .seek = ui_browser__argv_seek, .write = ui_browser__argv_write, .nr_entries = argc, }; return popup_menu__run(&menu); } int ui_browser__input_window(const char *title, const char *text, char *input, const char *exit_msg, int delay_secs) { int x, y, len, key; int max_len = 60, nr_lines = 0; static char buf[50]; const char *t; t = text; while (1) { const char *sep = strchr(t, '\n'); if (sep == NULL) sep = strchr(t, '\0'); len = sep - t; if (max_len < len) max_len = len; ++nr_lines; if (*sep == '\0') break; t = sep + 1; } pthread_mutex_lock(&ui__lock); max_len += 2; nr_lines += 8; y = SLtt_Screen_Rows / 2 - nr_lines / 2; x = SLtt_Screen_Cols / 2 - max_len / 2; SLsmg_set_color(0); SLsmg_draw_box(y, x++, nr_lines, max_len); if (title) { SLsmg_gotorc(y, x + 1); SLsmg_write_string((char *)title); } SLsmg_gotorc(++y, x); nr_lines -= 7; max_len -= 2; SLsmg_write_wrapped_string((unsigned char *)text, y, x, nr_lines, max_len, 1); y += nr_lines; len = 5; while (len--) { SLsmg_gotorc(y + len - 1, x); SLsmg_write_nstring((char *)" ", max_len); } SLsmg_draw_box(y++, x + 1, 3, max_len - 2); SLsmg_gotorc(y + 3, x); SLsmg_write_nstring((char *)exit_msg, max_len); SLsmg_refresh(); pthread_mutex_unlock(&ui__lock); x += 2; len = 0; key = ui__getch(delay_secs); while (key != K_TIMER && key != K_ENTER && key != K_ESC) { pthread_mutex_lock(&ui__lock); if (key == K_BKSPC) { if (len == 0) { pthread_mutex_unlock(&ui__lock); goto next_key; } SLsmg_gotorc(y, x + --len); SLsmg_write_char(' '); } else { buf[len] = key; SLsmg_gotorc(y, x + len++); SLsmg_write_char(key); } SLsmg_refresh(); pthread_mutex_unlock(&ui__lock); /* XXX more graceful overflow handling needed */ if (len == sizeof(buf) - 1) { ui_helpline__push("maximum size of symbol name reached!"); key = K_ENTER; break; } next_key: key = ui__getch(delay_secs); } buf[len] = '\0'; strncpy(input, buf, len+1); return key; }