Skip to content

Commit 9c66881

Browse files
committed
Add toggle options to help
1 parent b0f77a7 commit 9c66881

File tree

11 files changed

+92
-46
lines changed

11 files changed

+92
-46
lines changed

include/tig/line.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ struct ref;
8484
_(STAT_UNTRACKED, ""), \
8585
_(HELP_GROUP, ""), \
8686
_(HELP_ACTION, ""), \
87+
_(HELP_TOGGLE, ""), \
8788
_(DIFF_STAT, ""), \
8889
_(PALETTE_0, ""), \
8990
_(PALETTE_1, ""), \

src/help.c

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
#include "tig/argv.h"
1515
#include "tig/view.h"
1616
#include "tig/search.h"
17+
#include "tig/prompt.h"
1718
#include "tig/draw.h"
1819

20+
extern const struct menu_item toggle_menu_items[];
21+
22+
static struct keymap toggle_menu_keymap = {
23+
"toggle", NULL, 0, false
24+
};
25+
1926
/*
2027
* Help backend
2128
*/
@@ -26,8 +33,11 @@ struct help_state {
2633
};
2734

2835
struct help {
29-
struct keymap *keymap;
3036
enum request request;
37+
union {
38+
struct keymap *keymap;
39+
struct menu_item *menu;
40+
} item;
3141
union {
3242
const char *text;
3343
const struct request_info *req_info;
@@ -38,16 +48,30 @@ static bool
3848
help_draw(struct view *view, struct line *line, unsigned int lineno)
3949
{
4050
struct help *help = line->data;
41-
const struct keymap *keymap = help->keymap;
51+
const struct keymap *keymap = help->item.keymap;
4252
struct help_state *state = view->private;
4353

44-
if (line->type == LINE_SECTION) {
54+
if (line->type == LINE_SECTION && keymap) {
4555
draw_formatted(view, line->type, "[%c] %s bindings",
4656
keymap->hidden ? '+' : '-', keymap->name);
4757

4858
} else if (line->type == LINE_HELP_GROUP || !keymap) {
4959
draw_text(view, line->type, help->data.text);
5060

61+
} else if (line->type == LINE_HELP_TOGGLE) {
62+
struct menu_item *item = help->item.menu;
63+
char text_buf[40];
64+
65+
snprintf(text_buf, sizeof(text_buf), "%c", item->hotkey);
66+
if (draw_field(view, LINE_DEFAULT, text_buf, 28, ALIGN_RIGHT, false))
67+
return true;
68+
69+
if (draw_field(view, LINE_HELP_ACTION, (char*)item->data, 28, ALIGN_LEFT, false))
70+
return true;
71+
72+
snprintf(text_buf, sizeof(text_buf), "Toggle %s", item->text);
73+
draw_text(view, LINE_DEFAULT, text_buf);
74+
5175
} else if (help->request > REQ_RUN_REQUESTS) {
5276
struct run_request *req = get_run_request(help->request);
5377
const char *key = get_keys(keymap, help->request, true);
@@ -83,9 +107,9 @@ static bool
83107
help_grep(struct view *view, struct line *line)
84108
{
85109
struct help *help = line->data;
86-
const struct keymap *keymap = help->keymap;
110+
const struct keymap *keymap = help->item.keymap;
87111

88-
if (line->type == LINE_SECTION) {
112+
if (line->type == LINE_SECTION && keymap) {
89113
const char *text[] = { keymap->name, NULL };
90114

91115
return grep_text(view, text);
@@ -95,6 +119,10 @@ help_grep(struct view *view, struct line *line)
95119

96120
return grep_text(view, text);
97121

122+
} else if (line->type == LINE_HELP_TOGGLE) {
123+
const char *text[] = { help->item.menu->text, NULL };
124+
125+
return grep_text(view, text);
98126
} else if (help->request > REQ_RUN_REQUESTS) {
99127
struct run_request *req = get_run_request(help->request);
100128
const char *key = get_keys(keymap, help->request, true);
@@ -127,7 +155,7 @@ add_help_line(struct view *view, struct help **help_ptr, struct keymap *keymap,
127155

128156
if (!add_line_alloc(view, &help, type, 0, false))
129157
return false;
130-
help->keymap = keymap;
158+
help->item.keymap = keymap;
131159
if (help_ptr)
132160
*help_ptr = help;
133161
return true;
@@ -145,15 +173,15 @@ help_keys_visitor(void *data, const char *group, struct keymap *keymap,
145173

146174
if (iterator->keymap != keymap) {
147175
iterator->keymap = keymap;
148-
if (!add_help_line(iterator->view, &help, keymap, LINE_SECTION))
176+
if (!add_help_line(view, &help, keymap, LINE_SECTION))
149177
return false;
150178
}
151179

152180
if (keymap->hidden)
153181
return true;
154182

155183
if (group) {
156-
if (!add_help_line(iterator->view, &help, keymap, LINE_HELP_GROUP))
184+
if (!add_help_line(view, &help, keymap, LINE_HELP_GROUP))
157185
return false;
158186
help->data.text = group;
159187
}
@@ -177,6 +205,8 @@ help_open(struct view *view, enum open_flags flags)
177205
{
178206
struct help_request_iterator iterator = { view };
179207
struct help *help;
208+
const struct menu_item *menu = toggle_menu_items;
209+
int i;
180210

181211
reset_view(view);
182212

@@ -188,8 +218,28 @@ help_open(struct view *view, enum open_flags flags)
188218
return ERROR_OUT_OF_MEMORY;
189219
help->data.text = "";
190220

191-
return foreach_key(help_keys_visitor, &iterator, true)
192-
? SUCCESS : error("Failed to render key bindings");
221+
if (!foreach_key(help_keys_visitor, &iterator, true)) {
222+
error("Failed to render key bindings");
223+
return ERROR_OUT_OF_MEMORY;
224+
}
225+
226+
if (!add_help_line(view, &help, &toggle_menu_keymap, LINE_SECTION))
227+
return ERROR_OUT_OF_MEMORY;
228+
if (!toggle_menu_keymap.hidden) {
229+
if (!add_help_line(view, &help, NULL, LINE_HELP_GROUP))
230+
return ERROR_OUT_OF_MEMORY;
231+
help->data.text = "Toggle keys (enter: o <key>):";
232+
233+
i = 0;
234+
while (menu[i].data)
235+
{
236+
if (!add_help_line(view, &help, (struct keymap *)&menu[i], LINE_HELP_TOGGLE))
237+
return ERROR_OUT_OF_MEMORY;
238+
i++;
239+
}
240+
}
241+
242+
return SUCCESS;
193243
}
194244

195245
static enum request
@@ -200,7 +250,7 @@ help_request(struct view *view, enum request request, struct line *line)
200250
switch (request) {
201251
case REQ_ENTER:
202252
if (line->type == LINE_SECTION) {
203-
struct keymap *keymap = help->keymap;
253+
struct keymap *keymap = help->item.keymap;
204254

205255
keymap->hidden = !keymap->hidden;
206256
refresh_view(view);

src/tig.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,17 @@
8383
_('d', "untracked directory info", "status-show-untracked-dirs"), \
8484
_('|', "view split", "vertical-split"), \
8585

86+
87+
const struct menu_item toggle_menu_items[] = {
88+
#define DEFINE_TOGGLE_MENU(key, help, name) { key, help, name }
89+
TOGGLE_MENU_INFO(DEFINE_TOGGLE_MENU)
90+
{ 0 }
91+
};
92+
8693
static void
8794
toggle_option(struct view *view)
8895
{
89-
const struct menu_item menu[] = {
90-
#define DEFINE_TOGGLE_MENU(key, help, name) { key, help, name }
91-
TOGGLE_MENU_INFO(DEFINE_TOGGLE_MENU)
92-
{ 0 }
93-
};
96+
const struct menu_item *menu = toggle_menu_items;
9497
const char *toggle_argv[] = { "toggle", NULL, NULL };
9598
int i = 0;
9699

test/graph/20-tig-all-long-test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ assert_equals stdout <<EOF
250250
∙ │ prompt: convert sort actions to :toggle commands
251251
│ │ ∙ Restrict cursor navigation to actionable lines
252252
∙─│─╯ grep: fix detection of initial view in open_grep_view()
253-
∙ │ Fix toggle binding for show-filename
253+
∙ │ Fix toggle bindings for show-filename
254254
∙ │ Restore prompt position behavior
255255
∙ │ status: fix staging of untracked directories
256256
∙ │ help: show external command flags before the command

test/graph/20-tig-all-long-test.in

1 Byte
Binary file not shown.

test/help/all-keybindings-test.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Quick reference for tig keybindings:
2-
2+
33
[-] generic bindings
44
View switching
55
m view-main Show main view
@@ -126,4 +126,4 @@ External commands:
126126
[-] pager bindings
127127
Internal commands:
128128
@ :/^@@
129-
[help] - line 1 of 128 100%
129+
[help] - line 1 of 147 87%

test/help/default-test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ View manipulation
5151
R, <F5> refresh Reload and refresh view
5252
O maximize Maximize the current view
5353
q view-close Close the current view
54-
[help] - line 1 of 128 21%
54+
[help] - line 1 of 147 19%
5555
EOF
5656

5757
assert_equals 'help-search.screen' <<EOF
@@ -83,7 +83,7 @@ View manipulation
8383
R, <F5> refresh Reload and refresh view
8484
O maximize Maximize the current view
8585
q view-close Close the current view
86-
[help] - line 19 of 128 21%
86+
[help] - line 19 of 147 19%
8787
EOF
8888

8989
assert_equals 'help-collapsed.screen' <<EOF
@@ -115,5 +115,5 @@ External commands:
115115
External commands:
116116
C ?git checkout %(branch)
117117
! ?git branch -D %(branch)
118-
[help] - line 4 of 55 50%
118+
[help] - line 4 of 74 37%
119119
EOF

test/help/user-command-test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ Searching
5555
[-] main bindings
5656
Cursor navigation
5757
G move-last-line Move cursor to last line
58-
[help] - line 81 of 148 72%
58+
[help] - line 81 of 167 64%
5959
EOF

test/tigrc/env-vars-test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ Quick reference for tig keybindings:
3535
[-] generic bindings
3636
Misc
3737
: prompt Open the prompt
38-
[help] - line 1 of 5 100%
38+
[help] - line 1 of 24 20%
3939
EOF

test/tigrc/parse-test

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
. libtest.sh
66

7+
export LINES=20
8+
79
tigrc <<EOF
810
s # Unknown command
911
set # Missing name, equals and value
@@ -123,16 +125,6 @@ External commands:
123125
4 <quitting command
124126
5 +echoed command
125127
0 @?<+all modifiers
126-
127-
128-
129-
130-
131-
132-
133-
134-
135-
136-
137-
[help] - line 1 of 17 100%
128+
[-] toggle bindings
129+
[help] - line 1 of 36 50%
138130
EOF

0 commit comments

Comments
 (0)