Skip to content

Commit

Permalink
Add simple support for showing menues and use it for showing option menu
Browse files Browse the repository at this point in the history
The general goal is to have certain lesser used options not take up a
key for toggling.
  • Loading branch information
jonas committed Feb 22, 2009
1 parent 914710a commit 91e8041
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions manual.txt
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ z Stop all background loading. This can be useful if you use \
tig in a repository with a long history without limiting \
the revision log.
v Show version.
o Open option menu
'.' Toggle line numbers on/off.
D Toggle date display on/off.
A Toggle author display on/off.
Expand Down
92 changes: 92 additions & 0 deletions tig.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ typedef enum input_status (*input_handler)(void *data, char *buf, int c);
static char *prompt_input(const char *prompt, input_handler handler, void *data);
static bool prompt_yesno(const char *prompt);

struct menu_item {
int hotkey;
const char *text;
void *data;
};

static bool prompt_menu(const char *prompt, const struct menu_item *items, int *selected);

/*
* Allocation helpers ... Entering macro hell to never be seen again.
*/
Expand Down Expand Up @@ -831,6 +839,7 @@ run_io_load(const char **argv, const char *separators,
REQ_(FIND_PREV, "Find previous search match"), \
\
REQ_GROUP("Option manipulation") \
REQ_(OPTIONS, "Open option menu"), \
REQ_(TOGGLE_LINENO, "Toggle line numbers"), \
REQ_(TOGGLE_DATE, "Toggle date display"), \
REQ_(TOGGLE_AUTHOR, "Toggle author display"), \
Expand Down Expand Up @@ -1146,6 +1155,7 @@ static const struct keybinding default_keybindings[] = {
{ 'z', REQ_STOP_LOADING },
{ 'v', REQ_SHOW_VERSION },
{ 'r', REQ_SCREEN_REDRAW },
{ 'o', REQ_OPTIONS },
{ '.', REQ_TOGGLE_LINENO },
{ 'D', REQ_TOGGLE_DATE },
{ 'A', REQ_TOGGLE_AUTHOR },
Expand Down Expand Up @@ -2286,6 +2296,23 @@ toggle_view_option(bool *option, const char *help)
report("%sabling %s", *option ? "En" : "Dis", help);
}

static void
open_option_menu(void)
{
const struct menu_item menu[] = {
{ '.', "line numbers", &opt_line_number },
{ 'D', "date display", &opt_date },
{ 'A', "author display", &opt_author },
{ 'g', "revision graph display", &opt_rev_graph },
{ 'F', "reference display", &opt_show_refs },
{ 0 }
};
int selected = 0;

if (prompt_menu("Toggle option", menu, &selected))
toggle_view_option(menu[selected].data, menu[selected].text);
}

static void
maximize_view(struct view *view)
{
Expand Down Expand Up @@ -3276,6 +3303,10 @@ view_driver(struct view *view, enum request request)
maximize_view(view);
break;

case REQ_OPTIONS:
open_option_menu();
break;

case REQ_TOGGLE_LINENO:
toggle_view_option(&opt_line_number, "line numbers");
break;
Expand Down Expand Up @@ -6792,6 +6823,67 @@ read_prompt(const char *prompt)
return prompt_input(prompt, read_prompt_handler, NULL);
}

static bool prompt_menu(const char *prompt, const struct menu_item *items, int *selected)
{
enum input_status status = INPUT_OK;
int size = 0;

while (items[size].text)
size++;

while (status == INPUT_OK) {
const struct menu_item *item = &items[*selected];
int key;
int i;

mvwprintw(status_win, 0, 0, "%s (%d of %d) ",
prompt, *selected + 1, size);
if (item->hotkey)
wprintw(status_win, "[%c] ", (char) item->hotkey);
wprintw(status_win, "%s", item->text);
wclrtoeol(status_win);

key = get_input(COLS - 1);
switch (key) {
case KEY_RETURN:
case KEY_ENTER:
case '\n':
status = INPUT_STOP;
break;

case KEY_LEFT:
case KEY_UP:
*selected = *selected - 1;
if (*selected < 0)
*selected = size - 1;
break;

case KEY_RIGHT:
case KEY_DOWN:
*selected = (*selected + 1) % size;
break;

case KEY_ESC:
status = INPUT_CANCEL;
break;

default:
for (i = 0; items[i].text; i++)
if (items[i].hotkey == key) {
*selected = i;
status = INPUT_STOP;
break;
}
}
}

/* Clear the status window */
status_empty = FALSE;
report("");

return status != INPUT_CANCEL;
}

/*
* Repository properties
*/
Expand Down
1 change: 1 addition & 0 deletions tigrc.5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ screen-redraw Redraw the screen
screen-resize Resize the screen
show-version Show version information
stop-loading Stop all loading views
options Open options menu
toggle-lineno Toggle line numbers
toggle-date Toggle date display
toggle-author Toggle author display
Expand Down

0 comments on commit 91e8041

Please sign in to comment.