From d72d14735371aecee2d365ca002e50c5732f66d8 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 21 Jun 2018 08:19:55 -0400 Subject: [PATCH] allow single choice menus (#19) --- src/AbstractMenu.jl | 7 ++++--- src/MultiSelectMenu.jl | 6 +++--- src/RadioMenu.jl | 6 +++--- test/multiselect_menu.jl | 6 ++---- test/radio_menu.jl | 8 ++++---- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/AbstractMenu.jl b/src/AbstractMenu.jl index 9c9bd0b986716..367e184d9b7ae 100644 --- a/src/AbstractMenu.jl +++ b/src/AbstractMenu.jl @@ -10,7 +10,6 @@ doc string. # Functions The following functions can be called on all <:AbstractMenu types. -Details can be found in ## Exported @@ -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))) diff --git a/src/MultiSelectMenu.jl b/src/MultiSelectMenu.jl index 50b9685700cd7..e35baec18cf8a 100644 --- a/src/MultiSelectMenu.jl +++ b/src/MultiSelectMenu.jl @@ -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 diff --git a/src/RadioMenu.jl b/src/RadioMenu.jl index 1ca91998656d8..efbdca01c8ba0 100644 --- a/src/RadioMenu.jl +++ b/src/RadioMenu.jl @@ -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 diff --git a/test/multiselect_menu.jl b/test/multiselect_menu.jl index d0eda4c7c7ef0..e14d07e76d957 100644 --- a/test/multiselect_menu.jl +++ b/test/multiselect_menu.jl @@ -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 @@ -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') diff --git a/test/radio_menu.jl b/test/radio_menu.jl index a0e61c4e7c508..101bae3fe258c 100644 --- a/test/radio_menu.jl +++ b/test/radio_menu.jl @@ -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 @@ -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)