tty-prompt 0.18.0 → 0.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6b5dc469236b43c2439a9d62d9356d540c3aa0c3e359916b97fabc2ffe53a1f
4
- data.tar.gz: '0229e21607f28ce8407e109d58b26b4d2d782a4f883e5a5580f5db022caa8e0d'
3
+ metadata.gz: 4993a3639c2dec47c658cddd6a8c3d98b4efb7bec6cc3d1b90dfd184d9b6f889
4
+ data.tar.gz: d069f2c94118978bbfa04e54c967a0d8ce185adc6d7eb74446988350bca21d06
5
5
  SHA512:
6
- metadata.gz: a0965a8ec33d8b6a00860e7f15fb6ebb731de62213b4322f732dc8a6c5b41c2da3030082fd3bca1a29e46f141d4233b9db4cbc7ddf9cd5b67632b184d34ca69f
7
- data.tar.gz: 8c663d1388d1ec3adfa348e344d74bde55b5d2b61c33db30c877ea363a9e4a78f9723c9e36721912a6987395f93b1d25d4f5222abf0999795bb23bd5b97911f1
6
+ metadata.gz: c1dd9287d14e3aa2eb8357d9ff759cdec52182535bc9697f97d4da7ab7c111f27ec61bfc176cc16cc7c8f4f2ca1dcd3af4784df1c82e95b0382f4cb2dc7cc782
7
+ data.tar.gz: 62ed0bc598c3a6b6b65ed7a5f14a7bacd769a5d46dfa041afb67d4d7a4de407e4cb5334fc25942351551414c0c29d68a9fc51aa238e198931677639c3ad4d382
@@ -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]: http://github.com/piotrmurach/tty-prompt/compare/v0.18.0...v0.18.1
299
308
  [v0.18.0]: http://github.com/piotrmurach/tty-prompt/compare/v0.17.2...v0.18.0
300
309
  [v0.17.2]: http://github.com/piotrmurach/tty-prompt/compare/v0.17.1...v0.17.2
301
310
  [v0.17.1]: http://github.com/piotrmurach/tty-prompt/compare/v0.17.0...v0.17.1
data/README.md CHANGED
@@ -1344,7 +1344,7 @@ Please [see pastel](http://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
 
@@ -3,5 +3,5 @@
3
3
  require_relative "../lib/tty-prompt"
4
4
 
5
5
  prompt = TTY::Prompt.new
6
- choices = %w(/bin/nano /usr/bin/vim.basic /usr/bin/vim.tiny)
6
+ choices = %i(/bin/nano /usr/bin/vim.basic /usr/bin/vim.tiny)
7
7
  prompt.enum_select('Select an editor', choices, default: 2)
@@ -4,7 +4,7 @@ require_relative "../lib/tty-prompt"
4
4
 
5
5
  prompt = TTY::Prompt.new
6
6
 
7
- warriors = %w(Scorpion Kano Jax Kitana Raiden)
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
@@ -20,7 +20,8 @@ module TTY
20
20
  # @api public
21
21
  attr_reader :choices
22
22
 
23
- def_delegators :choices, :length, :size, :to_ary, :empty?, :values_at
23
+ def_delegators :choices, :length, :size, :to_ary, :empty?,
24
+ :values_at, :index
24
25
 
25
26
  # Convenience for creating choices
26
27
  #
@@ -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?
@@ -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)'.freeze
18
+ HELP = '(Use arrow%s keys, press Enter to select%s)'
19
19
 
20
- PAGE_HELP = '(Move up or down to reveal more choices)'.freeze
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[options.fetch(:default) { 1 }]
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
- ['', ", and letter keys to filter"]
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 |_choice|
164
- !_choice.disabled? &&
165
- _choice.name.downcase.include?(@filter.join.downcase)
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 = (choices.length).downto(1).to_a
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
- @active = @default.first
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 + ' ' + num + choice.name
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
- ' ' + num + choice.name + ' ' + choice.disabled.to_s
429
+ " #{num}#{choice.name} #{choice.disabled}"
425
430
  else
426
- ' ' * 2 + num + choice.name
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)'.freeze
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 = options[: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
- @active = @default.last unless @selected.empty?
50
- if choices[@active - 1] && choices[@active - 1].disabled?
51
- raise ConfigurationError,
52
- "active choice '#{choices[@active - 1]}' matches disabled item"
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 + ' ' + num + choice.name
105
+ "#{selected} #{num}#{choice.name}"
106
106
  elsif choice.disabled?
107
107
  @prompt.decorate(symbols[:cross], :red) +
108
- ' ' + num + choice.name + ' ' + choice.disabled.to_s
108
+ " #{num}#{choice.name} #{choice.disabled}"
109
109
  else
110
- symbols[:radio_off] + ' ' + num + choice.name
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"
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  class Prompt
5
- VERSION = '0.18.0'
5
+ VERSION = '0.18.1'
6
6
  end # Prompt
7
7
  end # TTY
@@ -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 = %w(A B C D)
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('A')
202
+ expect(answer).to eq(:A)
203
203
 
204
204
  expected_output =
205
- output_helper("What letter?", choices, "A") +
206
- exit_message("What letter?", "A")
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 = %w(vodka beer wine whisky bourbon)
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 active item is also disabled" do
428
+ it "fails when default item is also disabled" do
429
429
  prompt = TTY::TestPrompt.new
430
- choices = [{name: 'vodka', disabled: true}, 'beer', 'wine', 'whisky', 'bourbon']
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
- /active choice 'vodka' matches disabled item/)
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
@@ -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 = %w(Large Medium Small)
46
+ choices = %i(Large Medium Small)
47
47
  prompt.input << "\r"
48
48
  prompt.input.rewind
49
- expect(prompt.select('What size?', choices)).to eq('Large')
50
- expect(prompt.output.string).to eq([
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 default correct when disabled choice" do
633
- choices = [ {name: 'Small', disabled: '(out of stock)'}, 'Medium', 'Large', 'Huge' ]
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.0
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-24 00:00:00.000000000 Z
11
+ date: 2018-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: necromancer