cliprompt 0.0.3 → 0.0.4

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: ad8694ae5f71787a7a5e51c1451f7f15ce16abe4
4
- data.tar.gz: 1e41e408ad7acf5c27c3e7e031e4ee142abd5c59
3
+ metadata.gz: 130c7d8782cb21bd861497572e1f70c561d3713a
4
+ data.tar.gz: a7e8e0ed439cb3efb295931f8149a2f602c6df3e
5
5
  SHA512:
6
- metadata.gz: 1f66243c356834abe4dbb25e165496453121fd536e16d9f4ed3127618b2566a2f3d88f8d2a8cba76d1361b4a5b5bd02bf0c88c46395983b5b2115e60c666727b
7
- data.tar.gz: 992a171945a441a158ebd2ec25863944a12620d8da79322838029b62c4fc065e46f0b34ca17320f62f4b206dc591ef64c717cbb49862d5d9de534d9ac4cd8c6a
6
+ metadata.gz: 7a33c9fc219191d65e7f33fba11437d87795dfbc91387c6389ef9e79408297df0537f64158be384524f5097eddca13e7e700752a76d6f28ba67f3dcac1b72706
7
+ data.tar.gz: 2bdb0c2ee17d1e8017ee6d7b28eff9b58b7b5e15ecc1308b7bf5d3fbf8cee15973a30366379ecaaf574bb136c7ced84c99baac769afbe89bdde6cb5f06f709c0
data/CHANGELOG.md CHANGED
@@ -1,19 +1,25 @@
1
1
  Cliprompt Changelog
2
2
  =====================
3
3
 
4
+ v0.0.4 - 2014-05-24
5
+ --------------------
6
+
7
+ - add an option `aslist` for we can decide if we want it as a list or not
8
+ - add a way to display big list, when choices are more than 5, make a list out of them
9
+
4
10
  v0.0.3 - 2014-05-20
5
- -------------
11
+ --------------------
6
12
 
7
13
  - relax the boolean check so 1 and 0 can be used for yes and no (especially for env vars)
8
14
  - added a `guess` method to have env var override question if set
9
15
 
10
16
  v0.0.2 - 2014-05-20
11
- -----------
17
+ --------------------
12
18
 
13
19
  - fix case when numbers are used in choices
14
20
  - added some test coverage for further evolution easiness
15
21
 
16
22
  v0.0.1 - 2014-05-19
17
- -------------
23
+ --------------------
18
24
 
19
25
  - first draft
