From 4a19b753804ed0b06fab9681309941862eaa7227 Mon Sep 17 00:00:00 2001 From: Jonas Schulze Date: Tue, 13 Apr 2021 10:31:47 +0200 Subject: [PATCH] Add vim bindings to TerminalMenus (#37940) * `k` to move up * `j` to move down * `` as an alternative to `` --- stdlib/REPL/src/TerminalMenus/AbstractMenu.jl | 6 +++--- stdlib/REPL/test/TerminalMenus/runtests.jl | 20 +++++++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/stdlib/REPL/src/TerminalMenus/AbstractMenu.jl b/stdlib/REPL/src/TerminalMenus/AbstractMenu.jl index f01df5c389324..ee5bd4d426795 100644 --- a/stdlib/REPL/src/TerminalMenus/AbstractMenu.jl +++ b/stdlib/REPL/src/TerminalMenus/AbstractMenu.jl @@ -203,9 +203,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) @@ -217,7 +217,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 # + elseif c == 13 || c == Int(' ') # or # will break if pick returns true pick(m, cursor[]) && break elseif c == UInt32('q') diff --git a/stdlib/REPL/test/TerminalMenus/runtests.jl b/stdlib/REPL/test/TerminalMenus/runtests.jl index 62a91cc0a1256..ac577dfd9ab27 100644 --- a/stdlib/REPL/test/TerminalMenus/runtests.jl +++ b/stdlib/REPL/test/TerminalMenus/runtests.jl @@ -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 => " ") + 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]) @@ -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")