Skip to content

Commit

Permalink
Add vim bindings to TerminalMenus
Browse files Browse the repository at this point in the history
* `k` to move up
* `j` to move down
* `<space>` as an alternative to `<enter>`
  • Loading branch information
jonas-schulze committed Oct 8, 2020
1 parent aa5e76a commit 9ec7568
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
6 changes: 3 additions & 3 deletions stdlib/REPL/src/TerminalMenus/AbstractMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ function request(term::REPL.Terminals.TTYTerminal, m::AbstractMenu; cursor::Int=
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 @@ -206,7 +206,7 @@ function request(term::REPL.Terminals.TTYTerminal, m::AbstractMenu; cursor::Int=
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
19 changes: 15 additions & 4 deletions stdlib/REPL/test/TerminalMenus/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ using REPL.TerminalMenus
using Test

function simulate_input(expected, menu::TerminalMenus.AbstractMenu, keys...)
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 => " ")
errs = []
got = _simulate_input(keydict, menu, keys...)
got == expected || push!(errs, :arrows => got)
got = _simulate_input(vimdict, menu, keys...)
got == expected || push!(errs, :vim => got)
isempty(errs) || return errs
end

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

request(menu; suppress_output=true) == expected
request(menu; suppress_output=true)
end

include("radio_menu.jl")
Expand Down

0 comments on commit 9ec7568

Please sign in to comment.