tty-prompt 0.4.0 → 0.5.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.
@@ -83,7 +83,7 @@ RSpec.describe TTY::Prompt do
83
83
  end
84
84
  expect(value).to eq([{score: 1}])
85
85
  expect(prompt.output.string).to eq([
86
- "\e[?25lSelect drinks? \e[90m(Use arrow or number (0-9) keys, press Space to select and Enter to finish)\e[0m\n",
86
+ "\e[?25lSelect drinks? \e[90m(Use arrow or number (1-5) keys, press Space to select and Enter to finish)\e[0m\n",
87
87
  "‣ ⬡ 1) vodka\n",
88
88
  " ⬡ 2) beer\n",
89
89
  " ⬡ 3) wine\n",
@@ -116,7 +116,7 @@ RSpec.describe TTY::Prompt do
116
116
  end
117
117
  expect(value).to match_array([{score: 20}, {score: 50}])
118
118
  expect(prompt.output.string).to eq([
119
- "\e[?25lSelect drinks? beer, bourbon\n",
119
+ "\e[?25lSelect drinks? beer, bourbon \e[90m(Use arrow keys, press Space to select and Enter to finish)\e[0m\n",
120
120
  " ⬡ vodka\n",
121
121
  " \e[32m⬢\e[0m beer\n",
122
122
  " ⬡ wine\n",
@@ -174,4 +174,41 @@ RSpec.describe TTY::Prompt do
174
174
  "[?] Select drinks? \n\e[?25h"
175
175
  ].join)
176
176
  end
177
+
178
+ it "changes selected item color & marker" do
179
+ prompt = TTY::TestPrompt.new
180
+ choices = %w(vodka beer wine whisky bourbon)
181
+ prompt.input << "\r"
182
+ prompt.input.rewind
183
+ options = {default: [1], active_color: :blue, marker: '>'}
184
+ expect(prompt.multi_select("Select drinks?", choices, options)). to eq(['vodka'])
185
+ expect(prompt.output.string).to eq([
186
+ "\e[?25lSelect drinks? vodka \e[90m(Use arrow keys, press Space to select and Enter to finish)\e[0m\n",
187
+ "> \e[34m⬢\e[0m vodka\n",
188
+ " ⬡ beer\n",
189
+ " ⬡ wine\n",
190
+ " ⬡ whisky\n",
191
+ " ⬡ bourbon",
192
+ "\e[1000D\e[K\e[1A" * 5, "\e[1000D\e[K",
193
+ "Select drinks? \e[34mvodka\e[0m\n\e[?25h"
194
+ ].join)
195
+ end
196
+
197
+ it "changes help text" do
198
+ prompt = TTY::TestPrompt.new
199
+ choices = %w(vodka beer wine whisky bourbon)
200
+ prompt.input << "\r"
201
+ prompt.input.rewind
202
+ expect(prompt.multi_select("Select drinks?", choices, help: '(Bash keyboard)')). to eq([])
203
+ expect(prompt.output.string).to eq([
204
+ "\e[?25lSelect drinks? \e[90m(Bash keyboard)\e[0m\n",
205
+ "‣ ⬡ vodka\n",
206
+ " ⬡ beer\n",
207
+ " ⬡ wine\n",
208
+ " ⬡ whisky\n",
209
+ " ⬡ bourbon",
210
+ "\e[1000D\e[K\e[1A" * 5, "\e[1000D\e[K",
211
+ "Select drinks? \n\e[?25h"
212
+ ].join)
213
+ end
177
214
  end
@@ -103,7 +103,7 @@ RSpec.describe TTY::Prompt, '#select' do
103
103
  end
104
104
  expect(value).to eq(2)
105
105
  expect(prompt.output.string).to eq([
106
- "\e[?25lWhat size? \e[90m(Use arrow or number (0-9) keys, press Enter to select)\e[0m\n",
106
+ "\e[?25lWhat size? \e[90m(Use arrow or number (1-3) keys, press Enter to select)\e[0m\n",
107
107
  " 1. large\n",
108
108
  "\e[32m‣ 2. medium\e[0m\n",
109
109
  " 3. small",
@@ -122,7 +122,7 @@ RSpec.describe TTY::Prompt, '#select' do
122
122
  end
123
123
  expect(value).to eq('Good choice!')
124
124
  expect(prompt.output.string).to eq([
125
- "\e[?25lWhat size? \e[90m(Use arrow or number (0-9) keys, press Enter to select)\e[0m\n",
125
+ "\e[?25lWhat size? \e[90m(Use arrow or number (1-3) keys, press Enter to select)\e[0m\n",
126
126
  " 1) large\n",
127
127
  "\e[32m‣ 2) medium\e[0m\n",
128
128
  " 3) small",
@@ -137,7 +137,7 @@ RSpec.describe TTY::Prompt, '#select' do
137
137
  prompt.input.rewind
138
138
  expect(prompt.select('What size?', choices, default: 2, enum: '.')).to eq('Medium')
139
139
  expect(prompt.output.string).to eq([
140
- "\e[?25lWhat size? \e[90m(Use arrow or number (0-9) keys, press Enter to select)\e[0m\n",
140
+ "\e[?25lWhat size? \e[90m(Use arrow or number (1-3) keys, press Enter to select)\e[0m\n",
141
141
  " 1. Large\n",
142
142
  "\e[32m‣ 2. Medium\e[0m\n",
143
143
  " 3. Small",
@@ -150,10 +150,11 @@ RSpec.describe TTY::Prompt, '#select' do
150
150
  choices = %w(Large Medium Small)
151
151
  prompt.input << " "
152
152
  prompt.input.rewind
153
- value = prompt.select('What size?', choices, color: :blue, marker: '>')
153
+ options = {active_color: :blue, help_color: :red, marker: '>'}
154
+ value = prompt.select('What size?', choices, options)
154
155
  expect(value).to eq('Large')
155
156
  expect(prompt.output.string).to eq([
156
- "\e[?25lWhat size? \e[90m(Use arrow keys, press Enter to select)\e[0m\n",
157
+ "\e[?25lWhat size? \e[31m(Use arrow keys, press Enter to select)\e[0m\n",
157
158
  "\e[34m> Large\e[0m\n",
158
159
  " Medium\n",
159
160
  " Small",
@@ -193,4 +194,26 @@ RSpec.describe TTY::Prompt, '#select' do
193
194
  "[?] What size? \e[32mLarge\e[0m\n\e[?25h"
194
195
  ].join)
195
196
  end
197
+
198
+ it "verifies default index format" do
199
+ prompt = TTY::TestPrompt.new
200
+ choices = %w(Large Medium Small)
201
+ prompt.input << "\r"
202
+ prompt.input.rewind
203
+
204
+ expect {
205
+ prompt.select('What size?', choices, default: '')
206
+ }.to raise_error(TTY::Prompt::ConfigurationError, /in range \(1 - 3\)/)
207
+ end
208
+
209
+ it "verifies default index range" do
210
+ prompt = TTY::TestPrompt.new
211
+ choices = %w(Large Medium Small)
212
+ prompt.input << "\r"
213
+ prompt.input.rewind
214
+
215
+ expect {
216
+ prompt.select('What size?', choices, default: 10)
217
+ }.to raise_error(TTY::Prompt::ConfigurationError, /`10` out of range \(1 - 3\)/)
218
+ end
196
219
  end
@@ -51,4 +51,19 @@ RSpec.describe TTY::Prompt, '#slider' do
51
51
  "What size? \e[32m6\e[0m\n\e[?25h"
52
52
  ].join)
53
53
  end
54
+
55
+ it "changes display colors" do
56
+ prompt.input << "\r"
57
+ prompt.input.rewind
58
+ options = {active_color: :red, help_color: :cyan}
59
+ expect(prompt.slider('What size?', options)).to eq(5)
60
+ expect(prompt.output.string).to eq([
61
+ "\e[?25lWhat size? \e[36m(Use arrow keys, press Enter to select)\e[0m\n",
62
+ "|-----",
63
+ "\e[31mO\e[0m",
64
+ "-----| 5",
65
+ "\e[1000D\e[K\e[1A\e[1000D\e[K",
66
+ "What size? \e[31m5\e[0m\n\e[?25h"
67
+ ].join)
68
+ end
54
69
  end
@@ -1,11 +1,11 @@
1
1
  # encoding: utf-8
2
2
 
3
- RSpec.describe TTY::Prompt, '#yes?' do
3
+ RSpec.describe TTY::Prompt, 'confirmation' do
4
4
 
5
5
  subject(:prompt) { TTY::TestPrompt.new }
6
6
 
7
- context 'yes?' do
8
- it 'agrees' do
7
+ context '#yes?' do
8
+ it 'agrees with question' do
9
9
  prompt.input << 'yes'
10
10
  prompt.input.rewind
11
11
  expect(prompt.yes?("Are you a human?")).to eq(true)
@@ -13,11 +13,11 @@ RSpec.describe TTY::Prompt, '#yes?' do
13
13
  "Are you a human? \e[90m(Y/n)\e[0m ",
14
14
  "\e[1000D\e[K\e[1A",
15
15
  "\e[1000D\e[K",
16
- "Are you a human? \e[32myes\e[0m\n"
16
+ "Are you a human? \e[32mYes\e[0m\n"
17
17
  ].join)
18
18
  end
19
19
 
20
- it 'disagrees' do
20
+ it 'disagrees with question' do
21
21
  prompt.input << 'no'
22
22
  prompt.input.rewind
23
23
  expect(prompt.yes?("Are you a human?")).to eq(false)
@@ -32,38 +32,173 @@ RSpec.describe TTY::Prompt, '#yes?' do
32
32
  it 'assumes default true' do
33
33
  prompt.input << "\r"
34
34
  prompt.input.rewind
35
- expect(prompt.yes?("Are you a human?", default: true)).to eq(true)
35
+ expect(prompt.yes?("Are you a human?")).to eq(true)
36
+ expect(prompt.output.string).to eq([
37
+ "Are you a human? \e[90m(Y/n)\e[0m ",
38
+ "\e[1000D\e[K\e[1A",
39
+ "\e[1000D\e[K",
40
+ "Are you a human? \e[32mYes\e[0m\n"
41
+ ].join)
42
+ end
43
+
44
+ it 'changes default' do
45
+ prompt.input << "\n"
46
+ prompt.input.rewind
47
+ expect(prompt.yes?("Are you a human?", default: false)).to eq(false)
36
48
  expect(prompt.output.string).to eq([
37
49
  "Are you a human? \e[90m(Y/n)\e[0m ",
38
50
  "\e[1000D\e[K\e[1A",
39
51
  "\e[1000D\e[K",
40
- "Are you a human? \e[32mtrue\e[0m\n"
52
+ "Are you a human? \e[32mno\e[0m\n"
53
+ ].join)
54
+ end
55
+
56
+ it "defaults suffix and converter" do
57
+ prompt.input << "Nope\n"
58
+ prompt.input.rewind
59
+ result = prompt.yes?("Are you a human?") do |q|
60
+ q.positive 'Yup'
61
+ q.negative 'nope'
62
+ end
63
+ expect(result).to eq(false)
64
+ expect(prompt.output.string).to eq([
65
+ "Are you a human? \e[90m(Yup/nope)\e[0m ",
66
+ "\e[1000D\e[K\e[1A",
67
+ "\e[1000D\e[K",
68
+ "Are you a human? \e[32mnope\e[0m\n"
69
+ ].join)
70
+ end
71
+
72
+ it "defaults positive and negative" do
73
+ prompt.input << "Nope\n"
74
+ prompt.input.rewind
75
+ result = prompt.yes?("Are you a human?") do |q|
76
+ q.suffix 'Yup/nope'
77
+ end
78
+ expect(result).to eq(false)
79
+ expect(prompt.output.string).to eq([
80
+ "Are you a human? \e[90m(Yup/nope)\e[0m ",
81
+ "\e[1000D\e[K\e[1A",
82
+ "\e[1000D\e[K",
83
+ "Are you a human? \e[32mnope\e[0m\n"
84
+ ].join)
85
+ end
86
+
87
+ it "customizes question through options" do
88
+ prompt.input << "\r"
89
+ prompt.input.rewind
90
+ result = prompt.yes?("Are you a human?", suffix: 'Agree/Disagree',
91
+ positive: 'Agree', negative: 'Disagree')
92
+ expect(result).to eq(true)
93
+ expect(prompt.output.string).to eq([
94
+ "Are you a human? \e[90m(Agree/Disagree)\e[0m ",
95
+ "\e[1000D\e[K\e[1A",
96
+ "\e[1000D\e[K",
97
+ "Are you a human? \e[32mAgree\e[0m\n"
98
+ ].join)
99
+ end
100
+
101
+ it "customizes question through DSL" do
102
+ prompt.input << "disagree\r"
103
+ prompt.input.rewind
104
+ conversion = proc { |input| !input.match(/^agree$/i).nil? }
105
+ result = prompt.yes?("Are you a human?") do |q|
106
+ q.suffix 'Agree/Disagree'
107
+ q.positive 'Agree'
108
+ q.negative 'Disagree'
109
+ q.convert conversion
110
+ end
111
+ expect(result).to eq(false)
112
+ expect(prompt.output.string).to eq([
113
+ "Are you a human? \e[90m(Agree/Disagree)\e[0m ",
114
+ "\e[1000D\e[K\e[1A",
115
+ "\e[1000D\e[K",
116
+ "Are you a human? \e[32mDisagree\e[0m\n"
41
117
  ].join)
42
118
  end
43
119
  end
44
120
 
45
- context 'no?' do
46
- it 'disagrees' do
121
+ context '#no?' do
122
+ it 'agrees with question' do
47
123
  prompt.input << 'no'
48
124
  prompt.input.rewind
49
125
  expect(prompt.no?("Are you a human?")).to eq(true)
50
126
  expect(prompt.output.string).to eq([
51
- "Are you a human? \e[90m(Y/n)\e[0m ",
127
+ "Are you a human? \e[90m(y/N)\e[0m ",
52
128
  "\e[1000D\e[K\e[1A",
53
129
  "\e[1000D\e[K",
54
- "Are you a human? \e[32mno\e[0m\n"
130
+ "Are you a human? \e[32mNo\e[0m\n"
55
131
  ].join)
56
132
  end
57
133
 
58
- it 'agrees' do
134
+ it 'disagrees with question' do
59
135
  prompt.input << 'yes'
60
136
  prompt.input.rewind
61
137
  expect(prompt.no?("Are you a human?")).to eq(false)
62
138
  expect(prompt.output.string).to eq([
63
- "Are you a human? \e[90m(Y/n)\e[0m ",
139
+ "Are you a human? \e[90m(y/N)\e[0m ",
140
+ "\e[1000D\e[K\e[1A",
141
+ "\e[1000D\e[K",
142
+ "Are you a human? \e[32mYes\e[0m\n"
143
+ ].join)
144
+ end
145
+
146
+ it 'assumes default false' do
147
+ prompt.input << "\r"
148
+ prompt.input.rewind
149
+ expect(prompt.no?("Are you a human?")).to eq(true)
150
+ expect(prompt.output.string).to eq([
151
+ "Are you a human? \e[90m(y/N)\e[0m ",
152
+ "\e[1000D\e[K\e[1A",
153
+ "\e[1000D\e[K",
154
+ "Are you a human? \e[32mNo\e[0m\n"
155
+ ].join)
156
+ end
157
+
158
+ it 'changes default' do
159
+ prompt.input << "\r"
160
+ prompt.input.rewind
161
+ expect(prompt.no?("Are you a human?", default: true)).to eq(false)
162
+ expect(prompt.output.string).to eq([
163
+ "Are you a human? \e[90m(y/N)\e[0m ",
164
+ "\e[1000D\e[K\e[1A",
165
+ "\e[1000D\e[K",
166
+ "Are you a human? \e[32mYes\e[0m\n"
167
+ ].join)
168
+ end
169
+
170
+ it "defaults suffix and converter" do
171
+ prompt.input << "Yup\n"
172
+ prompt.input.rewind
173
+ result = prompt.no?("Are you a human?") do |q|
174
+ q.positive 'yup'
175
+ q.negative 'Nope'
176
+ end
177
+ expect(result).to eq(false)
178
+ expect(prompt.output.string).to eq([
179
+ "Are you a human? \e[90m(yup/Nope)\e[0m ",
180
+ "\e[1000D\e[K\e[1A",
181
+ "\e[1000D\e[K",
182
+ "Are you a human? \e[32myup\e[0m\n"
183
+ ].join)
184
+ end
185
+
186
+ it "customizes question through DSL" do
187
+ prompt.input << "agree\r"
188
+ prompt.input.rewind
189
+ conversion = proc { |input| !input.match(/^agree$/i).nil? }
190
+ result = prompt.no?("Are you a human?") do |q|
191
+ q.suffix 'Agree/Disagree'
192
+ q.positive 'Agree'
193
+ q.negative 'Disagree'
194
+ q.convert conversion
195
+ end
196
+ expect(result).to eq(false)
197
+ expect(prompt.output.string).to eq([
198
+ "Are you a human? \e[90m(Agree/Disagree)\e[0m ",
64
199
  "\e[1000D\e[K\e[1A",
65
200
  "\e[1000D\e[K",
66
- "Are you a human? \e[32myes\e[0m\n"
201
+ "Are you a human? \e[32mAgree\e[0m\n"
67
202
  ].join)
68
203
  end
69
204
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = [""]
11
11
  spec.summary = %q{A beautiful and powerful interactive command line prompt.}
12
12
  spec.description = %q{A beautiful and powerful interactive command line prompt with a robust API for getting and validating complex inputs.}
13
- spec.homepage = "http://peter-murach.github.io/tty"
13
+ spec.homepage = "https://piotrmurach.github.io/tty"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -25,6 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency 'wisper', '~> 1.6.1'
26
26
 
27
27
  spec.add_development_dependency 'bundler', '>= 1.5.0', '< 2.0'
28
- spec.add_development_dependency 'rspec', '~> 3.4.0'
29
28
  spec.add_development_dependency 'rake'
30
29
  end
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.0
4
+ version: 0.5.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-02-08 00:00:00.000000000 Z
11
+ date: 2016-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: necromancer
@@ -100,20 +100,6 @@ dependencies:
100
100
  - - <
101
101
  - !ruby/object:Gem::Version
102
102
  version: '2.0'
103
- - !ruby/object:Gem::Dependency
104
- name: rspec
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - ~>
108
- - !ruby/object:Gem::Version
109
- version: 3.4.0
110
- type: :development
111
- prerelease: false
112
- version_requirements: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - ~>
115
- - !ruby/object:Gem::Version
116
- version: 3.4.0
117
103
  - !ruby/object:Gem::Dependency
118
104
  name: rake
119
105
  requirement: !ruby/object:Gem::Requirement
@@ -144,10 +130,13 @@ files:
144
130
  - LICENSE.txt
145
131
  - README.md
146
132
  - Rakefile
133
+ - benchmarks/speed.rb
147
134
  - examples/ask.rb
135
+ - examples/collect.rb
148
136
  - examples/echo.rb
149
137
  - examples/enum.rb
150
138
  - examples/enum_select.rb
139
+ - examples/expand.rb
151
140
  - examples/in.rb
152
141
  - examples/mask.rb
153
142
  - examples/multi_select.rb
@@ -157,14 +146,17 @@ files:
157
146
  - examples/yes_no.rb
158
147
  - lib/tty-prompt.rb
159
148
  - lib/tty/prompt.rb
149
+ - lib/tty/prompt/answers_collector.rb
160
150
  - lib/tty/prompt/choice.rb
161
151
  - lib/tty/prompt/choices.rb
152
+ - lib/tty/prompt/confirm_question.rb
162
153
  - lib/tty/prompt/converter_dsl.rb
163
154
  - lib/tty/prompt/converter_registry.rb
164
155
  - lib/tty/prompt/converters.rb
165
156
  - lib/tty/prompt/distance.rb
166
157
  - lib/tty/prompt/enum_list.rb
167
158
  - lib/tty/prompt/evaluator.rb
159
+ - lib/tty/prompt/expander.rb
168
160
  - lib/tty/prompt/history.rb
169
161
  - lib/tty/prompt/list.rb
170
162
  - lib/tty/prompt/mask_question.rb
@@ -193,8 +185,10 @@ files:
193
185
  - spec/unit/choice/from_spec.rb
194
186
  - spec/unit/choices/add_spec.rb
195
187
  - spec/unit/choices/each_spec.rb
188
+ - spec/unit/choices/find_by_spec.rb
196
189
  - spec/unit/choices/new_spec.rb
197
190
  - spec/unit/choices/pluck_spec.rb
191
+ - spec/unit/collect_spec.rb
198
192
  - spec/unit/converters/convert_bool_spec.rb
199
193
  - spec/unit/converters/convert_char_spec.rb
200
194
  - spec/unit/converters/convert_custom_spec.rb
@@ -209,6 +203,7 @@ files:
209
203
  - spec/unit/enum_select_spec.rb
210
204
  - spec/unit/error_spec.rb
211
205
  - spec/unit/evaluator_spec.rb
206
+ - spec/unit/expand_spec.rb
212
207
  - spec/unit/keypress_spec.rb
213
208
  - spec/unit/mask_spec.rb
214
209
  - spec/unit/multi_select_spec.rb
@@ -245,7 +240,7 @@ files:
245
240
  - tasks/coverage.rake
246
241
  - tasks/spec.rake
247
242
  - tty-prompt.gemspec
248
- homepage: http://peter-murach.github.io/tty
243
+ homepage: https://piotrmurach.github.io/tty
249
244
  licenses:
250
245
  - MIT
251
246
  metadata: {}
@@ -276,8 +271,10 @@ test_files:
276
271
  - spec/unit/choice/from_spec.rb
277
272
  - spec/unit/choices/add_spec.rb
278
273
  - spec/unit/choices/each_spec.rb
274
+ - spec/unit/choices/find_by_spec.rb
279
275
  - spec/unit/choices/new_spec.rb
280
276
  - spec/unit/choices/pluck_spec.rb
277
+ - spec/unit/collect_spec.rb
281
278
  - spec/unit/converters/convert_bool_spec.rb
282
279
  - spec/unit/converters/convert_char_spec.rb
283
280
  - spec/unit/converters/convert_custom_spec.rb
@@ -292,6 +289,7 @@ test_files:
292
289
  - spec/unit/enum_select_spec.rb
293
290
  - spec/unit/error_spec.rb
294
291
  - spec/unit/evaluator_spec.rb
292
+ - spec/unit/expand_spec.rb
295
293
  - spec/unit/keypress_spec.rb
296
294
  - spec/unit/mask_spec.rb
297
295
  - spec/unit/multi_select_spec.rb