tty-prompt 0.21.0 → 0.22.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +37 -1
- data/README.md +372 -215
- data/lib/tty-prompt.rb +1 -2
- data/lib/tty/prompt.rb +145 -142
- data/lib/tty/prompt/answers_collector.rb +2 -2
- data/lib/tty/prompt/block_paginator.rb +1 -1
- data/lib/tty/prompt/choice.rb +1 -1
- data/lib/tty/prompt/choices.rb +28 -11
- data/lib/tty/prompt/confirm_question.rb +23 -12
- data/lib/tty/prompt/const.rb +17 -0
- data/lib/tty/prompt/converter_dsl.rb +6 -7
- data/lib/tty/prompt/converter_registry.rb +31 -26
- data/lib/tty/prompt/converters.rb +139 -32
- data/lib/tty/prompt/enum_list.rb +27 -19
- data/lib/tty/prompt/errors.rb +31 -0
- data/lib/tty/prompt/evaluator.rb +1 -1
- data/lib/tty/prompt/expander.rb +20 -12
- data/lib/tty/prompt/keypress.rb +3 -3
- data/lib/tty/prompt/list.rb +57 -25
- data/lib/tty/prompt/mask_question.rb +2 -2
- data/lib/tty/prompt/multi_list.rb +71 -27
- data/lib/tty/prompt/multiline.rb +6 -5
- data/lib/tty/prompt/paginator.rb +1 -1
- data/lib/tty/prompt/question.rb +56 -31
- data/lib/tty/prompt/question/checks.rb +18 -0
- data/lib/tty/prompt/selected_choices.rb +76 -0
- data/lib/tty/prompt/slider.rb +67 -8
- data/lib/tty/prompt/statement.rb +3 -3
- data/lib/tty/prompt/suggestion.rb +5 -5
- data/lib/tty/prompt/symbols.rb +58 -58
- data/lib/tty/prompt/test.rb +36 -0
- data/lib/tty/prompt/utils.rb +1 -3
- data/lib/tty/prompt/version.rb +1 -1
- metadata +12 -24
- data/lib/tty/prompt/messages.rb +0 -49
- data/lib/tty/test_prompt.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea89e6284f92e7de76bdd3457f6345912e11b7dc6c8c0249798aa0c88eb80759
|
4
|
+
data.tar.gz: 4620f5a0473bf600646066a8e0f970993d310ef7a85ab0e5abd928b846c2765e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd45c4a0690e88647f28762bdab648f1a71e85203691548e5e9a1a0a1775a02e670a512c52187f12720aadd17cdbba16f0fa4b302a5d1c9421c978224f9c6002
|
7
|
+
data.tar.gz: 5d7c1b924060658c808ccb5358bed6d9b7deda6a4a6a13f3fb14382344ce502f68bc2c327d6234a59a6abf23c42c1da8c587b4da481dbd4403d66d1d955bf804
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,40 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
-
## [v0.
|
3
|
+
## [v0.22.0] - 2020-07-20
|
4
|
+
|
5
|
+
### Added
|
6
|
+
* Add #slider format customization with a proc by Sven Pchnit(@2called-chaos)
|
7
|
+
* Add convert message customization
|
8
|
+
* Add conversion of input into Array, Hash or URI object
|
9
|
+
* Add callable objects as possible values in :active_color and :help_color
|
10
|
+
* Add shortcuts to select of all/reverse choices in #multi_select prompt
|
11
|
+
* Add :help option to #slider prompt
|
12
|
+
* Add :quiet option to remove final prompt output by Katelyn Schiesser (@slowbro)
|
13
|
+
* Add :show_help option to control help display in #select, #multi_select, #enum_select
|
14
|
+
and #slider prompts
|
15
|
+
|
16
|
+
### Changed
|
17
|
+
* Changed question :validation option to :validate by Sven Pachnit(@2called-chaos)
|
18
|
+
* Change ConverterRegistry to store only proc values and simplify interface
|
19
|
+
* Change Converters to stop raising errors and print console error messages instead
|
20
|
+
* Change :range conversion to handle float numbers
|
21
|
+
* Change yes?/no? prompt to infer default value from words and raise when
|
22
|
+
no boolean can be deduced
|
23
|
+
* Change Prompt#new to use keyword arguments
|
24
|
+
* Change #select/#multi_select prompts default help text
|
25
|
+
* Change #multi_select to preserve original ordering in returned answers
|
26
|
+
* Change to remove necromancer dependency
|
27
|
+
* Change TTY::TestPrompt to TTY::Prompt::Test
|
28
|
+
* Change #select,#multi_select & #enum_select to allow mix of options and block parameters configuration
|
29
|
+
* Change to allow filtering through symbol choice names
|
30
|
+
* Change all errors to inherit from common Error class
|
31
|
+
|
32
|
+
### Fixed
|
33
|
+
* Fix multiline prompt to return default value when no input provided
|
34
|
+
* Fix color option overriding in say, ok, error and warn prompts
|
35
|
+
* Fix Prompt#inspect format to display all public attributes
|
36
|
+
|
37
|
+
## [v0.21.0] - 2020-03-08
|
4
38
|
|
5
39
|
### Added
|
6
40
|
* Add :min option to #multi_select prompt by Katelyn Schiesser(@slowbro)
|
@@ -349,6 +383,8 @@
|
|
349
383
|
|
350
384
|
* Initial implementation and release
|
351
385
|
|
386
|
+
[v0.22.0]: https://github.com/piotrmurach/tty-prompt/compare/v0.21.0...v0.22.0
|
387
|
+
[v0.21.0]: https://github.com/piotrmurach/tty-prompt/compare/v0.20.0...v0.21.0
|
352
388
|
[v0.20.0]: https://github.com/piotrmurach/tty-prompt/compare/v0.19.0...v0.20.0
|
353
389
|
[v0.19.0]: https://github.com/piotrmurach/tty-prompt/compare/v0.18.1...v0.19.0
|
354
390
|
[v0.18.1]: https://github.com/piotrmurach/tty-prompt/compare/v0.18.0...v0.18.1
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
<div align="center">
|
2
|
-
<a href="https://piotrmurach.github.io/tty" target="_blank"><img width="130" src="https://
|
2
|
+
<a href="https://piotrmurach.github.io/tty" target="_blank"><img width="130" src="https://github.com/piotrmurach/tty/raw/master/images/tty.png" alt="tty logo" /></a>
|
3
3
|
</div>
|
4
4
|
|
5
5
|
# TTY::Prompt [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
|
@@ -23,6 +23,10 @@
|
|
23
23
|
|
24
24
|
**TTY::Prompt** provides independent prompt component for [TTY](https://github.com/piotrmurach/tty) toolkit.
|
25
25
|
|
26
|
+
<div align="center">
|
27
|
+
<img alt="Available prompts demo" src="assets/demo.gif" width="562" height="226">
|
28
|
+
</div>
|
29
|
+
|
26
30
|
## Features
|
27
31
|
|
28
32
|
* Number of prompt types for gathering user input
|
@@ -47,7 +51,7 @@ For Windows, consider installing [ConEmu](https://conemu.github.io/), [cmder](ht
|
|
47
51
|
Add this line to your application's Gemfile:
|
48
52
|
|
49
53
|
```ruby
|
50
|
-
gem
|
54
|
+
gem "tty-prompt"
|
51
55
|
```
|
52
56
|
|
53
57
|
And then execute:
|
@@ -63,15 +67,15 @@ Or install it yourself as:
|
|
63
67
|
* [1. Usage](#1-usage)
|
64
68
|
* [2. Interface](#2-interface)
|
65
69
|
* [2.1 ask](#21-ask)
|
66
|
-
* [2.1.1 convert](#211-convert)
|
67
|
-
* [2.1.2 default](#212-default)
|
68
|
-
* [2.1.3 value](#213-value)
|
69
|
-
* [2.1.4 echo](#214-echo)
|
70
|
+
* [2.1.1 :convert](#211-convert)
|
71
|
+
* [2.1.2 :default](#212-default)
|
72
|
+
* [2.1.3 :value](#213-value)
|
73
|
+
* [2.1.4 :echo](#214-echo)
|
70
74
|
* [2.1.5 error messages](#215-error-messages)
|
71
|
-
* [2.1.6 in](#216-in)
|
72
|
-
* [2.1.7 modify](#217-modify)
|
73
|
-
* [2.1.8 required](#218-required)
|
74
|
-
* [2.1.9 validate](#219-validate)
|
75
|
+
* [2.1.6 :in](#216-in)
|
76
|
+
* [2.1.7 :modify](#217-modify)
|
77
|
+
* [2.1.8 :required](#218-required)
|
78
|
+
* [2.1.9 :validate](#219-validate)
|
75
79
|
* [2.2 keypress](#22-keypress)
|
76
80
|
* [2.2.1 :timeout](#221-timeout)
|
77
81
|
* [2.3 multiline](#23-multiline)
|
@@ -81,20 +85,28 @@ Or install it yourself as:
|
|
81
85
|
* [2.6.1 choices](#261-choices)
|
82
86
|
* [2.6.1.1 :disabled](#2611-disabled)
|
83
87
|
* [2.6.2 select](#262-select)
|
84
|
-
* [2.6.2.1 :
|
85
|
-
* [2.6.2.2 :
|
86
|
-
* [2.6.2.3 :
|
88
|
+
* [2.6.2.1 :cycle](#2621-cycle)
|
89
|
+
* [2.6.2.2 :enum](#2622-enum)
|
90
|
+
* [2.6.2.3 :help](#2623-help)
|
91
|
+
* [2.6.2.4 :marker](#2624-marker)
|
92
|
+
* [2.6.2.5 :per_page](#2625-per_page)
|
93
|
+
* [2.6.2.6 :disabled](#2626-disabled)
|
94
|
+
* [2.6.2.7 :filter](#2627-filter)
|
87
95
|
* [2.6.3 multi_select](#263-multi_select)
|
88
|
-
* [2.6.3.1 :
|
89
|
-
* [2.6.3.2 :
|
90
|
-
* [2.6.3.3 :
|
91
|
-
* [2.6.3.4 :
|
92
|
-
* [2.6.3.5 :
|
96
|
+
* [2.6.3.1 :cycle](#2631-cycle)
|
97
|
+
* [2.6.3.2 :enum](#2632-enum)
|
98
|
+
* [2.6.3.3 :help](#2633-help)
|
99
|
+
* [2.6.3.4 :per_page](#2634-per_page)
|
100
|
+
* [2.6.3.5 :disabled](#2635-disabled)
|
101
|
+
* [2.6.3.6 :echo](#2636-echo)
|
102
|
+
* [2.6.3.7 :filter](#2637-filter)
|
103
|
+
* [2.6.3.8 :min](#2638-min)
|
104
|
+
* [2.6.3.9 :max](#2639-max)
|
93
105
|
* [2.6.4 enum_select](#264-enum_select)
|
94
106
|
* [2.6.4.1 :per_page](#2641-per_page)
|
95
107
|
* [2.6.4.1 :disabled](#2641-disabled)
|
96
108
|
* [2.7 expand](#27-expand)
|
97
|
-
* [2.7.1 auto_hint](#271-auto_hint)
|
109
|
+
* [2.7.1 :auto_hint](#271-auto_hint)
|
98
110
|
* [2.8 collect](#28-collect)
|
99
111
|
* [2.9 suggest](#29-suggest)
|
100
112
|
* [2.10 slider](#210-slider)
|
@@ -110,7 +122,8 @@ Or install it yourself as:
|
|
110
122
|
* [3.4 :help_color](#34-help_color)
|
111
123
|
* [3.5 :interrupt](#35-interrupt)
|
112
124
|
* [3.6 :prefix](#36-prefix)
|
113
|
-
* [3.7 :
|
125
|
+
* [3.7 :quiet](#37-quiet)
|
126
|
+
* [3.8 :track_history](#38-track_history)
|
114
127
|
|
115
128
|
## 1. Usage
|
116
129
|
|
@@ -125,14 +138,14 @@ prompt = TTY::Prompt.new
|
|
125
138
|
And then call `ask` with the question for simple input:
|
126
139
|
|
127
140
|
```ruby
|
128
|
-
prompt.ask(
|
141
|
+
prompt.ask("What is your name?", default: ENV["USER"])
|
129
142
|
# => What is your name? (piotr)
|
130
143
|
```
|
131
144
|
|
132
145
|
To confirm input use `yes?`:
|
133
146
|
|
134
147
|
```ruby
|
135
|
-
prompt.yes?(
|
148
|
+
prompt.yes?("Do you like Ruby?")
|
136
149
|
# => Do you like Ruby? (Y/n)
|
137
150
|
```
|
138
151
|
|
@@ -187,13 +200,13 @@ However, if you have a lot of options to choose from you may want to use `expand
|
|
187
200
|
|
188
201
|
```ruby
|
189
202
|
choices = [
|
190
|
-
{ key:
|
191
|
-
{ key:
|
192
|
-
{ key:
|
193
|
-
{ key:
|
194
|
-
{ key:
|
203
|
+
{ key: "y", name: "overwrite this file", value: :yes },
|
204
|
+
{ key: "n", name: "do not overwrite this file", value: :no },
|
205
|
+
{ key: "a", name: "overwrite this file and all later files", value: :all },
|
206
|
+
{ key: "d", name: "show diff", value: :diff },
|
207
|
+
{ key: "q", name: "quit; do not overwrite this file ", value: :quit }
|
195
208
|
]
|
196
|
-
prompt.expand(
|
209
|
+
prompt.expand("Overwrite Gemfile?", choices)
|
197
210
|
# =>
|
198
211
|
# Overwrite Gemfile? (enter "h" for help) [y,n,a,d,q,h]
|
199
212
|
```
|
@@ -202,14 +215,14 @@ If you wish to collect more than one answer use `collect`:
|
|
202
215
|
|
203
216
|
```ruby
|
204
217
|
result = prompt.collect do
|
205
|
-
key(:name).ask(
|
218
|
+
key(:name).ask("Name?")
|
206
219
|
|
207
|
-
key(:age).ask(
|
220
|
+
key(:age).ask("Age?", convert: :int)
|
208
221
|
|
209
222
|
key(:address) do
|
210
|
-
key(:street).ask(
|
211
|
-
key(:city).ask(
|
212
|
-
key(:zip).ask(
|
223
|
+
key(:street).ask("Street?", required: true)
|
224
|
+
key(:city).ask("City?")
|
225
|
+
key(:zip).ask("Zip?", validate: /\A\d{3}\Z/)
|
213
226
|
end
|
214
227
|
end
|
215
228
|
# =>
|
@@ -236,25 +249,41 @@ prompt.ask("What is your name?") do |q|
|
|
236
249
|
end
|
237
250
|
```
|
238
251
|
|
239
|
-
#### 2.1.1 convert
|
252
|
+
#### 2.1.1 `:convert`
|
240
253
|
|
241
254
|
The `convert` property is used to convert input to a required type.
|
242
255
|
|
243
|
-
By default no conversion is performed.
|
256
|
+
By default no conversion of input is performed. To change this use one of the following conversions:
|
244
257
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
+
* `:boolean`|`:bool` - e.g. 'yes/1/y/t/' becomes `true`, 'no/0/n/f' becomes `false`
|
259
|
+
* `:date` - parses dates formats "28/03/2020", "March 28th 2020"
|
260
|
+
* `:time` - parses time formats "11:20:03"
|
261
|
+
* `:float` - e.g. `-1` becomes `-1.0`
|
262
|
+
* `:int`|`:integer` - e.g. `+1` becomes `1`
|
263
|
+
* `:sym`|`:symbol` - e.g. "foo" becomes `:foo`
|
264
|
+
* `:filepath` - converts to file path
|
265
|
+
* `:path`|`:pathname` - converts to `Pathname` object
|
266
|
+
* `:range` - e.g. '1-10' becomes `1..10` range object
|
267
|
+
* `:regexp` - e.g. "foo|bar" becomes `/foo|bar/`
|
268
|
+
* `:uri` - converts to `URI` object
|
269
|
+
* `:list`|`:array` - e.g. 'a,b,c' becomes `["a", "b", "c"]`
|
270
|
+
* `:map`|`:hash` - e.g. 'a:1 b:2 c:3' becomes `{a: "1", b: "2", c: "3"}`
|
271
|
+
|
272
|
+
In addition you can specify a plural or append `list` or `array` to any base type:
|
273
|
+
|
274
|
+
* `:ints` or `:int_list` - will convert to a list of integers
|
275
|
+
* `:floats` or `:float_list` - will convert to a list of floats
|
276
|
+
* `:bools` or `:bool_list` - will convert to a list of booleans, e.g. `t,f,t` becomes `[true, false, true]`
|
277
|
+
|
278
|
+
Similarly, you can append `map` or `hash` to any base type:
|
279
|
+
|
280
|
+
* `:int_map`|`:integer_map`|`:int_hash` - will convert to a hash of integers, e.g `a:1 b:2 c:3` becomes `{a: 1, b: 2, c: 3}`
|
281
|
+
* `:bool_map` | `:boolean_map`|`:bool_hash` - will convert to a hash of booleans, e.g `a:t b:f c:t` becomes `{a: true, b: false, c: true}`
|
282
|
+
|
283
|
+
By default, `map` converts keys to symbols, if you wish to use strings instead specify key type like so:
|
284
|
+
|
285
|
+
* `:str_int_map` - will convert to a hash of string keys and integer values
|
286
|
+
* `:string_integer_hash` - will convert to a hash of string keys and integer values
|
258
287
|
|
259
288
|
For example, if you are interested in range type as answer do the following:
|
260
289
|
|
@@ -264,27 +293,54 @@ prompt.ask("Provide range of numbers?", convert: :range)
|
|
264
293
|
# => 1..10
|
265
294
|
```
|
266
295
|
|
296
|
+
If, on the other hand, you wish to convert input to a hash of integer values do:
|
297
|
+
|
298
|
+
```ruby
|
299
|
+
prompt.ask("Provide keys and values:", convert: :int_map)
|
300
|
+
# Provide keys and values: a=1 b=2 c=3
|
301
|
+
# => {a: 1, b: 2, c: 3}
|
302
|
+
```
|
303
|
+
|
304
|
+
If a user provides a wrong type for conversion an error message will be printed in the console:
|
305
|
+
|
306
|
+
```ruby
|
307
|
+
prompt.ask("Provide digit:", convert: float)
|
308
|
+
# Provide digit: x
|
309
|
+
# >> Cannot convert `x` into 'float' type
|
310
|
+
```
|
311
|
+
|
312
|
+
You can further customize error message:
|
313
|
+
|
314
|
+
```ruby
|
315
|
+
prompt.ask("Provide digit:", convert: float) do |q|
|
316
|
+
q.convert(:float, "Wrong value of %{value} for %{type} conversion"
|
317
|
+
# or
|
318
|
+
q.convert :float
|
319
|
+
q.messages[:convert?] = "Wrong value of %{value} for %{type} conversion"
|
320
|
+
end
|
321
|
+
```
|
322
|
+
|
267
323
|
You can also provide a custom conversion like so:
|
268
324
|
|
269
325
|
```ruby
|
270
|
-
prompt.ask(
|
326
|
+
prompt.ask("Ingredients? (comma sep list)") do |q|
|
271
327
|
q.convert -> (input) { input.split(/,\s*/) }
|
272
328
|
end
|
273
329
|
# Ingredients? (comma sep list) milk, eggs, flour
|
274
|
-
# => [
|
330
|
+
# => ["milk", "eggs", "flour"]
|
275
331
|
```
|
276
332
|
|
277
|
-
#### 2.1.2 default
|
333
|
+
#### 2.1.2 `:default`
|
278
334
|
|
279
335
|
The `:default` option is used if the user presses return key:
|
280
336
|
|
281
337
|
```ruby
|
282
|
-
prompt.ask(
|
338
|
+
prompt.ask("What is your name?", default: "Anonymous")
|
283
339
|
# =>
|
284
340
|
# What is your name? (Anonymous)
|
285
341
|
```
|
286
342
|
|
287
|
-
#### 2.1.3 value
|
343
|
+
#### 2.1.3 `:value`
|
288
344
|
|
289
345
|
To pre-populate the input line for editing use `:value` option:
|
290
346
|
|
@@ -294,12 +350,12 @@ prompt.ask("What is your name?", value: "Piotr")
|
|
294
350
|
# What is your name? Piotr
|
295
351
|
```
|
296
352
|
|
297
|
-
#### 2.1.4 echo
|
353
|
+
#### 2.1.4 `:echo`
|
298
354
|
|
299
355
|
To control whether the input is shown back in terminal or not use `:echo` option like so:
|
300
356
|
|
301
357
|
```ruby
|
302
|
-
prompt.ask(
|
358
|
+
prompt.ask("password:", echo: false)
|
303
359
|
```
|
304
360
|
|
305
361
|
#### 2.1.5 error messages
|
@@ -309,43 +365,43 @@ By default `tty-prompt` comes with predefined error messages for `required`, `in
|
|
309
365
|
You can change these and configure to your liking either by passing message as second argument with the option:
|
310
366
|
|
311
367
|
```ruby
|
312
|
-
prompt.ask(
|
313
|
-
q.validate(/\A\w+@\w+\.\w+\Z/,
|
368
|
+
prompt.ask("What is your email?") do |q|
|
369
|
+
q.validate(/\A\w+@\w+\.\w+\Z/, "Invalid email address")
|
314
370
|
end
|
315
371
|
```
|
316
372
|
|
317
373
|
Or change the `messages` key entry out of `:required?`, `:valid?`, `:range?`:
|
318
374
|
|
319
375
|
```ruby
|
320
|
-
prompt.ask(
|
376
|
+
prompt.ask("What is your email?") do |q|
|
321
377
|
q.validate(/\A\w+@\w+\.\w+\Z/)
|
322
|
-
q.messages[:valid?] =
|
378
|
+
q.messages[:valid?] = "Invalid email address"
|
323
379
|
end
|
324
380
|
```
|
325
381
|
|
326
382
|
To change default range validation error message do:
|
327
383
|
|
328
384
|
```ruby
|
329
|
-
prompt.ask(
|
330
|
-
q.in
|
331
|
-
q.messages[:range?] =
|
385
|
+
prompt.ask("How spicy on scale (1-5)? ") do |q|
|
386
|
+
q.in "1-5"
|
387
|
+
q.messages[:range?] = "%{value} out of expected range #{in}"
|
332
388
|
end
|
333
389
|
```
|
334
390
|
|
335
|
-
#### 2.1.6 in
|
391
|
+
#### 2.1.6 `:in`
|
336
392
|
|
337
393
|
In order to check that provided input falls inside a range of inputs use the `in` option. For example, if we wanted to ask a user for a single digit in given range we may do following:
|
338
394
|
|
339
395
|
```ruby
|
340
|
-
ask("Provide number in range: 0-9?") { |q| q.in(
|
396
|
+
ask("Provide number in range: 0-9?") { |q| q.in("0-9") }
|
341
397
|
```
|
342
398
|
|
343
|
-
#### 2.1.7 modify
|
399
|
+
#### 2.1.7 `:modify`
|
344
400
|
|
345
401
|
Set the `:modify` option if you want to handle whitespace or letter capitalization.
|
346
402
|
|
347
403
|
```ruby
|
348
|
-
prompt.ask(
|
404
|
+
prompt.ask("Enter text:") do |q|
|
349
405
|
q.modify :strip, :collapse
|
350
406
|
end
|
351
407
|
```
|
@@ -367,7 +423,7 @@ Available whitespace settings are:
|
|
367
423
|
:remove # remove all whitespace
|
368
424
|
```
|
369
425
|
|
370
|
-
#### 2.1.8 required
|
426
|
+
#### 2.1.8 `:required`
|
371
427
|
|
372
428
|
To ensure that input is provided use `:required` option:
|
373
429
|
|
@@ -377,18 +433,18 @@ prompt.ask("What's your phone number?", required: true)
|
|
377
433
|
# >> Value must be provided
|
378
434
|
```
|
379
435
|
|
380
|
-
#### 2.1.9 validate
|
436
|
+
#### 2.1.9 `:validate`
|
381
437
|
|
382
438
|
In order to validate that input matches a given pattern you can pass the `validate` option. Validate setting accepts `Regex`, `Proc` or `Symbol`.
|
383
439
|
|
384
440
|
```ruby
|
385
|
-
prompt.ask(
|
441
|
+
prompt.ask("What is your username?") do |q|
|
386
442
|
q.validate /^[^\.]+\.[^\.]+/
|
387
443
|
end
|
388
444
|
```
|
389
445
|
|
390
446
|
```ruby
|
391
|
-
prompt.ask(
|
447
|
+
prompt.ask("What is your username?") do |q|
|
392
448
|
q.validate { |input| input =~ /^[^\.]+\.[^\.]+/ }
|
393
449
|
end
|
394
450
|
```
|
@@ -396,7 +452,7 @@ end
|
|
396
452
|
The **TTY::Prompt** comes with built-in validations for `:email` and you can use them directly like so:
|
397
453
|
|
398
454
|
```ruby
|
399
|
-
prompt.ask(
|
455
|
+
prompt.ask("What is your email?") { |q| q.validate :email }
|
400
456
|
```
|
401
457
|
|
402
458
|
### 2.2. keypress
|
@@ -445,15 +501,15 @@ prompt.multiline("Description?")
|
|
445
501
|
The `multiline` uses similar options to those supported by `ask` prompt. For example, to provide default description:
|
446
502
|
|
447
503
|
```ruby
|
448
|
-
prompt.multiline("Description?", default:
|
504
|
+
prompt.multiline("Description?", default: "A super sweet prompt.")
|
449
505
|
```
|
450
506
|
|
451
507
|
Or using DSL:
|
452
508
|
|
453
509
|
```ruby
|
454
510
|
prompt.multiline("Description?") do |q|
|
455
|
-
q.default
|
456
|
-
q.help
|
511
|
+
q.default "A super sweet prompt."
|
512
|
+
q.help "Press thy ctrl+d to end"
|
457
513
|
end
|
458
514
|
```
|
459
515
|
|
@@ -462,28 +518,28 @@ end
|
|
462
518
|
If you require input of confidential information use `mask` method. By default each character that is printed is replaced by `•` symbol. All configuration options applicable to `ask` method can be used with `mask` as well.
|
463
519
|
|
464
520
|
```ruby
|
465
|
-
prompt.mask(
|
521
|
+
prompt.mask("What is your secret?")
|
466
522
|
# => What is your secret? ••••
|
467
523
|
```
|
468
524
|
|
469
525
|
The masking character can be changed by passing `:symbols` option with `:mask` key:
|
470
526
|
|
471
527
|
```ruby
|
472
|
-
heart = prompt.decorate(prompt.symbols[:heart] +
|
473
|
-
prompt.mask(
|
528
|
+
heart = prompt.decorate(prompt.symbols[:heart] + " ", :magenta)
|
529
|
+
prompt.mask("What is your secret?", symbols: {mask: heart})
|
474
530
|
# => What is your secret? ❤ ❤ ❤ ❤ ❤
|
475
531
|
```
|
476
532
|
|
477
533
|
If you don't wish to show any output use `:echo` option like so:
|
478
534
|
|
479
535
|
```ruby
|
480
|
-
prompt.mask(
|
536
|
+
prompt.mask("What is your secret?", echo: false)
|
481
537
|
```
|
482
538
|
|
483
539
|
You can also provide validation for your mask to enforce for instance strong passwords:
|
484
540
|
|
485
541
|
```ruby
|
486
|
-
prompt.mask(
|
542
|
+
prompt.mask("What is your secret?", mask: heart) do |q|
|
487
543
|
q.validate(/[a-z\ ]{5,15}/)
|
488
544
|
end
|
489
545
|
```
|
@@ -493,7 +549,7 @@ end
|
|
493
549
|
In order to display a query asking for boolean input from user use `yes?` like so:
|
494
550
|
|
495
551
|
```ruby
|
496
|
-
prompt.yes?(
|
552
|
+
prompt.yes?("Do you like Ruby?")
|
497
553
|
# =>
|
498
554
|
# Do you like Ruby? (Y/n)
|
499
555
|
```
|
@@ -504,7 +560,7 @@ It's enough to provide the `suffix` option for the prompt to accept matching ans
|
|
504
560
|
|
505
561
|
```ruby
|
506
562
|
prompt.yes?("Are you a human?") do |q|
|
507
|
-
q.suffix
|
563
|
+
q.suffix "Yup/nope"
|
508
564
|
end
|
509
565
|
# =>
|
510
566
|
# Are you a human? (Yup/nope)
|
@@ -515,8 +571,8 @@ Alternatively, instead of `suffix` option provide the `positive` and `negative`
|
|
515
571
|
```ruby
|
516
572
|
prompt.yes?("Are you a human?") do |q|
|
517
573
|
q.default false
|
518
|
-
q.positive
|
519
|
-
q.negative
|
574
|
+
q.positive "Yup"
|
575
|
+
q.negative "Nope"
|
520
576
|
end
|
521
577
|
# =>
|
522
578
|
# Are you a human? (yup/Nope)
|
@@ -525,10 +581,10 @@ end
|
|
525
581
|
Finally, providing all available options you can ask fully customized question:
|
526
582
|
|
527
583
|
```ruby
|
528
|
-
prompt.yes?(
|
529
|
-
q.suffix
|
530
|
-
q.positive
|
531
|
-
q.negative
|
584
|
+
prompt.yes?("Are you a human?") do |q|
|
585
|
+
q.suffix "Agree/Disagree"
|
586
|
+
q.positive "Agree"
|
587
|
+
q.negative "Disagree"
|
532
588
|
q.convert -> (input) { !input.match(/^agree$/i).nil? }
|
533
589
|
end
|
534
590
|
# =>
|
@@ -538,7 +594,7 @@ end
|
|
538
594
|
There is also the opposite for asking confirmation of negative question:
|
539
595
|
|
540
596
|
```ruby
|
541
|
-
prompt.no?(
|
597
|
+
prompt.no?("Do you hate Ruby?")
|
542
598
|
# =>
|
543
599
|
# Do you hate Ruby? (y/N)
|
544
600
|
```
|
@@ -565,9 +621,9 @@ Finally, you can define an array of choices where each choice is a hash value wi
|
|
565
621
|
|
566
622
|
```ruby
|
567
623
|
choices = [
|
568
|
-
{name:
|
569
|
-
{name:
|
570
|
-
{name:
|
624
|
+
{name: "small", value: 1},
|
625
|
+
{name: "medium", value: 2, disabled: "(out of stock)"},
|
626
|
+
{name: "large", value: 3}
|
571
627
|
]
|
572
628
|
```
|
573
629
|
|
@@ -576,20 +632,20 @@ You can specify `:key` as an additional option which will be used as short name
|
|
576
632
|
Another way to create menu with choices is using the DSL and the `choice` method. For example, the previous array of choices with hash values can be translated as:
|
577
633
|
|
578
634
|
```ruby
|
579
|
-
prompt.select(
|
580
|
-
menu.choice name:
|
581
|
-
menu.choice name:
|
582
|
-
menu.choice name:
|
635
|
+
prompt.select("What size?") do |menu|
|
636
|
+
menu.choice name: "small", value: 1
|
637
|
+
menu.choice name: "medium", value: 2, disabled: "(out of stock)"
|
638
|
+
menu.choice name: "large", value: 3
|
583
639
|
end
|
584
640
|
```
|
585
641
|
|
586
642
|
or in a more compact way:
|
587
643
|
|
588
644
|
```ruby
|
589
|
-
prompt.select(
|
590
|
-
menu.choice
|
591
|
-
menu.choice
|
592
|
-
menu.choice
|
645
|
+
prompt.select("What size?") do |menu|
|
646
|
+
menu.choice "small", 1
|
647
|
+
menu.choice "medium", 2, disabled: "(out of stock)"
|
648
|
+
menu.choice "large", 3
|
593
649
|
end
|
594
650
|
```
|
595
651
|
|
@@ -599,9 +655,9 @@ The `:disabled` key indicates to display a choice as currently unavailable to se
|
|
599
655
|
|
600
656
|
```ruby
|
601
657
|
choices = [
|
602
|
-
{name:
|
603
|
-
{name:
|
604
|
-
{name:
|
658
|
+
{name: "small", value: 1},
|
659
|
+
{name: "medium", value: 2, disabled: "(out of stock)"},
|
660
|
+
{name: "large", value: 3}
|
605
661
|
]
|
606
662
|
```
|
607
663
|
|
@@ -622,9 +678,9 @@ You can also provide options through DSL using the `choice` method for single en
|
|
622
678
|
|
623
679
|
```ruby
|
624
680
|
prompt.select("Choose your destiny?") do |menu|
|
625
|
-
menu.choice
|
626
|
-
menu.choice
|
627
|
-
menu.choice
|
681
|
+
menu.choice "Scorpion"
|
682
|
+
menu.choice "Kano"
|
683
|
+
menu.choice "Jax"
|
628
684
|
end
|
629
685
|
# =>
|
630
686
|
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
|
@@ -637,9 +693,9 @@ By default the choice name is used as return value, but you can provide your cus
|
|
637
693
|
|
638
694
|
```ruby
|
639
695
|
prompt.select("Choose your destiny?") do |menu|
|
640
|
-
menu.choice
|
641
|
-
menu.choice
|
642
|
-
menu.choice
|
696
|
+
menu.choice "Scorpion", 1
|
697
|
+
menu.choice "Kano", 2
|
698
|
+
menu.choice "Jax", -> { "Nice choice captain!" }
|
643
699
|
end
|
644
700
|
# =>
|
645
701
|
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
|
@@ -651,7 +707,7 @@ end
|
|
651
707
|
If you wish you can also provide a simple hash to denote choice name and its value like so:
|
652
708
|
|
653
709
|
```ruby
|
654
|
-
choices = {
|
710
|
+
choices = {"Scorpion" => 1, "Kano" => 2, "Jax" => 3}
|
655
711
|
prompt.select("Choose your destiny?", choices)
|
656
712
|
```
|
657
713
|
|
@@ -661,9 +717,9 @@ To mark particular answer as selected use `default` with index of the option sta
|
|
661
717
|
prompt.select("Choose your destiny?") do |menu|
|
662
718
|
menu.default 3
|
663
719
|
|
664
|
-
menu.choice
|
665
|
-
menu.choice
|
666
|
-
menu.choice
|
720
|
+
menu.choice "Scorpion", 1
|
721
|
+
menu.choice "Kano", 2
|
722
|
+
menu.choice "Jax", 3
|
667
723
|
end
|
668
724
|
# =>
|
669
725
|
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
|
@@ -672,6 +728,9 @@ end
|
|
672
728
|
# ‣ Jax
|
673
729
|
```
|
674
730
|
|
731
|
+
|
732
|
+
#### 2.6.2.1 `:cycle`
|
733
|
+
|
675
734
|
You can navigate the choices using the arrow keys or define your own key mappings (see [keyboard events](#212-keyboard-events). When reaching the top/bottom of the list, the selection does not cycle around by default. If you wish to enable cycling, you can pass `cycle: true` to `select` and `multi_select`:
|
676
735
|
|
677
736
|
```ruby
|
@@ -683,15 +742,17 @@ prompt.select("Choose your destiny?", %w(Scorpion Kano Jax), cycle: true)
|
|
683
742
|
# Jax
|
684
743
|
```
|
685
744
|
|
745
|
+
#### 2.6.2.2 `:enum`
|
746
|
+
|
686
747
|
For ordered choices set `enum` to any delimiter String. In that way, you can use arrows keys and numbers (0-9) to select the item.
|
687
748
|
|
688
749
|
```ruby
|
689
750
|
prompt.select("Choose your destiny?") do |menu|
|
690
|
-
menu.enum
|
751
|
+
menu.enum "."
|
691
752
|
|
692
|
-
menu.choice
|
693
|
-
menu.choice
|
694
|
-
menu.choice
|
753
|
+
menu.choice "Scorpion", 1
|
754
|
+
menu.choice "Kano", 2
|
755
|
+
menu.choice "Jax", 3
|
695
756
|
end
|
696
757
|
# =>
|
697
758
|
# Choose your destiny? (Use ↑/↓ arrow or number (0-9) keys, press Enter to select)
|
@@ -700,24 +761,40 @@ end
|
|
700
761
|
# ‣ 3. Jax
|
701
762
|
```
|
702
763
|
|
703
|
-
|
764
|
+
#### 2.6.2.3 `:help`
|
765
|
+
|
766
|
+
You can configure help message with `:help` and when to display it with `:show_help` options. The help can be displayed on `start`, `never` or `always`:
|
704
767
|
|
705
768
|
```ruby
|
706
769
|
choices = %w(Scorpion Kano Jax)
|
707
|
-
prompt.select("Choose your destiny?", choices, help: "(Bash keyboard)",
|
770
|
+
prompt.select("Choose your destiny?", choices, help: "(Bash keyboard keys)", show_help: :always)
|
708
771
|
# =>
|
709
|
-
# Choose your destiny? (Bash keyboard)
|
772
|
+
# Choose your destiny? (Bash keyboard keys)
|
710
773
|
# > Scorpion
|
711
774
|
# Kano
|
712
775
|
# Jax
|
713
776
|
```
|
714
777
|
|
715
|
-
#### 2.6.
|
778
|
+
#### 2.6.2.4 `:marker`
|
779
|
+
|
780
|
+
You can configure active marker like so:
|
781
|
+
|
782
|
+
```ruby
|
783
|
+
choices = %w(Scorpion Kano Jax)
|
784
|
+
prompt.select("Choose your destiny?", choices, symbols: { marker: ">" })
|
785
|
+
# =>
|
786
|
+
# Choose your destiny? (Use ↑/↓ and ←/→ arrow keys, press Enter to select)
|
787
|
+
# > Scorpion
|
788
|
+
# Kano
|
789
|
+
# Jax
|
790
|
+
```
|
791
|
+
|
792
|
+
#### 2.6.2.5 `:per_page`
|
716
793
|
|
717
794
|
By default the menu is paginated if selection grows beyond `6` items. To change this setting use `:per_page` configuration.
|
718
795
|
|
719
796
|
```ruby
|
720
|
-
letters = (
|
797
|
+
letters = ("A".."Z").to_a
|
721
798
|
prompt.select("Choose your letter?", letters, per_page: 4)
|
722
799
|
# =>
|
723
800
|
# Which letter? (Use ↑/↓ and ←/→ arrow keys, press Enter to select)
|
@@ -729,10 +806,10 @@ prompt.select("Choose your letter?", letters, per_page: 4)
|
|
729
806
|
|
730
807
|
You can also customise page navigation text using `:help` option:
|
731
808
|
```ruby
|
732
|
-
letters = (
|
809
|
+
letters = ("A".."Z").to_a
|
733
810
|
prompt.select("Choose your letter?") do |menu|
|
734
811
|
menu.per_page 4
|
735
|
-
menu.help
|
812
|
+
menu.help "(Wiggle thy finger up/down and left/right to see more)"
|
736
813
|
menu.choices letters
|
737
814
|
end
|
738
815
|
# =>
|
@@ -743,25 +820,25 @@ end
|
|
743
820
|
# D
|
744
821
|
```
|
745
822
|
|
746
|
-
#### 2.6.2.
|
823
|
+
#### 2.6.2.6 `:disabled`
|
747
824
|
|
748
825
|
To disable menu choice, use the `:disabled` key with a value that explains the reason for the choice being unavailable. For example, out of all warriors, the Goro is currently injured:
|
749
826
|
|
750
827
|
```ruby
|
751
828
|
warriors = [
|
752
|
-
|
753
|
-
|
754
|
-
{ name:
|
755
|
-
|
756
|
-
|
757
|
-
|
829
|
+
"Scorpion",
|
830
|
+
"Kano",
|
831
|
+
{ name: "Goro", disabled: "(injury)" },
|
832
|
+
"Jax",
|
833
|
+
"Kitana",
|
834
|
+
"Raiden"
|
758
835
|
]
|
759
836
|
```
|
760
837
|
|
761
838
|
The disabled choice will be displayed with a cross `✘` character next to it and followed by an explanation:
|
762
839
|
|
763
840
|
```ruby
|
764
|
-
prompt.select(
|
841
|
+
prompt.select("Choose your destiny?", warriors)
|
765
842
|
# =>
|
766
843
|
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
|
767
844
|
# ‣ Scorpion
|
@@ -772,13 +849,13 @@ prompt.select('Choose your destiny?', warriors)
|
|
772
849
|
# Raiden
|
773
850
|
```
|
774
851
|
|
775
|
-
#### 2.6.2.
|
852
|
+
#### 2.6.2.7 `:filter`
|
776
853
|
|
777
854
|
To activate dynamic list searching on letter/number key presses use `:filter` option:
|
778
855
|
|
779
856
|
```ruby
|
780
857
|
warriors = %w(Scorpion Kano Jax Kitana Raiden)
|
781
|
-
prompt.select(
|
858
|
+
prompt.select("Choose your destiny?", warriors, filter: true)
|
782
859
|
# =>
|
783
860
|
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select, and letter keys to filter)
|
784
861
|
# ‣ Scorpion
|
@@ -868,11 +945,21 @@ end
|
|
868
945
|
# ‣ ⬢ bourbon
|
869
946
|
```
|
870
947
|
|
948
|
+
#### 2.6.3.1 `:cycle`
|
949
|
+
|
950
|
+
Also like, `select`, the method takes an option `cycle` (which defaults to `false`), which lets you configure whether the selection should cycle around when reaching the top/bottom of the list when navigating:
|
951
|
+
|
952
|
+
```ruby
|
953
|
+
prompt.multi_select("Select drinks?", %w(vodka beer wine), cycle: true)
|
954
|
+
```
|
955
|
+
|
956
|
+
#### 2.6.3.2 `:enum`
|
957
|
+
|
871
958
|
Like `select`, for ordered choices set `enum` to any delimiter String. In that way, you can use arrows keys and numbers (0-9) to select the item.
|
872
959
|
|
873
960
|
```ruby
|
874
961
|
prompt.multi_select("Select drinks?") do |menu|
|
875
|
-
menu.enum
|
962
|
+
menu.enum ")"
|
876
963
|
|
877
964
|
menu.choice :vodka, {score: 10}
|
878
965
|
menu.choice :beer, {score: 20}
|
@@ -896,17 +983,13 @@ And when you press enter you will see the following selected:
|
|
896
983
|
# => [{score: 20}, {score: 50}]
|
897
984
|
```
|
898
985
|
|
899
|
-
|
900
|
-
|
901
|
-
```ruby
|
902
|
-
prompt.multi_select("Select drinks?", %w(vodka beer wine), cycle: true)
|
903
|
-
```
|
986
|
+
#### 2.6.3.3 `:help`
|
904
987
|
|
905
|
-
You can configure help message and
|
988
|
+
You can configure help message with `:help` and when to display it with `:show_help` options. The help can be displayed on `start`, `never` or `always`:
|
906
989
|
|
907
990
|
```ruby
|
908
991
|
choices = {vodka: 1, beer: 2, wine: 3, whisky: 4, bourbon: 5}
|
909
|
-
prompt.multi_select("Select drinks?", choices, help:
|
992
|
+
prompt.multi_select("Select drinks?", choices, help: "Press beer can against keyboard", show_help: :always)
|
910
993
|
# =>
|
911
994
|
# Select drinks? (Press beer can against keyboard)"
|
912
995
|
# ‣ ⬡ vodka
|
@@ -916,10 +999,12 @@ prompt.multi_select("Select drinks?", choices, help: 'Press beer can against key
|
|
916
999
|
# ⬡ bourbon
|
917
1000
|
```
|
918
1001
|
|
1002
|
+
#### 2.6.3.4 `:per_page`
|
1003
|
+
|
919
1004
|
By default the menu is paginated if selection grows beyond `6` items. To change this setting use `:per_page` configuration.
|
920
1005
|
|
921
1006
|
```ruby
|
922
|
-
letters = (
|
1007
|
+
letters = ("A".."Z").to_a
|
923
1008
|
prompt.multi_select("Choose your letter?", letters, per_page: 4)
|
924
1009
|
# =>
|
925
1010
|
# Which letter? (Use ↑/↓ and ←/→ arrow keys, press Space to select and Enter to finish)
|
@@ -929,25 +1014,25 @@ prompt.multi_select("Choose your letter?", letters, per_page: 4)
|
|
929
1014
|
# ⬡ D
|
930
1015
|
```
|
931
1016
|
|
932
|
-
#### 2.6.3.
|
1017
|
+
#### 2.6.3.5 `:disabled`
|
933
1018
|
|
934
1019
|
To disable menu choice, use the `:disabled` key with a value that explains the reason for the choice being unavailable. For example, out of all drinks, the sake and beer are currently out of stock:
|
935
1020
|
|
936
1021
|
```ruby
|
937
1022
|
drinks = [
|
938
|
-
|
939
|
-
{name:
|
940
|
-
|
941
|
-
{name:
|
942
|
-
|
943
|
-
|
1023
|
+
"bourbon",
|
1024
|
+
{name: "sake", disabled: "(out of stock)"},
|
1025
|
+
"vodka",
|
1026
|
+
{name: "beer", disabled: "(out of stock)"},
|
1027
|
+
"wine",
|
1028
|
+
"whisky"
|
944
1029
|
]
|
945
1030
|
```
|
946
1031
|
|
947
1032
|
The disabled choice will be displayed with a cross `✘` character next to it and followed by an explanation:
|
948
1033
|
|
949
1034
|
```ruby
|
950
|
-
prompt.multi_select(
|
1035
|
+
prompt.multi_select("Choose your favourite drink?", drinks)
|
951
1036
|
# =>
|
952
1037
|
# Choose your favourite drink? (Use ↑/↓ arrow keys, press Space to select and Enter to finish)
|
953
1038
|
# ‣ ⬡ bourbon
|
@@ -958,7 +1043,7 @@ prompt.multi_select('Choose your favourite drink?', drinks)
|
|
958
1043
|
# ⬡ whisky
|
959
1044
|
```
|
960
1045
|
|
961
|
-
#### 2.6.3.
|
1046
|
+
#### 2.6.3.6 `:echo`
|
962
1047
|
|
963
1048
|
To control whether the selected items are shown on the question
|
964
1049
|
header use the :echo option:
|
@@ -975,7 +1060,7 @@ prompt.multi_select("Select drinks?", choices, echo: false)
|
|
975
1060
|
# ‣ ⬢ 5) bourbon
|
976
1061
|
```
|
977
1062
|
|
978
|
-
#### 2.6.3.
|
1063
|
+
#### 2.6.3.7 `:filter`
|
979
1064
|
|
980
1065
|
To activate dynamic list filtering on letter/number typing, use the :filter option:
|
981
1066
|
|
@@ -1005,7 +1090,7 @@ If the user changes or deletes a filter, the choices previously selected remain
|
|
1005
1090
|
|
1006
1091
|
The `filter` option is not compatible with `enum`.
|
1007
1092
|
|
1008
|
-
#### 2.6.3.
|
1093
|
+
#### 2.6.3.8 `:min`
|
1009
1094
|
|
1010
1095
|
To force the minimum number of choices an user must select, use the `:min` option:
|
1011
1096
|
|
@@ -1021,7 +1106,7 @@ prompt.multi_select("Select drinks?", choices, min: 3)
|
|
1021
1106
|
# ‣ ⬡ bourbon
|
1022
1107
|
```
|
1023
1108
|
|
1024
|
-
#### 2.6.3.
|
1109
|
+
#### 2.6.3.9 `:max`
|
1025
1110
|
|
1026
1111
|
To limit the number of choices an user can select, use the `:max` option:
|
1027
1112
|
|
@@ -1043,7 +1128,7 @@ In order to ask for standard selection from indexed list you can use `enum_selec
|
|
1043
1128
|
|
1044
1129
|
```ruby
|
1045
1130
|
choices = %w(emacs nano vim)
|
1046
|
-
prompt.enum_select("Select an editor?")
|
1131
|
+
prompt.enum_select("Select an editor?", choices)
|
1047
1132
|
# =>
|
1048
1133
|
#
|
1049
1134
|
# Select an editor?
|
@@ -1058,9 +1143,9 @@ Similar to `select` and `multi_select`, you can provide question options through
|
|
1058
1143
|
```ruby
|
1059
1144
|
choices = %w(nano vim emacs)
|
1060
1145
|
prompt.enum_select("Select an editor?") do |menu|
|
1061
|
-
menu.choice :nano,
|
1062
|
-
menu.choice :vim,
|
1063
|
-
menu.choice :emacs,
|
1146
|
+
menu.choice :nano, "/bin/nano"
|
1147
|
+
menu.choice :vim, "/usr/bin/vim"
|
1148
|
+
menu.choice :emacs, "/usr/bin/emacs"
|
1064
1149
|
end
|
1065
1150
|
# =>
|
1066
1151
|
#
|
@@ -1079,11 +1164,11 @@ You can change the indexed numbers by passing `enum` option and the default opti
|
|
1079
1164
|
choices = %w(nano vim emacs)
|
1080
1165
|
prompt.enum_select("Select an editor?") do |menu|
|
1081
1166
|
menu.default 2
|
1082
|
-
menu.enum
|
1167
|
+
menu.enum "."
|
1083
1168
|
|
1084
|
-
menu.choice :nano,
|
1085
|
-
menu.choice :vim,
|
1086
|
-
menu.choice :emacs,
|
1169
|
+
menu.choice :nano, "/bin/nano"
|
1170
|
+
menu.choice :vim, "/usr/bin/vim"
|
1171
|
+
menu.choice :emacs, "/usr/bin/emacs"
|
1087
1172
|
end
|
1088
1173
|
# =>
|
1089
1174
|
#
|
@@ -1101,7 +1186,7 @@ end
|
|
1101
1186
|
By default the menu is paginated if selection grows beyond `6` items. To change this setting use `:per_page` configuration.
|
1102
1187
|
|
1103
1188
|
```ruby
|
1104
|
-
letters = (
|
1189
|
+
letters = ("A".."Z").to_a
|
1105
1190
|
prompt.enum_select("Choose your letter?", letters, per_page: 4)
|
1106
1191
|
# =>
|
1107
1192
|
# Which letter?
|
@@ -1119,10 +1204,10 @@ To make a choice unavailable use the `:disabled` option and, if you wish, as val
|
|
1119
1204
|
|
1120
1205
|
```ruby
|
1121
1206
|
choices = [
|
1122
|
-
{name: "Emacs", disabled:
|
1207
|
+
{name: "Emacs", disabled: "(not installed)"},
|
1123
1208
|
"Atom",
|
1124
1209
|
"GNU nano",
|
1125
|
-
{name: "Notepad++", disabled:
|
1210
|
+
{name: "Notepad++", disabled: "(not installed)"},
|
1126
1211
|
"Sublime",
|
1127
1212
|
"Vim"
|
1128
1213
|
]
|
@@ -1132,7 +1217,7 @@ The disabled choice will be displayed with a cross ✘ character next to it and
|
|
1132
1217
|
|
1133
1218
|
|
1134
1219
|
```ruby
|
1135
|
-
prompt.enum_select(
|
1220
|
+
prompt.enum_select("Select an editor", choices)
|
1136
1221
|
# =>
|
1137
1222
|
# Select an editor
|
1138
1223
|
# ✘ 1) Emacs (not installed)
|
@@ -1153,16 +1238,16 @@ As first argument `expand` takes the message to display and as a second an array
|
|
1153
1238
|
```ruby
|
1154
1239
|
choices = [
|
1155
1240
|
{
|
1156
|
-
key:
|
1157
|
-
name:
|
1241
|
+
key: "y",
|
1242
|
+
name: "overwrite this file",
|
1158
1243
|
value: :yes
|
1159
1244
|
}, {
|
1160
|
-
key:
|
1161
|
-
name:
|
1245
|
+
key: "n",
|
1246
|
+
name: "do not overwrite this file",
|
1162
1247
|
value: :no
|
1163
1248
|
}, {
|
1164
|
-
key:
|
1165
|
-
name:
|
1249
|
+
key: "q",
|
1250
|
+
name: "quit; do not overwrite this file ",
|
1166
1251
|
value: :quit
|
1167
1252
|
}
|
1168
1253
|
]
|
@@ -1171,19 +1256,19 @@ choices = [
|
|
1171
1256
|
The choices can also be provided through DSL using the `choice` method. The `:value` can be a primitive value or `Proc` instance that gets executed and whose value is used as returned type. For example:
|
1172
1257
|
|
1173
1258
|
```ruby
|
1174
|
-
prompt.expand(
|
1175
|
-
q.choice key:
|
1176
|
-
q.choice key:
|
1177
|
-
q.choice key:
|
1178
|
-
q.choice key:
|
1179
|
-
q.choice key:
|
1259
|
+
prompt.expand("Overwrite Gemfile?") do |q|
|
1260
|
+
q.choice key: "y", name: "Overwrite" do :ok end
|
1261
|
+
q.choice key: "n", name: "Skip", value: :no
|
1262
|
+
q.choice key: "a", name: "Overwrite all", value: :all
|
1263
|
+
q.choice key: "d", name: "Show diff", value: :diff
|
1264
|
+
q.choice key: "q", name: "Quit", value: :quit
|
1180
1265
|
end
|
1181
1266
|
```
|
1182
1267
|
|
1183
1268
|
The first element in the array of choices or provided via `choice` DSL will be the default choice, you can change that by passing `default` option.
|
1184
1269
|
|
1185
1270
|
```ruby
|
1186
|
-
prompt.expand(
|
1271
|
+
prompt.expand("Overwrite Gemfile?", choices)
|
1187
1272
|
# =>
|
1188
1273
|
# Overwrite Gemfile? (enter "h" for help) [y,n,q,h]
|
1189
1274
|
```
|
@@ -1213,7 +1298,7 @@ Run `examples/expand.rb` to see the prompt in action.
|
|
1213
1298
|
To show hint by default use `:auto_hint` option:
|
1214
1299
|
|
1215
1300
|
```ruby
|
1216
|
-
prompt.expand(
|
1301
|
+
prompt.expand("Overwrite Gemfile?", choices, auto_hint: true)
|
1217
1302
|
# =>
|
1218
1303
|
# Overwrite Gemfile? (enter "h" for help) [y,n,q,h]
|
1219
1304
|
# >> overwrite this file
|
@@ -1227,14 +1312,14 @@ For example to gather some contact information do:
|
|
1227
1312
|
|
1228
1313
|
```ruby
|
1229
1314
|
prompt.collect do
|
1230
|
-
key(:name).ask(
|
1315
|
+
key(:name).ask("Name?")
|
1231
1316
|
|
1232
|
-
key(:age).ask(
|
1317
|
+
key(:age).ask("Age?", convert: :int)
|
1233
1318
|
|
1234
1319
|
key(:address) do
|
1235
|
-
key(:street).ask(
|
1236
|
-
key(:city).ask(
|
1237
|
-
key(:zip).ask(
|
1320
|
+
key(:street).ask("Street?", required: true)
|
1321
|
+
key(:city).ask("City?")
|
1322
|
+
key(:zip).ask("Zip?", validate: /\A\d{3}\Z/)
|
1238
1323
|
end
|
1239
1324
|
end
|
1240
1325
|
# =>
|
@@ -1245,15 +1330,15 @@ In order to collect _mutliple values_ for a given key in a loop, chain `values`
|
|
1245
1330
|
|
1246
1331
|
```ruby
|
1247
1332
|
result = prompt.collect do
|
1248
|
-
key(:name).ask(
|
1333
|
+
key(:name).ask("Name?")
|
1249
1334
|
|
1250
|
-
key(:age).ask(
|
1335
|
+
key(:age).ask("Age?", convert: :int)
|
1251
1336
|
|
1252
1337
|
while prompt.yes?("continue?")
|
1253
1338
|
key(:addresses).values do
|
1254
|
-
key(:street).ask(
|
1255
|
-
key(:city).ask(
|
1256
|
-
key(:zip).ask(
|
1339
|
+
key(:street).ask("Street?", required: true)
|
1340
|
+
key(:city).ask("City?")
|
1341
|
+
key(:zip).ask("Zip?", validate: /\A\d{3}\Z/)
|
1257
1342
|
end
|
1258
1343
|
end
|
1259
1344
|
end
|
@@ -1273,7 +1358,7 @@ end
|
|
1273
1358
|
To suggest possible matches for the user input use `suggest` method like so:
|
1274
1359
|
|
1275
1360
|
```ruby
|
1276
|
-
prompt.suggest(
|
1361
|
+
prompt.suggest("sta", ["stage", "stash", "commit", "branch"])
|
1277
1362
|
# =>
|
1278
1363
|
# Did you mean one of these?
|
1279
1364
|
# stage
|
@@ -1284,7 +1369,7 @@ To customize query text presented pass `:single_text` and `:plural_text` options
|
|
1284
1369
|
|
1285
1370
|
```ruby
|
1286
1371
|
possible = %w(status stage stash commit branch blame)
|
1287
|
-
prompt.suggest(
|
1372
|
+
prompt.suggest("b", possible, indent: 4, single_text: "Perhaps you meant?")
|
1288
1373
|
# =>
|
1289
1374
|
# Perhaps you meant?
|
1290
1375
|
# blame
|
@@ -1297,28 +1382,28 @@ If you have constrained range of numbers for user to choose from you may conside
|
|
1297
1382
|
The slider provides easy visual way of picking a value marked by `●` symbol. You can set `:min`(defaults to 0), `:max` and `:step`(defaults to 1) options to configure slider range:
|
1298
1383
|
|
1299
1384
|
```ruby
|
1300
|
-
prompt.slider(
|
1385
|
+
prompt.slider("Volume", max: 100, step: 5)
|
1301
1386
|
# =>
|
1302
1387
|
# Volume ──────────●────────── 50
|
1303
|
-
# (Use arrow keys, press Enter to select)
|
1388
|
+
# (Use ←/→ arrow keys, press Enter to select)
|
1304
1389
|
```
|
1305
1390
|
|
1306
1391
|
By default the slider is configured to pick middle of the range as a start value, you can change this by using the `:default` option:
|
1307
1392
|
|
1308
1393
|
```ruby
|
1309
|
-
prompt.slider(
|
1394
|
+
prompt.slider("Volume", max: 100, step: 5, default: 75)
|
1310
1395
|
# =>
|
1311
1396
|
# Volume ───────────────●────── 75
|
1312
|
-
# (Use arrow keys, press Enter to select)
|
1397
|
+
# (Use ←/→ arrow keys, press Enter to select)
|
1313
1398
|
```
|
1314
1399
|
|
1315
1400
|
You can also change the default slider formatting using the `:format`. The value must contain the `:slider` token to show current value and any `sprintf` compatible flag for number display, in our case `%d`:
|
1316
1401
|
|
1317
1402
|
```ruby
|
1318
|
-
prompt.slider(
|
1403
|
+
prompt.slider("Volume", max: 100, step: 5, default: 75, format: "|:slider| %d%%")
|
1319
1404
|
# =>
|
1320
1405
|
# Volume |───────────────●──────| 75%
|
1321
|
-
# (Use arrow keys, press Enter to select)
|
1406
|
+
# (Use ←/→ arrow keys, press Enter to select)
|
1322
1407
|
```
|
1323
1408
|
|
1324
1409
|
You can also specify slider range with decimal numbers. For example, to have a step of `0.5` and display each value with a single decimal place use `%f` as format:
|
@@ -1327,22 +1412,41 @@ You can also specify slider range with decimal numbers. For example, to have a s
|
|
1327
1412
|
prompt.slider("Volume", max: 10, step: 0.5, default: 5, format: "|:slider| %.1f")
|
1328
1413
|
# =>
|
1329
1414
|
# Volume |───────────────●──────| 7.5
|
1330
|
-
# (Use arrow keys, press Enter to select)
|
1415
|
+
# (Use ←/→ arrow keys, press Enter to select)
|
1416
|
+
```
|
1417
|
+
|
1418
|
+
You can alternatively provide a proc/lambda to customize your formatting even further:
|
1419
|
+
|
1420
|
+
```ruby
|
1421
|
+
slider_format = -> (slider, value) { "|#{slider}| #{value.zero? ? "muted" : "%.1f"}" % value }
|
1422
|
+
prompt.slider("Volume", max: 10, step: 0.5, default: 0, format: slider_format)
|
1423
|
+
# =>
|
1424
|
+
# Volume |●─────────────────────| muted
|
1425
|
+
# (Use ←/→ arrow keys, press Enter to select)
|
1331
1426
|
```
|
1332
1427
|
|
1333
1428
|
If you wish to change the slider handle and the slider range display use `:symbols` option:
|
1334
1429
|
|
1335
1430
|
```ruby
|
1336
|
-
prompt.slider("Volume", max: 100, step: 5, default: 75, symbols: {bullet:
|
1431
|
+
prompt.slider("Volume", max: 100, step: 5, default: 75, symbols: {bullet: "x", line: "_"})
|
1337
1432
|
# =>
|
1338
1433
|
# Volume _______________x______ 75%
|
1339
|
-
# (Use arrow keys, press Enter to select)
|
1434
|
+
# (Use ←/→ arrow keys, press Enter to select)
|
1435
|
+
```
|
1436
|
+
|
1437
|
+
You can configure help message with `:help` and when to display with `:show_help` options. The help can be displayed on `start`, `never` or `always`:
|
1438
|
+
|
1439
|
+
```ruby
|
1440
|
+
prompt.slider("Volume", max: 10, default: 7, help: "(Move arrows left and right to set value)", show_help: :always)
|
1441
|
+
# =>
|
1442
|
+
# Volume ───────────────●────── 7
|
1443
|
+
# (Move arrows left and right to set value)
|
1340
1444
|
```
|
1341
1445
|
|
1342
1446
|
Slider can be configured through DSL as well:
|
1343
1447
|
|
1344
1448
|
```ruby
|
1345
|
-
prompt.slider(
|
1449
|
+
prompt.slider("What size?") do |range|
|
1346
1450
|
range.max 100
|
1347
1451
|
range.step 5
|
1348
1452
|
range.default 75
|
@@ -1350,7 +1454,7 @@ prompt.slider('What size?') do |range|
|
|
1350
1454
|
end
|
1351
1455
|
# =>
|
1352
1456
|
# Volume |───────────────●──────| 75%
|
1353
|
-
# (Use arrow keys, press Enter to select)
|
1457
|
+
# (Use ←/→ arrow keys, press Enter to select)
|
1354
1458
|
```
|
1355
1459
|
|
1356
1460
|
### 2.11 say
|
@@ -1408,11 +1512,11 @@ For example, to add vim like key navigation to `select` prompt one would do the
|
|
1408
1512
|
|
1409
1513
|
```ruby
|
1410
1514
|
prompt.on(:keypress) do |event|
|
1411
|
-
if event.value ==
|
1515
|
+
if event.value == "j"
|
1412
1516
|
prompt.trigger(:keydown)
|
1413
1517
|
end
|
1414
1518
|
|
1415
|
-
if event.value ==
|
1519
|
+
if event.value == "k"
|
1416
1520
|
prompt.trigger(:keyup)
|
1417
1521
|
end
|
1418
1522
|
end
|
@@ -1448,7 +1552,7 @@ The available events are:
|
|
1448
1552
|
Many prompts use symbols to display information. You can overwrite the default symbols for all the prompts using the `:symbols` key and hash of symbol names as value:
|
1449
1553
|
|
1450
1554
|
```ruby
|
1451
|
-
prompt = TTY::Prompt.new(symbols: {marker:
|
1555
|
+
prompt = TTY::Prompt.new(symbols: {marker: ">"})
|
1452
1556
|
```
|
1453
1557
|
|
1454
1558
|
The following symbols can be overwritten:
|
@@ -1470,16 +1574,37 @@ The following symbols can be overwritten:
|
|
1470
1574
|
|
1471
1575
|
### 3.2 `:active_color`
|
1472
1576
|
|
1473
|
-
All prompt types support `:active_color` option.
|
1577
|
+
All prompt types support `:active_color` option. By default it's set to `:green` value.
|
1578
|
+
|
1579
|
+
The `select`, `multi_select`, `enum_select` and `expand` prompts use the active color to highlight the currently selected choice.
|
1580
|
+
|
1581
|
+
The answer provided by the user is also highlighted with the active color.
|
1582
|
+
|
1583
|
+
This `:active_color` as value accepts either a color symbol or callable object.
|
1584
|
+
|
1585
|
+
For example, to change all prompts active color to `:cyan` do:
|
1474
1586
|
|
1475
1587
|
```ruby
|
1476
1588
|
prompt = TTY::Prompt.new(active_color: :cyan)
|
1477
1589
|
```
|
1478
1590
|
|
1479
|
-
|
1591
|
+
You could also use `pastel`:
|
1480
1592
|
|
1481
1593
|
```ruby
|
1482
|
-
|
1594
|
+
notice = Pastel.new.cyan.on_blue.detach
|
1595
|
+
prompt = TTY::Prompt.new(active_color: notice)
|
1596
|
+
````
|
1597
|
+
|
1598
|
+
Or use coloring of your own choice:
|
1599
|
+
|
1600
|
+
```
|
1601
|
+
prompt = TTY::Prompt.new(active_color: ->(str) { my-color-gem(str) })
|
1602
|
+
```
|
1603
|
+
|
1604
|
+
This option can be applied either globally for all prompts or individually:
|
1605
|
+
|
1606
|
+
```ruby
|
1607
|
+
prompt.select("What size?", %w(Large Medium Small), active_color: :cyan)
|
1483
1608
|
```
|
1484
1609
|
|
1485
1610
|
Please [see pastel](https://github.com/piotrmurach/pastel#3-supported-colors) for all supported colors.
|
@@ -1489,23 +1614,44 @@ Please [see pastel](https://github.com/piotrmurach/pastel#3-supported-colors) fo
|
|
1489
1614
|
If you wish to disable coloring for a prompt simply pass `:enable_color` option
|
1490
1615
|
|
1491
1616
|
```ruby
|
1492
|
-
prompt = TTY::Prompt.new(enable_color:
|
1617
|
+
prompt = TTY::Prompt.new(enable_color: false)
|
1493
1618
|
```
|
1494
1619
|
|
1495
1620
|
### 3.4 `:help_color`
|
1496
1621
|
|
1497
|
-
|
1622
|
+
The `:help_color` option is used to customize the display color for all the help text. By default it's set to `:bright_black` value.
|
1623
|
+
|
1624
|
+
Prompts such as `select`, `multi_select`, `expand` support `:help_color`. This option can be applied either globally for all prompts or individually.
|
1625
|
+
|
1626
|
+
The `:help_color` option as value accepts either a color symbol or callable object.
|
1627
|
+
|
1628
|
+
For example, to change all prompts help color to `:cyan` do:
|
1498
1629
|
|
1499
1630
|
```ruby
|
1500
1631
|
prompt = TTY::Prompt.new(help_color: :cyan)
|
1501
1632
|
```
|
1502
1633
|
|
1503
|
-
|
1634
|
+
You could also use `pastel`:
|
1504
1635
|
|
1505
1636
|
```ruby
|
1506
|
-
|
1637
|
+
notice = Pastel.new.cyan.on_blue.detach
|
1638
|
+
prompt = TTY::Prompt.new(help_color: notice)
|
1639
|
+
````
|
1640
|
+
|
1641
|
+
Or use coloring of your own choice:
|
1642
|
+
|
1643
|
+
```
|
1644
|
+
prompt = TTY::Prompt.new(help_color: ->(str) { my-color-gem(str) })
|
1645
|
+
```
|
1646
|
+
|
1647
|
+
Or configure `:help_color` for an individual prompt:
|
1648
|
+
|
1649
|
+
```ruby
|
1650
|
+
prompt.select("What size?", %w(Large Medium Small), help_color: :cyan)
|
1507
1651
|
```
|
1508
1652
|
|
1653
|
+
Please [see pastel](https://github.com/piotrmurach/pastel#3-supported-colors) for all supported colors.
|
1654
|
+
|
1509
1655
|
### 3.5 `:interrupt`
|
1510
1656
|
|
1511
1657
|
By default `InputInterrupt` error will be raised when the user hits the interrupt key(Control-C). However, you can customise this behaviour by passing the `:interrupt` option. The available options are:
|
@@ -1526,10 +1672,21 @@ prompt = TTY::Prompt.new(interrupt: :signal)
|
|
1526
1672
|
You can prefix each question asked using the `:prefix` option. This option can be applied either globally for all prompts or individual for each one:
|
1527
1673
|
|
1528
1674
|
```ruby
|
1529
|
-
prompt = TTY::Prompt.new(prefix:
|
1675
|
+
prompt = TTY::Prompt.new(prefix: "[?] ")
|
1530
1676
|
```
|
1531
1677
|
|
1532
|
-
### 3.7 `:
|
1678
|
+
### 3.7 `:quiet`
|
1679
|
+
|
1680
|
+
Prompts such as `select`, `multi_select`, `expand`, `slider` support `:quiet` which is used to disable re-echoing of the question and answer after selection is done. This option can be applied either globally for all prompts or individually.
|
1681
|
+
|
1682
|
+
```ruby
|
1683
|
+
# global
|
1684
|
+
prompt = TTY::Prompt.new(quiet: true)
|
1685
|
+
# single prompt
|
1686
|
+
prompt.select("What is your favorite color?", %w(blue yellow orange))
|
1687
|
+
````
|
1688
|
+
|
1689
|
+
### 3.8 `:track_history`
|
1533
1690
|
|
1534
1691
|
The prompts that accept line input such as `multiline` or `ask` provide history buffer that tracks all the lines entered during `TTY::Prompt.new` interactions. The history buffer provides previous or next lines when user presses up/down arrows respectively. However, if you wish to disable this behaviour use `:track_history` option like so:
|
1535
1692
|
|