inquirer.rb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f63e9bf358de3b79f5c60ccad3b272a6d7dc80d
4
- data.tar.gz: d09700b350451f94f2961a7d327c9b59b7fdcd42
3
+ metadata.gz: 9d0c59415e69c915f7f8e20f566a096e2218d5b9
4
+ data.tar.gz: c702cf48d146f65dd677b39b10eba81b176bc1f9
5
5
  SHA512:
6
- metadata.gz: 6c7de1dd06980bed7ccd599ad8554352a152b5b46959020211f04693f9fad08a8799d77c4c0191b9939a97b66f044aa40ddddd5bc28618bda15a68053968692d
7
- data.tar.gz: 9e10b66b41f0f913ba9594d98bca9b313875df9ca32061a2a8f4f4368d42b65f5588a40188712796bc8260f65696bffb11f522710208c050f131ee2f8f6e405f
6
+ metadata.gz: 27fef9a176a7c1d0f7359c0a4381831f70f8b552db4a8a7bba90d7af4f7f44435b9eb81560fdb415f69e5c9b20cee0c43cd9b9fd011c440e8306dba33772b16f
7
+ data.tar.gz: 82c9bdffd2f961f23a7b924cffc2e8311799a13db1dc1d64fde751ddea4d1d96de359bc91d42cf2bd94fc7e2117ce8944538b986c8d5f03ea74d08426eed7168
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Inquirer
1
+ # inquirer.rb
2
2
 
3
- A collection of common interactive command line user interfaces. A (not yet compleded) clone of the great [Inquirer.js](https://github.com/SBoudrias/Inquirer.js) and strongly inspired by the similar [inquirer.rb](https://github.com/arlimus/inquirer.rb).
3
+ A collection of common interactive command line user interfaces. A (not yet completed) clone of the great [Inquirer.js](https://github.com/SBoudrias/Inquirer.js) and strongly inspired by the similar [inquirer.rb](https://github.com/arlimus/inquirer.rb).
4
4
 
5
5
  Started as a fork of [inquirer.rb](https://github.com/arlimus/inquirer.rb) the changes become to fundamental to keep it that way.
6
6
 
@@ -16,7 +16,7 @@ Started as a fork of [inquirer.rb](https://github.com/arlimus/inquirer.rb) the c
16
16
  Add this line to your application's Gemfile:
17
17
 
18
18
  ```ruby
19
- gem 'inquirer'
19
+ gem 'inquirer.rb'
20
20
  ```
21
21
 
22
22
  And then execute:
@@ -25,7 +25,7 @@ And then execute:
25
25
 
26
26
  Or install it yourself as:
27
27
 
28
- $ gem install inquirer
28
+ $ gem install inquirer.rb
29
29
 
30
30
  ## Usage
31
31
 
@@ -85,6 +85,7 @@ There parameters are valid for all prompt types:
85
85
  - `:name` (String) The display value
86
86
  - `:short` (String) (optional) To display after selection
87
87
  - `:value` (String|Symbol) To save in the answers Hash
88
+ - `:when` (Proc|Boolean) (optional) Should return true or false depending on whether or not this question should be asked. The value can also be a simple boolean.
88
89
  - `:default` (Integer|String|Symbol) (optional) Must be the choice index (Integer) in the Array or a choice `:value` parameters value (String|Symbol).
89
90
 
90
91
  #### Checkbox (`type: :checkbox`)
@@ -96,6 +97,7 @@ There parameters are valid for all prompt types:
96
97
  - `:short` (String) (optional) To display after selection
97
98
  - `:value` (String|Symbol) To save in the answers Hash
98
99
  - `:checked` (Boolean) (optional) True choices will be checked by default
100
+ - `:when` (Proc|Boolean) (optional) Should return true or false depending on whether or not this question should be asked. The value can also be a simple boolean.
99
101
  - `:default` (Array) (optional) An Array of choices `:value` parameters values (String|Symbol).
100
102
  - `:validate` (Proc) (optional) Receive the user input and should return true if the value is valid, and an error message (String) otherwise. If false is returned, a default error message is provided.
101
103
 
@@ -143,6 +145,8 @@ There parameters are valid for all prompt types:
143
145
  - [x] `:choices` `:name` parameter (InquirerJS conform)
144
146
  - [x] `:choices` `:value` parameter (InquirerJS conform)
145
147
  - [x] `:choices` `:short` parameter (InquirerJS conform)
148
+ - [x] `:choices` `:when` (Proc) parameter
149
+ - [x] `:choices` `:when` (Boolean) parameter
146
150
  - [x] `:validate` (Proc) parameter (optional) (InquirerJS conform)
147
151
  - [ ] Tests
148
152
 
@@ -154,6 +158,8 @@ There parameters are valid for all prompt types:
154
158
  - [x] `:choices` `:name` parameter (InquirerJS conform)
155
159
  - [x] `:choices` `:value` parameter (InquirerJS conform)
156
160
  - [x] `:choices` `:checked` parameter (InquirerJS conform)
161
+ - [x] `:choices` `:when` (Proc) parameter
162
+ - [x] `:choices` `:when` (Boolean) parameter
157
163
  - [ ] `:choices` `:disabled` (String) parameter (InquirerJS conform)
158
164
  - [ ] `:choices` `:disabled` (Boolean) parameter (InquirerJS conform)
159
165
  - [ ] `:choices` `:disabled` (Proc) parameter (InquirerJS conform)
@@ -24,6 +24,14 @@ questions = [
24
24
  {
25
25
  name: 'Manfred',
26
26
  },
27
+ {
28
+ name: 'Holger',
29
+ when: lambda { |_args| false }
30
+ },
31
+ {
32
+ name: 'Tanja',
33
+ when: true
34
+ },
27
35
  ],
28
36
  },
29
37
  {
@@ -36,6 +36,14 @@ questions = [
36
36
  {
37
37
  name: 'Steve',
38
38
  },
39
+ {
40
+ name: 'Holger',
41
+ when: lambda { |_args| false }
42
+ },
43
+ {
44
+ name: 'Tanja',
45
+ when: true
46
+ },
39
47
  ],
40
48
  },
41
49
  {
@@ -43,6 +43,8 @@ module Inquirer
43
43
  )
44
44
 
45
45
  prompt = prompt[:manipulate].call( manipulate_parameter )
46
+
47
+ next if !prompt
46
48
  end
47
49
 
48
50
  type_parameter = prompt.merge(
@@ -18,6 +18,19 @@ module Checkbox
18
18
  @choices = []
19
19
  opts[:choices].each { |choice|
20
20
 
21
+ if choice[:when].is_a?(Proc)
22
+
23
+ when_parameter = opts.merge(
24
+ choice: choice,
25
+ )
26
+
27
+ ask_choice = choice[:when].call( when_parameter )
28
+
29
+ next if !ask_choice
30
+ elsif [true, false].include? choice[:when]
31
+ next if !choice[:when]
32
+ end
33
+
21
34
  choice[:value] ||= choice[:name]
22
35
 
23
36
  if !choice[:checked] && opts[:default].is_a?(Array)
@@ -17,6 +17,19 @@ module List
17
17
  @choices = []
18
18
  opts[:choices].each { |choice|
19
19
 
20
+ if choice[:when].is_a?(Proc)
21
+
22
+ when_parameter = opts.merge(
23
+ choice: choice,
24
+ )
25
+
26
+ ask_choice = choice[:when].call( when_parameter )
27
+
28
+ next if !ask_choice
29
+ elsif [true, false].include? choice[:when]
30
+ next if !choice[:when]
31
+ end
32
+
20
33
  choice[:value] ||= choice[:name]
21
34
 
22
35
  @choices.push(choice)
@@ -24,6 +37,8 @@ module List
24
37
 
25
38
  return nil if Array(@choices).empty?
26
39
 
40
+ return @choices[0][:value] if @choices.size == 1
41
+
27
42
  if default.is_a?(String) || default.is_a?(Symbol)
28
43
  @position = @choices.find_index { |choice| choice[:value] == default }
29
44
  @position ||= 0
@@ -65,10 +65,17 @@ module IOHelper
65
65
  print IOChar.newline * lines + @bottomline + IOChar.cursor_left * @bottomline.size + IOChar.cursor_up * lines
66
66
  end
67
67
  print @rendered
68
+
69
+ plain_last_line = plain_chars(@rendered.lines.last)
70
+ print IOChar.newline + IOChar.cursor_up + IOChar.cursor_right * plain_last_line.length
68
71
  end
69
72
 
70
73
  # clear the console based on the last text rendered
71
74
  def clear
75
+
76
+ # remove the trailing newline, otherwise an upper line will get eaten
77
+ @rendered.sub!(/\n\z/, '')
78
+
72
79
  # determine how many lines to move up
73
80
  lines = @rendered.scan(/\n/).length
74
81
 
@@ -120,4 +127,8 @@ module IOHelper
120
127
 
121
128
  result + IOChar.clear_line
122
129
  end
130
+
131
+ def plain_chars(string)
132
+ string.gsub(/\e\[([;\dA-Z]+)?m?/, '')
133
+ end
123
134
  end
@@ -1,3 +1,3 @@
1
1
  module Inquirer
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inquirer.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thorsten Eckel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-16 00:00:00.000000000 Z
11
+ date: 2016-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler