Skip to content

Commit

Permalink
REPL.TerminalMenus: Allow keybindings in RadioMenu (#41576)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Jul 15, 2021
1 parent e6aca89 commit 02c1cf3
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions stdlib/REPL/src/TerminalMenus/RadioMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Your favorite fruit is blueberry!
"""
mutable struct RadioMenu{C} <: _ConfiguredMenu{C}
options::Array{String,1}
keybindings::Vector{Char}
pagesize::Int
pageoffset::Int
selected::Int
Expand All @@ -43,8 +44,9 @@ user.
Any additional keyword arguments will be passed to [`TerminalMenus.Config`](@ref).
"""
function RadioMenu(options::Array{String,1}; pagesize::Int=10, warn::Bool=true, kwargs...)
function RadioMenu(options::Array{String,1}; pagesize::Int=10, warn::Bool=true, keybindings::Vector{Char}=Char[], kwargs...)
length(options) < 1 && error("RadioMenu must have at least one option")
length(keybindings) in [0, length(options)] || error("RadioMenu must have either no keybindings, or one per option")

# if pagesize is -1, use automatic paging
pagesize = pagesize == -1 ? length(options) : pagesize
Expand All @@ -57,10 +59,10 @@ function RadioMenu(options::Array{String,1}; pagesize::Int=10, warn::Bool=true,
selected = -1 # none

if !isempty(kwargs)
RadioMenu(options, pagesize, pageoffset, selected, Config(; kwargs...))
RadioMenu(options, keybindings, pagesize, pageoffset, selected, Config(; kwargs...))
else
warn && Base.depwarn("Legacy `RadioMenu` interface is deprecated, set a configuration option such as `RadioMenu(options; charset=:ascii)` to trigger the new interface.", :RadioMenu)
RadioMenu(options, pagesize, pageoffset, selected, CONFIG)
RadioMenu(options, keybindings, pagesize, pageoffset, selected, CONFIG)
end
end

Expand All @@ -83,6 +85,14 @@ function writeline(buf::IOBuffer, menu::RadioMenu{Config}, idx::Int, iscursor::B
print(buf, replace(menu.options[idx], "\n" => "\\n"))
end

function keypress(m::RadioMenu, i::UInt32)
isempty(m.keybindings) && return false
i = findfirst(isequal(i), Int.(m.keybindings))
isnothing(i) && return false
m.selected = i
return true
end

# Legacy interface
function writeLine(buf::IOBuffer, menu::RadioMenu{<:Dict}, idx::Int, cursor::Bool)
# print a ">" on the selected entry
Expand Down

0 comments on commit 02c1cf3

Please sign in to comment.