tty-prompt 0.18.1 → 0.19.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 +23 -0
- data/README.md +174 -63
- data/Rakefile +2 -2
- data/examples/ask_blank.rb +9 -0
- data/examples/enum_select_disabled.rb +1 -1
- data/examples/enum_select_paged.rb +1 -1
- data/examples/expand_auto.rb +29 -0
- data/examples/mask.rb +1 -1
- data/examples/multi_select.rb +1 -1
- data/examples/multi_select_disabled_paged.rb +22 -0
- data/examples/multi_select_paged.rb +1 -1
- data/examples/select_disabled_paged.rb +22 -0
- data/examples/select_paginated.rb +1 -1
- data/lib/tty/prompt.rb +46 -10
- data/lib/tty/prompt/{enum_paginator.rb → block_paginator.rb} +19 -18
- data/lib/tty/prompt/choice.rb +1 -3
- data/lib/tty/prompt/enum_list.rb +31 -9
- data/lib/tty/prompt/expander.rb +19 -1
- data/lib/tty/prompt/keypress.rb +30 -35
- data/lib/tty/prompt/list.rb +112 -40
- data/lib/tty/prompt/mask_question.rb +2 -3
- data/lib/tty/prompt/multi_list.rb +36 -12
- data/lib/tty/prompt/paginator.rb +37 -25
- data/lib/tty/prompt/question.rb +29 -5
- data/lib/tty/prompt/slider.rb +16 -8
- data/lib/tty/prompt/symbols.rb +30 -6
- data/lib/tty/prompt/timer.rb +75 -0
- data/lib/tty/prompt/version.rb +1 -1
- data/spec/spec_helper.rb +18 -2
- data/spec/unit/ask_spec.rb +45 -4
- data/spec/unit/{enum_paginator_spec.rb → block_paginator_spec.rb} +14 -5
- data/spec/unit/choice/from_spec.rb +16 -0
- data/spec/unit/enum_select_spec.rb +104 -32
- data/spec/unit/expand_spec.rb +104 -12
- data/spec/unit/keypress_spec.rb +2 -8
- data/spec/unit/mask_spec.rb +9 -1
- data/spec/unit/multi_select_spec.rb +348 -118
- data/spec/unit/paginator_spec.rb +29 -10
- data/spec/unit/select_spec.rb +390 -108
- data/spec/unit/slider_spec.rb +48 -6
- data/spec/unit/timer_spec.rb +29 -0
- data/tty-prompt.gemspec +4 -6
- metadata +17 -46
- data/lib/tty/prompt/timeout.rb +0 -78
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b95f394284033f244487a624ca98d9d4aca8a1d5b01c08d181a3bbfe509aa4a
|
4
|
+
data.tar.gz: 32031c9ca43c3098418f54051af00bdcba6075532ba0878a3b8efda844ba076f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: decec13d7b8a65ba78d5c2eea5ed3366976670c1a7e0ebe228c25cf5b494aa82fe3131e5521fe3c8d882d10b2db2aa6934749f1583f767630f4bf3a4fc59eb96
|
7
|
+
data.tar.gz: 7d1e877c0f4e1df2db06726cd8748b6e777f59cf055177d480610687db46ad7de12ee7cfe7aa454414816020bfa35ab5a1e273b680946a45d287b6563e7a1ff6
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,27 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.19.0] - 2019-05-27
|
4
|
+
|
5
|
+
### Added
|
6
|
+
* Add Prompt#debug to allow displaying values in terminal's top right corner
|
7
|
+
* Add :max to limit number of choices in #multi_select prompt
|
8
|
+
* Add :value to pre populate #ask prompt line content
|
9
|
+
* Add :auto_hint to expand default hint in #expand prompt by Ewoudt Kellerman(@hellola)
|
10
|
+
* Add Timer to track and timeout code execution
|
11
|
+
|
12
|
+
### Changed
|
13
|
+
* Change Paginator to expose #start_index & #end_index
|
14
|
+
* Change Paginator to figure out #start_index based on per page size and adjust boundaries to match active selection
|
15
|
+
* Change #ask prompt to allow no question
|
16
|
+
* Change #enum_select to automatically assigned non-disabled default option
|
17
|
+
* Change #enum_select to set default choice when navigating by page
|
18
|
+
* Change #select & #multi_select to allow navigation by page with left/right keys
|
19
|
+
* Change #keypress to use Timer
|
20
|
+
* Change Choice#from to allow any object coercible to string
|
21
|
+
* Change to remove test artifacts from the gem bundle
|
22
|
+
* Change to remove timers dependency
|
23
|
+
* Change to update tty-reader dependency
|
24
|
+
|
3
25
|
## [v0.18.1] - 2018-12-29
|
4
26
|
|
5
27
|
### Changed
|
@@ -304,6 +326,7 @@
|
|
304
326
|
|
305
327
|
* Initial implementation and release
|
306
328
|
|
329
|
+
[v0.19.0]: https://github.com/piotrmurach/tty-prompt/compare/v0.18.1...v0.19.0
|
307
330
|
[v0.18.1]: https://github.com/piotrmurach/tty-prompt/compare/v0.18.0...v0.18.1
|
308
331
|
[v0.18.0]: https://github.com/piotrmurach/tty-prompt/compare/v0.17.2...v0.18.0
|
309
332
|
[v0.17.2]: https://github.com/piotrmurach/tty-prompt/compare/v0.17.1...v0.17.2
|
data/README.md
CHANGED
@@ -65,12 +65,13 @@ Or install it yourself as:
|
|
65
65
|
* [2.1 ask](#21-ask)
|
66
66
|
* [2.1.1 convert](#211-convert)
|
67
67
|
* [2.1.2 default](#212-default)
|
68
|
-
* [2.1.3
|
69
|
-
* [2.1.4
|
70
|
-
* [2.1.5
|
71
|
-
* [2.1.6
|
72
|
-
* [2.1.7
|
73
|
-
* [2.1.8
|
68
|
+
* [2.1.3 value](#213-value)
|
69
|
+
* [2.1.4 echo](#214-echo)
|
70
|
+
* [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)
|
74
75
|
* [2.2 keypress](#22-keypress)
|
75
76
|
* [2.2.1 :timeout](#221-timeout)
|
76
77
|
* [2.3 multiline](#23-multiline)
|
@@ -80,14 +81,19 @@ Or install it yourself as:
|
|
80
81
|
* [2.6.1 choices](#261-choices)
|
81
82
|
* [2.6.1.1 :disabled](#2611-disabled)
|
82
83
|
* [2.6.2 select](#262-select)
|
83
|
-
* [2.6.2.1 :
|
84
|
-
* [2.6.2.2 :
|
84
|
+
* [2.6.2.1 :per_page](#2621-per_page)
|
85
|
+
* [2.6.2.2 :disabled](#2622-disabled)
|
86
|
+
* [2.6.2.3 :filter](#2623-filter)
|
85
87
|
* [2.6.3 multi_select](#263-multi_select)
|
86
88
|
* [2.6.3.1 :disabled](#2631-disabled)
|
87
89
|
* [2.6.3.2 :echo](#2632-echo)
|
88
90
|
* [2.6.3.3 :filter](#2633-filter)
|
91
|
+
* [2.6.3.4 :max](#2634-max)
|
89
92
|
* [2.6.4 enum_select](#264-enum_select)
|
93
|
+
* [2.6.4.1 :per_page](#2641-per_page)
|
94
|
+
* [2.6.4.1 :disabled](#2641-disabled)
|
90
95
|
* [2.7 expand](#27-expand)
|
96
|
+
* [2.7.1 auto_hint](#271-auto_hint)
|
91
97
|
* [2.8 collect](#28-collect)
|
92
98
|
* [2.9 suggest](#29-suggest)
|
93
99
|
* [2.10 slider](#210-slider)
|
@@ -97,12 +103,13 @@ Or install it yourself as:
|
|
97
103
|
* [2.11.3 error](#2113-error)
|
98
104
|
* [2.12 keyboard events](#212-keyboard-events)
|
99
105
|
* [3. settings](#3-settings)
|
100
|
-
* [3.1
|
101
|
-
* [3.2
|
102
|
-
* [3.3
|
103
|
-
* [3.4
|
104
|
-
* [3.5
|
105
|
-
* [3.6
|
106
|
+
* [3.1 :symbols](#31-symbols)
|
107
|
+
* [3.2 :active_color](#32-active_color)
|
108
|
+
* [3.3 :enable_color](#33-enable-color)
|
109
|
+
* [3.4 :help_color](#34-help_color)
|
110
|
+
* [3.5 :interrupt](#35-interrupt)
|
111
|
+
* [3.6 :prefix](#36-prefix)
|
112
|
+
* [3.7 :track_history](#37-track_history)
|
106
113
|
|
107
114
|
## 1. Usage
|
108
115
|
|
@@ -112,7 +119,7 @@ In order to start asking questions on the command line, create prompt:
|
|
112
119
|
prompt = TTY::Prompt.new
|
113
120
|
```
|
114
121
|
|
115
|
-
|
122
|
+
And then call `ask` with the question for simple input:
|
116
123
|
|
117
124
|
```ruby
|
118
125
|
prompt.ask('What is your name?', default: ENV['USER'])
|
@@ -138,7 +145,7 @@ Asking question with list of options couldn't be easier using `select` like so:
|
|
138
145
|
```ruby
|
139
146
|
prompt.select("Choose your destiny?", %w(Scorpion Kano Jax))
|
140
147
|
# =>
|
141
|
-
# Choose your destiny? (Use arrow keys, press Enter to select)
|
148
|
+
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
|
142
149
|
# ‣ Scorpion
|
143
150
|
# Kano
|
144
151
|
# Jax
|
@@ -151,7 +158,7 @@ choices = %w(vodka beer wine whisky bourbon)
|
|
151
158
|
prompt.multi_select("Select drinks?", choices)
|
152
159
|
# =>
|
153
160
|
#
|
154
|
-
# Select drinks? (Use arrow keys, press Space to select and Enter to finish)"
|
161
|
+
# Select drinks? (Use ↑/↓ arrow keys, press Space to select and Enter to finish)"
|
155
162
|
# ‣ ⬡ vodka
|
156
163
|
# ⬡ beer
|
157
164
|
# ⬡ wine
|
@@ -274,7 +281,17 @@ prompt.ask('What is your name?', default: 'Anonymous')
|
|
274
281
|
# What is your name? (Anonymous)
|
275
282
|
```
|
276
283
|
|
277
|
-
#### 2.1.3
|
284
|
+
#### 2.1.3 value
|
285
|
+
|
286
|
+
To pre-populate the input line for editing use `:value` option:
|
287
|
+
|
288
|
+
```ruby
|
289
|
+
prompt.ask("What is your name?", value: "Piotr")
|
290
|
+
# =>
|
291
|
+
# What is your name? Piotr
|
292
|
+
```
|
293
|
+
|
294
|
+
#### 2.1.4 echo
|
278
295
|
|
279
296
|
To control whether the input is shown back in terminal or not use `:echo` option like so:
|
280
297
|
|
@@ -282,7 +299,7 @@ To control whether the input is shown back in terminal or not use `:echo` option
|
|
282
299
|
prompt.ask('password:', echo: false)
|
283
300
|
```
|
284
301
|
|
285
|
-
#### 2.1.
|
302
|
+
#### 2.1.5 error messages
|
286
303
|
|
287
304
|
By default `tty-prompt` comes with predefined error messages for `required`, `in`, `validate` options.
|
288
305
|
|
@@ -294,7 +311,7 @@ prompt.ask('What is your email?') do |q|
|
|
294
311
|
end
|
295
312
|
```
|
296
313
|
|
297
|
-
|
314
|
+
Or change the `messages` key entry out of `:required?`, `:valid?`, `:range?`:
|
298
315
|
|
299
316
|
```ruby
|
300
317
|
prompt.ask('What is your email?') do |q|
|
@@ -303,7 +320,7 @@ prompt.ask('What is your email?') do |q|
|
|
303
320
|
end
|
304
321
|
```
|
305
322
|
|
306
|
-
|
323
|
+
To change default range validation error message do:
|
307
324
|
|
308
325
|
```ruby
|
309
326
|
prompt.ask('How spicy on scale (1-5)? ') do |q|
|
@@ -312,7 +329,7 @@ prompt.ask('How spicy on scale (1-5)? ') do |q|
|
|
312
329
|
end
|
313
330
|
```
|
314
331
|
|
315
|
-
#### 2.1.
|
332
|
+
#### 2.1.6 in
|
316
333
|
|
317
334
|
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:
|
318
335
|
|
@@ -320,7 +337,7 @@ In order to check that provided input falls inside a range of inputs use the `in
|
|
320
337
|
ask("Provide number in range: 0-9?") { |q| q.in('0-9') }
|
321
338
|
```
|
322
339
|
|
323
|
-
#### 2.1.
|
340
|
+
#### 2.1.7 modify
|
324
341
|
|
325
342
|
Set the `:modify` option if you want to handle whitespace or letter capitalization.
|
326
343
|
|
@@ -345,7 +362,7 @@ Available whitespace settings are:
|
|
345
362
|
:remove # remove all whitespace
|
346
363
|
```
|
347
364
|
|
348
|
-
#### 2.1.
|
365
|
+
#### 2.1.8 required
|
349
366
|
|
350
367
|
To ensure that input is provided use `:required` option:
|
351
368
|
|
@@ -355,7 +372,7 @@ prompt.ask("What's your phone number?", required: true)
|
|
355
372
|
# >> Value must be provided
|
356
373
|
```
|
357
374
|
|
358
|
-
#### 2.1.
|
375
|
+
#### 2.1.9 validate
|
359
376
|
|
360
377
|
In order to validate that input matches a given patter you can pass the `validate` option. Validate setting accepts `Regex`, `Proc` or `Symbol`.
|
361
378
|
|
@@ -420,7 +437,7 @@ The `multiline` uses similar options to those supported by `ask` prompt. For exa
|
|
420
437
|
prompt.multiline("Description?", default: 'A super sweet prompt.')
|
421
438
|
```
|
422
439
|
|
423
|
-
|
440
|
+
Or using DSL:
|
424
441
|
|
425
442
|
```ruby
|
426
443
|
prompt.multiline("Description?") do |q|
|
@@ -438,12 +455,12 @@ prompt.mask('What is your secret?')
|
|
438
455
|
# => What is your secret? ••••
|
439
456
|
```
|
440
457
|
|
441
|
-
The masking character can be changed by passing `:mask`
|
458
|
+
The masking character can be changed by passing `:symbols` option with `:mask` key:
|
442
459
|
|
443
460
|
```ruby
|
444
|
-
heart = prompt.decorate('
|
445
|
-
prompt.mask('What is your secret?', mask: heart)
|
446
|
-
# => What is your secret? ❤ ❤ ❤ ❤ ❤
|
461
|
+
heart = prompt.decorate(prompt.symbols[:heart] + ' ', :magenta)
|
462
|
+
prompt.mask('What is your secret?', symbols: {mask: heart})
|
463
|
+
# => What is your secret? ❤ ❤ ❤ ❤ ❤
|
447
464
|
```
|
448
465
|
|
449
466
|
If you don't wish to show any output use `:echo` option like so:
|
@@ -584,7 +601,7 @@ For asking questions involving list of options use `select` method by passing th
|
|
584
601
|
```ruby
|
585
602
|
prompt.select("Choose your destiny?", %w(Scorpion Kano Jax))
|
586
603
|
# =>
|
587
|
-
# Choose your destiny? (Use arrow keys, press Enter to select)
|
604
|
+
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
|
588
605
|
# ‣ Scorpion
|
589
606
|
# Kano
|
590
607
|
# Jax
|
@@ -599,7 +616,7 @@ prompt.select("Choose your destiny?") do |menu|
|
|
599
616
|
menu.choice 'Jax'
|
600
617
|
end
|
601
618
|
# =>
|
602
|
-
# Choose your destiny? (Use arrow keys, press Enter to select)
|
619
|
+
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
|
603
620
|
# ‣ Scorpion
|
604
621
|
# Kano
|
605
622
|
# Jax
|
@@ -614,7 +631,7 @@ prompt.select("Choose your destiny?") do |menu|
|
|
614
631
|
menu.choice 'Jax', -> { 'Nice choice captain!' }
|
615
632
|
end
|
616
633
|
# =>
|
617
|
-
# Choose your destiny? (Use arrow keys, press Enter to select)
|
634
|
+
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
|
618
635
|
# ‣ Scorpion
|
619
636
|
# Kano
|
620
637
|
# Jax
|
@@ -638,7 +655,7 @@ prompt.select("Choose your destiny?") do |menu|
|
|
638
655
|
menu.choice 'Jax', 3
|
639
656
|
end
|
640
657
|
# =>
|
641
|
-
# Choose your destiny? (Use arrow keys, press Enter to select)
|
658
|
+
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
|
642
659
|
# Scorpion
|
643
660
|
# Kano
|
644
661
|
# ‣ Jax
|
@@ -649,7 +666,7 @@ You can navigate the choices using the arrow keys or define your own key mapping
|
|
649
666
|
```ruby
|
650
667
|
prompt.select("Choose your destiny?", %w(Scorpion Kano Jax), cycle: true)
|
651
668
|
# =>
|
652
|
-
# Choose your destiny? (Use arrow keys, press Enter to select)
|
669
|
+
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
|
653
670
|
# ‣ Scorpion
|
654
671
|
# Kano
|
655
672
|
# Jax
|
@@ -666,7 +683,7 @@ prompt.select("Choose your destiny?") do |menu|
|
|
666
683
|
menu.choice 'Jax', 3
|
667
684
|
end
|
668
685
|
# =>
|
669
|
-
# Choose your destiny? (Use arrow or number (0-9) keys, press Enter to select)
|
686
|
+
# Choose your destiny? (Use ↑/↓ arrow or number (0-9) keys, press Enter to select)
|
670
687
|
# 1. Scorpion
|
671
688
|
# 2. Kano
|
672
689
|
# ‣ 3. Jax
|
@@ -676,7 +693,7 @@ You can configure help message and/or marker like so
|
|
676
693
|
|
677
694
|
```ruby
|
678
695
|
choices = %w(Scorpion Kano Jax)
|
679
|
-
prompt.select("Choose your destiny?", choices, help: "(Bash keyboard)", marker: '>')
|
696
|
+
prompt.select("Choose your destiny?", choices, help: "(Bash keyboard)", symbols: {marker: '>'})
|
680
697
|
# =>
|
681
698
|
# Choose your destiny? (Bash keyboard)
|
682
699
|
# > Scorpion
|
@@ -684,18 +701,19 @@ prompt.select("Choose your destiny?", choices, help: "(Bash keyboard)", marker:
|
|
684
701
|
# Jax
|
685
702
|
```
|
686
703
|
|
704
|
+
#### 2.6.1.1 `:per_page`
|
705
|
+
|
687
706
|
By default the menu is paginated if selection grows beyond `6` items. To change this setting use `:per_page` configuration.
|
688
707
|
|
689
708
|
```ruby
|
690
709
|
letters = ('A'..'Z').to_a
|
691
710
|
prompt.select("Choose your letter?", letters, per_page: 4)
|
692
711
|
# =>
|
693
|
-
# Which letter? (Use arrow keys, press Enter to select)
|
712
|
+
# Which letter? (Use ↑/↓ and ←/→ arrow keys, press Enter to select)
|
694
713
|
# ‣ A
|
695
714
|
# B
|
696
715
|
# C
|
697
716
|
# D
|
698
|
-
# (Move up or down to reveal more choices)
|
699
717
|
```
|
700
718
|
|
701
719
|
You can also customise page navigation text using `:page_help` option:
|
@@ -708,7 +726,7 @@ prompt.select("Choose your letter?") do |menu|
|
|
708
726
|
end
|
709
727
|
```
|
710
728
|
|
711
|
-
#### 2.6.2.
|
729
|
+
#### 2.6.2.2 `:disabled`
|
712
730
|
|
713
731
|
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:
|
714
732
|
|
@@ -728,7 +746,7 @@ The disabled choice will be displayed with a cross `✘` character next to it an
|
|
728
746
|
```ruby
|
729
747
|
prompt.select('Choose your destiny?', warriors)
|
730
748
|
# =>
|
731
|
-
# Choose your destiny? (Use arrow keys, press Enter to select)
|
749
|
+
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
|
732
750
|
# ‣ Scorpion
|
733
751
|
# Kano
|
734
752
|
# ✘ Goro (injury)
|
@@ -737,7 +755,7 @@ prompt.select('Choose your destiny?', warriors)
|
|
737
755
|
# Raiden
|
738
756
|
```
|
739
757
|
|
740
|
-
#### 2.6.2.
|
758
|
+
#### 2.6.2.3 `:filter`
|
741
759
|
|
742
760
|
To activate dynamic list searching on letter/number key presses use `:filter` option:
|
743
761
|
|
@@ -745,7 +763,7 @@ To activate dynamic list searching on letter/number key presses use `:filter` op
|
|
745
763
|
warriors = %w(Scorpion Kano Jax Kitana Raiden)
|
746
764
|
prompt.select('Choose your destiny?', warriors, filter: true)
|
747
765
|
# =>
|
748
|
-
# Choose your destiny? (Use arrow keys, press Enter to select, and letter keys to filter)
|
766
|
+
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select, and letter keys to filter)
|
749
767
|
# ‣ Scorpion
|
750
768
|
# Kano
|
751
769
|
# Jax
|
@@ -783,7 +801,7 @@ choices = %w(vodka beer wine whisky bourbon)
|
|
783
801
|
prompt.multi_select("Select drinks?", choices)
|
784
802
|
# =>
|
785
803
|
#
|
786
|
-
# Select drinks? (Use arrow keys, press Space to select and Enter to finish)"
|
804
|
+
# Select drinks? (Use ↑/↓ arrow keys, press Space to select and Enter to finish)"
|
787
805
|
# ‣ ⬡ vodka
|
788
806
|
# ⬡ beer
|
789
807
|
# ⬡ wine
|
@@ -887,12 +905,11 @@ By default the menu is paginated if selection grows beyond `6` items. To change
|
|
887
905
|
letters = ('A'..'Z').to_a
|
888
906
|
prompt.multi_select("Choose your letter?", letters, per_page: 4)
|
889
907
|
# =>
|
890
|
-
# Which letter? (Use arrow keys, press Space to select and Enter to finish)
|
908
|
+
# Which letter? (Use ↑/↓ and ←/→ arrow keys, press Space to select and Enter to finish)
|
891
909
|
# ‣ ⬡ A
|
892
910
|
# ⬡ B
|
893
911
|
# ⬡ C
|
894
912
|
# ⬡ D
|
895
|
-
# (Move up or down to reveal more choices)
|
896
913
|
```
|
897
914
|
|
898
915
|
#### 2.6.3.1 `:disabled`
|
@@ -915,7 +932,7 @@ The disabled choice will be displayed with a cross `✘` character next to it an
|
|
915
932
|
```ruby
|
916
933
|
prompt.multi_select('Choose your favourite drink?', drinks)
|
917
934
|
# =>
|
918
|
-
# Choose your favourite drink? (Use arrow keys, press Space to select and Enter to finish)
|
935
|
+
# Choose your favourite drink? (Use ↑/↓ arrow keys, press Space to select and Enter to finish)
|
919
936
|
# ‣ ⬡ bourbon
|
920
937
|
# ✘ sake (out of stock)
|
921
938
|
# ⬡ vodka
|
@@ -949,7 +966,7 @@ To activate dynamic list filtering on letter/number typing, use the :filter opti
|
|
949
966
|
choices = %w(vodka beer wine whisky bourbon)
|
950
967
|
prompt.multi_select("Select drinks?", choices, filter: true)
|
951
968
|
# =>
|
952
|
-
# Select drinks? (Use arrow keys, press Space to select and Enter to finish, and letter keys to filter)
|
969
|
+
# Select drinks? (Use ↑/↓ arrow keys, press Space to select and Enter to finish, and letter keys to filter)
|
953
970
|
# ‣ ⬡ vodka
|
954
971
|
# ⬡ beer
|
955
972
|
# ⬡ wine
|
@@ -971,6 +988,22 @@ If the user changes or deletes a filter, the choices previously selected remain
|
|
971
988
|
|
972
989
|
The `filter` option is not compatible with `enum`.
|
973
990
|
|
991
|
+
#### 2.6.3.4 `:max`
|
992
|
+
|
993
|
+
To limit the number of choices an user can select, use `:max` option:
|
994
|
+
|
995
|
+
```ruby
|
996
|
+
choices = %w(vodka beer wine whisky bourbon)
|
997
|
+
prompt.multi_select("Select drinks?", choices, max: 3)
|
998
|
+
# =>
|
999
|
+
# Select drinks? (max. 3) vodka, beer, whisky
|
1000
|
+
# ⬢ vodka
|
1001
|
+
# ⬢ beer
|
1002
|
+
# ⬡ wine
|
1003
|
+
# ⬢ whisky
|
1004
|
+
# ‣ ⬡ bourbon
|
1005
|
+
```
|
1006
|
+
|
974
1007
|
### 2.6.4 enum_select
|
975
1008
|
|
976
1009
|
In order to ask for standard selection from indexed list you can use `enum_select` and pass question together with possible choices:
|
@@ -1030,6 +1063,8 @@ end
|
|
1030
1063
|
# Select an editor? /usr/bin/vim
|
1031
1064
|
```
|
1032
1065
|
|
1066
|
+
#### 2.6.4.1 `:per_page`
|
1067
|
+
|
1033
1068
|
By default the menu is paginated if selection grows beyond `6` items. To change this setting use `:per_page` configuration.
|
1034
1069
|
|
1035
1070
|
```ruby
|
@@ -1045,11 +1080,42 @@ prompt.enum_select("Choose your letter?", letters, per_page: 4)
|
|
1045
1080
|
# (Press tab/right or left to reveal more choices)
|
1046
1081
|
```
|
1047
1082
|
|
1083
|
+
#### 2.6.4.2 `:disabled`
|
1084
|
+
|
1085
|
+
To make a choice unavailable use the `:disabled` option and, if you wish, as value provide a reason:
|
1086
|
+
|
1087
|
+
```ruby
|
1088
|
+
choices = [
|
1089
|
+
{name: "Emacs", disabled: '(not installed)'},
|
1090
|
+
"Atom",
|
1091
|
+
"GNU nano",
|
1092
|
+
{name: "Notepad++", disabled: '(not installed)'},
|
1093
|
+
"Sublime",
|
1094
|
+
"Vim"
|
1095
|
+
]
|
1096
|
+
```
|
1097
|
+
|
1098
|
+
The disabled choice will be displayed with a cross ✘ character next to it and followed by an explanation:
|
1099
|
+
|
1100
|
+
|
1101
|
+
```ruby
|
1102
|
+
prompt.enum_select('Select an editor', choices)
|
1103
|
+
# =>
|
1104
|
+
# Select an editor
|
1105
|
+
# ✘ 1) Emacs (not installed)
|
1106
|
+
# 2) Atom
|
1107
|
+
# 3) GNU nano
|
1108
|
+
# ✘ 4) Notepad++ (not installed)
|
1109
|
+
# 5) Sublime
|
1110
|
+
# 6) Vim
|
1111
|
+
# Choose 1-6 [2]:
|
1112
|
+
```
|
1113
|
+
|
1048
1114
|
### 2.7 expand
|
1049
1115
|
|
1050
1116
|
The `expand` provides a compact way to ask a question with many options.
|
1051
1117
|
|
1052
|
-
As first argument `expand` takes the message to display and as a second an array of choices. Compared to the `select`, `multi_select` and `enum_select`, the choices need to be objects that include `:key`, `:name` and `:value` keys. The `:key` must be a single character. The help choice is added automatically as the last option
|
1118
|
+
As first argument `expand` takes the message to display and as a second an array of choices. Compared to the `select`, `multi_select` and `enum_select`, the choices need to be objects that include `:key`, `:name` and `:value` keys. The `:key` must be a single character. The help choice is added automatically as the last option under the key `h`.
|
1053
1119
|
|
1054
1120
|
```ruby
|
1055
1121
|
choices = [
|
@@ -1109,6 +1175,17 @@ If user types `h` and presses enter, an expanded view will be shown which furthe
|
|
1109
1175
|
|
1110
1176
|
Run `examples/expand.rb` to see the prompt in action.
|
1111
1177
|
|
1178
|
+
#### 2.7.1 `:auto_hint`
|
1179
|
+
|
1180
|
+
To show hint by default use `:auto_hint` option:
|
1181
|
+
|
1182
|
+
```ruby
|
1183
|
+
prompt.expand('Overwrite Gemfile?', choices, auto_hint: true)
|
1184
|
+
# =>
|
1185
|
+
# Overwrite Gemfile? (enter "h" for help) [y,n,q,h]
|
1186
|
+
# >> overwrite this file
|
1187
|
+
```
|
1188
|
+
|
1112
1189
|
### 2.8 collect
|
1113
1190
|
|
1114
1191
|
In order to collect more than one answer use `collect` method. Using the `key` you can describe the answers key name. All the methods for asking user input such as `ask`, `mask`, `select` can be directly invoked on the key. The key composition is very flexible by allowing nested keys. If you want the value to be automatically converted to required type use [convert](#221-convert).
|
@@ -1184,12 +1261,12 @@ prompt.suggest('b', possible, indent: 4, single_text: 'Perhaps you meant?')
|
|
1184
1261
|
|
1185
1262
|
If you have constrained range of numbers for user to choose from you may consider using `slider`.
|
1186
1263
|
|
1187
|
-
The slider provides easy visual way of picking a value marked by
|
1264
|
+
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:
|
1188
1265
|
|
1189
1266
|
```ruby
|
1190
1267
|
prompt.slider('Volume', max: 100, step: 5)
|
1191
1268
|
# =>
|
1192
|
-
# Volume
|
1269
|
+
# Volume ──────────●────────── 50
|
1193
1270
|
# (Use arrow keys, press Enter to select)
|
1194
1271
|
```
|
1195
1272
|
|
@@ -1198,16 +1275,16 @@ By default the slider is configured to pick middle of the range as a start value
|
|
1198
1275
|
```ruby
|
1199
1276
|
prompt.slider('Volume', max: 100, step: 5, default: 75)
|
1200
1277
|
# =>
|
1201
|
-
# Volume
|
1278
|
+
# Volume ───────────────●────── 75
|
1202
1279
|
# (Use arrow keys, press Enter to select)
|
1203
1280
|
```
|
1204
1281
|
|
1205
|
-
You can also change the default slider formatting using the `:format`. The value must contain the `:slider` token
|
1282
|
+
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`:
|
1206
1283
|
|
1207
1284
|
```ruby
|
1208
1285
|
prompt.slider('Volume', max: 100, step: 5, default: 75, format: "|:slider| %d%")
|
1209
1286
|
# =>
|
1210
|
-
# Volume
|
1287
|
+
# Volume |───────────────●──────| 75%
|
1211
1288
|
# (Use arrow keys, press Enter to select)
|
1212
1289
|
```
|
1213
1290
|
|
@@ -1216,7 +1293,16 @@ You can also specify slider range with decimal numbers. For example, to have a s
|
|
1216
1293
|
```ruby
|
1217
1294
|
prompt.slider("Volume", max: 10, step: 0.5, default: 5, format: "|:slider| %.1f")
|
1218
1295
|
# =>
|
1219
|
-
# Volume
|
1296
|
+
# Volume |───────────────●──────| 7.5
|
1297
|
+
# (Use arrow keys, press Enter to select)
|
1298
|
+
```
|
1299
|
+
|
1300
|
+
If you wish to change the slider handle and the slider range display use `:symbols` option:
|
1301
|
+
|
1302
|
+
```ruby
|
1303
|
+
prompt.slider("Volume", max: 100, step: 5, default: 75, symbols: {bullet: 'x', line: '_'})
|
1304
|
+
# =>
|
1305
|
+
# Volume _______________x______ 75%
|
1220
1306
|
# (Use arrow keys, press Enter to select)
|
1221
1307
|
```
|
1222
1308
|
|
@@ -1230,13 +1316,13 @@ prompt.slider('What size?') do |range|
|
|
1230
1316
|
range.format "|:slider| %d%"
|
1231
1317
|
end
|
1232
1318
|
# =>
|
1233
|
-
# Volume
|
1319
|
+
# Volume |───────────────●──────| 75%
|
1234
1320
|
# (Use arrow keys, press Enter to select)
|
1235
1321
|
```
|
1236
1322
|
|
1237
1323
|
### 2.11 say
|
1238
1324
|
|
1239
|
-
To simply print message out to
|
1325
|
+
To simply print message out to standard output use `say` like so:
|
1240
1326
|
|
1241
1327
|
```ruby
|
1242
1328
|
prompt.say(...)
|
@@ -1324,7 +1410,32 @@ The available events are:
|
|
1324
1410
|
|
1325
1411
|
## 3 settings
|
1326
1412
|
|
1327
|
-
### 3.1
|
1413
|
+
### 3.1. `:symbols`
|
1414
|
+
|
1415
|
+
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:
|
1416
|
+
|
1417
|
+
```ruby
|
1418
|
+
prompt = TTY::Prompt.new(symbols: {marker: '>'})
|
1419
|
+
```
|
1420
|
+
|
1421
|
+
The following symbols can be overwritten:
|
1422
|
+
|
1423
|
+
| Symbols | Unicode | ASCII |
|
1424
|
+
| ----------- |:-------:|:-----:|
|
1425
|
+
| tick | `✓` | `√` |
|
1426
|
+
| cross | `✘` | `x` |
|
1427
|
+
| marker | `‣` | `>` |
|
1428
|
+
| dot | `•` | `.` |
|
1429
|
+
| bullet | `●` | `O` |
|
1430
|
+
| line | `─` | `-` |
|
1431
|
+
| radio_on | `⬢` | `(*)` |
|
1432
|
+
| radio_off | `⬡` | `( )` |
|
1433
|
+
| arrow_up | `↑` | `↑` |
|
1434
|
+
| arrow_down | `↓` | `↓` |
|
1435
|
+
| arrow_left | `←` | `←` |
|
1436
|
+
| arrow_right| `→` | `→` |
|
1437
|
+
|
1438
|
+
### 3.2 `:active_color`
|
1328
1439
|
|
1329
1440
|
All prompt types support `:active_color` option. In case of `select`, `multi_select`, `enum_select` or `expand` this color is used to highlight the currently selected choice. All the resulted inputs provided by user that are read in by the prompt as answer are highlighted with this color. This option can be applied either globally for all prompts or individually.
|
1330
1441
|
|
@@ -1340,7 +1451,7 @@ prompt.select('What size?', %w(Large Medium Small), active_color: :cyan)
|
|
1340
1451
|
|
1341
1452
|
Please [see pastel](https://github.com/piotrmurach/pastel#3-supported-colors) for all supported colors.
|
1342
1453
|
|
1343
|
-
### 3.
|
1454
|
+
### 3.3 `:enable_color`
|
1344
1455
|
|
1345
1456
|
If you wish to disable coloring for a prompt simply pass `:enable_color` option
|
1346
1457
|
|
@@ -1348,7 +1459,7 @@ If you wish to disable coloring for a prompt simply pass `:enable_color` option
|
|
1348
1459
|
prompt = TTY::Prompt.new(enable_color: true)
|
1349
1460
|
```
|
1350
1461
|
|
1351
|
-
### 3.
|
1462
|
+
### 3.4 `:help_color`
|
1352
1463
|
|
1353
1464
|
Prompts such as `select`, `multi_select`, `expand` support `:help_color` which is used to customize the help text. This option can be applied either globally for all prompts or individually.
|
1354
1465
|
|
@@ -1362,7 +1473,7 @@ or per individual input do:
|
|
1362
1473
|
prompt.select('What size?', %w(Large Medium Small), help_color: :cyan)
|
1363
1474
|
```
|
1364
1475
|
|
1365
|
-
### 3.
|
1476
|
+
### 3.5 `:interrupt`
|
1366
1477
|
|
1367
1478
|
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:
|
1368
1479
|
|
@@ -1377,7 +1488,7 @@ For example, to send interrupt signal do:
|
|
1377
1488
|
prompt = TTY::Prompt.new(interrupt: :signal)
|
1378
1489
|
```
|
1379
1490
|
|
1380
|
-
### 3.
|
1491
|
+
### 3.6 `:prefix`
|
1381
1492
|
|
1382
1493
|
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:
|
1383
1494
|
|
@@ -1385,7 +1496,7 @@ You can prefix each question asked using the `:prefix` option. This option can b
|
|
1385
1496
|
prompt = TTY::Prompt.new(prefix: '[?] ')
|
1386
1497
|
```
|
1387
1498
|
|
1388
|
-
### 3.
|
1499
|
+
### 3.7 `:track_history`
|
1389
1500
|
|
1390
1501
|
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:
|
1391
1502
|
|