Skip to content

Commit

Permalink
move TerminalMenus.jl into a submodule in stdlib/REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Feb 8, 2018
1 parent cf3b0ab commit 4d3a1ee
Show file tree
Hide file tree
Showing 19 changed files with 170 additions and 306 deletions.
149 changes: 148 additions & 1 deletion stdlib/REPL/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,154 @@ ENV["JULIA_WARN_COLOR"] = :yellow
ENV["JULIA_INFO_COLOR"] = :cyan
```

## References
# TerminalMenus

TerminalMenus is a submodule of the Julia REPL and enables small, low-profile interactive menus in the terminal.

## Examples

```julia
import REPL
using REPL.TerminalMenus

options = ["apple", "orange", "grape", "strawberry",
"blueberry", "peach", "lemon", "lime"]

```

### RadioMenu

The RadioMenu allows the user to select one option from the list. The `request`
function displays the interactive menu and returns the index of the selected
choice. If a user presses 'q' or `ctrl-c`, `request` will return a `-1`.


```julia
# `pagesize` is the number of items to be displayed at a time.
# The UI will scroll if the number of options is greater
# than the `pagesize`
menu = RadioMenu(options, pagesize=4)

# `request` displays the menu and returns the index after the
# user has selected a choice
choice = request("Choose your favorite fruit:", menu)

if choice != -1
println("Your favorite fruit is ", options[choice], "!")
else
println("Menu canceled.")
end

```

Output:

```
Choose your favorite fruit:
^ grape
strawberry
> blueberry
v peach
Your favorite fruit is blueberry!
```

### MultiSelectMenu

The MultiSelectMenu allows users to select many choices from a list.

```julia
# here we use the default `pagesize` 10
menu = MultiSelectMenu(options)

# `request` returns a `Set` of selected indices
# if the menu us canceled (ctrl-c or q), return an empty set
choices = request("Select the fruits you like:", menu)

if length(choices) > 0
println("You like the following fruits:")
for i in choices
println(" - ", options[i])
end
else
println("Menu canceled.")
end
```

Output:

```
Select the fruits you like:
[press: d=done, a=all, n=none]
[ ] apple
> [X] orange
[X] grape
[ ] strawberry
[ ] blueberry
[X] peach
[ ] lemon
[ ] lime
You like the following fruits:
- orange
- grape
- peach
```

## Customization / Configuation

All interface customization is done through the keyword only
`TerminalMenus.config()` function.

### 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
- `supress_output::Bool=false`: For testing. If true, menu will not be printed to console.
- `ctrl_c_interrupt::Bool=true`: If `false`, return empty on ^C, if `true` throw InterruptException() on ^C

### Examples

```julia
julia> menu = MultiSelectMenu(options, pagesize=5);

julia> request(menu) # ASCII is used by default
[press: d=done, a=all, n=none]
[ ] apple
[X] orange
[ ] grape
> [X] strawberry
v [ ] blueberry
Set([4, 2])

julia> TerminalMenus.config(charset=:unicode)

julia> request(menu)
[press: d=done, a=all, n=none]
⬚ apple
✓ orange
⬚ grape
✓ strawberry
⬚ blueberry
Set([4, 2])

julia> TerminalMenus.config(checked="YEP!", unchecked="NOPE", cursor='')

julia> request(menu)
[press: d=done, a=all, n=none]
NOPE apple
YEP! orange
NOPE grape
⧐ YEP! strawberry
NOPE blueberry
Set([4, 2])

```

# References

```@docs
Base.atreplinit
Expand Down
1 change: 1 addition & 0 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import ..LineEdit:
include("REPLCompletions.jl")
using .REPLCompletions

include("TerminalMenus/TerminalMenus.jl")
include("docview.jl")

function __init__()
Expand Down
5 changes: 0 additions & 5 deletions stdlib/REPL/src/TerminalMenus/.gitignore

This file was deleted.

35 changes: 0 additions & 35 deletions stdlib/REPL/src/TerminalMenus/.travis.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ varies based on menu type.
"""
request(m::AbstractMenu) = request(terminal, m)

function request(term::Base.Terminals.TTYTerminal, m::AbstractMenu)
function request(term::REPL.Terminals.TTYTerminal, m::AbstractMenu)
cursor = 1

menu_header = header(m)
Expand Down Expand Up @@ -212,7 +212,7 @@ Shorthand for `println(msg); request(m)`.
request(msg::AbstractString, m::AbstractMenu) =
request(terminal, msg, m)

function request(term::Base.Terminals.TTYTerminal,
function request(term::REPL.Terminals.TTYTerminal,
msg::AbstractString, m::AbstractMenu)
println(term.out_stream, msg)
request(term, m)
Expand Down
22 changes: 0 additions & 22 deletions stdlib/REPL/src/TerminalMenus/LICENSE.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function writeLine(buf::IOBuffer, menu::MultiSelectMenu, idx::Int, cursor::Bool)
print(buf, CONFIG[:unchecked], " ")
end

print(buf, replace(menu.options[idx], "\n", "\\n"))
print(buf, replace(menu.options[idx], "\n" => "\\n"))
end

# d: Done, return from request
Expand Down
Loading

0 comments on commit 4d3a1ee

Please sign in to comment.