Skip to content

Commit

Permalink
allow single choice menus (JuliaLang#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-paul committed Jun 21, 2018
1 parent 5908744 commit d72d147
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/AbstractMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ doc string.
# Functions
The following functions can be called on all <:AbstractMenu types.
Details can be found in
## Exported
Expand Down Expand Up @@ -262,8 +261,10 @@ function printMenu(out, m::AbstractMenu, cursor::Int; init::Bool=false)

writeLine(buf, m, i, i == cursor, term_width)

# dont print an \r\n on the last line
i != (m.pagesize+m.pageoffset) && print(buf, "\r\n")
# don't print an \r\n on the last line unless there is only one line
if m.pagesize == 1 || i != (m.pagesize+m.pageoffset)
print(buf, "\r\n")
end
end

print(out, String(take!(buf)))
Expand Down
6 changes: 3 additions & 3 deletions src/MultiSelectMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ were selected by the user.
- `pagesize::Int=10`: The number of options to be displayed at one time, the menu will scroll if length(options) > pagesize
"""
function MultiSelectMenu(options::Array{String,1}; pagesize::Int=10)
length(options) < 2 && error("MultiSelectMenu must have at least two options")
length(options) < 1 && error("MultiSelectMenu must have at least one option")

# if pagesize is -1, use automatic paging
pagesize = pagesize == -1 ? length(options) : pagesize
# pagesize shouldn't be bigger than options
pagesize = min(length(options), pagesize)
# after other checks, pagesize must be greater than 2
pagesize < 2 && error("pagesize must be >= 2")
# after other checks, pagesize must be greater than 1
pagesize < 1 && error("pagesize must be >= 1")

pageoffset = 0
selected = Set{Int}() # none
Expand Down
6 changes: 3 additions & 3 deletions src/RadioMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ user.
- `pagesize::Int=10`: The number of options to be displayed at one time, the menu will scroll if length(options) > pagesize
"""
function RadioMenu(options::Array{String,1}; pagesize::Int=10)
length(options) < 2 && error("RadioMenu must have at least two options")
length(options) < 1 && error("RadioMenu must have at least one option")

# if pagesize is -1, use automatic paging
pagesize = pagesize == -1 ? length(options) : pagesize
# pagesize shouldn't be bigger than options
pagesize = min(length(options), pagesize)
# after other checks, pagesize must be greater than 2
pagesize < 2 && error("pagesize must be >= 2")
# after other checks, pagesize must be greater than 1
pagesize < 1 && error("pagesize must be >= 1")

pageoffset = 0
selected = -1 # none
Expand Down
6 changes: 2 additions & 4 deletions test/multiselect_menu.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# Check to make sure types are imported properly
@test MultiSelectMenu <: TerminalMenus.AbstractMenu

# Invalid Menu Params
@test_throws ErrorException MultiSelectMenu(["one"])
@test_throws ErrorException MultiSelectMenu(["one", "two", "three"], pagesize=1)

# Constructor
@test MultiSelectMenu(["one", "two", "three"]).pagesize == 3
@test MultiSelectMenu(string.(1:30), pagesize=-1).pagesize == 30
Expand Down Expand Up @@ -34,3 +30,5 @@ 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, :enter, 'd')
multi_menu = MultiSelectMenu(["single option"])
@test simulateInput(Set([1]), multi_menu, :up, :up, :down, :enter, 'd')
8 changes: 4 additions & 4 deletions test/radio_menu.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# Check to make sure types are imported properly
@test RadioMenu <: TerminalMenus.AbstractMenu

# Invalid Menu Params
@test_throws ErrorException RadioMenu(["one"])
@test_throws ErrorException RadioMenu(["one", "two", "three"], pagesize=1)

# Constructor
@test RadioMenu(["one", "two", "three"]).pagesize == 3
@test RadioMenu(string.(1:30), pagesize=-1).pagesize == 30
Expand Down Expand Up @@ -38,3 +34,7 @@ TerminalMenus.writeLine(buf, radio_menu, 1, true, term_width)
# Test using STDIN
radio_menu = RadioMenu(string.(1:10))
@test simulateInput(3, radio_menu, :down, :down, :enter)
radio_menu = RadioMenu(["single option"])
@test simulateInput(1, radio_menu, :up, :up, :down, :up, :enter)
radio_menu = RadioMenu(string.(1:3), pagesize=1)
@test simulateInput(3, radio_menu, :down, :down, :down, :down, :enter)

0 comments on commit d72d147

Please sign in to comment.