Skip to content

Commit

Permalink
Merge pull request #1 from nick-paul/add-params
Browse files Browse the repository at this point in the history
Add config, unicode/ascii, scroll wrap
  • Loading branch information
Nicholas Paul authored Jun 25, 2017
2 parents f7a2e2f + be58eae commit 09440ce
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 19 deletions.
45 changes: 33 additions & 12 deletions src/AbstractMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,41 @@ function request(m::AbstractMenu)
try
while true
c = readKey()

if c == Int(ARROW_UP)
# move selection
cursor -= cursor > 1 ? 1 : 0
# scroll the page
if cursor < (2+m.pageoffset) && m.pageoffset > 0
m.pageoffset -= 1

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



elseif c == Int(ARROW_DOWN)
# move selection
cursor += cursor < length(options(m)) ? 1 : 0
# scroll page
if cursor >= m.pagesize + m.pageoffset && m.pagesize + m.pageoffset < length(options(m))
m.pageoffset += 1

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



elseif c == 13 # <enter>
# will break if pick returns true
pick(m, cursor) && break
Expand Down Expand Up @@ -80,10 +101,10 @@ function printMenu(m::AbstractMenu, cursor::Int; init=false)

if i == m.pageoffset+1 && m.pageoffset > 0
# first line && scrolled past first entry
print(buf, "^")
print(buf, CONFIG[:up_arrow])
elseif i == m.pagesize+m.pageoffset && i != length(options(m))
# last line && not last option
print(buf, "v")
print(buf, CONFIG[:down_arrow])
else
# non special line
print(buf, " ")
Expand Down
8 changes: 6 additions & 2 deletions src/MultiSelectMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ end

function writeLine(buf::IOBuffer, menu::MultiSelectMenu, idx::Int, cursor::Bool)
# print a ">" on the selected entry
cursor ? print(buf, "> ") : print(buf, " ")
idx in menu.selected ? print(buf, "[X] ") : print(buf, "[ ] ")
cursor ? print(buf, CONFIG[:cursor]," ") : print(buf, " ")
if idx in menu.selected
print(buf, CONFIG[:checked], " ")
else
print(buf, CONFIG[:unchecked], " ")
end

print(buf, replace(menu.options[idx], "\n", "\\n"))
end
Expand Down
2 changes: 1 addition & 1 deletion src/RadioMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ end

function writeLine(buf::IOBuffer, menu::RadioMenu, idx::Int, cursor::Bool)
# print a ">" on the selected entry
cursor ? print(buf, "> ") : print(buf, " ")
cursor ? print(buf, CONFIG[:cursor] ," ") : print(buf, " ")

print(buf, replace(menu.options[idx], "\n", "\\n"))
end
1 change: 1 addition & 0 deletions src/TerminalMenus.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module TerminalMenus

include("util.jl")
include("config.jl")

