tty-prompt 0.20.0 → 0.21.0
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 +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +29 -4
- data/lib/tty/prompt.rb +3 -3
- data/lib/tty/prompt/multi_list.rb +33 -10
- data/lib/tty/prompt/version.rb +1 -1
- metadata +11 -175
- data/Rakefile +0 -8
- data/examples/ask.rb +0 -7
- data/examples/ask_blank.rb +0 -9
- data/examples/ask_multiline.rb +0 -7
- data/examples/ask_valid.rb +0 -12
- data/examples/collect.rb +0 -21
- data/examples/echo.rb +0 -11
- data/examples/enum_select.rb +0 -7
- data/examples/enum_select_disabled.rb +0 -16
- data/examples/enum_select_paged.rb +0 -9
- data/examples/enum_select_wrapped.rb +0 -15
- data/examples/expand.rb +0 -29
- data/examples/expand_auto.rb +0 -29
- data/examples/in.rb +0 -9
- data/examples/inputs.rb +0 -10
- data/examples/key_events.rb +0 -15
- data/examples/keypress.rb +0 -9
- data/examples/mask.rb +0 -13
- data/examples/multi_select.rb +0 -8
- data/examples/multi_select_disabled.rb +0 -17
- data/examples/multi_select_disabled_paged.rb +0 -22
- data/examples/multi_select_paged.rb +0 -9
- data/examples/multi_select_wrapped.rb +0 -15
- data/examples/multiline.rb +0 -9
- data/examples/pause.rb +0 -9
- data/examples/select.rb +0 -24
- data/examples/select_disabled.rb +0 -18
- data/examples/select_disabled_paged.rb +0 -22
- data/examples/select_enum.rb +0 -8
- data/examples/select_filtered.rb +0 -11
- data/examples/select_paginated.rb +0 -11
- data/examples/select_wrapped.rb +0 -15
- data/examples/slider.rb +0 -6
- data/examples/validation.rb +0 -9
- data/examples/yes_no.rb +0 -7
- data/spec/spec_helper.rb +0 -61
- data/spec/unit/ask_spec.rb +0 -173
- data/spec/unit/block_paginator_spec.rb +0 -84
- data/spec/unit/choice/eql_spec.rb +0 -22
- data/spec/unit/choice/from_spec.rb +0 -130
- data/spec/unit/choices/add_spec.rb +0 -12
- data/spec/unit/choices/each_spec.rb +0 -13
- data/spec/unit/choices/find_by_spec.rb +0 -10
- data/spec/unit/choices/new_spec.rb +0 -10
- data/spec/unit/choices/pluck_spec.rb +0 -9
- data/spec/unit/collect_spec.rb +0 -96
- data/spec/unit/converters/convert_bool_spec.rb +0 -58
- data/spec/unit/converters/convert_char_spec.rb +0 -11
- data/spec/unit/converters/convert_custom_spec.rb +0 -14
- data/spec/unit/converters/convert_date_spec.rb +0 -34
- data/spec/unit/converters/convert_file_spec.rb +0 -18
- data/spec/unit/converters/convert_number_spec.rb +0 -39
- data/spec/unit/converters/convert_path_spec.rb +0 -15
- data/spec/unit/converters/convert_range_spec.rb +0 -22
- data/spec/unit/converters/convert_regex_spec.rb +0 -12
- data/spec/unit/converters/convert_string_spec.rb +0 -21
- data/spec/unit/converters/on_error_spec.rb +0 -9
- data/spec/unit/distance/distance_spec.rb +0 -73
- data/spec/unit/enum_select_spec.rb +0 -518
- data/spec/unit/error_spec.rb +0 -20
- data/spec/unit/evaluator_spec.rb +0 -67
- data/spec/unit/expand_spec.rb +0 -290
- data/spec/unit/keypress_spec.rb +0 -66
- data/spec/unit/mask_spec.rb +0 -140
- data/spec/unit/multi_select_spec.rb +0 -741
- data/spec/unit/multiline_spec.rb +0 -77
- data/spec/unit/new_spec.rb +0 -20
- data/spec/unit/ok_spec.rb +0 -10
- data/spec/unit/paginator_spec.rb +0 -92
- data/spec/unit/question/checks_spec.rb +0 -97
- data/spec/unit/question/default_spec.rb +0 -31
- data/spec/unit/question/echo_spec.rb +0 -38
- data/spec/unit/question/in_spec.rb +0 -115
- data/spec/unit/question/initialize_spec.rb +0 -12
- data/spec/unit/question/modifier/apply_to_spec.rb +0 -24
- data/spec/unit/question/modifier/letter_case_spec.rb +0 -41
- data/spec/unit/question/modifier/whitespace_spec.rb +0 -51
- data/spec/unit/question/modify_spec.rb +0 -41
- data/spec/unit/question/required_spec.rb +0 -92
- data/spec/unit/question/validate_spec.rb +0 -115
- data/spec/unit/question/validation/call_spec.rb +0 -31
- data/spec/unit/question/validation/coerce_spec.rb +0 -30
- data/spec/unit/result_spec.rb +0 -40
- data/spec/unit/say_spec.rb +0 -67
- data/spec/unit/select_spec.rb +0 -942
- data/spec/unit/slider_spec.rb +0 -142
- data/spec/unit/statement/initialize_spec.rb +0 -15
- data/spec/unit/subscribe_spec.rb +0 -22
- data/spec/unit/suggest_spec.rb +0 -28
- data/spec/unit/timer_spec.rb +0 -29
- data/spec/unit/warn_spec.rb +0 -21
- data/spec/unit/yes_no_spec.rb +0 -285
- data/tasks/console.rake +0 -11
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
- data/tty-prompt.gemspec +0 -40
data/spec/unit/result_spec.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Prompt::Result do
|
4
|
-
it "checks value to be invalid" do
|
5
|
-
question = double(:question)
|
6
|
-
result = TTY::Prompt::Result.new(question, nil)
|
7
|
-
|
8
|
-
answer = result.with { |quest, value|
|
9
|
-
if value.nil?
|
10
|
-
[value, ["`#{value}` provided cannot be empty"]]
|
11
|
-
else
|
12
|
-
value
|
13
|
-
end
|
14
|
-
}
|
15
|
-
expect(answer).to be_a(TTY::Prompt::Result::Failure)
|
16
|
-
expect(answer.success?).to eq(false)
|
17
|
-
expect(answer.errors).to eq(["`` provided cannot be empty"])
|
18
|
-
end
|
19
|
-
|
20
|
-
it "checks value to be valid" do
|
21
|
-
question = double(:question)
|
22
|
-
result = TTY::Prompt::Result.new(question, 'Piotr')
|
23
|
-
|
24
|
-
CheckRequired = Class.new do
|
25
|
-
def self.call(quest, value)
|
26
|
-
if value.nil?
|
27
|
-
[value, ["`#{value}` provided cannot be empty"]]
|
28
|
-
else
|
29
|
-
value
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
answer = result.with(CheckRequired)
|
35
|
-
expect(answer).to be_a(TTY::Prompt::Result::Success)
|
36
|
-
expect(answer.success?).to eq(true)
|
37
|
-
expect(answer.value).to eq('Piotr')
|
38
|
-
expect(answer.errors).to eq([])
|
39
|
-
end
|
40
|
-
end
|
data/spec/unit/say_spec.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Prompt, '#say' do
|
4
|
-
|
5
|
-
subject(:prompt) { TTY::TestPrompt.new }
|
6
|
-
|
7
|
-
it 'prints an empty message' do
|
8
|
-
prompt.say('')
|
9
|
-
expect(prompt.output.string).to eq('')
|
10
|
-
end
|
11
|
-
|
12
|
-
context 'with new line' do
|
13
|
-
it 'prints a message with newline' do
|
14
|
-
prompt.say("Hell yeah!\n")
|
15
|
-
expect(prompt.output.string).to eq("Hell yeah!\n")
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'prints a message with implicit newline' do
|
19
|
-
prompt.say("Hell yeah!\n")
|
20
|
-
expect(prompt.output.string).to eq("Hell yeah!\n")
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'prints a message with newline within text' do
|
24
|
-
prompt.say("Hell\n yeah!")
|
25
|
-
expect(prompt.output.string).to eq("Hell\n yeah!\n")
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'prints a message with newline within text and blank space' do
|
29
|
-
prompt.say("Hell\n yeah! ")
|
30
|
-
expect(prompt.output.string).to eq("Hell\n yeah! ")
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'prints a message without newline' do
|
34
|
-
prompt.say("Hell yeah!", newline: false)
|
35
|
-
expect(prompt.output.string).to eq("Hell yeah!")
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
context 'with tab or space' do
|
40
|
-
it 'prints ' do
|
41
|
-
prompt.say("Hell yeah!\t")
|
42
|
-
expect(prompt.output.string).to eq("Hell yeah!\t")
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context 'with color' do
|
47
|
-
it 'prints message with ansi color' do
|
48
|
-
prompt.say('Hell yeah!', color: :green)
|
49
|
-
expect(prompt.output.string).to eq("\e[32mHell yeah!\e[0m\n")
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'prints message with ansi color without newline' do
|
53
|
-
prompt.say('Hell yeah! ', color: :green)
|
54
|
-
expect(prompt.output.string).to eq("\e[32mHell yeah! \e[0m")
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
context 'without color' do
|
59
|
-
it 'prints message without ansi' do
|
60
|
-
prompt = TTY::TestPrompt.new(enable_color: false)
|
61
|
-
|
62
|
-
prompt.say('Hell yeah!', color: :green)
|
63
|
-
|
64
|
-
expect(prompt.output.string).to eq("Hell yeah!\n")
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
data/spec/unit/select_spec.rb
DELETED
@@ -1,942 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Prompt, '#select' do
|
4
|
-
|
5
|
-
subject(:prompt) { TTY::TestPrompt.new }
|
6
|
-
|
7
|
-
let(:symbols) { TTY::Prompt::Symbols.symbols }
|
8
|
-
let(:up_down) { "#{symbols[:arrow_up]}/#{symbols[:arrow_down]}" }
|
9
|
-
let(:left_right) { "#{symbols[:arrow_left]}/#{symbols[:arrow_right]}"}
|
10
|
-
|
11
|
-
def output_helper(prompt, choices, active, options = {})
|
12
|
-
raise ":init requires :hint" if options[:init] && options[:hint].nil?
|
13
|
-
hint = options[:hint]
|
14
|
-
init = options.fetch(:init, false)
|
15
|
-
enum = options[:enum]
|
16
|
-
|
17
|
-
out = []
|
18
|
-
out << "\e[?25l" if init
|
19
|
-
out << prompt << " "
|
20
|
-
out << "\e[90m(#{hint})\e[0m" if hint
|
21
|
-
out << "\n"
|
22
|
-
out << choices.map.with_index do |c, i|
|
23
|
-
name = c.is_a?(Hash) ? c[:name] : c
|
24
|
-
disabled = c.is_a?(Hash) ? c[:disabled] : false
|
25
|
-
num = (i + 1).to_s + enum if enum
|
26
|
-
if disabled
|
27
|
-
"\e[31m#{symbols[:cross]}\e[0m #{num}#{name} #{disabled}"
|
28
|
-
elsif name == active
|
29
|
-
"\e[32m#{symbols[:marker]} #{num}#{name}\e[0m"
|
30
|
-
else
|
31
|
-
" #{num}#{name}"
|
32
|
-
end
|
33
|
-
end.join("\n")
|
34
|
-
out << "\e[2K\e[1G\e[1A" * choices.count
|
35
|
-
out << "\e[2K\e[1G"
|
36
|
-
out << "\e[1A\e[2K\e[1G" if choices.empty?
|
37
|
-
out.join
|
38
|
-
end
|
39
|
-
|
40
|
-
def exit_message(prompt, choice)
|
41
|
-
"#{prompt} \e[32m#{choice}\e[0m\n\e[?25h"
|
42
|
-
end
|
43
|
-
|
44
|
-
# Ensure a wide prompt on CI
|
45
|
-
before { allow(TTY::Screen).to receive(:width).and_return(200) }
|
46
|
-
|
47
|
-
it "selects by default first option" do
|
48
|
-
choices = %i(Large Medium Small)
|
49
|
-
prompt.input << "\r"
|
50
|
-
prompt.input.rewind
|
51
|
-
|
52
|
-
expect(prompt.select('What size?', choices)).to eq(:Large)
|
53
|
-
expected_output = [
|
54
|
-
"\e[?25lWhat size? \e[90m(Use #{up_down} arrow keys, press Enter to select)\e[0m\n",
|
55
|
-
"\e[32m#{symbols[:marker]} Large\e[0m\n",
|
56
|
-
" Medium\n",
|
57
|
-
" Small",
|
58
|
-
"\e[2K\e[1G\e[1A" * 3,
|
59
|
-
"\e[2K\e[1G",
|
60
|
-
"What size? \e[32mLarge\e[0m\n\e[?25h"
|
61
|
-
].join
|
62
|
-
|
63
|
-
expect(prompt.output.string).to eq(expected_output)
|
64
|
-
end
|
65
|
-
|
66
|
-
it "allows navigation using events without errors" do
|
67
|
-
choices = %w(Large Medium Small)
|
68
|
-
prompt.input << "j" << "\r"
|
69
|
-
prompt.input.rewind
|
70
|
-
prompt.on(:keypress) do |event|
|
71
|
-
prompt.trigger(:keydown) if event.value == "j"
|
72
|
-
end
|
73
|
-
expect { prompt.select('What size?', choices) }.not_to output.to_stderr
|
74
|
-
expect(prompt.output.string).to eq([
|
75
|
-
"\e[?25lWhat size? \e[90m(Use #{up_down} arrow keys, press Enter to select)\e[0m\n",
|
76
|
-
"\e[32m#{symbols[:marker]} Large\e[0m\n",
|
77
|
-
" Medium\n",
|
78
|
-
" Small",
|
79
|
-
"\e[2K\e[1G\e[1A" * 3,
|
80
|
-
"\e[2K\e[1G",
|
81
|
-
"What size? \n",
|
82
|
-
" Large\n",
|
83
|
-
"\e[32m#{symbols[:marker]} Medium\e[0m\n",
|
84
|
-
" Small",
|
85
|
-
"\e[2K\e[1G\e[1A" * 3,
|
86
|
-
"\e[2K\e[1G",
|
87
|
-
"What size? \e[32mMedium\e[0m\n\e[?25h"
|
88
|
-
].join)
|
89
|
-
end
|
90
|
-
|
91
|
-
it "sets choice name and value" do
|
92
|
-
choices = {large: 1, medium: 2, small: 3}
|
93
|
-
prompt.input << " "
|
94
|
-
prompt.input.rewind
|
95
|
-
expect(prompt.select('What size?', choices, default: 1)).to eq(1)
|
96
|
-
expect(prompt.output.string).to eq([
|
97
|
-
"\e[?25lWhat size? \e[90m(Use #{up_down} arrow keys, press Enter to select)\e[0m\n",
|
98
|
-
"\e[32m#{symbols[:marker]} large\e[0m\n",
|
99
|
-
" medium\n",
|
100
|
-
" small",
|
101
|
-
"\e[2K\e[1G\e[1A" * 3,
|
102
|
-
"\e[2K\e[1G",
|
103
|
-
"What size? \e[32mlarge\e[0m\n\e[?25h"
|
104
|
-
].join)
|
105
|
-
end
|
106
|
-
|
107
|
-
it "sets choice name through DSL" do
|
108
|
-
prompt.input << " "
|
109
|
-
prompt.input.rewind
|
110
|
-
value = prompt.select('What size?') do |menu|
|
111
|
-
menu.symbols marker: '>'
|
112
|
-
|
113
|
-
menu.choice "Large"
|
114
|
-
menu.choice "Medium"
|
115
|
-
menu.choice "Small"
|
116
|
-
end
|
117
|
-
expect(value).to eq('Large')
|
118
|
-
expect(prompt.output.string).to eq([
|
119
|
-
"\e[?25lWhat size? \e[90m(Use #{up_down} arrow keys, press Enter to select)\e[0m\n",
|
120
|
-
"\e[32m> Large\e[0m\n",
|
121
|
-
" Medium\n",
|
122
|
-
" Small",
|
123
|
-
"\e[2K\e[1G\e[1A" * 3,
|
124
|
-
"\e[2K\e[1G",
|
125
|
-
"What size? \e[32mLarge\e[0m\n\e[?25h"
|
126
|
-
].join)
|
127
|
-
end
|
128
|
-
|
129
|
-
it "sets choice name & value through DSL" do
|
130
|
-
prompt = TTY::TestPrompt.new(symbols: {marker: '>'})
|
131
|
-
prompt.input << " "
|
132
|
-
prompt.input.rewind
|
133
|
-
value = prompt.select('What size?') do |menu|
|
134
|
-
menu.choice :large, 1
|
135
|
-
menu.choice :medium, 2
|
136
|
-
menu.choice :small, 3
|
137
|
-
end
|
138
|
-
expect(value).to eq(1)
|
139
|
-
expect(prompt.output.string).to eq([
|
140
|
-
"\e[?25lWhat size? \e[90m(Use #{up_down} arrow keys, press Enter to select)\e[0m\n",
|
141
|
-
"\e[32m> large\e[0m\n",
|
142
|
-
" medium\n",
|
143
|
-
" small",
|
144
|
-
"\e[2K\e[1G\e[1A" * 3,
|
145
|
-
"\e[2K\e[1G",
|
146
|
-
"What size? \e[32mlarge\e[0m\n\e[?25h"
|
147
|
-
].join)
|
148
|
-
end
|
149
|
-
|
150
|
-
it "sets choices and single choice through DSL" do
|
151
|
-
prompt.input << " "
|
152
|
-
prompt.input.rewind
|
153
|
-
value = prompt.select('What size?') do |menu|
|
154
|
-
menu.choice 'Large'
|
155
|
-
menu.choices %w(Medium Small)
|
156
|
-
end
|
157
|
-
expect(value).to eq('Large')
|
158
|
-
expect(prompt.output.string).to eq([
|
159
|
-
"\e[?25lWhat size? \e[90m(Use #{up_down} arrow keys, press Enter to select)\e[0m\n",
|
160
|
-
"\e[32m#{symbols[:marker]} Large\e[0m\n",
|
161
|
-
" Medium\n",
|
162
|
-
" Small",
|
163
|
-
"\e[2K\e[1G\e[1A" * 3,
|
164
|
-
"\e[2K\e[1G",
|
165
|
-
"What size? \e[32mLarge\e[0m\n\e[?25h"
|
166
|
-
].join)
|
167
|
-
end
|
168
|
-
|
169
|
-
it "sets choice name & value through DSL" do
|
170
|
-
prompt.input << " "
|
171
|
-
prompt.input.rewind
|
172
|
-
value = prompt.select('What size?') do |menu|
|
173
|
-
menu.default 2
|
174
|
-
menu.enum '.'
|
175
|
-
|
176
|
-
menu.choice :large, 1
|
177
|
-
menu.choice :medium, 2
|
178
|
-
menu.choice :small, 3
|
179
|
-
end
|
180
|
-
expect(value).to eq(2)
|
181
|
-
expect(prompt.output.string).to eq([
|
182
|
-
"\e[?25lWhat size? \e[90m(Use #{up_down} arrow or number (1-3) keys, press Enter to select)\e[0m\n",
|
183
|
-
" 1. large\n",
|
184
|
-
"\e[32m#{symbols[:marker]} 2. medium\e[0m\n",
|
185
|
-
" 3. small",
|
186
|
-
"\e[2K\e[1G\e[1A" * 3,
|
187
|
-
"\e[2K\e[1G",
|
188
|
-
"What size? \e[32mmedium\e[0m\n\e[?25h"
|
189
|
-
].join)
|
190
|
-
end
|
191
|
-
|
192
|
-
it "sets choice value to proc and executes it" do
|
193
|
-
prompt.input << " "
|
194
|
-
prompt.input.rewind
|
195
|
-
value = prompt.select('What size?', default: 2, enum: ')') do |menu|
|
196
|
-
menu.choice :large, 1
|
197
|
-
menu.choice :medium do 'Good choice!' end
|
198
|
-
menu.choice :small, 3
|
199
|
-
end
|
200
|
-
|
201
|
-
expect(value).to eq('Good choice!')
|
202
|
-
expect(prompt.output.string).to eq([
|
203
|
-
"\e[?25lWhat size? \e[90m(Use #{up_down} arrow or number (1-3) keys, press Enter to select)\e[0m\n",
|
204
|
-
" 1) large\n",
|
205
|
-
"\e[32m#{symbols[:marker]} 2) medium\e[0m\n",
|
206
|
-
" 3) small",
|
207
|
-
"\e[2K\e[1G\e[1A" * 3,
|
208
|
-
"\e[2K\e[1G",
|
209
|
-
"What size? \e[32mmedium\e[0m\n\e[?25h"
|
210
|
-
].join)
|
211
|
-
end
|
212
|
-
|
213
|
-
it "sets default option through hash syntax" do
|
214
|
-
choices = %w(Large Medium Small)
|
215
|
-
prompt.input << " "
|
216
|
-
prompt.input.rewind
|
217
|
-
expect(prompt.select('What size?', choices, default: 2, enum: '.')).to eq('Medium')
|
218
|
-
expect(prompt.output.string).to eq([
|
219
|
-
"\e[?25lWhat size? \e[90m(Use #{up_down} arrow or number (1-3) keys, press Enter to select)\e[0m\n",
|
220
|
-
" 1. Large\n",
|
221
|
-
"\e[32m#{symbols[:marker]} 2. Medium\e[0m\n",
|
222
|
-
" 3. Small",
|
223
|
-
"\e[2K\e[1G\e[1A" * 3,
|
224
|
-
"\e[2K\e[1G",
|
225
|
-
"What size? \e[32mMedium\e[0m\n\e[?25h"
|
226
|
-
].join)
|
227
|
-
end
|
228
|
-
|
229
|
-
it "changes selected item color & marker" do
|
230
|
-
choices = %w(Large Medium Small)
|
231
|
-
prompt = TTY::TestPrompt.new(symbols: {marker: '>'})
|
232
|
-
prompt.input << " "
|
233
|
-
prompt.input.rewind
|
234
|
-
options = {active_color: :blue, help_color: :red, symbols: {marker: '>' }}
|
235
|
-
|
236
|
-
value = prompt.select('What size?', choices, **options)
|
237
|
-
|
238
|
-
expect(value).to eq('Large')
|
239
|
-
expect(prompt.output.string).to eq([
|
240
|
-
"\e[?25lWhat size? \e[31m(Use #{up_down} arrow keys, press Enter to select)\e[0m\n",
|
241
|
-
"\e[34m> Large\e[0m\n",
|
242
|
-
" Medium\n",
|
243
|
-
" Small",
|
244
|
-
"\e[2K\e[1G\e[1A" * 3,
|
245
|
-
"\e[2K\e[1G",
|
246
|
-
"What size? \e[34mLarge\e[0m\n\e[?25h"
|
247
|
-
].join)
|
248
|
-
end
|
249
|
-
|
250
|
-
it "changes help text" do
|
251
|
-
choices = %w(Large Medium Small)
|
252
|
-
prompt.input << " "
|
253
|
-
prompt.input.rewind
|
254
|
-
value = prompt.select('What size?', choices, help: "(Bash keyboard)")
|
255
|
-
expect(value).to eq('Large')
|
256
|
-
expect(prompt.output.string).to eq([
|
257
|
-
"\e[?25lWhat size? \e[90m(Bash keyboard)\e[0m\n",
|
258
|
-
"\e[32m#{symbols[:marker]} Large\e[0m\n",
|
259
|
-
" Medium\n",
|
260
|
-
" Small",
|
261
|
-
"\e[2K\e[1G\e[1A" * 3,
|
262
|
-
"\e[2K\e[1G",
|
263
|
-
"What size? \e[32mLarge\e[0m\n\e[?25h"
|
264
|
-
].join)
|
265
|
-
end
|
266
|
-
|
267
|
-
it "changes help text through DSL" do
|
268
|
-
choices = %w(Large Medium Small)
|
269
|
-
prompt.input << " "
|
270
|
-
prompt.input.rewind
|
271
|
-
value = prompt.select('What size?') do |menu|
|
272
|
-
menu.help "(Bash keyboard)"
|
273
|
-
menu.choices choices
|
274
|
-
end
|
275
|
-
expect(value).to eq('Large')
|
276
|
-
expect(prompt.output.string).to eq([
|
277
|
-
"\e[?25lWhat size? \e[90m(Bash keyboard)\e[0m\n",
|
278
|
-
"\e[32m#{symbols[:marker]} Large\e[0m\n",
|
279
|
-
" Medium\n",
|
280
|
-
" Small",
|
281
|
-
"\e[2K\e[1G\e[1A" * 3,
|
282
|
-
"\e[2K\e[1G",
|
283
|
-
"What size? \e[32mLarge\e[0m\n\e[?25h"
|
284
|
-
].join)
|
285
|
-
end
|
286
|
-
|
287
|
-
it "sets prompt prefix" do
|
288
|
-
prompt = TTY::TestPrompt.new(prefix: '[?] ')
|
289
|
-
choices = %w(Large Medium Small)
|
290
|
-
prompt.input << "\r"
|
291
|
-
prompt.input.rewind
|
292
|
-
expect(prompt.select('What size?', choices)).to eq('Large')
|
293
|
-
expect(prompt.output.string).to eq([
|
294
|
-
"\e[?25l[?] What size? \e[90m(Use #{up_down} arrow keys, press Enter to select)\e[0m\n",
|
295
|
-
"\e[32m#{symbols[:marker]} Large\e[0m\n",
|
296
|
-
" Medium\n",
|
297
|
-
" Small",
|
298
|
-
"\e[2K\e[1G\e[1A" * 3,
|
299
|
-
"\e[2K\e[1G",
|
300
|
-
"[?] What size? \e[32mLarge\e[0m\n\e[?25h"
|
301
|
-
].join)
|
302
|
-
end
|
303
|
-
|
304
|
-
context "when paginated" do
|
305
|
-
it "paginates long selections" do
|
306
|
-
choices = %w(A B C D E F G H)
|
307
|
-
prompt.input << "\r"
|
308
|
-
prompt.input.rewind
|
309
|
-
|
310
|
-
answer = prompt.select("What letter?", choices, per_page: 3, default: 4)
|
311
|
-
|
312
|
-
expect(answer).to eq('D')
|
313
|
-
expected_output = [
|
314
|
-
"\e[?25lWhat letter? \e[90m(Use #{up_down} and #{left_right} arrow keys, press Enter to select)\e[0m\n",
|
315
|
-
"\e[32m#{symbols[:marker]} D\e[0m\n",
|
316
|
-
" E\n",
|
317
|
-
" F",
|
318
|
-
"\e[2K\e[1G\e[1A" * 3,
|
319
|
-
"\e[2K\e[1G",
|
320
|
-
"What letter? \e[32mD\e[0m\n\e[?25h",
|
321
|
-
].join
|
322
|
-
|
323
|
-
expect(prompt.output.string).to eq(expected_output)
|
324
|
-
end
|
325
|
-
|
326
|
-
it "paginates choices as hash object" do
|
327
|
-
prompt = TTY::TestPrompt.new
|
328
|
-
choices = {A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8}
|
329
|
-
prompt.input << "\r"
|
330
|
-
prompt.input.rewind
|
331
|
-
|
332
|
-
answer = prompt.select("What letter?", choices, per_page: 3, default: 4)
|
333
|
-
|
334
|
-
expect(answer).to eq(4)
|
335
|
-
expected_output = [
|
336
|
-
"\e[?25lWhat letter? \e[90m(Use #{up_down} and #{left_right} arrow keys, press Enter to select)\e[0m\n",
|
337
|
-
"\e[32m#{symbols[:marker]} D\e[0m\n",
|
338
|
-
" E\n",
|
339
|
-
" F",
|
340
|
-
"\e[2K\e[1G\e[1A" * 3,
|
341
|
-
"\e[2K\e[1G",
|
342
|
-
"What letter? \e[32mD\e[0m\n\e[?25h",
|
343
|
-
].join
|
344
|
-
expect(prompt.output.string).to eq(expected_output)
|
345
|
-
end
|
346
|
-
|
347
|
-
it "paginates long selections through DSL" do
|
348
|
-
prompt = TTY::TestPrompt.new
|
349
|
-
choices = %w(A B C D E F G H)
|
350
|
-
prompt.input << "\r"
|
351
|
-
prompt.input.rewind
|
352
|
-
|
353
|
-
answer = prompt.select("What letter?") do |menu|
|
354
|
-
menu.per_page 3
|
355
|
-
menu.default 4
|
356
|
-
|
357
|
-
menu.choices choices
|
358
|
-
end
|
359
|
-
|
360
|
-
expect(answer).to eq('D')
|
361
|
-
|
362
|
-
|
363
|
-
expected_output = [
|
364
|
-
"\e[?25lWhat letter? \e[90m(Use #{up_down} and #{left_right} arrow keys, press Enter to select)\e[0m\n",
|
365
|
-
"\e[32m#{symbols[:marker]} D\e[0m\n",
|
366
|
-
" E\n",
|
367
|
-
" F",
|
368
|
-
"\e[2K\e[1G\e[1A" * 3,
|
369
|
-
"\e[2K\e[1G",
|
370
|
-
"What letter? \e[32mD\e[0m\n\e[?25h",
|
371
|
-
].join
|
372
|
-
expect(prompt.output.string).to eq(expected_output)
|
373
|
-
end
|
374
|
-
|
375
|
-
it "navigates evenly paged output with right arrow until end of selection" do
|
376
|
-
prompt = TTY::TestPrompt.new
|
377
|
-
choices = ('1'..'12').to_a
|
378
|
-
prompt.on(:keypress) { |e| prompt.trigger(:keyright) if e.value == "l" }
|
379
|
-
prompt.input << "l" << "l" << "l" << "\r"
|
380
|
-
prompt.input.rewind
|
381
|
-
|
382
|
-
answer = prompt.select("What number?", choices, per_page: 4)
|
383
|
-
|
384
|
-
expect(answer).to eq('9')
|
385
|
-
|
386
|
-
expected_output = [
|
387
|
-
output_helper('What number?', choices[0..3], "1", init: true,
|
388
|
-
hint: "Use #{up_down} and #{left_right} arrow keys, press Enter to select"),
|
389
|
-
output_helper('What number?', choices[4..7], "5"),
|
390
|
-
output_helper('What number?', choices[8..11], "9"),
|
391
|
-
output_helper('What number?', choices[8..11], "9"),
|
392
|
-
"What number? \e[32m9\e[0m\n\e[?25h",
|
393
|
-
].join
|
394
|
-
|
395
|
-
expect(prompt.output.string).to eq(expected_output)
|
396
|
-
end
|
397
|
-
|
398
|
-
it "navigates unevenly paged output with right arrow until the end of selection" do
|
399
|
-
prompt = TTY::TestPrompt.new
|
400
|
-
choices = ('1'..'10').to_a
|
401
|
-
prompt.on(:keypress) { |e| prompt.trigger(:keyright) if e.value == "l" }
|
402
|
-
prompt.input << "l" << "l" << "l" << "\r"
|
403
|
-
prompt.input.rewind
|
404
|
-
|
405
|
-
answer = prompt.select("What number?", choices, default: 4, per_page: 4)
|
406
|
-
|
407
|
-
expect(answer).to eq('10')
|
408
|
-
|
409
|
-
expected_output = [
|
410
|
-
output_helper('What number?', choices[3..6], "4", init: true,
|
411
|
-
hint: "Use #{up_down} and #{left_right} arrow keys, press Enter to select"),
|
412
|
-
output_helper('What number?', choices[4..7], "8"),
|
413
|
-
output_helper('What number?', choices[8..9], "10"),
|
414
|
-
output_helper('What number?', choices[8..9], "10"),
|
415
|
-
"What number? \e[32m10\e[0m\n\e[?25h",
|
416
|
-
].join
|
417
|
-
|
418
|
-
expect(prompt.output.string).to eq(expected_output)
|
419
|
-
end
|
420
|
-
|
421
|
-
it "navigates left and right" do
|
422
|
-
prompt = TTY::TestPrompt.new
|
423
|
-
choices = ('1'..'10').to_a
|
424
|
-
prompt.on(:keypress) { |e|
|
425
|
-
prompt.trigger(:keyright) if e.value == "l"
|
426
|
-
prompt.trigger(:keyleft) if e.value == "h"
|
427
|
-
}
|
428
|
-
prompt.input << "l" << "l" << "h" << "\r"
|
429
|
-
prompt.input.rewind
|
430
|
-
|
431
|
-
answer = prompt.select("What number?", choices, default: 2, per_page: 4)
|
432
|
-
|
433
|
-
expect(answer).to eq('6')
|
434
|
-
|
435
|
-
expected_output = [
|
436
|
-
output_helper('What number?', choices[0..3], "2", init: true,
|
437
|
-
hint: "Use #{up_down} and #{left_right} arrow keys, press Enter to select"),
|
438
|
-
output_helper('What number?', choices[4..7], "6"),
|
439
|
-
output_helper('What number?', choices[8..9], "10"),
|
440
|
-
output_helper('What number?', choices[4..7], "6"),
|
441
|
-
"What number? \e[32m6\e[0m\n\e[?25h",
|
442
|
-
].join
|
443
|
-
|
444
|
-
expect(prompt.output.string).to eq(expected_output)
|
445
|
-
end
|
446
|
-
|
447
|
-
it "combines up/down navigation with left/right" do
|
448
|
-
prompt = TTY::TestPrompt.new
|
449
|
-
choices = ('1'..'11').to_a
|
450
|
-
prompt.on(:keypress) { |e|
|
451
|
-
prompt.trigger(:keyup) if e.value == "k"
|
452
|
-
prompt.trigger(:keydown) if e.value == "j"
|
453
|
-
prompt.trigger(:keyright) if e.value == "l"
|
454
|
-
prompt.trigger(:keyleft) if e.value == "h"
|
455
|
-
}
|
456
|
-
prompt.input << "j" << "l" << "k" << "k" << "h" << "\r"
|
457
|
-
prompt.input.rewind
|
458
|
-
|
459
|
-
answer = prompt.select("What number?", choices, default: 2, per_page: 4)
|
460
|
-
|
461
|
-
expect(answer).to eq('1')
|
462
|
-
|
463
|
-
expected_output = [
|
464
|
-
output_helper('What number?', choices[0..3], "2", init: true,
|
465
|
-
hint: "Use #{up_down} and #{left_right} arrow keys, press Enter to select"),
|
466
|
-
output_helper('What number?', choices[0..3], "3"),
|
467
|
-
output_helper('What number?', choices[4..7], "7"),
|
468
|
-
output_helper('What number?', choices[4..7], "6"),
|
469
|
-
output_helper('What number?', choices[3..6], "5"),
|
470
|
-
output_helper('What number?', choices[0..3], "1"),
|
471
|
-
"What number? \e[32m1\e[0m\n\e[?25h"
|
472
|
-
].join
|
473
|
-
|
474
|
-
expect(prompt.output.string).to eq(expected_output)
|
475
|
-
end
|
476
|
-
|
477
|
-
it "navigates pages up/down with disabled items" do
|
478
|
-
prompt = TTY::TestPrompt.new
|
479
|
-
prompt.on(:keypress) { |e|
|
480
|
-
prompt.trigger(:keyup) if e.value == "k"
|
481
|
-
prompt.trigger(:keydown) if e.value == "j"
|
482
|
-
}
|
483
|
-
choices = [
|
484
|
-
'1',
|
485
|
-
{name: '2', disabled: 'out'},
|
486
|
-
'3',
|
487
|
-
{name: '4', disabled: 'out'},
|
488
|
-
'5',
|
489
|
-
{name: '6', disabled: 'out'},
|
490
|
-
{name: '7', disabled: 'out'},
|
491
|
-
'8',
|
492
|
-
'9',
|
493
|
-
{name: '10', disabled: 'out'},
|
494
|
-
]
|
495
|
-
|
496
|
-
prompt.input << "j" << "j" << "j" << "j" << "\r"
|
497
|
-
prompt.input.rewind
|
498
|
-
|
499
|
-
answer = prompt.select("What number?", choices, per_page: 4)
|
500
|
-
|
501
|
-
expect(answer).to eq('9')
|
502
|
-
|
503
|
-
expected_output = [
|
504
|
-
output_helper('What number?', choices[0..3], "1", init: true,
|
505
|
-
hint: "Use #{up_down} and #{left_right} arrow keys, press Enter to select"),
|
506
|
-
output_helper('What number?', choices[0..3], "3"),
|
507
|
-
output_helper('What number?', choices[2..5], "5"),
|
508
|
-
output_helper('What number?', choices[5..8], "8"),
|
509
|
-
output_helper('What number?', choices[6..9], "9"),
|
510
|
-
"What number? \e[32m9\e[0m\n\e[?25h"
|
511
|
-
].join('')
|
512
|
-
|
513
|
-
expect(prompt.output.string).to eq(expected_output)
|
514
|
-
end
|
515
|
-
|
516
|
-
it "navigates pages left/right with disabled items" do
|
517
|
-
prompt = TTY::TestPrompt.new
|
518
|
-
prompt.on(:keypress) { |e|
|
519
|
-
prompt.trigger(:keyright) if e.value == "l"
|
520
|
-
prompt.trigger(:keyleft) if e.value == "h"
|
521
|
-
}
|
522
|
-
choices = [
|
523
|
-
{name: '1', disabled: 'out'},
|
524
|
-
'2',
|
525
|
-
{name: '3', disabled: 'out'},
|
526
|
-
'4',
|
527
|
-
'5',
|
528
|
-
{name: '6', disabled: 'out'},
|
529
|
-
'7',
|
530
|
-
'8',
|
531
|
-
'9',
|
532
|
-
{name: '10', disabled: 'out'}
|
533
|
-
]
|
534
|
-
|
535
|
-
prompt.input << "l" << "l" << "l" << "h" << "h" << "h" << "\r"
|
536
|
-
prompt.input.rewind
|
537
|
-
|
538
|
-
answer = prompt.select("What number?", choices, per_page: 4)
|
539
|
-
|
540
|
-
expect(answer).to eq('2')
|
541
|
-
|
542
|
-
expected_output = [
|
543
|
-
output_helper('What number?', choices[0..3], "2", init: true,
|
544
|
-
hint: "Use #{up_down} and #{left_right} arrow keys, press Enter to select"),
|
545
|
-
output_helper('What number?', choices[4..7], "7"),
|
546
|
-
output_helper('What number?', choices[8..9], "9"),
|
547
|
-
output_helper('What number?', choices[8..9], "9"),
|
548
|
-
output_helper('What number?', choices[4..7], "5"),
|
549
|
-
output_helper('What number?', choices[0..3], "2"),
|
550
|
-
output_helper('What number?', choices[0..3], "2"),
|
551
|
-
"What number? \e[32m2\e[0m\n\e[?25h"
|
552
|
-
].join('')
|
553
|
-
|
554
|
-
expect(prompt.output.string).to eq(expected_output)
|
555
|
-
end
|
556
|
-
end
|
557
|
-
|
558
|
-
context 'with :cycle option' do
|
559
|
-
it "doesn't cycle by default" do
|
560
|
-
prompt = TTY::TestPrompt.new
|
561
|
-
choices = %w(A B C)
|
562
|
-
prompt.on(:keypress) { |e| prompt.trigger(:keydown) if e.value == "j" }
|
563
|
-
prompt.input << "j" << "j" << "j" << "\r"
|
564
|
-
prompt.input.rewind
|
565
|
-
|
566
|
-
value = prompt.select("What letter?", choices)
|
567
|
-
|
568
|
-
expect(value).to eq("C")
|
569
|
-
expected_output = [
|
570
|
-
output_helper("What letter?", choices, "A", init: true,
|
571
|
-
hint: "Use #{up_down} arrow keys, press Enter to select"),
|
572
|
-
output_helper("What letter?", choices, "B"),
|
573
|
-
output_helper("What letter?", choices, "C"),
|
574
|
-
output_helper("What letter?", choices, "C"),
|
575
|
-
"What letter? \e[32mC\e[0m\n\e[?25h"
|
576
|
-
].join
|
577
|
-
expect(prompt.output.string).to eq(expected_output)
|
578
|
-
end
|
579
|
-
|
580
|
-
it "cycles around when configured to do so" do
|
581
|
-
prompt = TTY::TestPrompt.new
|
582
|
-
choices = %w(A B C)
|
583
|
-
prompt.on(:keypress) { |e| prompt.trigger(:keydown) if e.value == "j" }
|
584
|
-
prompt.input << "j" << "j" << "j" << "\r"
|
585
|
-
prompt.input.rewind
|
586
|
-
|
587
|
-
answer = prompt.select("What letter?", choices, cycle: true)
|
588
|
-
|
589
|
-
expect(answer).to eq("A")
|
590
|
-
expected_output = [
|
591
|
-
output_helper("What letter?", choices, "A", init: true,
|
592
|
-
hint: "Use #{up_down} arrow keys, press Enter to select"),
|
593
|
-
output_helper("What letter?", choices, "B"),
|
594
|
-
output_helper("What letter?", choices, "C"),
|
595
|
-
output_helper("What letter?", choices, "A"),
|
596
|
-
"What letter? \e[32mA\e[0m\n\e[?25h"
|
597
|
-
].join
|
598
|
-
expect(prompt.output.string).to eq(expected_output)
|
599
|
-
end
|
600
|
-
|
601
|
-
it "cycles around disabled items" do
|
602
|
-
prompt = TTY::TestPrompt.new
|
603
|
-
choices = [
|
604
|
-
{name: 'A', disabled: '(out)'},
|
605
|
-
{name: 'B'},
|
606
|
-
{name: 'C', disabled: '(out)'},
|
607
|
-
{name: 'D'},
|
608
|
-
{name: 'E', disabled: '(out)'},
|
609
|
-
]
|
610
|
-
prompt.on(:keypress) { |e| prompt.trigger(:keydown) if e.value == "j" }
|
611
|
-
prompt.input << "j" << "j" << "j" << "\r"
|
612
|
-
prompt.input.rewind
|
613
|
-
value = prompt.select("What letter?", choices, cycle: true, default: 2)
|
614
|
-
expect(value).to eq("D")
|
615
|
-
|
616
|
-
expected_output =
|
617
|
-
output_helper("What letter?", choices, "B", init: true,
|
618
|
-
hint: "Use #{up_down} arrow keys, press Enter to select") +
|
619
|
-
output_helper("What letter?", choices, "D") +
|
620
|
-
output_helper("What letter?", choices, "B") +
|
621
|
-
output_helper("What letter?", choices, "D") +
|
622
|
-
"What letter? \e[32mD\e[0m\n\e[?25h"
|
623
|
-
|
624
|
-
expect(prompt.output.string).to eq(expected_output)
|
625
|
-
end
|
626
|
-
|
627
|
-
it "cycles choices using left/right arrows" do
|
628
|
-
prompt = TTY::TestPrompt.new
|
629
|
-
choices = ('1'..'10').to_a
|
630
|
-
prompt.on(:keypress) { |e|
|
631
|
-
prompt.trigger(:keyright) if e.value == "l"
|
632
|
-
prompt.trigger(:keyleft) if e.value == "h"
|
633
|
-
}
|
634
|
-
prompt.input << "l" << "l" << "l" << "h" << "\r"
|
635
|
-
prompt.input.rewind
|
636
|
-
|
637
|
-
answer = prompt.select("What number?", choices, default: 2, per_page: 4, cycle: true)
|
638
|
-
|
639
|
-
expect(answer).to eq('10')
|
640
|
-
|
641
|
-
expected_output = [
|
642
|
-
output_helper('What number?', choices[0..3], "2", init: true,
|
643
|
-
hint: "Use #{up_down} and #{left_right} arrow keys, press Enter to select"),
|
644
|
-
output_helper('What number?', choices[4..7], "6"),
|
645
|
-
output_helper('What number?', choices[8..9], "10"),
|
646
|
-
output_helper('What number?', choices[0..3], "2"),
|
647
|
-
output_helper('What number?', choices[8..9], "10"),
|
648
|
-
"What number? \e[32m10\e[0m\n\e[?25h",
|
649
|
-
].join
|
650
|
-
|
651
|
-
expect(prompt.output.string).to eq(expected_output)
|
652
|
-
end
|
653
|
-
|
654
|
-
it "cycles pages left/right with disabled items" do
|
655
|
-
prompt = TTY::TestPrompt.new
|
656
|
-
prompt.on(:keypress) { |e|
|
657
|
-
prompt.trigger(:keyright) if e.value == "l"
|
658
|
-
prompt.trigger(:keyleft) if e.value == "h"
|
659
|
-
}
|
660
|
-
choices = [
|
661
|
-
{name: '1', disabled: 'out'},
|
662
|
-
'2',
|
663
|
-
{name: '3', disabled: 'out'},
|
664
|
-
'4',
|
665
|
-
'5',
|
666
|
-
{name: '6', disabled: 'out'},
|
667
|
-
'7',
|
668
|
-
'8',
|
669
|
-
'9',
|
670
|
-
{name: '10', disabled: 'out'}
|
671
|
-
]
|
672
|
-
|
673
|
-
prompt.input << "l" << "l" << "l" << "h" << "h" << "h" << "\r"
|
674
|
-
prompt.input.rewind
|
675
|
-
|
676
|
-
answer = prompt.select("What number?", choices, per_page: 4, cycle: true)
|
677
|
-
|
678
|
-
expect(answer).to eq('2')
|
679
|
-
|
680
|
-
expected_output = [
|
681
|
-
output_helper('What number?', choices[0..3], "2", init: true,
|
682
|
-
hint: "Use #{up_down} and #{left_right} arrow keys, press Enter to select"),
|
683
|
-
output_helper('What number?', choices[4..7], "7"),
|
684
|
-
output_helper('What number?', choices[8..9], "9"),
|
685
|
-
output_helper('What number?', choices[0..3], "2"),
|
686
|
-
output_helper('What number?', choices[8..9], "9"),
|
687
|
-
output_helper('What number?', choices[4..7], "5"),
|
688
|
-
output_helper('What number?', choices[0..3], "2"),
|
689
|
-
"What number? \e[32m2\e[0m\n\e[?25h"
|
690
|
-
].join('')
|
691
|
-
|
692
|
-
expect(prompt.output.string).to eq(expected_output)
|
693
|
-
end
|
694
|
-
end
|
695
|
-
|
696
|
-
it "verifies default index format" do
|
697
|
-
prompt = TTY::TestPrompt.new
|
698
|
-
choices = %w(Large Medium Small)
|
699
|
-
prompt.input << "\r"
|
700
|
-
prompt.input.rewind
|
701
|
-
|
702
|
-
expect {
|
703
|
-
prompt.select('What size?', choices, default: '')
|
704
|
-
}.to raise_error(TTY::Prompt::ConfigurationError, /in range \(1 - 3\)/)
|
705
|
-
end
|
706
|
-
|
707
|
-
it "doesn't paginate short selections" do
|
708
|
-
prompt = TTY::TestPrompt.new
|
709
|
-
choices = %w(A B C D)
|
710
|
-
prompt.input << "\r"
|
711
|
-
prompt.input.rewind
|
712
|
-
value = prompt.select("What letter?", choices, per_page: 4, default: 1)
|
713
|
-
expect(value).to eq('A')
|
714
|
-
|
715
|
-
expect(prompt.output.string).to eq([
|
716
|
-
"\e[?25lWhat letter? \e[90m(Use #{up_down} arrow keys, press Enter to select)\e[0m\n",
|
717
|
-
"\e[32m#{symbols[:marker]} A\e[0m\n",
|
718
|
-
" B\n",
|
719
|
-
" C\n",
|
720
|
-
" D",
|
721
|
-
"\e[2K\e[1G\e[1A" * 4,
|
722
|
-
"\e[2K\e[1G",
|
723
|
-
"What letter? \e[32mA\e[0m\n\e[?25h",
|
724
|
-
].join)
|
725
|
-
end
|
726
|
-
|
727
|
-
it "verifies default index range" do
|
728
|
-
prompt = TTY::TestPrompt.new
|
729
|
-
choices = %w(Large Medium Small)
|
730
|
-
prompt.input << "\r"
|
731
|
-
prompt.input.rewind
|
732
|
-
|
733
|
-
expect {
|
734
|
-
prompt.select("What size?", choices, default: 10)
|
735
|
-
}.to raise_error(TTY::Prompt::ConfigurationError, /`10` out of range \(1 - 3\)/)
|
736
|
-
end
|
737
|
-
|
738
|
-
context "with filter" do
|
739
|
-
it "doesn't allow mixing enumeration and filter" do
|
740
|
-
prompt = TTY::TestPrompt.new
|
741
|
-
|
742
|
-
expect {
|
743
|
-
prompt.select("What size?", [], enum: '.', filter: true)
|
744
|
-
}.to raise_error(TTY::Prompt::ConfigurationError, "Enumeration can't be used with filter")
|
745
|
-
end
|
746
|
-
|
747
|
-
it "filters and chooses a uniquely matching entry, ignoring case" do
|
748
|
-
prompt = TTY::TestPrompt.new
|
749
|
-
prompt.input << "U" << "g" << "\r"
|
750
|
-
prompt.input.rewind
|
751
|
-
|
752
|
-
answer = prompt.select("What size?", %w(Small Medium Large Huge), filter: true)
|
753
|
-
expect(answer).to eql("Huge")
|
754
|
-
|
755
|
-
actual_prompt_output = prompt.output.string
|
756
|
-
|
757
|
-
expected_prompt_output =
|
758
|
-
output_helper("What size?", %w(Small Medium Large Huge), "Small", init: true,
|
759
|
-
hint: "Use #{up_down} arrow keys, press Enter to select, and letter keys to filter") +
|
760
|
-
output_helper("What size?", %w(Medium Huge), "Medium", hint: 'Filter: "U"') +
|
761
|
-
output_helper("What size?", %w(Huge), "Huge", hint: 'Filter: "Ug"') +
|
762
|
-
exit_message("What size?", "Huge")
|
763
|
-
|
764
|
-
expect(actual_prompt_output).to eql(expected_prompt_output)
|
765
|
-
end
|
766
|
-
|
767
|
-
it "filters and chooses the first of multiple matching entries" do
|
768
|
-
prompt = TTY::TestPrompt.new
|
769
|
-
prompt.input << "g" << "\r"
|
770
|
-
prompt.input.rewind
|
771
|
-
|
772
|
-
answer = prompt.select("What size?", %w(Small Medium Large Huge), filter: true)
|
773
|
-
expect(answer).to eql("Large")
|
774
|
-
|
775
|
-
actual_prompt_output = prompt.output.string
|
776
|
-
expected_prompt_output =
|
777
|
-
output_helper("What size?", %w(Small Medium Large Huge), "Small", init: true,
|
778
|
-
hint: "Use #{up_down} arrow keys, press Enter to select, and letter keys to filter") +
|
779
|
-
output_helper("What size?", %w(Large Huge), "Large", hint: 'Filter: "g"') +
|
780
|
-
exit_message("What size?", "Large")
|
781
|
-
|
782
|
-
expect(actual_prompt_output).to eql(expected_prompt_output)
|
783
|
-
end
|
784
|
-
|
785
|
-
it "filters based on alphanumeric and punctuation characters" do
|
786
|
-
prompt = TTY::TestPrompt.new
|
787
|
-
prompt.input << "p" << "*" << "2" << "\r"
|
788
|
-
prompt.input.rewind
|
789
|
-
|
790
|
-
answer = prompt.select("What email?", %w(p*1@mail.com p*2@mail.com p*3@mail.com), filter: true)
|
791
|
-
expect(answer).to eql("p*2@mail.com")
|
792
|
-
|
793
|
-
actual_prompt_output = prompt.output.string
|
794
|
-
expected_prompt_output =
|
795
|
-
output_helper("What email?", %w(p*1@mail.com p*2@mail.com p*3@mail.com), "p*1@mail.com", init: true,
|
796
|
-
hint: "Use #{up_down} arrow keys, press Enter to select, and letter keys to filter") +
|
797
|
-
output_helper("What email?", %w(p*1@mail.com p*2@mail.com p*3@mail.com), "p*1@mail.com", hint: 'Filter: "p"') +
|
798
|
-
output_helper("What email?", %w(p*1@mail.com p*2@mail.com p*3@mail.com), "p*1@mail.com", hint: 'Filter: "p*"') +
|
799
|
-
output_helper("What email?", %w(p*2@mail.com), "p*2@mail.com", hint: 'Filter: "p*2"') +
|
800
|
-
exit_message("What email?", "p*2@mail.com")
|
801
|
-
|
802
|
-
expect(actual_prompt_output).to eql(expected_prompt_output)
|
803
|
-
end
|
804
|
-
|
805
|
-
# This test can't be done in an exact way, at least, with the current framework
|
806
|
-
it "doesn't exit when there are no matching entries" do
|
807
|
-
prompt = TTY::TestPrompt.new
|
808
|
-
prompt.on(:keypress) { |e| prompt.trigger(:keybackspace) if e.value == "a" }
|
809
|
-
prompt.input << "z" << "\r" # shows no entry, blocking exit
|
810
|
-
prompt.input << "a" << "\r" # triggers Backspace before `a` (see above)
|
811
|
-
prompt.input.rewind
|
812
|
-
|
813
|
-
answer = prompt.select("What size?", %w(Tiny Medium Large Huge), filter: true)
|
814
|
-
expect(answer).to eql("Large")
|
815
|
-
|
816
|
-
actual_prompt_output = prompt.output.string
|
817
|
-
expected_prompt_output =
|
818
|
-
output_helper("What size?", %w(Tiny Medium Large Huge), "Tiny", init: true,
|
819
|
-
hint: "Use #{up_down} arrow keys, press Enter to select, and letter keys to filter") +
|
820
|
-
output_helper("What size?", %w(), "", hint: 'Filter: "z"') +
|
821
|
-
output_helper("What size?", %w(), "", hint: 'Filter: "z"') +
|
822
|
-
output_helper("What size?", %w(Large), "Large", hint: 'Filter: "a"') +
|
823
|
-
exit_message("What size?", "Large")
|
824
|
-
|
825
|
-
expect(actual_prompt_output).to eql(expected_prompt_output)
|
826
|
-
end
|
827
|
-
|
828
|
-
it "cancels a selection" do
|
829
|
-
prompt = TTY::TestPrompt.new
|
830
|
-
prompt.on(:keypress) { |e| prompt.trigger(:keydelete) if e.value == "S" }
|
831
|
-
prompt.input << "Hu"
|
832
|
-
prompt.input << "S" # triggers Canc before `S` (see above)
|
833
|
-
prompt.input << "\r"
|
834
|
-
prompt.input.rewind
|
835
|
-
|
836
|
-
answer = prompt.select("What size?", %w(Small Medium Large Huge), filter: true)
|
837
|
-
expect(answer).to eql("Small")
|
838
|
-
|
839
|
-
expected_prompt_output =
|
840
|
-
output_helper("What size?", %w(Small Medium Large Huge), "Small", init: true,
|
841
|
-
hint: "Use #{up_down} arrow keys, press Enter to select, and letter keys to filter") +
|
842
|
-
output_helper("What size?", %w(Huge), "Huge", hint: 'Filter: "H"') +
|
843
|
-
output_helper("What size?", %w(Huge), "Huge", hint: 'Filter: "Hu"') +
|
844
|
-
output_helper("What size?", %w(Small), "Small", hint: 'Filter: "S"') +
|
845
|
-
exit_message("What size?", "Small")
|
846
|
-
|
847
|
-
expect(prompt.output.string).to eql(expected_prompt_output)
|
848
|
-
end
|
849
|
-
end
|
850
|
-
|
851
|
-
context 'with :disabled choice' do
|
852
|
-
it "omits disabled choice when navigating menu" do
|
853
|
-
choices = [ 'Small', 'Medium', {name: 'Large', disabled: '(out of stock)'}, 'Huge' ]
|
854
|
-
prompt = TTY::TestPrompt.new
|
855
|
-
prompt.input << "j" << "j" << "\r"
|
856
|
-
prompt.input.rewind
|
857
|
-
prompt.on(:keypress) { |e| prompt.trigger(:keydown) if e.value == "j" }
|
858
|
-
|
859
|
-
answer = prompt.select("What size?", choices)
|
860
|
-
expect(answer).to eq('Huge')
|
861
|
-
|
862
|
-
expected_output =
|
863
|
-
output_helper("What size?", choices, "Small", init: true,
|
864
|
-
hint: "Use #{up_down} arrow keys, press Enter to select") +
|
865
|
-
output_helper("What size?", choices, "Medium") +
|
866
|
-
output_helper("What size?", choices, "Huge") +
|
867
|
-
"What size? \e[32mHuge\e[0m\n\e[?25h"
|
868
|
-
|
869
|
-
expect(prompt.output.string).to eq(expected_output)
|
870
|
-
end
|
871
|
-
|
872
|
-
it "doesn't show disabled choice when filtering choices" do
|
873
|
-
choices = [ 'A', 'B', {name: 'C', disabled: '(unavailable)'}, 'D' ]
|
874
|
-
prompt = TTY::TestPrompt.new
|
875
|
-
prompt.on(:keypress) { |e| prompt.trigger(:keybackspace) if e.value == "a" }
|
876
|
-
prompt.input << "c" << "\r" # nothing matches
|
877
|
-
prompt.input << "a" << "\r" # backtracks & chooses default option
|
878
|
-
prompt.input.rewind
|
879
|
-
|
880
|
-
answer = prompt.select("What letter?", choices, filter: true)
|
881
|
-
expect(answer).to eq('A')
|
882
|
-
|
883
|
-
expected_output =
|
884
|
-
output_helper("What letter?", choices, "A", init: true,
|
885
|
-
hint: "Use #{up_down} arrow keys, press Enter to select, and letter keys to filter") +
|
886
|
-
output_helper("What letter?", [], "", hint: 'Filter: "c"') +
|
887
|
-
output_helper("What letter?", [], "", hint: 'Filter: "c"') +
|
888
|
-
output_helper("What letter?", ['A'], "A", hint: 'Filter: "a"') +
|
889
|
-
exit_message("What letter?", "A")
|
890
|
-
|
891
|
-
expect(prompt.output.string).to eq(expected_output)
|
892
|
-
end
|
893
|
-
|
894
|
-
it "omits disabled choice when number key is pressed" do
|
895
|
-
choices = [ 'Small', {name: 'Medium', disabled: '(out of stock)'}, 'Large' ]
|
896
|
-
prompt = TTY::TestPrompt.new
|
897
|
-
prompt.input << "2" << "\r" << "\r"
|
898
|
-
prompt.input.rewind
|
899
|
-
answer = prompt.select('What size?') do |menu|
|
900
|
-
menu.enum ')'
|
901
|
-
|
902
|
-
menu.choice 'Small', 1
|
903
|
-
menu.choice 'Medium', 2, disabled: '(out of stock)'
|
904
|
-
menu.choice 'Large', 3
|
905
|
-
end
|
906
|
-
expect(answer).to eq(1)
|
907
|
-
|
908
|
-
expected_output =
|
909
|
-
output_helper("What size?", choices, "Small", init: true, enum: ') ',
|
910
|
-
hint: "Use #{up_down} arrow or number (1-3) keys, press Enter to select") +
|
911
|
-
output_helper("What size?", choices, "Small", enum: ') ') +
|
912
|
-
"What size? \e[32mSmall\e[0m\n\e[?25h"
|
913
|
-
|
914
|
-
expect(prompt.output.string).to eq(expected_output)
|
915
|
-
end
|
916
|
-
|
917
|
-
it "sets active to be first non-disabled choice" do
|
918
|
-
choices = [
|
919
|
-
{name: 'Small', disabled: '(out of stock)'}, 'Medium', 'Large', 'Huge'
|
920
|
-
]
|
921
|
-
prompt = TTY::TestPrompt.new
|
922
|
-
prompt.input << "\r"
|
923
|
-
prompt.input.rewind
|
924
|
-
|
925
|
-
answer = prompt.select("What size?", choices)
|
926
|
-
expect(answer).to eq('Medium')
|
927
|
-
end
|
928
|
-
|
929
|
-
it "prevents setting default to disabled choice" do
|
930
|
-
choices = [
|
931
|
-
{name: 'Small', disabled: '(out of stock)'}, 'Medium', 'Large', 'Huge'
|
932
|
-
]
|
933
|
-
prompt = TTY::TestPrompt.new
|
934
|
-
prompt.input << "\r"
|
935
|
-
prompt.input.rewind
|
936
|
-
|
937
|
-
expect {
|
938
|
-
prompt.select("What size?", choices, default: 1)
|
939
|
-
}.to raise_error(TTY::Prompt::ConfigurationError, /default index `1` matches disabled choice item/)
|
940
|
-
end
|
941
|
-
end
|
942
|
-
end
|