Skip to content

Commit

Permalink
Merge pull request JuliaLang#18 from bjarthur/bja/advance
Browse files Browse the repository at this point in the history
advance cursor after selecting item in multi-select menu
  • Loading branch information
Nicholas Paul authored Jun 18, 2018
2 parents 13e4b6c + f6f4292 commit 5908744
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
32 changes: 19 additions & 13 deletions src/AbstractMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ varies based on menu type.
request(m::AbstractMenu) = request(terminal, m)

function request(term::Base.Terminals.TTYTerminal, m::AbstractMenu)

function advance_cursor()
if cursor < length(options(m))
# move selection up
cursor += 1
# scroll page
if cursor >= m.pagesize + m.pageoffset && m.pagesize + m.pageoffset < length(options(m))
m.pageoffset += 1
end
elseif CONFIG[:scroll_wrap]
# wrap to top
cursor = 1
m.pageoffset = 0
end
end

cursor = 1

menu_header = header(m)
Expand Down Expand Up @@ -133,19 +149,7 @@ function request(term::Base.Terminals.TTYTerminal, m::AbstractMenu)
end

elseif c == Int(ARROW_DOWN)

if cursor < length(options(m))
# move selection up
cursor += 1
# scroll page
if cursor >= m.pagesize + m.pageoffset && m.pagesize + m.pageoffset < length(options(m))
m.pageoffset += 1
end
elseif CONFIG[:scroll_wrap]
# wrap to top
cursor = 1
m.pageoffset = 0
end
advance_cursor()

elseif c == Int(PAGE_UP)
# If we're at the bottom, move the page 1 less to move the cursor up from
Expand All @@ -172,6 +176,8 @@ function request(term::Base.Terminals.TTYTerminal, m::AbstractMenu)
elseif c == 13 # <enter>
# will break if pick returns true
pick(m, cursor) && break
advance_cursor()

elseif c == UInt32('q')
cancel(m)
break
Expand Down
4 changes: 2 additions & 2 deletions src/MultiSelectMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A menu that allows a user to select a multiple options from a list.
```julia
julia> request(MultiSelectMenu(options))
Select the fruits you like:
[press: d=done, a=all, n=none]
[press: d=done, a=all, n=none, <enter>=select]
[ ] apple
> [X] orange
[X] grape
Expand Down Expand Up @@ -82,7 +82,7 @@ function pick(menu::MultiSelectMenu, cursor::Int)
push!(menu.selected, cursor)
end

return false #break out of the menu
return false #don't break out of the menu
end

function writeLine(buf::IOBuffer, menu::MultiSelectMenu, idx::Int, cursor::Bool, term_width::Int)
Expand Down
2 changes: 1 addition & 1 deletion test/multiselect_menu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ TerminalMenus.writeLine(buf, multi_menu, 1, true, term_width)

# Test SDTIN
multi_menu = MultiSelectMenu(string.(1:10))
@test simulateInput(Set([1,2]), multi_menu, :enter, :down, :enter, 'd')
@test simulateInput(Set([1,2]), multi_menu, :enter, :enter, 'd')

0 comments on commit 5908744

Please sign in to comment.