Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vim bindings to TerminalMenus #37940

Merged
merged 1 commit into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions stdlib/REPL/src/TerminalMenus/AbstractMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ function request(term::REPL.Terminals.TTYTerminal, m::AbstractMenu; cursor::Unio
lastoption = numoptions(m)
c = readkey(term.in_stream)

if c == Int(ARROW_UP)
if c == Int(ARROW_UP) || c == Int('k')
cursor[] = move_up!(m, cursor[], lastoption)
elseif c == Int(ARROW_DOWN)
elseif c == Int(ARROW_DOWN) || c == Int('j')
cursor[] = move_down!(m, cursor[], lastoption)
elseif c == Int(PAGE_UP)
cursor[] = page_up!(m, cursor[], lastoption)
Expand All @@ -211,7 +211,7 @@ function request(term::REPL.Terminals.TTYTerminal, m::AbstractMenu; cursor::Unio
elseif c == Int(END_KEY)
cursor[] = lastoption
m.pageoffset = lastoption - m.pagesize
elseif c == 13 # <enter>
elseif c == 13 || c == Int(' ') # <enter> or <space>
# will break if pick returns true
pick(m, cursor[]) && break
elseif c == UInt32('q')
Expand Down
20 changes: 16 additions & 4 deletions stdlib/REPL/test/TerminalMenus/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@ using Test

function simulate_input(expected, menu::TerminalMenus.AbstractMenu, keys...;
kwargs...)
keydict = Dict(:up => "\e[A",
:down => "\e[B",
:enter => "\r")
keydict = Dict(:up => "\e[A",
:down => "\e[B",
:enter => "\r")
vimdict = Dict(:up => "k",
:down => "j",
:enter => " ")
jonas-schulze marked this conversation as resolved.
Show resolved Hide resolved
errs = []
got = _simulate_input(keydict, deepcopy(menu), keys...; kwargs...)
got == expected || push!(errs, :arrows => got)
got = _simulate_input(vimdict, menu, keys...; kwargs...)
got == expected || push!(errs, :vim => got)
isempty(errs) || return errs
end

function _simulate_input(keydict, menu::TerminalMenus.AbstractMenu, keys...;
kwargs...)
for key in keys
if isa(key, Symbol)
write(stdin.buffer, keydict[key])
Expand All @@ -18,7 +30,7 @@ function simulate_input(expected, menu::TerminalMenus.AbstractMenu, keys...;
end
end

request(menu; suppress_output=true, kwargs...) == expected
request(menu; suppress_output=true, kwargs...)
end

include("radio_menu.jl")
Expand Down