cliprompt 0.0.4 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 130c7d8782cb21bd861497572e1f70c561d3713a
4
- data.tar.gz: a7e8e0ed439cb3efb295931f8149a2f602c6df3e
3
+ metadata.gz: 61b352cf68eda3ca18f4051cacd85dd3047ac665
4
+ data.tar.gz: 156fb8f2414b6f4eee18ddb63ac8c05b25f33673
5
5
  SHA512:
6
- metadata.gz: 7a33c9fc219191d65e7f33fba11437d87795dfbc91387c6389ef9e79408297df0537f64158be384524f5097eddca13e7e700752a76d6f28ba67f3dcac1b72706
7
- data.tar.gz: 2bdb0c2ee17d1e8017ee6d7b28eff9b58b7b5e15ecc1308b7bf5d3fbf8cee15973a30366379ecaaf574bb136c7ced84c99baac769afbe89bdde6cb5f06f709c0
6
+ metadata.gz: be597dccf32f0a9c761b2b7d88b4af36040cfa91942371f8f64d99263ad5346e976d36b66bb38b9b8e4449b43ca023ba1b3d787142136a9ee8a970c07843520a
7
+ data.tar.gz: 03fd5bd35e18510a1b30c06e87c46e6122948f05ae2629bf23b6b2346fb5b15407d539feede545c4b69e3cc78dab94c6bcb0c176b194e5f3119819c33b1db79c
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  Cliprompt Changelog
2
2
  =====================
3
3
 
4
+ v0.0.5 - 2014-05-27
5
+ --------------------
6
+
7
+ - fix to one space after question that was 2 spaces when no default is provided
8
+ - when choices are a list, show the default in the question, in more than in the list
9
+ - clean some ruby warnings (running with --debug helps)
10
+
4
11
  v0.0.4 - 2014-05-24
5
12
  --------------------
6
13
 
data/example.rb CHANGED
@@ -64,6 +64,6 @@ class Myclass
64
64
  end
65
65
 
66
66
  m = Myclass.new
67
- #m.guessit
68
- #m.askit
67
+ m.guessit
68
+ m.askit
69
69
  m.biglist
data/lib/cliprompt.rb CHANGED
@@ -16,28 +16,16 @@ module Cliprompt
16
16
  if options[0].class == Optionset
17
17
  opts = options[0]
18
18
  else
19
- opts = Optionset.new *options
20
- end
21
- if opts.aslist
22
- output.puts "#{question}"
23
- opts.choices.each_with_index do |choice, i|
24
- if opts.default == choice
25
- output.printf "> %-3s %s\n", i, choice
26
- else
27
- output.printf " %-3s %s\n", i, choice
28
- end
29
- end
30
- output.print "#{MSG_CHOSE_A_NUMBER} "
31
- else
32
- output.print "#{question} #{opts.display} "
19
+ opts = Optionset.new(*options)
33
20
  end
21
+ output.print "#{question} #{opts.display}"
34
22
  answer = input.gets.chomp
35
23
  output.flush
36
24
  opts.validate(question, answer)
37
25
  end
38
26
 
39
27
  def guess(env, question, *options)
40
- opts = Optionset.new *options
28
+ opts = Optionset.new(*options)
41
29
  if ENV[env]
42
30
  opts.validate(question, ENV[env])
43
31
  else
@@ -63,9 +63,9 @@ module Cliprompt
63
63
  end
64
64
 
65
65
  def parse_string(arg)
66
- if arg.match /^[yY1](es)?(\/)?[nN0](o)?/
66
+ if /^[yY1](es)?(\/)?[nN0](o)?/.match(arg)
67
67
  @boolean = true
68
- if /y(es)?(\/)?N/.match arg
68
+ if /y(es)?(\/)?N/.match(arg)
69
69
  @default = false
70
70
  else
71
71
  @default = true
@@ -76,18 +76,54 @@ module Cliprompt
76
76
  end
77
77
 
78
78
  def display
79
- back = ''
80
79
  if @boolean
81
- back = @default ? "[Y/n]" : "[y/N]"
80
+ display_boolean
81
+ elsif @choices.count > 0
82
+ if @aslist
83
+ display_list
84
+ else
85
+ display_choices
86
+ end
82
87
  else
