tty-prompt 0.8.0 → 0.9.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 +12 -0
- data/README.md +66 -6
- data/appveyor.yml +23 -0
- data/examples/enum_paged.rb +9 -0
- data/examples/select_paginated.rb +9 -0
- data/lib/tty-prompt.rb +2 -0
- data/lib/tty/prompt/enum_list.rb +127 -28
- data/lib/tty/prompt/enum_paginator.rb +54 -0
- data/lib/tty/prompt/list.rb +55 -9
- data/lib/tty/prompt/multi_list.rb +3 -3
- data/lib/tty/prompt/paginator.rb +88 -0
- data/lib/tty/prompt/reader.rb +15 -35
- data/lib/tty/prompt/version.rb +1 -1
- data/spec/unit/enum_paginator_spec.rb +49 -0
- data/spec/unit/enum_select_spec.rb +161 -7
- data/spec/unit/multi_select_spec.rb +41 -0
- data/spec/unit/paginator_spec.rb +47 -0
- data/spec/unit/reader/read_keypress_spec.rb +10 -0
- data/spec/unit/reader/read_line_spec.rb +9 -2
- data/spec/unit/reader/read_multiline_spec.rb +8 -0
- data/spec/unit/select_spec.rb +43 -2
- metadata +11 -2
@@ -211,4 +211,45 @@ RSpec.describe TTY::Prompt do
|
|
211
211
|
"Select drinks? \n\e[?25h"
|
212
212
|
].join)
|
213
213
|
end
|
214
|
+
|
215
|
+
it "paginates long selections" do
|
216
|
+
prompt = TTY::TestPrompt.new
|
217
|
+
choices = %w(A B C D E F G H)
|
218
|
+
prompt.input << "\r"
|
219
|
+
prompt.input.rewind
|
220
|
+
value = prompt.multi_select("What letter?", choices, per_page: 3, default: 4)
|
221
|
+
expect(value).to eq(['D'])
|
222
|
+
expect(prompt.output.string).to eq([
|
223
|
+
"\e[?25lWhat letter? D \e[90m(Use arrow keys, press Space to select and Enter to finish)\e[0m\n",
|
224
|
+
"‣ \e[32m⬢\e[0m D\n",
|
225
|
+
" ⬡ E\n",
|
226
|
+
" ⬡ F\n",
|
227
|
+
"\e[90m(Move up or down to reveal more choices)\e[0m",
|
228
|
+
"\e[1000D\e[K\e[1A" * 4 + "\e[1000D\e[K",
|
229
|
+
"What letter? \e[32mD\e[0m\n\e[?25h",
|
230
|
+
].join)
|
231
|
+
end
|
232
|
+
|
233
|
+
it "paginates long selections through DSL" do
|
234
|
+
prompt = TTY::TestPrompt.new
|
235
|
+
choices = %w(A B C D E F G H)
|
236
|
+
prompt.input << "\r"
|
237
|
+
prompt.input.rewind
|
238
|
+
value = prompt.multi_select("What letter?") do |menu|
|
239
|
+
menu.per_page 3
|
240
|
+
menu.page_help '(Wiggle thy finger up or down to see more)'
|
241
|
+
menu.default 4
|
242
|
+
menu.choices choices
|
243
|
+
end
|
244
|
+
expect(value).to eq(['D'])
|
245
|
+
expect(prompt.output.string).to eq([
|
246
|
+
"\e[?25lWhat letter? D \e[90m(Use arrow keys, press Space to select and Enter to finish)\e[0m\n",
|
247
|
+
"‣ \e[32m⬢\e[0m D\n",
|
248
|
+
" ⬡ E\n",
|
249
|
+
" ⬡ F\n",
|
250
|
+
"\e[90m(Wiggle thy finger up or down to see more)\e[0m",
|
251
|
+
"\e[1000D\e[K\e[1A" * 4 + "\e[1000D\e[K",
|
252
|
+
"What letter? \e[32mD\e[0m\n\e[?25h",
|
253
|
+
].join)
|
254
|
+
end
|
214
255
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
RSpec.describe TTY::Prompt::Paginator, '#paginate' do
|
4
|
+
it "paginates items matching per_page count" do
|
5
|
+
list = %w(a b c d e f)
|
6
|
+
paginator = described_class.new({per_page: 3})
|
7
|
+
|
8
|
+
expect(paginator.paginate(list, 1).to_a).to eq([['a',0], ['b',1], ['c',2]])
|
9
|
+
expect(paginator.paginate(list, 2).to_a).to eq([['a',0], ['b',1], ['c',2]])
|
10
|
+
expect(paginator.paginate(list, 3).to_a).to eq([['a',0], ['b',1], ['c',2]])
|
11
|
+
expect(paginator.paginate(list, 4).to_a).to eq([['b',1], ['c',2], ['d',3]])
|
12
|
+
expect(paginator.paginate(list, 5).to_a).to eq([['c',2], ['d',3], ['e',4]])
|
13
|
+
expect(paginator.paginate(list, 6).to_a).to eq([['d',3], ['e',4], ['f',5]])
|
14
|
+
expect(paginator.paginate(list, 7).to_a).to eq([['d',3], ['e',4], ['f',5]])
|
15
|
+
end
|
16
|
+
|
17
|
+
it "paginates items not matching per_page count" do
|
18
|
+
list = %w(a b c d e f g)
|
19
|
+
paginator = described_class.new({per_page: 3})
|
20
|
+
|
21
|
+
expect(paginator.paginate(list, 1).to_a).to eq([['a',0], ['b',1], ['c',2]])
|
22
|
+
expect(paginator.paginate(list, 2).to_a).to eq([['a',0], ['b',1], ['c',2]])
|
23
|
+
expect(paginator.paginate(list, 3).to_a).to eq([['a',0], ['b',1], ['c',2]])
|
24
|
+
expect(paginator.paginate(list, 4).to_a).to eq([['b',1], ['c',2], ['d',3]])
|
25
|
+
expect(paginator.paginate(list, 5).to_a).to eq([['c',2], ['d',3], ['e',4]])
|
26
|
+
expect(paginator.paginate(list, 6).to_a).to eq([['d',3], ['e',4], ['f',5]])
|
27
|
+
expect(paginator.paginate(list, 7).to_a).to eq([['e',4], ['f',5], ['g',6]])
|
28
|
+
expect(paginator.paginate(list, 8).to_a).to eq([['e',4], ['f',5], ['g',6]])
|
29
|
+
end
|
30
|
+
|
31
|
+
it "finds maximum index for current selection" do
|
32
|
+
list = %w(a b c d e f g)
|
33
|
+
paginator = described_class.new({per_page: 3, default: 0})
|
34
|
+
|
35
|
+
paginator.paginate(list, 4)
|
36
|
+
expect(paginator.max_index).to eq(3)
|
37
|
+
paginator.paginate(list, 5)
|
38
|
+
expect(paginator.max_index).to eq(4)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "starts with default selection" do
|
42
|
+
list = %w(a b c d e f g)
|
43
|
+
paginator = described_class.new({per_page: 3, default: 3})
|
44
|
+
|
45
|
+
expect(paginator.paginate(list, 4).to_a).to eq([['d',3], ['e',4], ['f',5]])
|
46
|
+
end
|
47
|
+
end
|
@@ -14,6 +14,16 @@ RSpec.describe TTY::Prompt::Reader, '#read_keypress' do
|
|
14
14
|
expect(answer).to eq("\e[A")
|
15
15
|
end
|
16
16
|
|
17
|
+
it 'reads multibyte key press' do
|
18
|
+
reader = described_class.new(input, out)
|
19
|
+
input << "ㄱ"
|
20
|
+
input.rewind
|
21
|
+
|
22
|
+
answer = reader.read_keypress
|
23
|
+
|
24
|
+
expect(answer).to eq("ㄱ")
|
25
|
+
end
|
26
|
+
|
17
27
|
context 'when Ctrl+C pressed' do
|
18
28
|
it "defaults to raising InputInterrupt" do
|
19
29
|
reader = described_class.new(input, out)
|
@@ -8,14 +8,14 @@ RSpec.describe TTY::Prompt::Reader, '#read_line' do
|
|
8
8
|
|
9
9
|
it 'masks characters' do
|
10
10
|
mask = '*'
|
11
|
-
input << "password
|
11
|
+
input << "password"
|
12
12
|
input.rewind
|
13
13
|
answer = reader.read_line(mask)
|
14
14
|
expect(answer).to eq("password")
|
15
15
|
end
|
16
16
|
|
17
17
|
it "echoes characters back" do
|
18
|
-
input << "password
|
18
|
+
input << "password"
|
19
19
|
input.rewind
|
20
20
|
answer = reader.read_line
|
21
21
|
expect(answer).to eq("password")
|
@@ -28,4 +28,11 @@ RSpec.describe TTY::Prompt::Reader, '#read_line' do
|
|
28
28
|
answer = reader.read_line
|
29
29
|
expect(answer).to eq('acc')
|
30
30
|
end
|
31
|
+
|
32
|
+
it 'reads multibyte line' do
|
33
|
+
input << "한글"
|
34
|
+
input.rewind
|
35
|
+
answer = reader.read_line
|
36
|
+
expect(answer).to eq("한글")
|
37
|
+
end
|
31
38
|
end
|
@@ -34,4 +34,12 @@ RSpec.describe TTY::Prompt::Reader, '#read_multiline' do
|
|
34
34
|
reader.read_multiline { |line| lines << line }
|
35
35
|
expect(lines).to eq(['First line', 'Second line', 'Third line'])
|
36
36
|
end
|
37
|
+
|
38
|
+
it 'reads multibyte lines' do
|
39
|
+
input << "국경의 긴 터널을 빠져나오자\n설국이었다."
|
40
|
+
input.rewind
|
41
|
+
lines = []
|
42
|
+
reader.read_multiline { |line| lines << line }
|
43
|
+
expect(lines).to eq(["국경의 긴 터널을 빠져나오자", '설국이었다.'])
|
44
|
+
end
|
37
45
|
end
|
data/spec/unit/select_spec.rb
CHANGED
@@ -174,7 +174,7 @@ RSpec.describe TTY::Prompt, '#select' do
|
|
174
174
|
"\e[32m‣ Large\e[0m\n",
|
175
175
|
" Medium\n",
|
176
176
|
" Small",
|
177
|
-
"\e[1000D\e[K\e[1A\e[1000D\e[K
|
177
|
+
"\e[1000D\e[K\e[1A" * 3 + "\e[1000D\e[K",
|
178
178
|
"What size? \e[32mLarge\e[0m\n\e[?25h"
|
179
179
|
].join)
|
180
180
|
end
|
@@ -190,11 +190,52 @@ RSpec.describe TTY::Prompt, '#select' do
|
|
190
190
|
"\e[32m‣ Large\e[0m\n",
|
191
191
|
" Medium\n",
|
192
192
|
" Small",
|
193
|
-
"\e[1000D\e[K\e[1A\e[1000D\e[K
|
193
|
+
"\e[1000D\e[K\e[1A" * 3 + "\e[1000D\e[K",
|
194
194
|
"[?] What size? \e[32mLarge\e[0m\n\e[?25h"
|
195
195
|
].join)
|
196
196
|
end
|
197
197
|
|
198
|
+
it "paginates long selections" do
|
199
|
+
choices = %w(A B C D E F G H)
|
200
|
+
prompt.input << "\r"
|
201
|
+
prompt.input.rewind
|
202
|
+
value = prompt.select("What letter?", choices, per_page: 3, default: 4)
|
203
|
+
expect(value).to eq('D')
|
204
|
+
expect(prompt.output.string).to eq([
|
205
|
+
"\e[?25lWhat letter? \e[90m(Use arrow keys, press Enter to select)\e[0m\n",
|
206
|
+
"\e[32m‣ D\e[0m\n",
|
207
|
+
" E\n",
|
208
|
+
" F\n",
|
209
|
+
"\e[90m(Move up or down to reveal more choices)\e[0m",
|
210
|
+
"\e[1000D\e[K\e[1A" * 4 + "\e[1000D\e[K",
|
211
|
+
"What letter? \e[32mD\e[0m\n\e[?25h",
|
212
|
+
].join)
|
213
|
+
end
|
214
|
+
|
215
|
+
it "paginates long selections through DSL" do
|
216
|
+
prompt = TTY::TestPrompt.new
|
217
|
+
choices = %w(A B C D E F G H)
|
218
|
+
prompt.input << "\r"
|
219
|
+
prompt.input.rewind
|
220
|
+
value = prompt.select('What letter?') do |menu|
|
221
|
+
menu.per_page 3
|
222
|
+
menu.page_help '(Wiggle thy finger up or down to see more)'
|
223
|
+
menu.default 4
|
224
|
+
|
225
|
+
menu.choices choices
|
226
|
+
end
|
227
|
+
expect(value).to eq('D')
|
228
|
+
expect(prompt.output.string).to eq([
|
229
|
+
"\e[?25lWhat letter? \e[90m(Use arrow keys, press Enter to select)\e[0m\n",
|
230
|
+
"\e[32m‣ D\e[0m\n",
|
231
|
+
" E\n",
|
232
|
+
" F\n",
|
233
|
+
"\e[90m(Wiggle thy finger up or down to see more)\e[0m",
|
234
|
+
"\e[1000D\e[K\e[1A" * 4 + "\e[1000D\e[K",
|
235
|
+
"What letter? \e[32mD\e[0m\n\e[?25h",
|
236
|
+
].join)
|
237
|
+
end
|
238
|
+
|
198
239
|
it "verifies default index format" do
|
199
240
|
prompt = TTY::TestPrompt.new
|
200
241
|
choices = %w(Large Medium Small)
|
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.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: necromancer
|
@@ -131,11 +131,13 @@ files:
|
|
131
131
|
- LICENSE.txt
|
132
132
|
- README.md
|
133
133
|
- Rakefile
|
134
|
+
- appveyor.yml
|
134
135
|
- benchmarks/speed.rb
|
135
136
|
- examples/ask.rb
|
136
137
|
- examples/collect.rb
|
137
138
|
- examples/echo.rb
|
138
139
|
- examples/enum.rb
|
140
|
+
- examples/enum_paged.rb
|
139
141
|
- examples/enum_select.rb
|
140
142
|
- examples/expand.rb
|
141
143
|
- examples/in.rb
|
@@ -143,6 +145,7 @@ files:
|
|
143
145
|
- examples/mask.rb
|
144
146
|
- examples/multi_select.rb
|
145
147
|
- examples/select.rb
|
148
|
+
- examples/select_paginated.rb
|
146
149
|
- examples/slider.rb
|
147
150
|
- examples/validation.rb
|
148
151
|
- examples/yes_no.rb
|
@@ -157,12 +160,14 @@ files:
|
|
157
160
|
- lib/tty/prompt/converters.rb
|
158
161
|
- lib/tty/prompt/distance.rb
|
159
162
|
- lib/tty/prompt/enum_list.rb
|
163
|
+
- lib/tty/prompt/enum_paginator.rb
|
160
164
|
- lib/tty/prompt/evaluator.rb
|
161
165
|
- lib/tty/prompt/expander.rb
|
162
166
|
- lib/tty/prompt/history.rb
|
163
167
|
- lib/tty/prompt/list.rb
|
164
168
|
- lib/tty/prompt/mask_question.rb
|
165
169
|
- lib/tty/prompt/multi_list.rb
|
170
|
+
- lib/tty/prompt/paginator.rb
|
166
171
|
- lib/tty/prompt/question.rb
|
167
172
|
- lib/tty/prompt/question/checks.rb
|
168
173
|
- lib/tty/prompt/question/modifier.rb
|
@@ -200,6 +205,7 @@ files:
|
|
200
205
|
- spec/unit/converters/convert_regex_spec.rb
|
201
206
|
- spec/unit/converters/convert_string_spec.rb
|
202
207
|
- spec/unit/distance/distance_spec.rb
|
208
|
+
- spec/unit/enum_paginator_spec.rb
|
203
209
|
- spec/unit/enum_select_spec.rb
|
204
210
|
- spec/unit/error_spec.rb
|
205
211
|
- spec/unit/evaluator_spec.rb
|
@@ -210,6 +216,7 @@ files:
|
|
210
216
|
- spec/unit/multiline_spec.rb
|
211
217
|
- spec/unit/new_spec.rb
|
212
218
|
- spec/unit/ok_spec.rb
|
219
|
+
- spec/unit/paginator_spec.rb
|
213
220
|
- spec/unit/question/checks_spec.rb
|
214
221
|
- spec/unit/question/default_spec.rb
|
215
222
|
- spec/unit/question/echo_spec.rb
|
@@ -286,6 +293,7 @@ test_files:
|
|
286
293
|
- spec/unit/converters/convert_regex_spec.rb
|
287
294
|
- spec/unit/converters/convert_string_spec.rb
|
288
295
|
- spec/unit/distance/distance_spec.rb
|
296
|
+
- spec/unit/enum_paginator_spec.rb
|
289
297
|
- spec/unit/enum_select_spec.rb
|
290
298
|
- spec/unit/error_spec.rb
|
291
299
|
- spec/unit/evaluator_spec.rb
|
@@ -296,6 +304,7 @@ test_files:
|
|
296
304
|
- spec/unit/multiline_spec.rb
|
297
305
|
- spec/unit/new_spec.rb
|
298
306
|
- spec/unit/ok_spec.rb
|
307
|
+
- spec/unit/paginator_spec.rb
|
299
308
|
- spec/unit/question/checks_spec.rb
|
300
309
|
- spec/unit/question/default_spec.rb
|
301
310
|
- spec/unit/question/echo_spec.rb
|