Skip to content

Commit 4a19b75

Browse files
Add vim bindings to TerminalMenus (#37940)
* `k` to move up * `j` to move down * `<space>` as an alternative to `<enter>`
1 parent 2ba139c commit 4a19b75

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

stdlib/REPL/src/TerminalMenus/AbstractMenu.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ function request(term::REPL.Terminals.TTYTerminal, m::AbstractMenu; cursor::Unio
203203
lastoption = numoptions(m)
204204
c = readkey(term.in_stream)
205205

206-
if c == Int(ARROW_UP)
206+
if c == Int(ARROW_UP) || c == Int('k')
207207
cursor[] = move_up!(m, cursor[], lastoption)
208-
elseif c == Int(ARROW_DOWN)
208+
elseif c == Int(ARROW_DOWN) || c == Int('j')
209209
cursor[] = move_down!(m, cursor[], lastoption)
210210
elseif c == Int(PAGE_UP)
211211
cursor[] = page_up!(m, cursor[], lastoption)
@@ -217,7 +217,7 @@ function request(term::REPL.Terminals.TTYTerminal, m::AbstractMenu; cursor::Unio
217217
elseif c == Int(END_KEY)
218218
cursor[] = lastoption
219219
m.pageoffset = lastoption - m.pagesize
220-
elseif c == 13 # <enter>
220+
elseif c == 13 || c == Int(' ') # <enter> or <space>
221221
# will break if pick returns true
222222
pick(m, cursor[]) && break
223223
elseif c == UInt32('q')

stdlib/REPL/test/TerminalMenus/runtests.jl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,22 @@ using Test
66

77
function simulate_input(expected, menu::TerminalMenus.AbstractMenu, keys...;
88
kwargs...)
9-
keydict = Dict(:up => "\e[A",
10-
:down => "\e[B",
11-
:enter => "\r")
9+
keydict = Dict(:up => "\e[A",
10+
:down => "\e[B",
11+
:enter => "\r")
12+
vimdict = Dict(:up => "k",
13+
:down => "j",
14+
:enter => " ")
15+
errs = []
16+
got = _simulate_input(keydict, deepcopy(menu), keys...; kwargs...)
17+
got == expected || push!(errs, :arrows => got)
18+
got = _simulate_input(vimdict, menu, keys...; kwargs...)
19+
got == expected || push!(errs, :vim => got)
20+
isempty(errs) || return errs
21+
end
1222

23+
function _simulate_input(keydict, menu::TerminalMenus.AbstractMenu, keys...;
24+
kwargs...)
1325
for key in keys
1426
if isa(key, Symbol)
1527
write(stdin.buffer, keydict[key])
@@ -18,7 +30,7 @@ function simulate_input(expected, menu::TerminalMenus.AbstractMenu, keys...;
1830
end
1931
end
2032

21-
request(menu; suppress_output=true, kwargs...) == expected
33+
request(menu; suppress_output=true, kwargs...)
2234
end
2335

2436
include("radio_menu.jl")

0 commit comments

Comments
 (0)