include("AbstractMenu.jl")
include("RadioMenu.jl")
Expand Down
67 changes: 67 additions & 0 deletions src/config.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""global menu configuration parameters"""
CONFIG = Dict()


"""
config( <see arguments> )
Configure global Menu parameters
# Arguments
- `charset::Symbol=:na`: ui characters to use (`:ascii` or `:unicode`); overridden by other arguments
- `cursor::Char='>'|'→'`: character to use for cursor
- `up_arrow::Char='^'|'↑'`: character to use for up arrow
- `down_arrow::Char='v'|'↓'`: character to use for down arrow
- `checked::String="[X]"|"✓"`: string to use for checked
- `unchecked::String="[ ]"|"⬚")`: string to use for unchecked
- `scroll::Symbol=:na`: If `:wrap` then wrap the cursor around top and bottom, if :`nowrap` do not wrap cursor
"""
function config(;charset::Symbol = :na,
scroll::Symbol = :na,
cursor::Char = '\0',
up_arrow::Char = '\0',
down_arrow::Char = '\0',
checked::String = "",
unchecked::String = "")

if !(charset in [:na, :ascii, :unicode])
error("charset should be :ascii or :unicode, recieved $charset")
end

if !(scroll in [:na, :wrap, :nowrap])
error("scroll must be :wrap or :nowrap, recieved $scroll")
end

if scroll == :wrap
CONFIG[:scroll_wrap] = true
elseif scroll == :nowrap
CONFIG[:scroll_wrap] = false
end

if charset == :ascii
cursor = '>'
up_arrow = '^'
down_arrow = 'v'
checked = "[X]"
unchecked = "[ ]"
elseif charset == :unicode
cursor = ''
up_arrow = ''
down_arrow = ''
checked = ""
unchecked = ""
end

cursor != '\0' && (CONFIG[:cursor] = cursor)
up_arrow != '\0' && (CONFIG[:up_arrow] = up_arrow)
down_arrow != '\0' && (CONFIG[:down_arrow] = down_arrow)
checked != "" && (CONFIG[:checked] = checked)
unchecked != "" && (CONFIG[:unchecked] = unchecked)

# Don't return anything
nothing
end

# Set up defaults
config(charset=:ascii, scroll=:nowrap)
19 changes: 17 additions & 2 deletions test/multiselect_menu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
@test MultiSelectMenu <: TerminalMenus.AbstractMenu

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

# Constructor
@test MultiSelectMenu(["one", "two", "three"]).pagesize == 3
Expand All @@ -14,3 +14,18 @@
multi_menu = MultiSelectMenu(string.(1:20))
@test TerminalMenus.options(multi_menu) == string.(1:20)
@test TerminalMenus.header(multi_menu) == "[press: d=done, a=all, n=none]"

# Output
TerminalMenus.config() # Use default chars
CONFIG = TerminalMenus.CONFIG

multi_menu = MultiSelectMenu(string.(1:10))
buf = IOBuffer()
TerminalMenus.writeLine(buf, multi_menu, 1, true)
@test String(take!(buf)) == string(CONFIG[:cursor], " ", CONFIG[:unchecked], " 1")
TerminalMenus.config(cursor='+')
TerminalMenus.writeLine(buf, multi_menu, 1, true)
@test String(take!(buf)) == string("+ ", CONFIG[:unchecked], " 1")
TerminalMenus.config(charset=:unicode)
TerminalMenus.writeLine(buf, multi_menu, 1, true)
@test String(take!(buf)) == string(CONFIG[:cursor], " ", CONFIG[:unchecked], " 1")
19 changes: 17 additions & 2 deletions test/radio_menu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
@test RadioMenu <: TerminalMenus.AbstractMenu

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

# Constructor
@test RadioMenu(["one", "two", "three"]).pagesize == 3
Expand All @@ -17,3 +17,18 @@ radio_menu.selected = 2
TerminalMenus.cancel(radio_menu)
@test radio_menu.selected == -1
@test TerminalMenus.header(radio_menu) == ""

# Output
TerminalMenus.config() # Use default chars
CONFIG = TerminalMenus.CONFIG

radio_menu = RadioMenu(string.(1:10))
buf = IOBuffer()
TerminalMenus.writeLine(buf, radio_menu, 1, true)
@test String(take!(buf)) == string(CONFIG[:cursor], " 1")
TerminalMenus.config(cursor='+')
TerminalMenus.writeLine(buf, radio_menu, 1, true)
@test String(take!(buf)) == "+ 1"
TerminalMenus.config(charset=:unicode)
TerminalMenus.writeLine(buf, radio_menu, 1, true)
@test String(take!(buf)) == string(CONFIG[:cursor], " 1")
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,18 @@ using Base.Test

include("radio_menu.jl")
include("multiselect_menu.jl")

# Other test

# scroll must only accept symbols
@test_throws TypeError TerminalMenus.config(scroll=true)
# :foo is not a valid scroll option
@test_throws ErrorException TerminalMenus.config(scroll=:foo)
# Test scroll wrap
TerminalMenus.config(scroll=:wrap)
@test TerminalMenus.CONFIG[:scroll_wrap] == true
# Updating some params shouldn't change other ones
TerminalMenus.config(charset=:ascii)
@test TerminalMenus.CONFIG[:scroll_wrap] == true
TerminalMenus.config(scroll=:nowrap)
@test TerminalMenus.CONFIG[:scroll_wrap] == false

0 comments on commit 09440ce

Please sign in to comment.