data/README.md CHANGED
@@ -7,7 +7,7 @@ Cliprompt
7
7
  [![Dependency Status](https://gemnasium.com/mose/cliprompt.svg)](https://gemnasium.com/mose/cliprompt)
8
8
  [![Code Climate](https://codeclimate.com/github/mose/cliprompt.png)](https://codeclimate.com/github/mose/cliprompt)
9
9
 
10
- This library provides a simple DSL for managing user interaction in a CLI application. Still under development, not stable yet. Check the [Changelog](https://github.com/mose/cliprompt/blob/master/CHANGELOG.md) to see what is in already.
10
+ This library provides a simple DSL for managing user interaction in a CLI application. Still under development, but usable already. Check the [Changelog](https://github.com/mose/cliprompt/blob/master/CHANGELOG.md) to see what is in already.
11
11
 
12
12
  Features
13
13
  ----------
data/example.rb CHANGED
@@ -40,6 +40,15 @@ class Myclass
40
40
  showguess 'SOMEVAR', "what is your var?", [12, 'aa']
41
41
  end
42
42
 
43
+ def biglist
44
+ puts '-------------------'
45
+ puts 'a long list of choices'
46
+ show 'a list with default?', [22, 33, 44, '=55', 66, 77, 'something else']
47
+ show 'a list without default?', [22, 33, 44, '55', 66, 77, 'something else']
48
+ show 'a big list of choices but not in list?', choices: [22, 33, 44, '=55', 66, 77, 'something else'], aslist: false
49
+ show 'a small list of choices but we want it in list?', choices: [22, 33, '=44'], aslist: true
50
+ end
51
+
43
52
  def show(*args)
44
53
  it = ask *args
45
54
  puts "-- returned #{it.inspect}"
@@ -55,5 +64,6 @@ class Myclass
55
64
  end
56
65
 
57
66
  m = Myclass.new
58
- m.guessit
59
- m.askit
67
+ #m.guessit
68
+ #m.askit
69
+ m.biglist
@@ -5,13 +5,14 @@ module Cliprompt
5
5
 
6
6
  class Optionset
7
7
 
8
- attr_reader :choices, :default, :boolean, :envdefault
8
+ attr_reader :choices, :default, :boolean, :envdefault, :aslist
9
9
 
10
10
  def initialize(options = nil)
11
11
  @choices = []
12
12
  @default = nil
13
13
  @boolean = false
14
14
  @envdefault = nil
15
+ @aslist = false
15
16
  @type = options.class.name.downcase
16
17
  meth = "parse_#{@type}".to_sym
17
18
  if respond_to? meth
@@ -32,6 +33,11 @@ module Cliprompt
32
33
  else
33
34
  @default ||= args[:default] || args['default']
34
35
  end
36
+ if args[:aslist] == false || args['aslist'] == false
37
+ @aslist = false
38
+ elsif args[:aslist] == true || args['aslist'] == true
39
+ @aslist = true
40
+ end
35
41
  @boolean = args[:boolean] || args['boolean']
36
42
  @default = true if (@boolean && @default.nil?)
37
43
  @envdefault = args[:env] || args['env']
@@ -45,6 +51,7 @@ module Cliprompt
45
51
  a
46
52
  end
47
53
  end
54
+ @aslist = (@choices.count > 5)
48
55
  end
49
56
 
50
57
  def parse_fixnum(arg)
@@ -87,7 +94,11 @@ module Cliprompt
87
94
  elsif @boolean
88
95
  check_boolean question, answer
89
96
  elsif @choices.count > 0
90
- check_choices question, answer
97
+ if @aslist
98
+ check_list question, answer
99
+ else
100
+ check_choices question, answer
101
+ end
91
102
  else
92
103
  answer
93
104
  end
@@ -108,6 +119,11 @@ module Cliprompt
108
119
  answer
109
120
  end
110
121
 
122
+ def check_list(question, answer)
123
+ return ask_again(question, Cliprompt::MSG_CHOSE_IN_LIST) unless answer.to_i < @choices.count
124
+ @choices[answer.to_i]
125
+ end
126
+
111
127
  def ask_again(question, msg)
112
128
  Cliprompt.shout msg
113
129
  Cliprompt.ask question, self
@@ -1,3 +1,3 @@
1
1
  module Cliprompt
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/cliprompt.rb CHANGED
@@ -10,6 +10,7 @@ module Cliprompt
10
10
  MSG_MANDATORY_TEXT = "Sorry you need to fill that information."
11
11
  MSG_YES_OR_NO = "You need to answer by yes, no, y, n, 1 or 0."
12
12
  MSG_CHOSE_IN_LIST = "You need to chose between the available options."
13
+ MSG_CHOSE_A_NUMBER = "Choose a number:"
13
14
 
14
15
  def ask(question, *options)
15
16
  if options[0].class == Optionset
@@ -17,7 +18,19 @@ module Cliprompt
17
18
  else
18
19
  opts = Optionset.new *options
19
20
  end
20
- output.print "#{question} #{opts.display} "
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} "
33
+ end
21
34
  answer = input.gets.chomp
22
35
  output.flush
23
36
  opts.validate(question, answer)
@@ -31,6 +31,30 @@ describe Cliprompt do
31
31
  Then { expect(subject.ask(question, args)).to eq answer }
32
32
  And { expect(output.string).to eq "#{question} " }
33
33
  end
34
+ context 'with choices requested as a list,' do
35
+ When(:args) { { choices: ['aaa', 'bbb', 'ccc'], aslist: true } }
36
+ When { input.stub(:gets).and_return '1' }
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} " }
39
+ end
40
+ context 'with choices requested as a list with a default,' do
41
+ When(:args) { { choices: ['=aaa', 'bbb', 'ccc'], aslist: true } }
42
+ When { input.stub(:gets).and_return '' }
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} " }
45
+ end
46
+ context 'with choices requested as a non-list despite there are many choices,' do
47
+ When(:args) { { choices: %w(aaa bbb ccc ddd eee fff), aslist: false } }
48
+ When { input.stub(:gets).and_return 'bbb' }
49
+ Then { expect(subject.ask(question, args)).to eq 'bbb' }
50
+ And { expect(output.string).to eq "#{question} (aaa / bbb / ccc / ddd / eee / fff) " }
51
+ end
52
+ context 'with a big list of choices,' do
53
+ When(:args) { %w(aaa bbb ccc ddd eee fff) }
54
+ When { input.stub(:gets).and_return '5' }
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} " }
57
+ end
34
58
  end
35
59
 
36
60
  describe '.guess' do
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.3
4
+ version: 0.0.4
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-20 00:00:00.000000000 Z
11
+ date: 2014-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: paint