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

REPL.TerminalMenus: Allow keybindings in RadioMenu #41576

Merged
Merged
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
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