83
- if @choices.count > 0
84
- back += "(#{@choices.join(' / ')})"
88
+ display_default
89
+ end
90
+ end
91
+
92
+ def display_boolean
93
+ @default ? "[Y/n] " : "[y/N] "
94
+ end
95
+
96
+ def display_list
97
+ back = "\n"
98
+ choices.each_with_index do |choice, i|
99
+ if @default == choice
100
+ back << sprintf("> %-3s %s\n", i, choice)
101
+ else
102
+ back << sprintf(" %-3s %s\n", i, choice)
85
103
  end
86
- back += "[#{@default}]" if @default
87
104
  end
105
+ back << "#{Cliprompt::MSG_CHOSE_A_NUMBER} "
106
+ back << display_default_index.to_s
107
+ return back
108
+ end
109
+
110
+ def display_choices
111
+ back = ''
112
+ if @choices.count > 0
113
+ back << "(#{@choices.join(' / ')}) "
114
+ end
115
+ back << display_default.to_s
88
116
  return back
89
117
  end
90
118
 
119
+ def display_default
120
+ "[#{@default}] " if @default
121
+ end
122
+
123
+ def display_default_index
124
+ "[#{@choices.index(@default)}] " if @default
125
+ end
126
+
91
127
  def validate(question, answer)
92
128
  if answer == ''
93
129
  check_default question
@@ -1,3 +1,3 @@
1
1
  module Cliprompt
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -23,7 +23,7 @@ describe Cliprompt::Optionset do
23
23
  When(:display) { set.display }
24
24
  Then { expect(choices).to eq options }
25
25
  Then { expect(default).to be_false }
26
- Then { expect(display).to eq '(xxx / yyy / zzz)' }
26
+ Then { expect(set.display).to eq '(xxx / yyy / zzz) ' }
27
27
  end
28
28
 
29
29
  context "when there is a default specified (#{['xxx', '=yyy', 'zzz'].to_s})," do
@@ -35,7 +35,7 @@ describe Cliprompt::Optionset do
35
35
  When(:display) { set.display }
36
36
  Then { expect(choices).to eq options }
37
37
  Then { expect(default).to eq 'yyy' }
38
- Then { expect(display).to eq '(xxx / yyy / zzz)[yyy]' }
38
+ Then { expect(display).to eq '(xxx / yyy / zzz) [yyy] ' }
39
39
  end
40
40
 
41
41
  context "when there is a mixed numeric and string choices (#{[22, 'yyy', 'zzz'].to_s})," do
@@ -46,7 +46,7 @@ describe Cliprompt::Optionset do
46
46
  When(:display) { set.display }
47
47
  Then { expect(choices).to eq options.map(&:to_s) }
48
48
  Then { expect(default).to be_false }
49
- Then { expect(display).to eq '(22 / yyy / zzz)' }
49
+ Then { expect(display).to eq '(22 / yyy / zzz) ' }
50
50
  end
51
51
  end
52
52
 
@@ -61,7 +61,7 @@ describe Cliprompt::Optionset do
61
61
  When(:display) { set.display }
62
62
  Then { expect(choices).to eq ['xxx', 'yyy', 'zzz'] }
63
63
  Then { expect(default).to eq 'xxx' }
64
- Then { expect(display).to eq '(xxx / yyy / zzz)[xxx]' }
64
+ Then { expect(display).to eq '(xxx / yyy / zzz) [xxx] ' }
65
65
  end
66
66
  context "when using string keys (#{{ 'default' => 'xxx', 'choices' => ['xxx', 'yyy', 'zzz'] }.to_s})," do
67
67
  Given(:options) { { 'default' => 'xxx', 'choices' => ['xxx', 'yyy', 'zzz'] } }
@@ -71,12 +71,12 @@ describe Cliprompt::Optionset do
71
71
  When(:display) { set.display }
72
72
  Then { expect(choices).to eq ['xxx', 'yyy', 'zzz'] }
73
73
  Then { expect(default).to eq 'xxx' }
74
- Then { expect(display).to eq '(xxx / yyy / zzz)[xxx]' }
74
+ Then { expect(display).to eq '(xxx / yyy / zzz) [xxx] ' }
75
75
  end
76
76
  end
77
77
 
78
78
  context 'when there is only boolean,' do
