forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.jl
183 lines (155 loc) · 6.98 KB
/
config.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# This file is a part of Julia. License is MIT: https://julialang.org/license
abstract type AbstractConfig end
struct Config <: AbstractConfig
cursor::Char
up_arrow::Char
down_arrow::Char
updown_arrow::Char
scroll_wrap::Bool
ctrl_c_interrupt::Bool
end
struct MultiSelectConfig <: AbstractConfig
config::Config
checked::String
unchecked::String
end
"""
Config(; scroll_wrap=false, ctrl_c_interrupt=true, charset=:ascii, cursor::Char, up_arrow::Char, down_arrow::Char)
Configure behavior for selection menus via keyword arguments:
- `scroll_wrap`, if `true`, causes the menu to wrap around when scrolling above the first
or below the last entry
- `ctrl_c_interrupt`, if `true`, throws an `InterruptException` if the user hits Ctrl-C
during menu selection. If `false`, [`TerminalMenus.request`](@ref) will return the
default result from [`TerminalMenus.selected`](@ref).
- `charset` affects the default values for `cursor`, `up_arrow`, and `down_arrow`,
and can be `:ascii` or `:unicode`
- `cursor` is the character printed to indicate the option that will be chosen by
hitting "Enter." Defaults are '>' or '→', depending on `charset`.
- `up_arrow` is the character printed when the display does not include the first entry.
Defaults are '^' or '↑', depending on `charset`.
- `down_arrow` is the character printed when the display does not include the last entry.
Defaults are 'v' or '↓', depending on `charset`.
Subtypes of `ConfiguredMenu` will print `cursor`, `up_arrow`, and `down_arrow` automatically
as needed, your `writeline` method should not print them.
!!! compat Julia 1.6
`Config` is available as of Julia 1.6. On older releases use the global `CONFIG`.
"""
function Config(;
charset::Symbol = :ascii,
cursor::Char = '\0',
up_arrow::Char = '\0',
down_arrow::Char = '\0',
updown_arrow::Char = '\0',
scroll_wrap::Bool = false,
ctrl_c_interrupt::Bool = true)
charset === :ascii || charset === :unicode ||
throw(ArgumentError("charset should be :ascii or :unicode, received $charset"))
if cursor == '\0'
cursor = charset === :ascii ? '>' : '→'
end
if up_arrow == '\0'
up_arrow = charset === :ascii ? '^' : '↑'
end
if down_arrow == '\0'
down_arrow = charset === :ascii ? 'v' : '↓'
end
if updown_arrow == '\0'
updown_arrow = charset === :ascii ? 'I' : '↕'
end
return Config(cursor, up_arrow, down_arrow, updown_arrow, scroll_wrap, ctrl_c_interrupt)
end
"""
MultiSelectConfig(; charset=:ascii, checked::String, unchecked::String, kwargs...)
Configure behavior for a multiple-selection menu via keyword arguments:
- `checked` is the string to print when an option has been selected.
Defaults are "[X]" or "✓", depending on `charset`.
- `unchecked` is the string to print when an option has not been selected.
Defaults are "[ ]" or "⬚", depending on `charset`.
All other keyword arguments are as described for [`TerminalMenus.Config`](@ref).
`checked` and `unchecked` are not printed automatically, and should be printed by
your `writeline` method.
!!! compat Julia 1.6
`MultiSelectConfig` is available as of Julia 1.6. On older releases use the global `CONFIG`.
"""
function MultiSelectConfig(;
charset::Symbol = :ascii,
checked::String = "",
unchecked::String = "",
kwargs...)
charset === :ascii || charset === :unicode || throw(ArgumentError("charset should be :ascii or :unicode, received $charset"))
if isempty(checked)
checked = charset === :ascii ? "[X]" : "✓"
end
if isempty(unchecked)
unchecked = charset === :ascii ? "[ ]" : "⬚"
end
return MultiSelectConfig(Config(; charset=charset, kwargs...), checked, unchecked)
end
## Below is the old-style CONFIG interface, kept for backwards compatibility.
## Not recommended for any new menu types.
"""
CONFIG
Global menu configuration parameters
!!! compat Julia 1.6
`CONFIG` is deprecated, instead configure menus via their constructors.
"""
const CONFIG = Dict{Symbol,Union{Char,String,Bool}}()
"""
config( <see arguments> )
Keyword-only function to 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=:nowrap`: If `:wrap` wrap cursor around top and bottom, if :`nowrap` do not wrap cursor
- `supress_output::Bool=false`: Ignored legacy argument, pass `suppress_output` as a keyword argument to `request` instead.
- `ctrl_c_interrupt::Bool=true`: If `false`, return empty on ^C, if `true` throw InterruptException() on ^C
!!! compat Julia 1.6
As of Julia 1.6, `config` is deprecated. Use `Config` or `MultiSelectConfig` instead.
"""
function config(;charset::Symbol = :na,
scroll::Symbol = :na,
cursor::Char = '\0',
up_arrow::Char = '\0',
down_arrow::Char = '\0',
updown_arrow::Char = '\0',
checked::String = "",
unchecked::String = "",
supress_output::Union{Nothing, Bool}=nothing, # typo was documented, unfortunately
ctrl_c_interrupt::Union{Nothing, Bool}=nothing)
if charset === :ascii
cursor = '>'
up_arrow = '^'
down_arrow = 'v'
updown_arrow = 'I'
checked = "[X]"
unchecked = "[ ]"
elseif charset === :unicode
cursor = '→'
up_arrow = '↑'
down_arrow = '↓'
updown_arrow = '↕'
checked = "✓"
unchecked = "⬚"
elseif charset === :na
else
throw(ArgumentError("charset should be :ascii or :unicode, received $charset"))
end
scroll ∉ [:na, :wrap, :nowrap] && throw(ArgumentError("scroll must be :wrap or :nowrap, received $scroll"))
scroll === :wrap && (CONFIG[:scroll_wrap] = true)
scroll === :nowrap && (CONFIG[:scroll_wrap] = false)
cursor != '\0' && (CONFIG[:cursor] = cursor)
up_arrow != '\0' && (CONFIG[:up_arrow] = up_arrow)
down_arrow != '\0' && (CONFIG[:down_arrow] = down_arrow)
updown_arrow != '\0' && (CONFIG[:updown_arrow] = updown_arrow)
checked != "" && (CONFIG[:checked] = checked)
unchecked != "" && (CONFIG[:unchecked] = unchecked)
supress_output isa Bool && (CONFIG[:supress_output] = supress_output)
ctrl_c_interrupt isa Bool && (CONFIG[:ctrl_c_interrupt] = ctrl_c_interrupt)
return nothing
end
# Set up defaults
config(charset=:ascii, scroll=:nowrap, supress_output=false, ctrl_c_interrupt=true)