diff options
author | Tiago Cunha <tcunha@gmx.com> | 2012-08-12 19:28:20 +0000 |
---|---|---|
committer | Tiago Cunha <tcunha@gmx.com> | 2012-08-12 19:28:20 +0000 |
commit | 200b0e5821d83de8c83fcc564fe7eda9253939e0 (patch) | |
tree | d1d71f2cc5c061f1f4b3675990a4ebffbe0d55ca /window-choose.c | |
parent | 30b801decf261b5e489b1067ee6f04bda40a3d17 (diff) |
Sync OpenBSD patchset 1156:
Instead of numbering choose mode items 0-9a-z and then nothing, number
them all and if there are more than 10 use a prompt when 0-9 is
pressed. From Thomas Adam.
Diffstat (limited to 'window-choose.c')
-rw-r--r-- | window-choose.c | 159 |
1 files changed, 109 insertions, 50 deletions
diff --git a/window-choose.c b/window-choose.c index f3857633..43b5a7b1 100644 --- a/window-choose.c +++ b/window-choose.c @@ -40,6 +40,11 @@ void window_choose_write_line( void window_choose_scroll_up(struct window_pane *); void window_choose_scroll_down(struct window_pane *); +enum window_choose_input_type { + WINDOW_CHOOSE_NORMAL = -1, + WINDOW_CHOOSE_GOTO_ITEM, +}; + const struct window_mode window_choose_mode = { window_choose_init, window_choose_free, @@ -55,27 +60,39 @@ struct window_choose_mode_data { struct mode_key_data mdata; ARRAY_DECL(, struct window_choose_mode_item) list; + int width; u_int top; u_int selected; + enum window_choose_input_type input_type; + const char *input_prompt; + char *input_str; void (*callbackfn)(struct window_choose_data *); void (*freefn)(struct window_choose_data *); }; -int window_choose_key_index(struct window_choose_mode_data *, u_int); -int window_choose_index_key(struct window_choose_mode_data *, int); +int window_choose_index_key(int); +void window_choose_prompt_input(enum window_choose_input_type, + const char *, struct window_pane *, int); void window_choose_add(struct window_pane *wp, struct window_choose_data *wcd) { struct window_choose_mode_data *data = wp->modedata; struct window_choose_mode_item *item; + int width; + char tmp[10]; ARRAY_EXPAND(&data->list, 1); item = &ARRAY_LAST(&data->list); item->name = format_expand(wcd->ft, wcd->ft_template); item->wcd = wcd; + item->pos = ARRAY_LENGTH(&data->list) - 1; + + width = snprintf (tmp, sizeof tmp, "%u", item->pos); + if (width > data->width) + data->width = width; } void @@ -107,6 +124,9 @@ window_choose_init(struct window_pane *wp) data->callbackfn = NULL; data->freefn = NULL; + data->input_type = WINDOW_CHOOSE_NORMAL; + data->input_str = xstrdup(""); + data->input_prompt = NULL; ARRAY_INIT(&data->list); data->top = 0; @@ -156,6 +176,7 @@ window_choose_free(struct window_pane *wp) free(item->name); } ARRAY_FREE(&data->list); + free(data->input_str); screen_free(&data->screen); free(data); @@ -190,6 +211,24 @@ window_choose_fire_callback( wp->mode = oldmode; } +void +window_choose_prompt_input(enum window_choose_input_type input_type, + const char *prompt, struct window_pane *wp, int key) +{ + struct window_choose_mode_data *data = wp->modedata; + size_t input_len; + + data->input_type = input_type; + data->input_prompt = prompt; + input_len = strlen(data->input_str) + 2; + + data->input_str = xrealloc(data->input_str, 1, input_len); + data->input_str[input_len - 2] = key; + data->input_str[input_len - 1] = '\0'; + + window_choose_redraw_screen(wp); +} + /* ARGSUSED */ void window_choose_key(struct window_pane *wp, unused struct session *sess, int key) @@ -198,7 +237,8 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key) struct screen *s = &data->screen; struct screen_write_ctx ctx; struct window_choose_mode_item *item; - u_int items; + size_t input_len; + u_int items, n; int idx; items = ARRAY_LENGTH(&data->list); @@ -209,9 +249,21 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key) window_pane_reset_mode(wp); break; case MODEKEYCHOICE_CHOOSE: - item = &ARRAY_ITEM(&data->list, data->selected); - window_choose_fire_callback(wp, item->wcd); - window_pane_reset_mode(wp); + switch (data->input_type) { + case WINDOW_CHOOSE_NORMAL: + item = &ARRAY_ITEM(&data->list, data->selected); + window_choose_fire_callback(wp, item->wcd); + window_pane_reset_mode(wp); + break; + case WINDOW_CHOOSE_GOTO_ITEM: + n = strtonum(data->input_str, 0, INT_MAX, NULL); + if (n > items - 1) + break; + item = &ARRAY_ITEM(&data->list, n); + window_choose_fire_callback(wp, item->wcd); + window_pane_reset_mode(wp); + break; + } break; case MODEKEYCHOICE_UP: if (items == 0) @@ -309,15 +361,36 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key) data->top = data->selected; window_choose_redraw_screen(wp); break; - default: - idx = window_choose_index_key(data, key); - if (idx < 0 || (u_int) idx >= ARRAY_LENGTH(&data->list)) + case MODEKEYCHOICE_BACKSPACE: + input_len = strlen(data->input_str); + if (input_len > 0) + data->input_str[input_len - 1] = '\0'; + window_choose_redraw_screen(wp); + break; + case MODEKEYCHOICE_STARTNUMBERPREFIX: + if (key < '0' && key > '9') break; - data->selected = idx; - item = &ARRAY_ITEM(&data->list, data->selected); - window_choose_fire_callback(wp, item->wcd); - window_pane_reset_mode(wp); + /* + * If there's less than ten items (0-9) then pressing a number + * will automatically select that item; otherwise, prompt for + * the item to go to. + */ + if (ARRAY_LENGTH(&data->list) - 1 <= 9) { + idx = window_choose_index_key(key); + if (idx < 0 || (u_int) idx >= ARRAY_LENGTH(&data->list)) + break; + data->selected = idx; + + item = &ARRAY_ITEM(&data->list, data->selected); + window_choose_fire_callback(wp, item->wcd); + window_pane_reset_mode(wp); + } else { + window_choose_prompt_input( + WINDOW_CHOOSE_GOTO_ITEM, "Goto item", wp, key); + } + break; + default: break; } } @@ -358,65 +431,51 @@ window_choose_write_line( struct options *oo = &wp->window->options; struct screen *s = &data->screen; struct grid_cell gc; - int utf8flag, key; + size_t last, xoff = 0; + char hdr[32]; + int utf8flag; if (data->callbackfn == NULL) fatalx("called before callback assigned"); + last = screen_size_y(s) - 1; utf8flag = options_get_number(&wp->window->options, "utf8"); memcpy(&gc, &grid_default_cell, sizeof gc); - if (data->selected == data->top + py) { - colour_set_fg(&gc, options_get_number(oo, "mode-fg")); - colour_set_bg(&gc, options_get_number(oo, "mode-bg")); - gc.attr |= options_get_number(oo, "mode-attr"); - } + if (data->selected == data->top + py) + window_mode_attrs(&gc, oo); screen_write_cursormove(ctx, 0, py); if (data->top + py < ARRAY_LENGTH(&data->list)) { item = &ARRAY_ITEM(&data->list, data->top + py); - key = window_choose_key_index(data, data->top + py); - if (key != -1) { - screen_write_nputs(ctx, screen_size_x(s) - 1, - &gc, utf8flag, "(%c) %s", key, item->name); - } else { - screen_write_nputs(ctx, screen_size_x(s) - 1, - &gc, utf8flag, " %s", item->name); - } + screen_write_nputs(ctx, screen_size_x(s) - 1, + &gc, utf8flag, "(%*d) %s", data->width, + item->pos, item->name); } while (s->cx < screen_size_x(s)) screen_write_putc(ctx, &gc, ' '); -} -int -window_choose_key_index(struct window_choose_mode_data *data, u_int idx) -{ - static const char keys[] = "0123456789abcdefghijklmnopqrstuvwxyz"; - const char *ptr; - int mkey; + if (data->input_type != WINDOW_CHOOSE_NORMAL) { + window_mode_attrs(&gc, oo); - for (ptr = keys; *ptr != '\0'; ptr++) { - mkey = mode_key_lookup(&data->mdata, *ptr); - if (mkey != MODEKEY_NONE && mkey != MODEKEY_OTHER) - continue; - if (idx-- == 0) - return (*ptr); + xoff = xsnprintf(hdr, sizeof hdr, + "%s: %s", data->input_prompt, data->input_str); + screen_write_cursormove(ctx, 0, last); + screen_write_puts(ctx, &gc, "%s", hdr); + screen_write_cursormove(ctx, xoff, py); + memcpy(&gc, &grid_default_cell, sizeof gc); } - return (-1); + } int -window_choose_index_key(struct window_choose_mode_data *data, int key) +window_choose_index_key(int key) { - static const char keys[] = "0123456789abcdefghijklmnopqrstuvwxyz"; + static const char keys[] = "0123456789"; const char *ptr; - int mkey; u_int idx = 0; for (ptr = keys; *ptr != '\0'; ptr++) { - mkey = mode_key_lookup(&data->mdata, *ptr); - if (mkey != MODEKEY_NONE && mkey != MODEKEY_OTHER) - continue; if (key == *ptr) return (idx); idx++; @@ -514,7 +573,7 @@ window_choose_ctx(struct window_choose_data *cdata) struct window_choose_data * window_choose_add_session(struct window_pane *wp, struct cmd_ctx *ctx, - struct session *s, const char *template, char *action, u_int idx) + struct session *s, const char *template, char *action, u_int idx) { struct window_choose_data *wcd; @@ -535,8 +594,8 @@ window_choose_add_session(struct window_pane *wp, struct cmd_ctx *ctx, struct window_choose_data * window_choose_add_window(struct window_pane *wp, struct cmd_ctx *ctx, - struct session *s, struct winlink *wl, const char *template, - char *action, u_int idx) + struct session *s, struct winlink *wl, const char *template, + char *action, u_int idx) { struct window_choose_data *wcd; char *action_data; |