79
- context 'when no default is given, default booean is Y,' do
79
+ context 'when no default is given, default booean is Y,' do
80
80
  Given(:options) { { boolean: true } }
81
81
  Given(:set) { Cliprompt::Optionset.new(options) }
82
82
  When(:default) { set.default }
@@ -84,7 +84,7 @@ describe Cliprompt::Optionset do
84
84
  When(:boolean) { set.boolean }
85
85
  Then { expect(default).to be_true }
86
86
  Then { expect(boolean).to be_true }
87
- Then { expect(display).to eq '[Y/n]' }
87
+ Then { expect(display).to eq '[Y/n] ' }
88
88
  end
89
89
  context 'when default is given as true, default booean is Y,' do
90
90
  Given(:options) { { boolean: true, default: true } }
@@ -94,7 +94,7 @@ describe Cliprompt::Optionset do
94
94
  When(:boolean) { set.boolean }
95
95
  Then { expect(default).to be_true }
96
96
  Then { expect(boolean).to be_true }
97
- Then { expect(display).to eq '[Y/n]' }
97
+ Then { expect(display).to eq '[Y/n] ' }
98
98
  end
99
99
  context 'when default is given as false, default boolean is N,' do
100
100
  Given(:options) { { boolean: true, default: false } }
@@ -104,7 +104,7 @@ describe Cliprompt::Optionset do
104
104
  When(:boolean) { set.boolean }
105
105
  Then { expect(default).to be_false }
106
106
  Then { expect(boolean).to be_true }
107
- Then { expect(display).to eq '[y/N]' }
107
+ Then { expect(display).to eq '[y/N] ' }
108
108
  end
109
109
  end
110
110
  end
@@ -129,7 +129,9 @@ describe Cliprompt::Optionset do
129
129
  Given(:options) { 'something' }
130
130
  Given(:set) { Cliprompt::Optionset.new(options) }
131
131
  When(:default) { set.default }
132
+ When(:display) { set.display }
132
133
  Then { expect(default).to eq options }
134
+ Then { expect(display).to eq '[something] ' }
133
135
  end
134
136
 
135
137
  context 'when a "yesno" kind of string is passed,' do
@@ -141,7 +143,7 @@ describe Cliprompt::Optionset do
141
143
  When(:boolean) { set.boolean }
142
144
  Then { expect(default).to be_true }
143
145
  Then { expect(boolean).to be_true }
144
- Then { expect(display).to eq '[Y/n]' }
146
+ Then { expect(display).to eq '[Y/n] ' }
145
147
  end
146
148
  context 'when using yn,' do
147
149
  Given(:options) { 'yn' }
@@ -149,7 +151,7 @@ describe Cliprompt::Optionset do
149
151
  When(:default) { set.default }
150
152
  When(:display) { set.display }
151
153
  Then { expect(default).to be_true }
152
- Then { expect(display).to eq '[Y/n]' }
154
+ Then { expect(display).to eq '[Y/n] ' }
153
155
  end
154
156
  context 'when using YN,' do
155
157
  Given(:options) { 'YN' }
@@ -157,7 +159,7 @@ describe Cliprompt::Optionset do
157
159
  When(:default) { set.default }
158
160
  When(:display) { set.display }
159
161
  Then { expect(default).to be_true }
160
- Then { expect(display).to eq '[Y/n]' }
162
+ Then { expect(display).to eq '[Y/n] ' }
161
163
  end
162
164
  context 'when using yesNo,' do
163
165
  Given(:options) { 'yesNo' }
@@ -165,7 +167,7 @@ describe Cliprompt::Optionset do
165
167
  When(:default) { set.default }
166
168
  When(:display) { set.display }
167
169
  Then { expect(default).to be_false }
168
- Then { expect(display).to eq '[y/N]' }
170
+ Then { expect(display).to eq '[y/N] ' }
169
171
  end
170
172
  context 'when using yN,' do
171
173
  Given(:options) { 'yesNo' }
@@ -173,7 +175,7 @@ describe Cliprompt::Optionset do
173
175
  When(:default) { set.default }
174
176
  When(:display) { set.display }
175
177
  Then { expect(default).to be_false }
176
- Then { expect(display).to eq '[y/N]' }
178
+ Then { expect(display).to eq '[y/N] ' }
177
179
  end
