Skip to content

Commit 9fb7aa3

Browse files
committed
Add expand/collapse all options to help
1 parent d783032 commit 9fb7aa3

File tree

7 files changed

+61
-23
lines changed

7 files changed

+61
-23
lines changed

src/help.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919

2020
extern const struct menu_item toggle_menu_items[];
2121

22+
static const char collapse_expand_names[2][14] = {
23+
"Collapse all", "Expand all"
24+
};
25+
static struct keymap collapse_expand_keymap = {
26+
collapse_expand_names[0], NULL, 0, false
27+
};
2228
static struct keymap toggle_menu_keymap = {
2329
"toggle", NULL, 0, false
2430
};
@@ -52,8 +58,9 @@ help_draw(struct view *view, struct line *line, unsigned int lineno)
5258
struct help_state *state = view->private;
5359

5460
if (line->type == LINE_SECTION && keymap) {
55-
draw_formatted(view, line->type, "[%c] %s bindings",
56-
keymap->hidden ? '+' : '-', keymap->name);
61+
draw_formatted(view, line->type, "[%c] %s %s",
62+
keymap->hidden ? '+' : '-', keymap->name,
63+
(keymap == &collapse_expand_keymap) ? "sections" : "bindings" );
5764

5865
} else if (line->type == LINE_HELP_GROUP || !keymap) {
5966
draw_text(view, line->type, help->data.text);
@@ -200,6 +207,21 @@ help_keys_visitor(void *data, const char *group, struct keymap *keymap,
200207
return true;
201208
}
202209

210+
static bool
211+
help_collapse_expand_keys_visitor(void *data, const char *group, struct keymap *keymap,
212+
enum request request, const char *key,
213+
const struct request_info *req_info, const struct run_request *run_req)
214+
{
215+
struct help_request_iterator *iterator = data;
216+
217+
if (iterator->keymap != keymap) {
218+
iterator->keymap = keymap;
219+
keymap->hidden = collapse_expand_keymap.hidden;
220+
}
221+
222+
return true;
223+
}
224+
203225
static enum status_code
204226
help_open(struct view *view, enum open_flags flags)
205227
{
@@ -214,6 +236,9 @@ help_open(struct view *view, enum open_flags flags)
214236
return ERROR_OUT_OF_MEMORY;
215237
help->data.text = "Quick reference for tig keybindings:";
216238

239+
if (!add_help_line(view, &help, &collapse_expand_keymap, LINE_SECTION))
240+
return ERROR_OUT_OF_MEMORY;
241+
217242
if (!add_help_line(view, &help, NULL, LINE_DEFAULT))
218243
return ERROR_OUT_OF_MEMORY;
219244
help->data.text = "";
@@ -253,6 +278,13 @@ help_request(struct view *view, enum request request, struct line *line)
253278
struct keymap *keymap = help->item.keymap;
254279

255280
keymap->hidden = !keymap->hidden;
281+
if (keymap == &collapse_expand_keymap) {
282+
struct help_request_iterator iterator = { view };
283+
284+
collapse_expand_keymap.name = collapse_expand_names[keymap->hidden];
285+
foreach_key(help_collapse_expand_keys_visitor, &iterator, true);
286+
toggle_menu_keymap.hidden = keymap->hidden;
287+
}
256288
refresh_view(view);
257289
}
258290
return REQ_NONE;

test/help/all-keybindings-test.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Quick reference for tig keybindings:
2+
[-] Collapse all sections
23

34
[-] generic bindings
45
View switching
@@ -125,5 +126,4 @@ External commands:
125126
! ?git stash drop %(stash)
126127
[-] pager bindings
127128
Internal commands:
128-
@ :/^@@
129-
[help] - line 1 of 147 87%
129+
[help] - line 1 of 148 86%

test/help/default-test

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ steps '
99
:/View manipulation
1010
:save-display help-search.screen
1111
12-
:3
13-
:enter
1412
:4
1513
:enter
14+
:5
15+
:enter
1616
:save-display help-collapsed.screen
1717
'
1818

@@ -24,6 +24,7 @@ test_tig
2424

2525
assert_equals 'help-default.screen' <<EOF
2626
Quick reference for tig keybindings:
27+
[-] Collapse all sections
2728
2829
[-] generic bindings
2930
View switching
@@ -50,12 +51,12 @@ View manipulation
5051
<Tab> view-next Move focus to the next view
5152
R, <F5> refresh Reload and refresh view
5253
O maximize Maximize the current view
53-
q view-close Close the current view
54-
[help] - line 1 of 147 19%
54+
[help] - line 1 of 148 18%
5555
EOF
5656

5757
assert_equals 'help-search.screen' <<EOF
5858
Quick reference for tig keybindings:
59+
[-] Collapse all sections
5960
6061
[-] generic bindings
6162
View switching
@@ -82,12 +83,12 @@ View manipulation
8283
<Tab> view-next Move focus to the next view
8384
R, <F5> refresh Reload and refresh view
8485
O maximize Maximize the current view
85-
q view-close Close the current view
86-
[help] - line 19 of 147 19%
86+
[help] - line 20 of 148 18%
8787
EOF
8888

8989
assert_equals 'help-collapsed.screen' <<EOF
9090
Quick reference for tig keybindings:
91+
[-] Collapse all sections
9192
9293
[+] generic bindings
9394
[+] search bindings
@@ -114,6 +115,5 @@ External commands:
114115
[-] refs bindings
115116
External commands:
116117
C ?git checkout %(branch)
117-
! ?git branch -D %(branch)
118-
[help] - line 4 of 74 37%
118+
[help] - line 5 of 75 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 167 64%
58+
[help] - line 82 of 168 64%
5959
EOF

test/tigrc/env-vars-test

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
. libtest.sh
44

5-
LINES=7
5+
LINES=8
66

77
# Disable built-in configuration
88
export TIGRC_SYSTEM=does-not-exist
@@ -31,9 +31,10 @@ EOF
3131

3232
assert_equals help.screen <<EOF
3333
Quick reference for tig keybindings:
34+
[-] Collapse all sections
3435
3536
[-] generic bindings
3637
Misc
3738
: prompt Open the prompt
38-
[help] - line 1 of 24 20%
39+
[help] - line 1 of 25 24%
3940
EOF

test/tigrc/parse-test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ EOF
109109

110110
assert_equals help.screen <<EOF
111111
Quick reference for tig keybindings:
112+
[-] Collapse all sections
112113
113114
[-] generic bindings
114115
View switching
@@ -125,6 +126,5 @@ External commands:
125126
4 <quitting command
126127
5 +echoed command
127128
0 @?<+all modifiers
128-
[-] toggle bindings
129-
[help] - line 1 of 36 50%
129+
[help] - line 1 of 37 48%
130130
EOF

test/tigrc/source-test

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
. libtest.sh
44

5-
export LINES=9
5+
export LINES=10
66

77
# Disable built-in configuration
88
export TIGRC_SYSTEM=does-not-exist
@@ -22,13 +22,14 @@ test_case source-file-plain \
2222
" \
2323
<<EOF
2424
Quick reference for tig keybindings:
25+
[-] Collapse all sections
2526
2627
[-] generic bindings
2728
View manipulation
2829
q quit Close all views and quit
2930
Misc
3031
: prompt Open the prompt
31-
[help] - line 1 of 26 26%
32+
[help] - line 1 of 27 29%
3233
EOF
3334

3435
test_case source-file-quietly \
@@ -42,13 +43,14 @@ test_case source-file-quietly \
4243
" \
4344
<<EOF
4445
Quick reference for tig keybindings:
46+
[-] Collapse all sections
4547
4648
[-] generic bindings
4749
View manipulation
4850
q quit Close all views and quit
4951
Misc
5052
: prompt Open the prompt
51-
[help] - line 1 of 26 26%
53+
[help] - line 1 of 27 29%
5254
EOF
5355

5456
test_case source-nonexistent-file \
@@ -64,13 +66,14 @@ test_case source-nonexistent-file \
6466
tig warning: Errors while loading HOME/.tigrc." \
6567
<<EOF
6668
Quick reference for tig keybindings:
69+
[-] Collapse all sections
6770
6871
[-] generic bindings
6972
Misc
7073
: prompt Open the prompt
7174
[-] toggle bindings
7275
Toggle keys (enter: o <key>):
73-
[help] - line 1 of 24 29%
76+
[help] - line 1 of 25 32%
7477
EOF
7578

7679
test_case source-nonexistent-file-quietly \
@@ -84,13 +87,14 @@ test_case source-nonexistent-file-quietly \
8487
" \
8588
<<EOF
8689
Quick reference for tig keybindings:
90+
[-] Collapse all sections
8791
8892
[-] generic bindings
8993
Misc
9094
: prompt Open the prompt
9195
[-] toggle bindings
9296
Toggle keys (enter: o <key>):
93-
[help] - line 1 of 24 29%
97+
[help] - line 1 of 25 32%
9498
EOF
9599

96100
test_case source-tilde-path \
@@ -104,13 +108,14 @@ test_case source-tilde-path \
104108
" \
105109
<<EOF
106110
Quick reference for tig keybindings:
111+
[-] Collapse all sections
107112
108113
[-] generic bindings
109114
View manipulation
110115
q quit Close all views and quit
111116
Misc
112117
: prompt Open the prompt
113-
[help] - line 1 of 26 26%
118+
[help] - line 1 of 27 29%
114119
EOF
115120

116121
run_test_cases

0 commit comments

Comments
 (0)