tty-prompt 0.18.0 → 0.18.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +1 -1
- data/examples/enum_select.rb +1 -1
- data/examples/select.rb +5 -1
- data/lib/tty/prompt/choices.rb +2 -1
- data/lib/tty/prompt/enum_list.rb +1 -1
- data/lib/tty/prompt/list.rb +21 -15
- data/lib/tty/prompt/multi_list.rb +10 -10
- data/lib/tty/prompt/version.rb +1 -1
- data/spec/unit/enum_select_spec.rb +4 -4
- data/spec/unit/multi_select_spec.rb +21 -5
- data/spec/unit/select_spec.rb +24 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4993a3639c2dec47c658cddd6a8c3d98b4efb7bec6cc3d1b90dfd184d9b6f889
|
4
|
+
data.tar.gz: d069f2c94118978bbfa04e54c967a0d8ce185adc6d7eb74446988350bca21d06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1dd9287d14e3aa2eb8357d9ff759cdec52182535bc9697f97d4da7ab7c111f27ec61bfc176cc16cc7c8f4f2ca1dcd3af4784df1c82e95b0382f4cb2dc7cc782
|
7
|
+
data.tar.gz: 62ed0bc598c3a6b6b65ed7a5f14a7bacd769a5d46dfa041afb67d4d7a4de407e4cb5334fc25942351551414c0c29d68a9fc51aa238e198931677639c3ad4d382
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.18.1] - 2018-12-29
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
* Change #multi_select & #select to auto select first non-disabled active choice
|
7
|
+
|
8
|
+
### Fixed
|
9
|
+
* Fix #select, #multi_select & #enum_select to allow for symbols as choice names
|
10
|
+
|
3
11
|
## [v0.18.0] - 2018-11-24
|
4
12
|
|
5
13
|
### Changed
|
@@ -296,6 +304,7 @@
|
|
296
304
|
|
297
305
|
* Initial implementation and release
|
298
306
|
|
307
|
+
[v0.18.1]: https://github.com/piotrmurach/tty-prompt/compare/v0.18.0...v0.18.1
|
299
308
|
[v0.18.0]: https://github.com/piotrmurach/tty-prompt/compare/v0.17.2...v0.18.0
|
300
309
|
[v0.17.2]: https://github.com/piotrmurach/tty-prompt/compare/v0.17.1...v0.17.2
|
301
310
|
[v0.17.1]: https://github.com/piotrmurach/tty-prompt/compare/v0.17.0...v0.17.1
|
data/README.md
CHANGED
@@ -1344,7 +1344,7 @@ Please [see pastel](https://github.com/piotrmurach/pastel#3-supported-colors) fo
|
|
1344
1344
|
|
1345
1345
|
If you wish to disable coloring for a prompt simply pass `:enable_color` option
|
1346
1346
|
|
1347
|
-
```
|
1347
|
+
```ruby
|
1348
1348
|
prompt = TTY::Prompt.new(enable_color: true)
|
1349
1349
|
```
|
1350
1350
|
|
data/examples/enum_select.rb
CHANGED
data/examples/select.rb
CHANGED
@@ -4,7 +4,7 @@ require_relative "../lib/tty-prompt"
|
|
4
4
|
|
5
5
|
prompt = TTY::Prompt.new
|
6
6
|
|
7
|
-
warriors = %
|
7
|
+
warriors = %i(Scorpion Kano Jax Kitana Raiden)
|
8
8
|
|
9
9
|
prompt.on(:keypress) do |event|
|
10
10
|
if event.value == 'j'
|
@@ -15,6 +15,10 @@ prompt.on(:keypress) do |event|
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
+
prompt.on(:keyescape) do |event|
|
19
|
+
exit(1)
|
20
|
+
end
|
21
|
+
|
18
22
|
answer = prompt.select('Choose your destiny?', warriors)
|
19
23
|
|
20
24
|
puts answer.inspect
|
data/lib/tty/prompt/choices.rb
CHANGED
data/lib/tty/prompt/enum_list.rb
CHANGED
@@ -359,7 +359,7 @@ module TTY
|
|
359
359
|
|
360
360
|
@paginator.paginate(@choices, @page_active, @per_page) do |choice, index|
|
361
361
|
num = (index + 1).to_s + @enum + ' '
|
362
|
-
selected = num + choice.name
|
362
|
+
selected = num.to_s + choice.name.to_s
|
363
363
|
output << if index + 1 == @active && !choice.disabled?
|
364
364
|
(' ' * 2) + @prompt.decorate(selected, @active_color)
|
365
365
|
elsif choice.disabled?
|
data/lib/tty/prompt/list.rb
CHANGED
@@ -15,12 +15,12 @@ module TTY
|
|
15
15
|
class List
|
16
16
|
include Symbols
|
17
17
|
|
18
|
-
HELP = '(Use arrow%s keys, press Enter to select%s)'
|
18
|
+
HELP = '(Use arrow%s keys, press Enter to select%s)'
|
19
19
|
|
20
|
-
PAGE_HELP = '(Move up or down to reveal more choices)'
|
20
|
+
PAGE_HELP = '(Move up or down to reveal more choices)'
|
21
21
|
|
22
22
|
# Allowed keys for filter, along with backspace and canc.
|
23
|
-
FILTER_KEYS_MATCHER = /\A([[:alnum:]]|[[:punct:]])\Z
|
23
|
+
FILTER_KEYS_MATCHER = /\A([[:alnum:]]|[[:punct:]])\Z/.freeze
|
24
24
|
|
25
25
|
# Create instance of TTY::Prompt::List menu.
|
26
26
|
#
|
@@ -42,8 +42,7 @@ module TTY
|
|
42
42
|
@prompt = prompt
|
43
43
|
@prefix = options.fetch(:prefix) { @prompt.prefix }
|
44
44
|
@enum = options.fetch(:enum) { nil }
|
45
|
-
@default = Array[
|
46
|
-
@active = @default.first
|
45
|
+
@default = Array(options[:default])
|
47
46
|
@choices = Choices.new
|
48
47
|
@active_color = options.fetch(:active_color) { @prompt.active_color }
|
49
48
|
@help_color = options.fetch(:help_color) { @prompt.help_color }
|
@@ -122,7 +121,7 @@ module TTY
|
|
122
121
|
tokens = if enumerate?
|
123
122
|
[" or number (1-#{choices.size})", '']
|
124
123
|
elsif filterable?
|
125
|
-
['',
|
124
|
+
['', ', and letter keys to filter']
|
126
125
|
else
|
127
126
|
['', '']
|
128
127
|
end
|
@@ -160,9 +159,9 @@ module TTY
|
|
160
159
|
if !filterable? || @filter.empty?
|
161
160
|
@choices
|
162
161
|
else
|
163
|
-
@choices.select do |
|
164
|
-
!
|
165
|
-
|
162
|
+
@choices.select do |choice|
|
163
|
+
!choice.disabled? &&
|
164
|
+
choice.name.downcase.include?(@filter.join.downcase)
|
166
165
|
end
|
167
166
|
end
|
168
167
|
else
|
@@ -195,6 +194,7 @@ module TTY
|
|
195
194
|
|
196
195
|
def keynum(event)
|
197
196
|
return unless enumerate?
|
197
|
+
|
198
198
|
value = event.value.to_i
|
199
199
|
return unless (1..choices.count).cover?(value)
|
200
200
|
return if choices[value - 1].disabled?
|
@@ -218,7 +218,7 @@ module TTY
|
|
218
218
|
if prev_active
|
219
219
|
@active = prev_active
|
220
220
|
elsif @cycle
|
221
|
-
searchable =
|
221
|
+
searchable = choices.length.downto(1).to_a
|
222
222
|
prev_active = search_choice_in(searchable)
|
223
223
|
|
224
224
|
@active = prev_active if prev_active
|
@@ -277,7 +277,12 @@ module TTY
|
|
277
277
|
# @api private
|
278
278
|
def setup_defaults
|
279
279
|
validate_defaults
|
280
|
-
|
280
|
+
|
281
|
+
if !@default.empty?
|
282
|
+
@active = @default.first
|
283
|
+
else
|
284
|
+
@active = @choices.index { |choice| !choice.disabled? } + 1
|
285
|
+
end
|
281
286
|
end
|
282
287
|
|
283
288
|
# Validate default indexes to be within range
|
@@ -398,7 +403,7 @@ module TTY
|
|
398
403
|
def render_header
|
399
404
|
if @done
|
400
405
|
selected_item = choices[@active - 1].name
|
401
|
-
@prompt.decorate(selected_item, @active_color)
|
406
|
+
@prompt.decorate(selected_item.to_s, @active_color)
|
402
407
|
elsif @first_render
|
403
408
|
@prompt.decorate(help, @help_color)
|
404
409
|
elsif filterable? && @filter.any?
|
@@ -417,13 +422,13 @@ module TTY
|
|
417
422
|
@paginator.paginate(choices, @active, @per_page) do |choice, index|
|
418
423
|
num = enumerate? ? (index + 1).to_s + @enum + ' ' : ''
|
419
424
|
message = if index + 1 == @active && !choice.disabled?
|
420
|
-
selected = @marker
|
425
|
+
selected = "#{@marker} #{num}#{choice.name}"
|
421
426
|
@prompt.decorate(selected.to_s, @active_color)
|
422
427
|
elsif choice.disabled?
|
423
428
|
@prompt.decorate(symbols[:cross], :red) +
|
424
|
-
|
429
|
+
" #{num}#{choice.name} #{choice.disabled}"
|
425
430
|
else
|
426
|
-
|
431
|
+
" #{num}#{choice.name}"
|
427
432
|
end
|
428
433
|
max_index = paginated? ? @paginator.max_index : choices.size - 1
|
429
434
|
newline = (index == max_index) ? '' : "\n"
|
@@ -440,6 +445,7 @@ module TTY
|
|
440
445
|
# @api private
|
441
446
|
def render_footer
|
442
447
|
return '' unless paginated?
|
448
|
+
|
443
449
|
colored_footer = @prompt.decorate(@page_help, @help_color)
|
444
450
|
"\n" + colored_footer
|
445
451
|
end
|
@@ -9,7 +9,7 @@ module TTY
|
|
9
9
|
#
|
10
10
|
# @api private
|
11
11
|
class MultiList < List
|
12
|
-
HELP = '(Use arrow%s keys, press Space to select and Enter to finish%s)'
|
12
|
+
HELP = '(Use arrow%s keys, press Space to select and Enter to finish%s)'
|
13
13
|
|
14
14
|
# Create instance of TTY::Prompt::MultiList menu.
|
15
15
|
#
|
@@ -20,8 +20,7 @@ module TTY
|
|
20
20
|
def initialize(prompt, options)
|
21
21
|
super
|
22
22
|
@selected = []
|
23
|
-
@help
|
24
|
-
@default = Array(options[:default])
|
23
|
+
@help = options[:help]
|
25
24
|
@echo = options.fetch(:echo, true)
|
26
25
|
end
|
27
26
|
|
@@ -46,10 +45,11 @@ module TTY
|
|
46
45
|
validate_defaults
|
47
46
|
# At this stage, @choices matches all the visible choices.
|
48
47
|
@selected = @choices.values_at(*@default.map { |d| d - 1 })
|
49
|
-
|
50
|
-
if
|
51
|
-
|
52
|
-
|
48
|
+
|
49
|
+
if !@default.empty?
|
50
|
+
@active = @default.last
|
51
|
+
else
|
52
|
+
@active = @choices.index { |choice| !choice.disabled? } + 1
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -102,12 +102,12 @@ module TTY
|
|
102
102
|
indicator += ' '
|
103
103
|
message = if @selected.include?(choice) && !choice.disabled?
|
104
104
|
selected = @prompt.decorate(symbols[:radio_on], @active_color)
|
105
|
-
selected
|
105
|
+
"#{selected} #{num}#{choice.name}"
|
106
106
|
elsif choice.disabled?
|
107
107
|
@prompt.decorate(symbols[:cross], :red) +
|
108
|
-
|
108
|
+
" #{num}#{choice.name} #{choice.disabled}"
|
109
109
|
else
|
110
|
-
symbols[:radio_off]
|
110
|
+
"#{symbols[:radio_off]} #{num}#{choice.name}"
|
111
111
|
end
|
112
112
|
max_index = paginated? ? @paginator.max_index : choices.size - 1
|
113
113
|
newline = (index == max_index) ? '' : "\n"
|
data/lib/tty/prompt/version.rb
CHANGED
@@ -193,17 +193,17 @@ RSpec.describe TTY::Prompt do
|
|
193
193
|
end
|
194
194
|
|
195
195
|
it "doesn't paginate short selections" do
|
196
|
-
choices = %
|
196
|
+
choices = %i(A B C D)
|
197
197
|
prompt = TTY::TestPrompt.new
|
198
198
|
prompt.input << "\r"
|
199
199
|
prompt.input.rewind
|
200
200
|
|
201
201
|
answer = prompt.enum_select("What letter?", choices, per_page: 4, default: 1)
|
202
|
-
expect(answer).to eq(
|
202
|
+
expect(answer).to eq(:A)
|
203
203
|
|
204
204
|
expected_output =
|
205
|
-
output_helper("What letter?", choices,
|
206
|
-
exit_message("What letter?",
|
205
|
+
output_helper("What letter?", choices, :A) +
|
206
|
+
exit_message("What letter?", :A)
|
207
207
|
|
208
208
|
expect(prompt.output.string).to eq(expected_output)
|
209
209
|
end
|
@@ -45,7 +45,7 @@ RSpec.describe TTY::Prompt do
|
|
45
45
|
|
46
46
|
it "selects nothing when return pressed immediately" do
|
47
47
|
prompt = TTY::TestPrompt.new
|
48
|
-
choices = %
|
48
|
+
choices = %i(vodka beer wine whisky bourbon)
|
49
49
|
prompt.input << "\r"
|
50
50
|
prompt.input.rewind
|
51
51
|
expect(prompt.multi_select("Select drinks?", choices)). to eq([])
|
@@ -425,13 +425,29 @@ RSpec.describe TTY::Prompt do
|
|
425
425
|
end
|
426
426
|
|
427
427
|
context "with :disabled" do
|
428
|
-
it "fails when
|
428
|
+
it "fails when default item is also disabled" do
|
429
429
|
prompt = TTY::TestPrompt.new
|
430
|
-
choices = [
|
430
|
+
choices = [
|
431
|
+
{name: 'vodka', disabled: true},
|
432
|
+
'beer', 'wine', 'whisky', 'bourbon'
|
433
|
+
]
|
431
434
|
expect {
|
432
|
-
prompt.multi_select("Select drinks?", choices)
|
435
|
+
prompt.multi_select("Select drinks?", choices, default: 1)
|
433
436
|
}.to raise_error(TTY::Prompt::ConfigurationError,
|
434
|
-
/
|
437
|
+
/default index `1` matches disabled choice item/)
|
438
|
+
end
|
439
|
+
|
440
|
+
it "adjusts active index to match first non-disabled choice" do
|
441
|
+
choices = [
|
442
|
+
{name: 'vodka', disabled: true},
|
443
|
+
'beer', 'wine', 'whisky', 'bourbon'
|
444
|
+
]
|
445
|
+
prompt = TTY::TestPrompt.new
|
446
|
+
prompt.input << " " << "\r"
|
447
|
+
prompt.input.rewind
|
448
|
+
|
449
|
+
answer = prompt.multi_select("Select drinks?", choices)
|
450
|
+
expect(answer).to eq(['beer'])
|
435
451
|
end
|
436
452
|
|
437
453
|
it "omits disabled choice when nagivating menu" do
|
data/spec/unit/select_spec.rb
CHANGED
@@ -43,11 +43,12 @@ RSpec.describe TTY::Prompt, '#select' do
|
|
43
43
|
before { allow(TTY::Screen).to receive(:width).and_return(200) }
|
44
44
|
|
45
45
|
it "selects by default first option" do
|
46
|
-
choices = %
|
46
|
+
choices = %i(Large Medium Small)
|
47
47
|
prompt.input << "\r"
|
48
48
|
prompt.input.rewind
|
49
|
-
|
50
|
-
expect(prompt.
|
49
|
+
|
50
|
+
expect(prompt.select('What size?', choices)).to eq(:Large)
|
51
|
+
expected_output = [
|
51
52
|
"\e[?25lWhat size? \e[90m(Use arrow keys, press Enter to select)\e[0m\n",
|
52
53
|
"\e[32m#{symbols[:pointer]} Large\e[0m\n",
|
53
54
|
" Medium\n",
|
@@ -55,7 +56,9 @@ RSpec.describe TTY::Prompt, '#select' do
|
|
55
56
|
"\e[2K\e[1G\e[1A" * 3,
|
56
57
|
"\e[2K\e[1G",
|
57
58
|
"What size? \e[32mLarge\e[0m\n\e[?25h"
|
58
|
-
].join
|
59
|
+
].join
|
60
|
+
|
61
|
+
expect(prompt.output.string).to eq(expected_output)
|
59
62
|
end
|
60
63
|
|
61
64
|
it "allows navigation using events without errors" do
|
@@ -629,14 +632,28 @@ RSpec.describe TTY::Prompt, '#select' do
|
|
629
632
|
expect(prompt.output.string).to eq(expected_output)
|
630
633
|
end
|
631
634
|
|
632
|
-
it "sets
|
633
|
-
choices = [
|
635
|
+
it "sets active to be first non-disabled choice" do
|
636
|
+
choices = [
|
637
|
+
{name: 'Small', disabled: '(out of stock)'}, 'Medium', 'Large', 'Huge'
|
638
|
+
]
|
639
|
+
prompt = TTY::TestPrompt.new
|
640
|
+
prompt.input << "\r"
|
641
|
+
prompt.input.rewind
|
642
|
+
|
643
|
+
answer = prompt.select("What size?", choices)
|
644
|
+
expect(answer).to eq('Medium')
|
645
|
+
end
|
646
|
+
|
647
|
+
it "prevents setting default to disabled choice" do
|
648
|
+
choices = [
|
649
|
+
{name: 'Small', disabled: '(out of stock)'}, 'Medium', 'Large', 'Huge'
|
650
|
+
]
|
634
651
|
prompt = TTY::TestPrompt.new
|
635
652
|
prompt.input << "\r"
|
636
653
|
prompt.input.rewind
|
637
654
|
|
638
655
|
expect {
|
639
|
-
prompt.select("What size?", choices)
|
656
|
+
prompt.select("What size?", choices, default: 1)
|
640
657
|
}.to raise_error(TTY::Prompt::ConfigurationError, /default index `1` matches disabled choice item/)
|
641
658
|
end
|
642
659
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tty-prompt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.18.
|
4
|
+
version: 0.18.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: necromancer
|