178
180
  context 'when using y/N,' do
179
181
  Given(:options) { 'yesNo' }
@@ -181,7 +183,7 @@ describe Cliprompt::Optionset do
181
183
  When(:default) { set.default }
182
184
  When(:display) { set.display }
183
185
  Then { expect(default).to be_false }
184
- Then { expect(display).to eq '[y/N]' }
186
+ Then { expect(display).to eq '[y/N] ' }
185
187
  end
186
188
  end
187
189
 
@@ -16,7 +16,7 @@ describe Cliprompt do
16
16
  When(:args) { }
17
17
  When { input.stub(:gets).and_return answer }
18
18
  Then { expect(subject.ask(question, args)).to eq answer }
19
- And { expect(output.string).to eq "#{question} " }
19
+ And { expect(output.string).to eq "#{question} " }
20
20
  end
21
21
  context 'with a default,' do
22
22
  When(:default) { 'ooo' }
@@ -29,19 +29,19 @@ describe Cliprompt do
29
29
  When(:args) { Cliprompt::Optionset.new() }
30
30
  When { input.stub(:gets).and_return answer }
31
31
  Then { expect(subject.ask(question, args)).to eq answer }
32
- And { expect(output.string).to eq "#{question} " }
32
+ And { expect(output.string).to eq "#{question} " }
33
33
  end
34
34
  context 'with choices requested as a list,' do
35
35
  When(:args) { { choices: ['aaa', 'bbb', 'ccc'], aslist: true } }
36
36
  When { input.stub(:gets).and_return '1' }
37
37
  Then { expect(subject.ask(question, args)).to eq 'bbb' }
38
- And { expect(output.string).to eq "#{question}\n 0 aaa\n 1 bbb\n 2 ccc\n#{Cliprompt::MSG_CHOSE_A_NUMBER} " }
38
+ And { expect(output.string).to eq "#{question} \n 0 aaa\n 1 bbb\n 2 ccc\n#{Cliprompt::MSG_CHOSE_A_NUMBER} " }
39
39
  end
40
40
  context 'with choices requested as a list with a default,' do
41
41
  When(:args) { { choices: ['=aaa', 'bbb', 'ccc'], aslist: true } }
42
42
  When { input.stub(:gets).and_return '' }
43
43
  Then { expect(subject.ask(question, args)).to eq 'aaa' }
44
- And { expect(output.string).to eq "#{question}\n> 0 aaa\n 1 bbb\n 2 ccc\n#{Cliprompt::MSG_CHOSE_A_NUMBER} " }
44
+ And { expect(output.string).to eq "#{question} \n> 0 aaa\n 1 bbb\n 2 ccc\n#{Cliprompt::MSG_CHOSE_A_NUMBER} [0] " }
45
45
  end
46
46
  context 'with choices requested as a non-list despite there are many choices,' do
47
47
  When(:args) { { choices: %w(aaa bbb ccc ddd eee fff), aslist: false } }
@@ -53,7 +53,7 @@ describe Cliprompt do
53
53
  When(:args) { %w(aaa bbb ccc ddd eee fff) }
54
54
  When { input.stub(:gets).and_return '5' }
55
55
  Then { expect(subject.ask(question, args)).to eq 'fff' }
56
- And { expect(output.string).to eq "#{question}\n 0 aaa\n 1 bbb\n 2 ccc\n 3 ddd\n 4 eee\n 5 fff\n#{Cliprompt::MSG_CHOSE_A_NUMBER} " }
56
+ And { expect(output.string).to eq "#{question} \n 0 aaa\n 1 bbb\n 2 ccc\n 3 ddd\n 4 eee\n 5 fff\n#{Cliprompt::MSG_CHOSE_A_NUMBER} " }
57
57
  end
58
58
  end
59
59
 
@@ -71,7 +71,7 @@ describe Cliprompt do
71
71
  When(:answer) { 'ooo' }
72
72
  When { input.stub(:gets).and_return answer }
73
73
  Then { expect(subject.guess(env_var, question, args)).to eq answer }
74
- And { expect(output.string).to eq "#{question} " }
74
+ And { expect(output.string).to eq "#{question} " }
75
75
  end
76
76
  end
77
77
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cliprompt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - mose
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-24 00:00:00.000000000 Z
11
+ date: 2014-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: paint