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

Accept RefValue cursor as TerminalMenu request argument. #38393

Merged
merged 2 commits into from
Nov 15, 2020
Merged
Changes from 1 commit
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
29 changes: 18 additions & 11 deletions stdlib/REPL/src/TerminalMenus/AbstractMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ end
# TODO Julia2.0: get rid of parametric intermediate, making it just
# abstract type ConfiguredMenu <: AbstractMenu end
# Or perhaps just make all menus ConfiguredMenus
# Also consider making `cursor` a mandatory field in the Menu structs
# instead of going via the RefValue in `request`.
abstract type _ConfiguredMenu{C} <: AbstractMenu end
const ConfiguredMenu = _ConfiguredMenu{<:AbstractConfig}

Expand Down Expand Up @@ -162,19 +164,24 @@ selected(m::AbstractMenu) = m.selected
request(m::AbstractMenu; cursor=1)

Display the menu and enter interactive mode. `cursor` indicates the item
number used for the initial cursor position.
number used for the initial cursor position. `cursor` can be either an
`Int` or a `RefValue{Int}`. The latter is useful for observation and
control of the cursor position from the outside.

Returns `selected(m)`.
"""
request(m::AbstractMenu; kwargs...) = request(terminal, m; kwargs...)

function request(term::REPL.Terminals.TTYTerminal, m::AbstractMenu; cursor::Int=1, suppress_output=false)
function request(term::REPL.Terminals.TTYTerminal, m::AbstractMenu; cursor::Union{Int, Base.RefValue{Int}}=1, suppress_output=false)
if cursor isa Int
cursor = Ref(cursor)
end
menu_header = header(m)
!suppress_output && !isempty(menu_header) && println(term.out_stream, menu_header)

state = nothing
if !suppress_output
state = printmenu(term.out_stream, m, cursor, init=true)
state = printmenu(term.out_stream, m, cursor[], init=true)
end

raw_mode_enabled = try
Expand All @@ -193,22 +200,22 @@ function request(term::REPL.Terminals.TTYTerminal, m::AbstractMenu; cursor::Int=
c = readkey(term.in_stream)

if c == Int(ARROW_UP)
cursor = move_up!(m, cursor, lastoption)
cursor[] = move_up!(m, cursor[], lastoption)
elseif c == Int(ARROW_DOWN)
cursor = move_down!(m, cursor, lastoption)
cursor[] = move_down!(m, cursor[], lastoption)
elseif c == Int(PAGE_UP)
cursor = page_up!(m, cursor, lastoption)
cursor[] = page_up!(m, cursor[], lastoption)
elseif c == Int(PAGE_DOWN)
cursor = page_down!(m, cursor, lastoption)
cursor[] = page_down!(m, cursor[], lastoption)
elseif c == Int(HOME_KEY)
cursor = 1
cursor[] = 1
m.pageoffset = 0
elseif c == Int(END_KEY)
cursor = lastoption
cursor[] = lastoption
m.pageoffset = lastoption - m.pagesize
elseif c == 13 # <enter>
# will break if pick returns true
pick(m, cursor) && break
pick(m, cursor[]) && break
elseif c == UInt32('q')
cancel(m)
break
Expand All @@ -221,7 +228,7 @@ function request(term::REPL.Terminals.TTYTerminal, m::AbstractMenu; cursor::Int=
end

if !suppress_output
state = printmenu(term.out_stream, m, cursor, oldstate=state)
state = printmenu(term.out_stream, m, cursor[], oldstate=state)
end
end
finally # always disable raw mode
Expand Down