highline 2.1.0 → 3.0.0.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +0 -5
- data/Changelog.md +13 -0
- data/README.md +36 -0
- data/examples/custom_parser_custom_validator.rb +39 -0
- data/highline.gemspec +2 -1
- data/lib/highline/io_console_compatible.rb +1 -1
- data/lib/highline/question/answer_converter.rb +2 -5
- data/lib/highline/question.rb +21 -7
- data/lib/highline/question_asker.rb +1 -0
- data/lib/highline/terminal/io_console.rb +1 -1
- data/lib/highline/terminal/unix_stty.rb +6 -4
- data/lib/highline/terminal.rb +1 -1
- data/lib/highline/version.rb +1 -1
- data/lib/highline.rb +4 -0
- metadata +21 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 00a520ba256dca588d4af090b8a9c41d808bd208966d3d3eabaf17a5e5516ad1
|
4
|
+
data.tar.gz: c0ab504488edce75cb6e290a119467ffb91ebbd9abbab3acedcc3da7198ad554
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '09efdc10dabb3dfedf2155faff822c845f819119f18abdcaa298607df6cf279ed955d693e31db5038509a16dbbee35f61fa18dc88591cfa6d2aa9543213d0129'
|
7
|
+
data.tar.gz: c0a80e2ed2caaada155c2b66433dcafec06e0abb63bfe206470e65697b46f8a9888bbc7d2c64bb21f7cbf46218b5561a00f62f115f9d4355d098cbf911cf1523
|
data/.github/workflows/ci.yml
CHANGED
data/Changelog.md
CHANGED
@@ -2,6 +2,19 @@
|
|
2
2
|
|
3
3
|
Below is a complete listing of changes for each revision of HighLine.
|
4
4
|
|
5
|
+
### 3.0.0.pre.1 / 2023-04-27
|
6
|
+
* PR #263 - Release 3.0.0.pre.1
|
7
|
+
* Raise minimum Ruby version requirement to 3.0
|
8
|
+
* PR #262 - Do not call stty on non-tty (@kbrock)
|
9
|
+
* PR #260 / I #43 - Ctrl-U (erase line) handling (@abinoam, issue by @gutenye)
|
10
|
+
* PR #259 / I #236 - Handle Ctrl-C when Question#echo = false (@abinoam, @Fahhetah, issue by @aspyct)
|
11
|
+
* PR #258 / I #246 - Add validation class support (@abinoam, issue by @Joshfindit)
|
12
|
+
* Make it dry-types compatible through the use of `#valid?`
|
13
|
+
* Solve the multiple answers in one line problem with a combination of custom coercion (parser) and custom validation
|
14
|
+
* PR #257 / I #233 - Show Question#default hint for non String values (@abinoam, issue by @branch14)
|
15
|
+
* Add Question#default_hint_show to allow disabling it.
|
16
|
+
* PR #256 / I #249 - Fix Array validation in Question#in (@abinoam, issue by @esotericpig)
|
17
|
+
|
5
18
|
### 2.1.0 / 2022-12-31
|
6
19
|
* PR #255 - Change minimum Ruby version requirement to 2.3 (@abinoam)
|
7
20
|
* PR #254 - Improve Github Actions file (@abinoam)
|
data/README.md
CHANGED
@@ -43,12 +43,48 @@ puts "You have answered: #{answer}"
|
|
43
43
|
|
44
44
|
cli.ask("Company? ") { |q| q.default = "none" }
|
45
45
|
|
46
|
+
## Disable default value hint showing
|
47
|
+
|
48
|
+
my_special_default_object = Object.new
|
49
|
+
|
50
|
+
cli.ask("Question? ") do |q|
|
51
|
+
q.default = my_special_default_object
|
52
|
+
q.default_hint_show = false
|
53
|
+
end
|
54
|
+
|
46
55
|
|
47
56
|
# Validation
|
48
57
|
|
49
58
|
cli.ask("Age? ", Integer) { |q| q.in = 0..105 }
|
50
59
|
cli.ask("Name? (last, first) ") { |q| q.validate = /\A\w+, ?\w+\Z/ }
|
51
60
|
|
61
|
+
## Validation with custom class
|
62
|
+
class ZeroToTwentyFourValidator
|
63
|
+
def self.valid?(answer)
|
64
|
+
(0..24).include? answer.to_i
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.inspect
|
68
|
+
"(0..24) rule"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
cli.ask("What hour of the day is it?: ", Integer) do |q|
|
73
|
+
q.validate = ZeroToTwentyFourValidator
|
74
|
+
end
|
75
|
+
|
76
|
+
## Validation with Dry::Types
|
77
|
+
## `Dry::Types` provides a `valid?` method so it can be used effortlessly
|
78
|
+
|
79
|
+
require 'dry-type'
|
80
|
+
|
81
|
+
module Types
|
82
|
+
include Dry.Types
|
83
|
+
end
|
84
|
+
|
85
|
+
cli.ask("Type an integer:", Integer) do |q|
|
86
|
+
q.validate = Types::Coercible::Integer
|
87
|
+
end
|
52
88
|
|
53
89
|
# Type conversion for answers:
|
54
90
|
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'highline'
|
2
|
+
|
3
|
+
cli = HighLine.new
|
4
|
+
|
5
|
+
# The parser
|
6
|
+
class ArrayOfNumbersFromString
|
7
|
+
def self.parse(string)
|
8
|
+
string.scan(/\d+/).map(&:to_i)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# The validator
|
13
|
+
class ArrayOfNumbersFromStringInRange
|
14
|
+
def self.in?(range)
|
15
|
+
new(range)
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_reader :range
|
19
|
+
|
20
|
+
def initialize(range)
|
21
|
+
@range = range
|
22
|
+
end
|
23
|
+
|
24
|
+
def valid?(answer)
|
25
|
+
ary = ArrayOfNumbersFromString.parse(answer)
|
26
|
+
ary.all? ->(number) { range.include? number }
|
27
|
+
end
|
28
|
+
|
29
|
+
def inspect
|
30
|
+
"in range #@range validator"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
answer = cli.ask("Which number? (0 or <Enter> to skip): ", ArrayOfNumbersFromString) { |q|
|
35
|
+
q.validate = ArrayOfNumbersFromStringInRange.in?(0..10)
|
36
|
+
q.default = 0
|
37
|
+
}
|
38
|
+
|
39
|
+
puts "Your answer was: #{answer} and it was correctly validated and coerced into an #{answer.class}"
|
data/highline.gemspec
CHANGED
@@ -27,9 +27,10 @@ DESCRIPTION
|
|
27
27
|
|
28
28
|
spec.extra_rdoc_files = %w[README.md TODO Changelog.md LICENSE]
|
29
29
|
|
30
|
-
spec.required_ruby_version = ">=
|
30
|
+
spec.required_ruby_version = ">= 3.0"
|
31
31
|
|
32
32
|
spec.add_development_dependency "bundler"
|
33
33
|
spec.add_development_dependency "rake"
|
34
34
|
spec.add_development_dependency "minitest"
|
35
|
+
spec.add_development_dependency "dry-types"
|
35
36
|
end
|
@@ -9,7 +9,7 @@ class HighLine
|
|
9
9
|
extend Forwardable
|
10
10
|
|
11
11
|
def_delegators :@question,
|
12
|
-
:answer, :answer=,
|
12
|
+
:answer, :answer=,
|
13
13
|
:directory, :answer_type, :choices_complete
|
14
14
|
|
15
15
|
# It should be initialized with a Question object.
|
@@ -26,10 +26,7 @@ class HighLine
|
|
26
26
|
# it makes the conversion and returns the answer.
|
27
27
|
# @return [Object] the converted answer.
|
28
28
|
def convert
|
29
|
-
|
30
|
-
|
31
|
-
self.answer = convert_by_answer_type
|
32
|
-
check_range
|
29
|
+
self.answer = convert_by_answer_type if answer_type
|
33
30
|
answer
|
34
31
|
end
|
35
32
|
|
data/lib/highline/question.rb
CHANGED
@@ -56,6 +56,7 @@ class HighLine
|
|
56
56
|
@completion = @answer_type
|
57
57
|
|
58
58
|
@echo = true
|
59
|
+
@default_hint_show = true
|
59
60
|
@whitespace = :strip
|
60
61
|
@case = nil
|
61
62
|
@in = nil
|
@@ -136,10 +137,17 @@ class HighLine
|
|
136
137
|
attr_accessor :case
|
137
138
|
# Used to provide a default answer to this question.
|
138
139
|
attr_accessor :default
|
140
|
+
# Set it to a truthy or falsy value to enable or disable showing the default
|
141
|
+
# value hint between vertical bars (pipes) when asking the question.
|
142
|
+
# Defaults to +true+
|
143
|
+
attr_accessor :default_hint_show
|
139
144
|
#
|
140
145
|
# If set to a Regexp, the answer must match (before type conversion).
|
141
146
|
# Can also be set to a Proc which will be called with the provided
|
142
147
|
# answer to validate with a +true+ or +false+ return.
|
148
|
+
# It's possible to use a custom validator class. It must respond to
|
149
|
+
# `#valid?`. The result of `#inspect` will be used in error messages.
|
150
|
+
# See README.md for details.
|
143
151
|
#
|
144
152
|
attr_accessor :validate
|
145
153
|
# Used to control range checks for answer.
|
@@ -252,7 +260,7 @@ class HighLine
|
|
252
260
|
# Same as {#answer_type}.
|
253
261
|
|
254
262
|
def build_responses(message_source = answer_type)
|
255
|
-
|
263
|
+
append_default_to_template if default_hint_show
|
256
264
|
|
257
265
|
new_hash = build_responses_new_hash(message_source)
|
258
266
|
# Update our internal responses with the new hash
|
@@ -497,7 +505,8 @@ class HighLine
|
|
497
505
|
def valid_answer?
|
498
506
|
!validate ||
|
499
507
|
(validate.is_a?(Regexp) && answer =~ validate) ||
|
500
|
-
(validate.is_a?(Proc) && validate[answer])
|
508
|
+
(validate.is_a?(Proc) && validate[answer]) ||
|
509
|
+
(validate.respond_to?(:valid?) && validate.valid?(answer))
|
501
510
|
end
|
502
511
|
|
503
512
|
#
|
@@ -607,15 +616,20 @@ class HighLine
|
|
607
616
|
# Trailing whitespace is preserved so the function of HighLine.say() is
|
608
617
|
# not affected.
|
609
618
|
#
|
610
|
-
def
|
619
|
+
def append_default_to_template
|
620
|
+
return unless default.respond_to? :to_s
|
621
|
+
|
622
|
+
default_str = default.to_s
|
623
|
+
return if default_str.empty?
|
624
|
+
|
611
625
|
if template =~ /([\t ]+)\Z/
|
612
|
-
template << "|#{
|
626
|
+
template << "|#{default_str}|#{Regexp.last_match(1)}"
|
613
627
|
elsif template == ""
|
614
|
-
template << "|#{
|
628
|
+
template << "|#{default_str}| "
|
615
629
|
elsif template[-1, 1] == "\n"
|
616
|
-
template[-2, 0] = " |#{
|
630
|
+
template[-2, 0] = " |#{default_str}|"
|
617
631
|
else
|
618
|
-
template << " |#{
|
632
|
+
template << " |#{default_str}|"
|
619
633
|
end
|
620
634
|
end
|
621
635
|
|
@@ -20,7 +20,9 @@ class HighLine
|
|
20
20
|
rescue LoadError
|
21
21
|
end
|
22
22
|
|
23
|
-
if
|
23
|
+
if !@output.tty?
|
24
|
+
[80, 24]
|
25
|
+
elsif /solaris/ =~ RUBY_PLATFORM &&
|
24
26
|
`stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/
|
25
27
|
[Regexp.last_match(2), Regexp.last_match(1)].map(&:to_i)
|
26
28
|
elsif `stty size` =~ /^(\d+)\s(\d+)$/
|
@@ -32,13 +34,13 @@ class HighLine
|
|
32
34
|
|
33
35
|
# (see Terminal#raw_no_echo_mode)
|
34
36
|
def raw_no_echo_mode
|
35
|
-
|
36
|
-
system "stty raw -echo -icanon isig"
|
37
|
+
save_stty
|
38
|
+
system "stty raw -echo -icanon isig" if input.tty?
|
37
39
|
end
|
38
40
|
|
39
41
|
# (see Terminal#restore_mode)
|
40
42
|
def restore_mode
|
41
|
-
|
43
|
+
restore_stty
|
42
44
|
print "\r"
|
43
45
|
end
|
44
46
|
|
data/lib/highline/terminal.rb
CHANGED
data/lib/highline/version.rb
CHANGED
data/lib/highline.rb
CHANGED
@@ -538,6 +538,7 @@ class HighLine
|
|
538
538
|
terminal.raw_no_echo_mode_exec do
|
539
539
|
loop do
|
540
540
|
character = terminal.get_character
|
541
|
+
raise Interrupt if character == "\u0003"
|
541
542
|
break unless character
|
542
543
|
break if ["\n", "\r"].include? character
|
543
544
|
|
@@ -545,6 +546,9 @@ class HighLine
|
|
545
546
|
if character == "\b" || character == "\u007F"
|
546
547
|
chopped = line.chop!
|
547
548
|
output_erase_char if chopped && question.echo
|
549
|
+
elsif character == "\cU"
|
550
|
+
line.size.times { output_erase_char } if question.echo
|
551
|
+
line = ""
|
548
552
|
elsif character == "\e"
|
549
553
|
ignore_arrow_key
|
550
554
|
else
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: highline
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0.pre.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Edward Gray II
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: dry-types
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: |
|
56
70
|
A high-level IO library that provides validation, type conversion, and more for
|
57
71
|
command-line interfaces. HighLine also includes a complete menu system that can
|
@@ -83,6 +97,7 @@ files:
|
|
83
97
|
- examples/asking_for_arrays.rb
|
84
98
|
- examples/basic_usage.rb
|
85
99
|
- examples/color_scheme.rb
|
100
|
+
- examples/custom_parser_custom_validator.rb
|
86
101
|
- examples/get_character.rb
|
87
102
|
- examples/limit.rb
|
88
103
|
- examples/menus.rb
|
@@ -136,14 +151,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
136
151
|
requirements:
|
137
152
|
- - ">="
|
138
153
|
- !ruby/object:Gem::Version
|
139
|
-
version: '
|
154
|
+
version: '3.0'
|
140
155
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
156
|
requirements:
|
142
|
-
- - "
|
157
|
+
- - ">"
|
143
158
|
- !ruby/object:Gem::Version
|
144
|
-
version:
|
159
|
+
version: 1.3.1
|
145
160
|
requirements: []
|
146
|
-
rubygems_version: 3.4.
|
161
|
+
rubygems_version: 3.4.6
|
147
162
|
signing_key:
|
148
163
|
specification_version: 4
|
149
164
|
summary: HighLine is a high-level command-line IO library.
|