tty-prompt 0.18.0 → 0.22.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.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +92 -0
  3. data/README.md +549 -248
  4. data/lib/tty-prompt.rb +1 -2
  5. data/lib/tty/prompt.rb +187 -143
  6. data/lib/tty/prompt/answers_collector.rb +5 -5
  7. data/lib/tty/prompt/{enum_paginator.rb → block_paginator.rb} +20 -19
  8. data/lib/tty/prompt/choice.rb +5 -7
  9. data/lib/tty/prompt/choices.rb +29 -11
  10. data/lib/tty/prompt/confirm_question.rb +38 -16
  11. data/lib/tty/prompt/const.rb +17 -0
  12. data/lib/tty/prompt/converter_dsl.rb +6 -7
  13. data/lib/tty/prompt/converter_registry.rb +31 -26
  14. data/lib/tty/prompt/converters.rb +139 -32
  15. data/lib/tty/prompt/enum_list.rb +57 -27
  16. data/lib/tty/prompt/errors.rb +31 -0
  17. data/lib/tty/prompt/evaluator.rb +1 -1
  18. data/lib/tty/prompt/expander.rb +39 -13
  19. data/lib/tty/prompt/keypress.rb +31 -36
  20. data/lib/tty/prompt/list.rb +175 -65
  21. data/lib/tty/prompt/mask_question.rb +4 -5
  22. data/lib/tty/prompt/multi_list.rb +124 -33
  23. data/lib/tty/prompt/multiline.rb +7 -6
  24. data/lib/tty/prompt/paginator.rb +38 -26
  25. data/lib/tty/prompt/question.rb +83 -34
  26. data/lib/tty/prompt/question/checks.rb +18 -0
  27. data/lib/tty/prompt/question/validation.rb +3 -3
  28. data/lib/tty/prompt/selected_choices.rb +76 -0
  29. data/lib/tty/prompt/slider.rb +83 -16
  30. data/lib/tty/prompt/statement.rb +3 -3
  31. data/lib/tty/prompt/suggestion.rb +6 -6
  32. data/lib/tty/prompt/symbols.rb +58 -34
  33. data/lib/tty/prompt/test.rb +36 -0
  34. data/lib/tty/prompt/timer.rb +75 -0
  35. data/lib/tty/prompt/utils.rb +1 -3
  36. data/lib/tty/prompt/version.rb +1 -1
  37. metadata +29 -227
  38. data/Rakefile +0 -8
  39. data/examples/ask.rb +0 -7
  40. data/examples/ask_valid.rb +0 -12
  41. data/examples/collect.rb +0 -21
  42. data/examples/echo.rb +0 -11
  43. data/examples/enum_select.rb +0 -7
  44. data/examples/enum_select_disabled.rb +0 -16
  45. data/examples/enum_select_paged.rb +0 -9
  46. data/examples/enum_select_wrapped.rb +0 -15
  47. data/examples/expand.rb +0 -29
  48. data/examples/in.rb +0 -9
  49. data/examples/inputs.rb +0 -10
  50. data/examples/key_events.rb +0 -15
  51. data/examples/keypress.rb +0 -9
  52. data/examples/mask.rb +0 -13
  53. data/examples/multi_select.rb +0 -8
  54. data/examples/multi_select_disabled.rb +0 -17
  55. data/examples/multi_select_paged.rb +0 -9
  56. data/examples/multi_select_wrapped.rb +0 -15
  57. data/examples/multiline.rb +0 -9
  58. data/examples/pause.rb +0 -9
  59. data/examples/select.rb +0 -20
  60. data/examples/select_disabled.rb +0 -18
  61. data/examples/select_enum.rb +0 -8
  62. data/examples/select_filtered.rb +0 -11
  63. data/examples/select_paginated.rb +0 -11
  64. data/examples/select_wrapped.rb +0 -15
  65. data/examples/slider.rb +0 -6
  66. data/examples/validation.rb +0 -9
  67. data/examples/yes_no.rb +0 -7
  68. data/lib/tty/prompt/messages.rb +0 -49
  69. data/lib/tty/prompt/timeout.rb +0 -78
  70. data/lib/tty/test_prompt.rb +0 -20
  71. data/spec/spec_helper.rb +0 -45
  72. data/spec/unit/ask_spec.rb +0 -132
  73. data/spec/unit/choice/eql_spec.rb +0 -22
  74. data/spec/unit/choice/from_spec.rb +0 -96
  75. data/spec/unit/choices/add_spec.rb +0 -12
  76. data/spec/unit/choices/each_spec.rb +0 -13
  77. data/spec/unit/choices/find_by_spec.rb +0 -10
  78. data/spec/unit/choices/new_spec.rb +0 -10
  79. data/spec/unit/choices/pluck_spec.rb +0 -9
  80. data/spec/unit/collect_spec.rb +0 -96
  81. data/spec/unit/converters/convert_bool_spec.rb +0 -58
  82. data/spec/unit/converters/convert_char_spec.rb +0 -11
  83. data/spec/unit/converters/convert_custom_spec.rb +0 -14
  84. data/spec/unit/converters/convert_date_spec.rb +0 -34
  85. data/spec/unit/converters/convert_file_spec.rb +0 -18
  86. data/spec/unit/converters/convert_number_spec.rb +0 -39
  87. data/spec/unit/converters/convert_path_spec.rb +0 -15
  88. data/spec/unit/converters/convert_range_spec.rb +0 -22
  89. data/spec/unit/converters/convert_regex_spec.rb +0 -12
  90. data/spec/unit/converters/convert_string_spec.rb +0 -21
  91. data/spec/unit/converters/on_error_spec.rb +0 -9
  92. data/spec/unit/distance/distance_spec.rb +0 -73
  93. data/spec/unit/enum_paginator_spec.rb +0 -75
  94. data/spec/unit/enum_select_spec.rb +0 -446
  95. data/spec/unit/error_spec.rb +0 -20
  96. data/spec/unit/evaluator_spec.rb +0 -67
  97. data/spec/unit/expand_spec.rb +0 -198
  98. data/spec/unit/keypress_spec.rb +0 -72
  99. data/spec/unit/mask_spec.rb +0 -132
  100. data/spec/unit/multi_select_spec.rb +0 -495
  101. data/spec/unit/multiline_spec.rb +0 -77
  102. data/spec/unit/new_spec.rb +0 -20
  103. data/spec/unit/ok_spec.rb +0 -10
  104. data/spec/unit/paginator_spec.rb +0 -73
  105. data/spec/unit/question/checks_spec.rb +0 -97
  106. data/spec/unit/question/default_spec.rb +0 -31
  107. data/spec/unit/question/echo_spec.rb +0 -38
  108. data/spec/unit/question/in_spec.rb +0 -115
  109. data/spec/unit/question/initialize_spec.rb +0 -12
  110. data/spec/unit/question/modifier/apply_to_spec.rb +0 -24
  111. data/spec/unit/question/modifier/letter_case_spec.rb +0 -41
  112. data/spec/unit/question/modifier/whitespace_spec.rb +0 -51
  113. data/spec/unit/question/modify_spec.rb +0 -41
  114. data/spec/unit/question/required_spec.rb +0 -92
  115. data/spec/unit/question/validate_spec.rb +0 -115
  116. data/spec/unit/question/validation/call_spec.rb +0 -31
  117. data/spec/unit/question/validation/coerce_spec.rb +0 -30
  118. data/spec/unit/result_spec.rb +0 -40
  119. data/spec/unit/say_spec.rb +0 -67
  120. data/spec/unit/select_spec.rb +0 -643
  121. data/spec/unit/slider_spec.rb +0 -100
  122. data/spec/unit/statement/initialize_spec.rb +0 -15
  123. data/spec/unit/subscribe_spec.rb +0 -22
  124. data/spec/unit/suggest_spec.rb +0 -28
  125. data/spec/unit/warn_spec.rb +0 -21
  126. data/spec/unit/yes_no_spec.rb +0 -251
  127. data/tasks/console.rake +0 -11
  128. data/tasks/coverage.rake +0 -11
  129. data/tasks/spec.rake +0 -29
  130. data/tty-prompt.gemspec +0 -33
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Prompt::EnumPaginator, '#paginate' do
4
- it "ignores per_page when equal items " do
5
- list = %w(a b c d)
6
- paginator = described_class.new({per_page: 4})
7
-
8
- expect(paginator.paginate(list, 1).to_a).to eq([
9
- ['a',0],['b',1],['c',2],['d',3]])
10
- end
11
-
12
- it "ignores per_page when less items " do
13
- list = %w(a b c d)
14
- paginator = described_class.new({per_page: 5})
15
-
16
- expect(paginator.paginate(list, 1).to_a).to eq([
17
- ['a',0],['b',1],['c',2],['d',3]])
18
- end
19
-
20
- it "paginates items matching per_page count" do
21
- list = %w(a b c d e f)
22
- paginator = described_class.new({per_page: 3})
23
-
24
- expect(paginator.paginate(list, 1).to_a).to eq([['a',0], ['b',1], ['c',2]])
25
- expect(paginator.paginate(list, 2).to_a).to eq([['a',0], ['b',1], ['c',2]])
26
- expect(paginator.paginate(list, 3).to_a).to eq([['a',0], ['b',1], ['c',2]])
27
- expect(paginator.paginate(list, 4).to_a).to eq([['d',3], ['e',4], ['f',5]])
28
- expect(paginator.paginate(list, 5).to_a).to eq([['d',3], ['e',4], ['f',5]])
29
- expect(paginator.paginate(list, 6).to_a).to eq([['d',3], ['e',4], ['f',5]])
30
- expect(paginator.paginate(list, 7).to_a).to eq([['d',3], ['e',4], ['f',5]])
31
- end
32
-
33
- it "paginates items not matching per_page count" do
34
- list = %w(a b c d e f g)
35
- paginator = described_class.new({per_page: 3})
36
-
37
- expect(paginator.paginate(list, 1).to_a).to eq([['a',0], ['b',1], ['c',2]])
38
- expect(paginator.paginate(list, 2).to_a).to eq([['a',0], ['b',1], ['c',2]])
39
- expect(paginator.paginate(list, 3).to_a).to eq([['a',0], ['b',1], ['c',2]])
40
- expect(paginator.paginate(list, 4).to_a).to eq([['d',3], ['e',4], ['f',5]])
41
- expect(paginator.paginate(list, 5).to_a).to eq([['d',3], ['e',4], ['f',5]])
42
- expect(paginator.paginate(list, 6).to_a).to eq([['d',3], ['e',4], ['f',5]])
43
- expect(paginator.paginate(list, 7).to_a).to eq([['g',6]])
44
- expect(paginator.paginate(list, 8).to_a).to eq([['g',6]])
45
- end
46
-
47
- it "finds maximum index for current selection" do
48
- list = %w(a b c d e f g)
49
- paginator = described_class.new({per_page: 3, default: 0})
50
-
51
- paginator.paginate(list, 4)
52
- expect(paginator.max_index).to eq(5)
53
- paginator.paginate(list, 5)
54
- expect(paginator.max_index).to eq(5)
55
- paginator.paginate(list, 7)
56
- expect(paginator.max_index).to eq(8)
57
- end
58
-
59
- it "starts with default selection" do
60
- list = %w(a b c d e f g)
61
- paginator = described_class.new({per_page: 3, default: 3})
62
-
63
- expect(paginator.paginate(list, 4).to_a).to eq([['d',3], ['e',4], ['f',5]])
64
- end
65
-
66
- it "doesn't accept invalid pagination" do
67
- list = %w(a b c d e f g)
68
-
69
- paginator = described_class.new({per_page: 0})
70
-
71
- expect {
72
- paginator.paginate(list, 4)
73
- }.to raise_error(TTY::Prompt::InvalidArgument, /per_page must be > 0/)
74
- end
75
- end
@@ -1,446 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Prompt do
4
- let(:symbols) { TTY::Prompt::Symbols.symbols }
5
-
6
- def output_helper(prompt, choices, active, options = {})
7
- enum = options.fetch(:enum, ')')
8
- input = options[:input]
9
- error = options[:error]
10
- default = options.fetch(:default, 1)
11
-
12
- out = []
13
- out << prompt << " \n"
14
- out << choices.map.with_index do |c, i|
15
- name = c.is_a?(Hash) ? c[:name] : c
16
- disabled = c.is_a?(Hash) ? c[:disabled] : false
17
- num = (i + 1).to_s + enum
18
- if disabled
19
- "\e[31m#{symbols[:cross]}\e[0m #{num} #{name} #{disabled}"
20
- elsif name == active
21
- " \e[32m#{num} #{name}\e[0m"
22
- else
23
- " #{num} #{name}"
24
- end
25
- end.join("\n")
26
- out << "\n"
27
- choice = " Choose 1-#{choices.count} [#{default}]: "
28
- choice = choice + input.to_s if input
29
- out << choice
30
- if error
31
- out << "\n"
32
- out << "\e[31m>>\e[0m #{error}"
33
- out << "\e[A\e[1G\e[#{choice.size}C"
34
- end
35
- out << "\e[2K\e[1G\e[1A" * (choices.count + 1)
36
- out << "\e[2K\e[1G\e[J"
37
- out.join
38
- end
39
-
40
- def exit_message(prompt, choice)
41
- "#{prompt} \e[32m#{choice}\e[0m\n"
42
- end
43
-
44
- it "raises configuration error when wrong default" do
45
- prompt = TTY::TestPrompt.new
46
- choices = %w(/bin/nano /usr/bin/vim.basic /usr/bin/vim.tiny)
47
-
48
- expect {
49
- prompt.enum_select("Select an editor?", choices, default: 100)
50
- }.to raise_error(TTY::Prompt::ConfigurationError,
51
- /default index 100 out of range \(1 - 3\)/)
52
- end
53
-
54
- it "selects default option when return pressed immediately" do
55
- choices = %w(/bin/nano /usr/bin/vim.basic /usr/bin/vim.tiny)
56
- prompt = TTY::TestPrompt.new
57
- prompt.input << "\n"
58
- prompt.input.rewind
59
-
60
- answer = prompt.enum_select("Select an editor?", choices)
61
- expect(answer).to eq('/bin/nano')
62
-
63
- expected_output =
64
- output_helper("Select an editor?", choices, "/bin/nano") +
65
- exit_message("Select an editor?", "/bin/nano")
66
-
67
- expect(prompt.output.string).to eq(expected_output)
68
- end
69
-
70
- it "selects option by index from the list" do
71
- choices = %w(/bin/nano /usr/bin/vim.basic /usr/bin/vim.tiny)
72
- prompt = TTY::TestPrompt.new
73
- prompt.input << "3\n"
74
- prompt.input.rewind
75
-
76
- answer = prompt.enum_select("Select an editor?", choices, default: 2)
77
- expect(answer).to eq('/usr/bin/vim.tiny')
78
-
79
- expected_output =
80
- output_helper("Select an editor?", choices, "/usr/bin/vim.basic", default: 2) +
81
- output_helper("Select an editor?", choices, "/usr/bin/vim.tiny", default: 2, input: '3') +
82
- exit_message("Select an editor?", "/usr/bin/vim.tiny")
83
-
84
- expect(prompt.output.string).to eq(expected_output)
85
- end
86
-
87
- it "selects option through DSL" do
88
- choices = %w(/bin/nano /usr/bin/vim.basic /usr/bin/vim.tiny)
89
- prompt = TTY::TestPrompt.new
90
- prompt.input << "1\n"
91
- prompt.input.rewind
92
- answer = prompt.enum_select("Select an editor?") do |menu|
93
- menu.default 2
94
- menu.enum '.'
95
-
96
- menu.choice "/bin/nano"
97
- menu.choice "/usr/bin/vim.basic"
98
- menu.choice "/usr/bin/vim.tiny"
99
- end
100
- expect(answer).to eq('/bin/nano')
101
-
102
- expected_output =
103
- output_helper("Select an editor?", choices, "/usr/bin/vim.basic", default: 2, enum: '.') +
104
- output_helper("Select an editor?", choices, "/bin/nano", default: 2, enum: '.', input: 1) +
105
- exit_message("Select an editor?", "/bin/nano")
106
-
107
- expect(prompt.output.string).to eq(expected_output)
108
- end
109
-
110
- it "selects option through DSL with key and value" do
111
- choices = %w(nano vim emacs)
112
- prompt = TTY::TestPrompt.new
113
- prompt.input << "\n"
114
- prompt.input.rewind
115
-
116
- answer = prompt.enum_select("Select an editor?") do |menu|
117
- menu.default 2
118
-
119
- menu.choice :nano, '/bin/nano'
120
- menu.choice :vim, '/usr/bin/vim'
121
- menu.choice :emacs, '/usr/bin/emacs'
122
- end
123
-
124
- expect(answer).to eq('/usr/bin/vim')
125
-
126
- expected_output =
127
- output_helper("Select an editor?", choices, "vim", default: 2) +
128
- exit_message("Select an editor?", "vim")
129
-
130
- expect(prompt.output.string).to eq(expected_output)
131
- end
132
-
133
- it "changes colors for selection, hint and error" do
134
- prompt = TTY::TestPrompt.new
135
- choices = %w(/bin/nano /usr/bin/vim.basic /usr/bin/vim.tiny)
136
- prompt.input << "\n"
137
- prompt.input.rewind
138
- options = {active_color: :red, help_color: :blue, error_color: :green}
139
- expect(prompt.enum_select("Select an editor?", choices, options)).to eq('/bin/nano')
140
- expect(prompt.output.string).to eq([
141
- "Select an editor? \n",
142
- " \e[31m1) /bin/nano\e[0m\n",
143
- " 2) /usr/bin/vim.basic\n",
144
- " 3) /usr/bin/vim.tiny\n",
145
- " Choose 1-3 [1]: ",
146
- "\e[2K\e[1G\e[1A" * 4,
147
- "\e[2K\e[1G\e[J",
148
- "Select an editor? \e[31m/bin/nano\e[0m\n"
149
- ].join)
150
- end
151
-
152
- it "displays error with unrecognized input" do
153
- choices = %w(/bin/nano /usr/bin/vim.basic /usr/bin/vim.tiny)
154
- prompt = TTY::TestPrompt.new
155
- prompt.input << "11\n2\n"
156
- prompt.input.rewind
157
-
158
- answer = prompt.enum_select("Select an editor?", choices)
159
- expect(answer).to eq('/usr/bin/vim.basic')
160
-
161
- expected_output =
162
- output_helper("Select an editor?", choices, "/bin/nano") +
163
- output_helper("Select an editor?", choices, "/bin/nano", input: '1') +
164
- output_helper("Select an editor?", choices, "/bin/nano", input: '11') +
165
- output_helper("Select an editor?", choices, "/bin/nano", error: 'Please enter a valid number', input: '') +
166
- output_helper("Select an editor?", choices, "/usr/bin/vim.basic", error: 'Please enter a valid number', input: '2') +
167
- exit_message("Select an editor?", "/usr/bin/vim.basic")
168
-
169
- expect(prompt.output.string).to eq(expected_output)
170
- end
171
-
172
- it "paginates long selections" do
173
- choices = %w(A B C D E F G H)
174
- prompt = TTY::TestPrompt.new
175
- prompt.input << "\n"
176
- prompt.input.rewind
177
-
178
- answer = prompt.enum_select("What letter?", choices, per_page: 3, default: 4)
179
- expect(answer).to eq('D')
180
-
181
- expect(prompt.output.string).to eq([
182
- "What letter? \n",
183
- " \e[32m4) D\e[0m\n",
184
- " 5) E\n",
185
- " 6) F\n",
186
- " Choose 1-8 [4]: ",
187
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
188
- "\e[A\e[1G\e[18C",
189
- "\e[2K\e[1G\e[1A" * 4,
190
- "\e[2K\e[1G\e[J",
191
- "What letter? \e[32mD\e[0m\n"
192
- ].join)
193
- end
194
-
195
- it "doesn't paginate short selections" do
196
- choices = %w(A B C D)
197
- prompt = TTY::TestPrompt.new
198
- prompt.input << "\r"
199
- prompt.input.rewind
200
-
201
- answer = prompt.enum_select("What letter?", choices, per_page: 4, default: 1)
202
- expect(answer).to eq('A')
203
-
204
- expected_output =
205
- output_helper("What letter?", choices, "A") +
206
- exit_message("What letter?", "A")
207
-
208
- expect(prompt.output.string).to eq(expected_output)
209
- end
210
-
211
- it "shows pages matching input" do
212
- prompt = TTY::TestPrompt.new
213
- choices = %w(A B C D E F G H)
214
- prompt.input << "11\n\b\n"
215
- prompt.input.rewind
216
- value = prompt.enum_select("What letter?", choices, per_page: 3)
217
- expect(value).to eq('A')
218
- expect(prompt.output.string).to eq([
219
- "What letter? \n",
220
- " \e[32m1) A\e[0m\n",
221
- " 2) B\n",
222
- " 3) C\n",
223
- " Choose 1-8 [1]: ",
224
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
225
- "\e[A\e[1G\e[18C",
226
- "\e[2K\e[1G\e[1A" * 4,
227
- "\e[2K\e[1G\e[J",
228
- "What letter? \n",
229
- " \e[32m1) A\e[0m\n",
230
- " 2) B\n",
231
- " 3) C\n",
232
- " Choose 1-8 [1]: 1",
233
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
234
- "\e[A\e[1G\e[19C",
235
- "\e[2K\e[1G\e[1A" * 4,
236
- "\e[2K\e[1G\e[J",
237
- "What letter? \n",
238
- " \e[32m1) A\e[0m\n",
239
- " 2) B\n",
240
- " 3) C\n",
241
- " Choose 1-8 [1]: 11",
242
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
243
- "\e[A\e[1G\e[20C",
244
- "\e[2K\e[1G\e[1A" * 4,
245
- "\e[2K\e[1G\e[J",
246
- "What letter? \n",
247
- " \e[32m1) A\e[0m\n",
248
- " 2) B\n",
249
- " 3) C\n",
250
- " Choose 1-8 [1]: \n",
251
- "\e[31m>>\e[0m Please enter a valid number",
252
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
253
- "\e[A\e[1G\e[A\e[1G\e[18C",
254
- "\e[2K\e[1G\e[1A" * 4,
255
- "\e[2K\e[1G\e[J",
256
- "What letter? \n",
257
- " \e[32m1) A\e[0m\n",
258
- " 2) B\n",
259
- " 3) C\n",
260
- " Choose 1-8 [1]: \n",
261
- "\e[31m>>\e[0m Please enter a valid number",
262
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
263
- "\e[A\e[1G\e[A\e[1G\e[18C",
264
- "\e[2K\e[1G\e[1A" * 4,
265
- "\e[2K\e[1G\e[J",
266
- "What letter? \e[32mA\e[0m\n"
267
- ].join)
268
- end
269
-
270
- it "switches through pages with tab key" do
271
- prompt = TTY::TestPrompt.new
272
- choices = %w(A B C D E F G H)
273
- prompt.input << "\t\n"
274
- prompt.input.rewind
275
- value = prompt.enum_select("What letter?") do |menu|
276
- menu.default 4
277
- menu.per_page 3
278
- menu.choices choices
279
- end
280
- expect(value).to eq('D')
281
- expect(prompt.output.string).to eq([
282
- "What letter? \n",
283
- " \e[32m4) D\e[0m\n",
284
- " 5) E\n",
285
- " 6) F\n",
286
- " Choose 1-8 [4]: ",
287
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
288
- "\e[A\e[1G\e[18C",
289
- "\e[2K\e[1G\e[1A" * 4,
290
- "\e[2K\e[1G\e[J",
291
- "What letter? \n",
292
- " 7) G\n",
293
- " 8) H\n",
294
- " Choose 1-8 [4]: ",
295
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
296
- "\e[A\e[1G\e[18C",
297
- "\e[2K\e[1G\e[1A" * 3,
298
- "\e[2K\e[1G\e[J",
299
- "What letter? \e[32mD\e[0m\n"
300
- ].join)
301
- end
302
-
303
- it "doesn't cycle around by default" do
304
- prompt = TTY::TestPrompt.new
305
- choices = %w(A B C D E F)
306
- prompt.input << "\t" << "\t" << "\n"
307
- prompt.input.rewind
308
- value = prompt.enum_select("What letter?") do |menu|
309
- menu.default 1
310
- menu.per_page 3
311
- menu.choices choices
312
- end
313
- expect(value).to eq("A")
314
- expect(prompt.output.string).to eq([
315
- "What letter? \n",
316
- " \e[32m1) A\e[0m\n",
317
- " 2) B\n",
318
- " 3) C\n",
319
- " Choose 1-6 [1]: ",
320
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
321
- "\e[A\e[1G\e[18C",
322
- "\e[2K\e[1G\e[1A" * 4,
323
- "\e[2K\e[1G\e[J",
324
- "What letter? \n",
325
- " 4) D\n",
326
- " 5) E\n",
327
- " 6) F\n",
328
- " Choose 1-6 [1]: ",
329
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
330
- "\e[A\e[1G\e[18C",
331
- "\e[2K\e[1G\e[1A" * 4,
332
- "\e[2K\e[1G\e[J",
333
- "What letter? \n",
334
- " 4) D\n",
335
- " 5) E\n",
336
- " 6) F\n",
337
- " Choose 1-6 [1]: ",
338
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
339
- "\e[A\e[1G\e[18C",
340
- "\e[2K\e[1G\e[1A" * 4,
341
- "\e[2K\e[1G\e[J",
342
- "What letter? \e[32mA\e[0m\n"
343
- ].join)
344
- end
345
-
346
- it "cycles around when configured to do so" do
347
- prompt = TTY::TestPrompt.new
348
- choices = %w(A B C D E F)
349
- prompt.input << "\t" << "\t" << "\n"
350
- prompt.input.rewind
351
- value = prompt.enum_select("What letter?", cycle: true) do |menu|
352
- menu.default 1
353
- menu.per_page 3
354
- menu.choices choices
355
- end
356
- expect(value).to eq("A")
357
- expect(prompt.output.string).to eq([
358
- "What letter? \n",
359
- " \e[32m1) A\e[0m\n",
360
- " 2) B\n",
361
- " 3) C\n",
362
- " Choose 1-6 [1]: ",
363
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
364
- "\e[A\e[1G\e[18C",
365
- "\e[2K\e[1G\e[1A" * 4,
366
- "\e[2K\e[1G\e[J",
367
- "What letter? \n",
368
- " 4) D\n",
369
- " 5) E\n",
370
- " 6) F\n",
371
- " Choose 1-6 [1]: ",
372
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
373
- "\e[A\e[1G\e[18C",
374
- "\e[2K\e[1G\e[1A" * 4,
375
- "\e[2K\e[1G\e[J",
376
- "What letter? \n",
377
- " \e[32m1) A\e[0m\n",
378
- " 2) B\n",
379
- " 3) C\n",
380
- " Choose 1-6 [1]: ",
381
- "\n\e[90m(Press tab/right or left to reveal more choices)\e[0m",
382
- "\e[A\e[1G\e[18C",
383
- "\e[2K\e[1G\e[1A" * 4,
384
- "\e[2K\e[1G\e[J",
385
- "What letter? \e[32mA\e[0m\n"
386
- ].join)
387
- end
388
-
389
- context "with :disabled choice" do
390
- it "fails when active item is also disabled" do
391
- prompt = TTY::TestPrompt.new
392
- choices = [{name: 'A', disabled: true}, 'B', 'C', 'D', 'E']
393
- expect {
394
- prompt.enum_select("What letter?", choices)
395
- }.to raise_error(TTY::Prompt::ConfigurationError,
396
- /default index 1 matches disabled choice item/)
397
- end
398
-
399
- it "doesn't allow to choose disabled choice and defaults" do
400
- choices = ['A', {name: 'B', disabled: '(out)'}, 'C', 'D', 'E', 'F']
401
- prompt = TTY::TestPrompt.new
402
- prompt.input << "2" << "\n" << "3" << "\n"
403
- prompt.input.rewind
404
-
405
- answer = prompt.enum_select("What letter?", choices)
406
- expect(answer).to eq("C")
407
-
408
- expected_output =
409
- output_helper("What letter?", choices, 'A') +
410
- output_helper("What letter?", choices, 'A', input: '2') +
411
- output_helper("What letter?", choices, 'A', input: '', error: 'Please enter a valid number') +
412
- output_helper("What letter?", choices, 'C', input: '3', error: 'Please enter a valid number') +
413
- exit_message("What letter?", "C")
414
-
415
- expect(prompt.output.string).to eq(expected_output)
416
- end
417
-
418
- it "omits disabled choice when navigating with numbers" do
419
- choices = [
420
- {name: 'A'},
421
- {name: 'B', disabled: '(out)'},
422
- {name: 'C', disabled: '(out)'},
423
- {name: 'D'},
424
- {name: 'E'}
425
- ]
426
- prompt = TTY::TestPrompt.new
427
- prompt.on(:keypress) { |e| prompt.trigger(:keydelete) if e.value == "B"}
428
- prompt.input << "2" << "\u007F" << "3" << "\u007F" << '4' << "\n"
429
- prompt.input.rewind
430
-
431
- answer = prompt.enum_select("What letter?", choices)
432
- expect(answer).to eq("D")
433
-
434
- expected_output =
435
- output_helper("What letter?", choices, 'A') +
436
- output_helper("What letter?", choices, 'A', input: '2') +
437
- output_helper("What letter?", choices, 'A', input: '') +
438
- output_helper("What letter?", choices, 'A', input: '3') +
439
- output_helper("What letter?", choices, 'A', input: '') +
440
- output_helper("What letter?", choices, 'D', input: '4') +
441
- exit_message("What letter?", "D")
442
-
443
- expect(prompt.output.string).to eq(expected_output)
444
- end
445
- end
446
- end