tty-prompt 0.23.0 → 0.23.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: 3dcdfb83f0945b162d19c5a068386009fbc5cc2f90969374018ee9aad72bd116
4
- data.tar.gz: 004ebca9f017ea58bca8d4400e61d4817394b55343c0ecc3a3727f8818103d8f
3
+ metadata.gz: 69ced9f6a4234a7234fc2dbce1bc17a982afd2922d7a5c30e0f57030c0970e01
4
+ data.tar.gz: 26835914ed361e5617660f8bbd9106c101d2963a6e7ae335c4c57ed910658773
5
5
  SHA512:
6
- metadata.gz: e9fe7560e4dd4aca6e47d6ca67dca40079b4350ac97834d9bdca8c21254048097b338092177bebe5a1a2da1609fce1e155ee4a373c3d16c6979fc261a02dcc52
7
- data.tar.gz: eff0e563cde0179831056cf107f61e17681269b705f7d499a657d84f365197500c5ea0220aad8b3959ef3d82bd0ac1db49d8beb4566881cb257112b2f69beafa
6
+ metadata.gz: 767a3e943e5740eddbcf14fe9d25fd1d14054402c45d7020762b2cc78842720699223ec27fd229ea70cfb47ab6879b5fd144a76073e44092ed4b8eca1a56e357
7
+ data.tar.gz: 65b4c4d5ec2d06b900ef4bbd2012eb12c1c1952fe2b7c8ba1865714393da5c34f232a13a35bbefeee870b57d2cb741c77586725bd00a3a1e6a7a61521dfb8c13
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Change log
2
2
 
3
+ ## [v0.23.1] - 2021-04-17
4
+
5
+ ### Changed
6
+ * Change validate to allow access to invalid input inside the message
7
+
8
+ ### Fixed
9
+ * Fix Choice#from to differentiate between no value being set and nil value
10
+
3
11
  ## [v0.23.0] - 2020-12-14
4
12
 
5
13
  ### Added
@@ -394,6 +402,7 @@
394
402
 
395
403
  * Initial implementation and release
396
404
 
405
+ [v0.23.1]: https://github.com/piotrmurach/tty-prompt/compare/v0.23.0...v0.23.1
397
406
  [v0.23.0]: https://github.com/piotrmurach/tty-prompt/compare/v0.22.0...v0.23.0
398
407
  [v0.22.0]: https://github.com/piotrmurach/tty-prompt/compare/v0.21.0...v0.22.0
399
408
  [v0.21.0]: https://github.com/piotrmurach/tty-prompt/compare/v0.20.0...v0.21.0
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2015 Piotr Murach
1
+ Copyright (c) 2015 Piotr Murach (piotrmurach.com)
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -357,7 +357,7 @@ prompt.ask("password:", echo: false)
357
357
 
358
358
  #### 2.1.5 error messages
359
359
 
360
- By default `tty-prompt` comes with predefined error messages for `required`, `in`, `validate` options.
360
+ By default `tty-prompt` comes with predefined error messages for `convert`, `required`, `in`, `validate` options.
361
361
 
362
362
  You can change these and configure to your liking either by passing message as second argument with the option:
363
363
 
@@ -367,7 +367,7 @@ prompt.ask("What is your email?") do |q|
367
367
  end
368
368
  ```
369
369
 
370
- Or change the `messages` key entry out of `:required?`, `:valid?`, `:range?`:
370
+ Or change the `messages` key entry out of `:convert?`, `:range?`, `:required?` and `:valid?`:
371
371
 
372
372
  ```ruby
373
373
  prompt.ask("What is your email?") do |q|
@@ -432,7 +432,9 @@ prompt.ask("What's your phone number?", required: true)
432
432
 
433
433
  #### 2.1.9 `:validate`
434
434
 
435
- In order to validate that input matches a given pattern you can pass the `validate` option. Validate setting accepts `Regex`, `Proc` or `Symbol`.
435
+ In order to validate that input matches a given pattern you can pass the `validate` option/method.
436
+
437
+ Validate accepts `Regex`, `Proc` or `Symbol`.
436
438
 
437
439
  ```ruby
438
440
  prompt.ask("What is your username?") do |q|
@@ -440,18 +442,37 @@ prompt.ask("What is your username?") do |q|
440
442
  end
441
443
  ```
442
444
 
445
+ The above can also be expressed as a `Proc`:
446
+
443
447
  ```ruby
444
448
  prompt.ask("What is your username?") do |q|
445
449
  q.validate ->(input) { input =~ /\A[^.]+\.[^.]+\Z/ }
446
450
  end
447
451
  ```
448
452
 
449
- The **TTY::Prompt** comes with built-in validations for `:email` and you can use them directly like so:
453
+ There is a built-in validation for `:email` and you can use it directly like so:
450
454
 
451
455
  ```ruby
452
456
  prompt.ask("What is your email?") { |q| q.validate :email }
453
457
  ```
454
458
 
459
+ The default validation message is `"Your answer is invalid (must match %{valid})"` and you can customise it by passing in a second argument:
460
+
461
+ ```ruby
462
+ prompt.ask("What is your username?") do |q|
463
+ q.validate(/\A[^.]+\.[^.]+\Z/, "Invalid username: %{value}, must match %{valid}")
464
+ end
465
+ ```
466
+
467
+ The default message can also be set using `messages` and the `:valid?` key:
468
+
469
+ ```ruby
470
+ prompt.ask("What is your username?") do |q|
471
+ q.validate(/\A[^.]+\.[^.]+\Z/)
472
+ q.messages[:valid?] = "Invalid username: %{value}, must match %{valid}")
473
+ end
474
+ ```
475
+
455
476
  ### 2.2. keypress
456
477
 
457
478
  In order to ask question that awaits a single character answer use `keypress` prompt like so:
@@ -31,12 +31,7 @@ module TTY
31
31
  when Choice
32
32
  val
33
33
  when Array
34
- name, value, options = *val
35
- if name.is_a?(Hash)
36
- convert_hash(name)
37
- else
38
- new(name.to_s, (value.nil? ? name.to_s : value), **(options || {}))
39
- end
34
+ convert_array(val)
40
35
  when Hash
41
36
  convert_hash(val)
42
37
  else
@@ -44,7 +39,29 @@ module TTY
44
39
  end
45
40
  end
46
41
 
47
- # Convert hash into choice
42
+ # Convert an array into choice
43
+ #
44
+ # @param [Array<Object>]
45
+ #
46
+ # @return [Choice]
47
+ #
48
+ # @api public
49
+ def self.convert_array(val)
50
+ name, value, options = *val
51
+ if name.is_a?(Hash)
52
+ convert_hash(name)
53
+ elsif val.size == 1
54
+ new(name.to_s, name.to_s)
55
+ else
56
+ new(name.to_s, value, **(options || {}))
57
+ end
58
+ end
59
+
60
+ # Convert a hash into choice
61
+ #
62
+ # @param [Hash<Symbol,Object>]
63
+ #
64
+ # @return [Choice]
48
65
  #
49
66
  # @api public
50
67
  def self.convert_hash(val)
@@ -56,7 +56,7 @@ module TTY
56
56
  Validation.new(question.validation).call(value))
57
57
  [value]
58
58
  else
59
- tokens = { valid: question.validation.inspect }
59
+ tokens = { valid: question.validation.inspect, value: value }
60
60
  [value, question.message_for(:valid?, tokens)]
61
61
  end
62
62
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  class Prompt
5
- VERSION = "0.23.0"
5
+ VERSION = "0.23.1"
6
6
  end # Prompt
7
7
  end # TTY
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.23.0
4
+ version: 0.23.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: 2020-12-14 00:00:00.000000000 Z
11
+ date: 2021-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel