inquirer.rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,63 @@
1
+ lib = File.expand_path('../../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'inquirer'
5
+
6
+ questions = [
7
+ {
8
+ name: :are_you_sure_type,
9
+ type: :confirm,
10
+ message: 'Are you sure?',
11
+ },
12
+ {
13
+ name: :are_you_sure_default,
14
+ type: :confirm,
15
+ message: 'Are you sure?',
16
+ default: true,
17
+ },
18
+ {
19
+ name: :are_you_sure_never,
20
+ type: :confirm,
21
+ message: 'Are you sure?',
22
+ when: false,
23
+ },
24
+ {
25
+ name: :are_you_sure_never_lambda,
26
+ type: :confirm,
27
+ message: 'Are you sure?',
28
+ when: lambda { |answers| true },
29
+ },
30
+ {
31
+ name: :are_you_sure_when_previous,
32
+ type: :confirm,
33
+ message: 'Are you sure?',
34
+ when: lambda { |answers| answers[:are_you_sure_default] },
35
+ },
36
+ {
37
+ name: :are_you_sure_validate_true,
38
+ type: :confirm,
39
+ message: 'Are you sure?',
40
+ validate: lambda { |answer| answer },
41
+ },
42
+ {
43
+ name: :are_you_sure_validate_true_message,
44
+ type: :confirm,
45
+ message: 'Are you sure?',
46
+ validate: lambda { |answer|
47
+ return true if answer
48
+ 'Only yes!'
49
+ },
50
+ },
51
+ {
52
+ name: :are_you_sure_filter_tyler,
53
+ type: :confirm,
54
+ message: 'Are you sure?',
55
+ filter: lambda { |answer|
56
+ 'Tyler'
57
+ },
58
+ },
59
+ ]
60
+
61
+ answers = Inquirer.prompt(questions)
62
+
63
+ p answers
@@ -0,0 +1,67 @@
1
+ lib = File.expand_path('../../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'inquirer'
5
+
6
+ questions = [
7
+ {
8
+ name: :user_name_no_type,
9
+ message: 'What is your name?',
10
+ },
11
+ {
12
+ name: :user_name_type,
13
+ type: :input,
14
+ message: 'What is your name?',
15
+ },
16
+ {
17
+ name: :user_name_default_tyler,
18
+ type: :input,
19
+ message: 'What is your name?',
20
+ default: 'Tyler',
21
+ },
22
+ {
23
+ name: :user_name_never,
24
+ type: :input,
25
+ message: 'What is your name?',
26
+ when: false,
27
+ },
28
+ {
29
+ name: :user_name_never_lambda,
30
+ type: :input,
31
+ message: 'What is your name?',
32
+ when: lambda { |answers| true },
33
+ },
34
+ {
35
+ name: :user_name_when_tyler,
36
+ type: :input,
37
+ message: 'What is your name?',
38
+ when: lambda { |answers| answers[:user_name_default] == 'Tyler' },
39
+ },
40
+ {
41
+ name: :user_name_validate_tyler,
42
+ type: :input,
43
+ message: 'What is your name?',
44
+ validate: lambda { |answer| answer == 'Tyler' },
45
+ },
46
+ {
47
+ name: :user_name_validate_tyler_message,
48
+ type: :input,
49
+ message: 'What is your name?',
50
+ validate: lambda { |answer|
51
+ return true if answer == 'Tyler'
52
+ 'Only Tyler!'
53
+ },
54
+ },
55
+ {
56
+ name: :user_name_filter_tyler,
57
+ type: :input,
58
+ message: 'What is your name?',
59
+ filter: lambda { |answer|
60
+ 'Tyler'
61
+ },
62
+ },
63
+ ]
64
+
65
+ answers = Inquirer.prompt(questions)
66
+
67
+ p answers
@@ -0,0 +1,185 @@
1
+ lib = File.expand_path('../../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'inquirer'
5
+
6
+ questions = [
7
+ {
8
+ name: :whos_best,
9
+ type: :list,
10
+ message: "Who's the best?",
11
+ choices: [
12
+ {
13
+ name: 'Karl',
14
+ short: 'Kalli',
15
+ },
16
+ {
17
+ name: 'Bert',
18
+ value: "It's Bert!"
19
+ },
20
+ {
21
+ name: 'Berta',
22
+ value: :love
23
+ },
24
+ {
25
+ name: 'Manfred',
26
+ },
27
+ {
28
+ name: 'Tyler',
29
+ },
30
+ {
31
+ name: 'Rust',
32
+ },
33
+ {
34
+ name: 'Marty',
35
+ },
36
+ {
37
+ name: 'Steve',
38
+ },
39
+ ],
40
+ },
41
+ {
42
+ name: :whos_best_default_value,
43
+ type: :list,
44
+ message: "Who's the best?",
45
+ choices: [
46
+ {
47
+ name: 'Karl',
48
+ short: 'Kalli',
49
+ },
50
+ {
51
+ name: 'Bert',
52
+ value: "It's Bert!"
53
+ },
54
+ {
55
+ name: 'Berta',
56
+ value: :love
57
+ },
58
+ {
59
+ name: 'Manfred',
60
+ },
61
+ ],
62
+ default: :love,
63
+ },
64
+ {
65
+ name: :whos_best_default_index,
66
+ type: :list,
67
+ message: "Who's the best?",
68
+ choices: [
69
+ {
70
+ name: 'Karl',
71
+ short: 'Kalli',
72
+ },
73
+ {
74
+ name: 'Bert',
75
+ value: "It's Bert!"
76
+ },
77
+ {
78
+ name: 'Berta',
79
+ value: :love
80
+ },
81
+ {
82
+ name: 'Manfred',
83
+ },
84
+ ],
85
+ default: 2,
86
+ },
87
+ {
88
+ name: :whos_best_never,
89
+ type: :list,
90
+ message: "Who's the best?",
91
+ choices: [
92
+ {
93
+ name: 'Karl',
94
+ short: 'Kalli',
95
+ },
96
+ {
97
+ name: 'Bert',
98
+ value: "It's Bert!"
99
+ },
100
+ {
101
+ name: 'Berta',
102
+ value: :love
103
+ },
104
+ {
105
+ name: 'Manfred',
106
+ },
107
+ ],
108
+ when: false,
109
+ },
110
+ {
111
+ name: :whos_best_never_lambda,
112
+ type: :list,
113
+ message: "Who's the best?",
114
+ choices: [
115
+ {
116
+ name: 'Karl',
117
+ short: 'Kalli',
118
+ },
119
+ {
120
+ name: 'Bert',
121
+ value: "It's Bert!"
122
+ },
123
+ {
124
+ name: 'Berta',
125
+ value: :love
126
+ },
127
+ {
128
+ name: 'Manfred',
129
+ },
130
+ ],
131
+ when: lambda { |answers| true },
132
+ },
133
+ {
134
+ name: :whos_best_when_previous,
135
+ type: :list,
136
+ message: "Who's the best?",
137
+ choices: [
138
+ {
139
+ name: 'Karl',
140
+ short: 'Kalli',
141
+ },
142
+ {
143
+ name: 'Bert',
144
+ value: "It's Bert!"
145
+ },
146
+ {
147
+ name: 'Berta',
148
+ value: :love
149
+ },
150
+ {
151
+ name: 'Manfred',
152
+ },
153
+ ],
154
+ when: lambda { |answers| answers[:whos_best_default_index].size > 1 },
155
+ },
156
+ {
157
+ name: :whos_best_filter_tyler,
158
+ type: :list,
159
+ message: "Who's the best?",
160
+ choices: [
161
+ {
162
+ name: 'Karl',
163
+ short: 'Kalli',
164
+ },
165
+ {
166
+ name: 'Bert',
167
+ value: "It's Bert!"
168
+ },
169
+ {
170
+ name: 'Berta',
171
+ value: :love
172
+ },
173
+ {
174
+ name: 'Manfred',
175
+ },
176
+ ],
177
+ filter: lambda { |answer|
178
+ ['Tyler']
179
+ },
180
+ },
181
+ ]
182
+
183
+ answers = Inquirer.prompt(questions)
184
+
185
+ p answers
@@ -0,0 +1,63 @@
1
+ lib = File.expand_path('../../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'inquirer'
5
+
6
+ questions = [
7
+ {
8
+ name: :password,
9
+ type: :password,
10
+ message: 'What should be your password?',
11
+ },
12
+ {
13
+ name: :password_default,
14
+ type: :password,
15
+ message: 'What should be your password?',
16
+ default: 'Password',
17
+ },
18
+ {
19
+ name: :password_never,
20
+ type: :password,
21
+ message: 'What should be your password?',
22
+ when: false,
23
+ },
24
+ {
25
+ name: :password_never_lambda,
26
+ type: :password,
27
+ message: 'What should be your password?',
28
+ when: lambda { |answers| true },
29
+ },
30
+ {
31
+ name: :password_default_password,
32
+ type: :password,
33
+ message: 'What should be your password?',
34
+ when: lambda { |answers| answers[:password_default] == 'Password' },
35
+ },
36
+ {
37
+ name: :password_validate_password,
38
+ type: :password,
39
+ message: 'What should be your password?',
40
+ validate: lambda { |answer| answer == 'Password' },
41
+ },
42
+ {
43
+ name: :password_validate_password_message,
44
+ type: :password,
45
+ message: 'What should be your password?',
46
+ validate: lambda { |answer|
47
+ return true if answer == 'Password'
48
+ 'Only Password!'
49
+ },
50
+ },
51
+ {
52
+ name: :password_filter_password,
53
+ type: :password,
54
+ message: 'What should be your password?',
55
+ filter: lambda { |answer|
56
+ 'Password'
57
+ },
58
+ },
59
+ ]
60
+
61
+ answers = Inquirer.prompt(questions)
62
+
63
+ p answers
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'inquirer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'inquirer.rb'
8
+ spec.version = Inquirer::VERSION
9
+ spec.authors = ['Thorsten Eckel']
10
+ spec.email = ['te@znuny.com']
11
+
12
+ spec.summary = 'A collection of common interactive command line user interfaces.'
13
+ spec.description = '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).'
14
+ spec.homepage = 'https://github.com/thorsteneckel/inquirer'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.11'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+
23
+ if( RUBY_VERSION.start_with? '1.' )
24
+ spec.add_dependency 'term-ansicolor', '~> 1.2.2'
25
+ else
26
+ spec.add_dependency 'term-ansicolor', '>= 1.3'
27
+ end
28
+
29
+ if( RUBY_ENGINE == 'rbx' )
30
+ spec.add_dependency 'rubysl-mutex_m'
31
+ spec.add_dependency 'rubysl-singleton'
32
+ spec.add_dependency 'rubysl-io-console'
33
+ end
34
+ end
@@ -0,0 +1,86 @@
1
+ require 'inquirer/version'
2
+ require 'inquirer/utils/iohelper'
3
+ require 'inquirer/prompts/checkbox'
4
+ require 'inquirer/prompts/confirm'
5
+ require 'inquirer/prompts/input'
6
+ require 'inquirer/prompts/list'
7
+ require 'inquirer/prompts/password'
8
+
9
+ module Inquirer
10
+
11
+ extend self
12
+
13
+ def prompt(prompts)
14
+
15
+ answers = {}
16
+ prompts.each { |prompt|
17
+
18
+ # TODO: maybe use prompt.fetch(:type, :input) ???
19
+ prompt[:type] ||= :input
20
+
21
+ prompt_answers = []
22
+
23
+ if prompt[:when].is_a?(Proc)
24
+
25
+ ask_prompt = prompt[:when].call( answers )
26
+
27
+ next if !ask_prompt
28
+ elsif [true, false].include? prompt[:when]
29
+ next if !prompt[:when]
30
+ end
31
+
32
+ if !prompt[:repeat].is_a?(Integer) and ![true, false].include? prompt[:repeat] # Boolean check :(
33
+ prompt[:repeat] = 1
34
+ end
35
+
36
+ repeat_prompt = lambda { |repeat_counter|
37
+
38
+ if prompt[:manipulate] && prompt[:manipulate].is_a?(Proc)
39
+
40
+ manipulate_parameter = prompt.merge(
41
+ answers: answers,
42
+ repeat_counter: repeat_counter,
43
+ )
44
+
45
+ prompt = prompt[:manipulate].call( manipulate_parameter )
46
+ end
47
+
48
+ type_parameter = prompt.merge(
49
+ answers: answers,
50
+ repeat_counter: repeat_counter,
51
+ )
52
+
53
+ object = Kernel.const_get(prompt[:type].to_s.capitalize)
54
+ answer = object.prompt(type_parameter)
55
+
56
+ if prompt[:filter] && prompt[:filter].is_a?(Proc)
57
+ answer = prompt[:filter].call( answer )
58
+ end
59
+
60
+ prompt_answers.push answer
61
+ }
62
+
63
+ if prompt[:repeat].is_a?(Integer)
64
+ (1..prompt[:repeat]).each(&repeat_prompt)
65
+ else
66
+
67
+ begin
68
+ repeat_counter = 1
69
+ loop {
70
+ repeat_prompt.call(repeat_counter)
71
+ repeat_counter += 1
72
+ }
73
+ rescue Interrupt
74
+ end
75
+ end
76
+
77
+ if prompt[:repeat] == 1
78
+ answers[ prompt[:name] ] = prompt_answers.first
79
+ else
80
+ answers[ prompt[:name] ] = prompt_answers
81
+ end
82
+ }
83
+
84
+ answers
85
+ end
86
+ end