console-glitter 0.1.4 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: baf523bf1aeebc54f79df59d20d85caf0562e5a3
4
- data.tar.gz: 519b75adaec42c3ac17b6d51a48998e50944a8cb
3
+ metadata.gz: 96c51391b27aac84317c5e5cb026fb15a7ffc980
4
+ data.tar.gz: 462d17ab7b25eb8ccd3dd478f9492bf22e77b9f8
5
5
  SHA512:
6
- metadata.gz: b95ffb8053587a36bfb8f7924beafe4b6684ace0886ad7e42e712d45cfc4a056ebb79c6c87f8fa2e9c8bdc42d0504d50ef664b713942251baf038fd73dbc0cb8
7
- data.tar.gz: 8b7e398bf8389fecf839b885d67fb13128abb153a6a4f7afb298d542c3b67808ab17f5cd43f80bc217fc59a0b00b172915b9dba48906c20ac3d60d736c94ffb9
6
+ metadata.gz: 37b65808e6c64222d1b7007be17159e5b951f5f2be40be24d799b5c94d9ab2c79eec145500fa68cf85f723e135833aee9ccd1a8445a1af3bbc04888e217d1e8b
7
+ data.tar.gz: ebec9efc7368e6f5edffaf742f867876b08e11e10fcf42bbf7774e2c43eb7baed8193ed907529ebcfe04b5e5ad87ffce8c130f4859bed20c402bc45fad47e8aa
@@ -1,5 +1,6 @@
1
1
  require 'console-glitter/version'
2
2
  require 'console-glitter/ansi'
3
+ require 'readline'
3
4
  require 'io/console'
4
5
 
5
6
  module ConsoleGlitter
@@ -23,20 +24,23 @@ module ConsoleGlitter
23
24
  # specified above). Valid responses may be
24
25
  # any class with a match method such as
25
26
  # Strings or Regexps.
27
+ # wordlist - Array of words to be used for input auto-completion.
28
+ # (default: [])
29
+ # block - Lambda which will override the default autocompletion lambda
30
+ # as defined in autocomplete_lambda if present. (default: nil)
26
31
  #
27
32
  # Returns a String containing the answer provided by the user.
28
- def prompt(question, options = {})
33
+ def prompt(question, options = {}, wordlist = [], block = nil)
29
34
  default = options[:default_answer].to_s
30
35
  allow_empty = options[:allow_empty]
31
36
  valid = regexify_answers(options[:valid_answers])
32
37
 
33
38
  default_display = " [#{default.strip}]" unless default.empty?
34
- question.strip!
39
+ question = "#{question.strip}#{default_display}> "
35
40
 
36
41
  answer = nil
37
42
  while answer.nil?
38
- print "#{question}#{default_display}> "
39
- answer = $stdin.readline.strip
43
+ answer = user_prompt(question, wordlist, block)
40
44
  answer = default if answer.empty?
41
45
 
42
46
  if answer.empty?
@@ -89,6 +93,36 @@ module ConsoleGlitter
89
93
  puts
90
94
  end
91
95
 
96
+ # Public: Wrap Console#prompt, specifically targeting filesystem paths.
97
+ #
98
+ # question - Query to be presented to the user.
99
+ # options - Hash containing arguments defining acceptable responses.
100
+ # (default: {}):
101
+ # :default_answer - String containing the default answer. If
102
+ # this is nil, a non-empty answer MUST be
103
+ # given.
104
+ # :allow_empty - Whether or not to allow empty responses.
105
+ # Unless explicitly allowed, empty answers
106
+ # will be rejected.
107
+ # :valid_answers - An Array containing all valid responses. If
108
+ # this is empty, any answer will be accepted
109
+ # (unless empty answers are disallowed as
110
+ # specified above). Valid responses may be
111
+ # any class with a match method such as
112
+ # Strings or Regexps.
113
+ #
114
+ # Returns a String containing the answer provided by the user.
115
+ def filesystem_prompt(question, args = {})
116
+ fs_lambda = lambda { |d| Dir[d + '*'].grep(/^#{Regexp.escape(d)}/) }
117
+ old_append = Readline.completion_append_character
118
+
119
+ Readline.completion_append_character = ''
120
+ response = prompt(question, args, [], fs_lambda)
121
+ Readline.completion_append_character = old_append
122
+
123
+ response
124
+ end
125
+
92
126
  # Public: Render a "spinner" on the command line and yield to a block,
93
127
  # reporting success if nothing is raised, or else reporting failure.
94
128
  #
@@ -176,6 +210,39 @@ module ConsoleGlitter
176
210
 
177
211
  private
178
212
 
213
+ # Internal: Promp user for input via Readline, handling automatic setting
214
+ # and restoring of Readline's autocompletion proc, avoiding trampling over
215
+ # other uses.
216
+ #
217
+ # question - String containing a question to be displayed to the user.
218
+ # wordlist - Array containing Strings which are considered valid answers
219
+ # for autocompletion.
220
+ # block - Lambda which will override the default autocompletion lambda
221
+ # as defined in autocomplete_lambda if present. (default: nil)
222
+ #
223
+ # Returns a String.
224
+ def user_prompt(question, wordlist, block = nil)
225
+ block = autocomplete_lambda(wordlist) if block.nil?
226
+ old_completion_proc = Readline.completion_proc
227
+
228
+ Readline.completion_proc = block
229
+ response = Readline.readline(question)
230
+ Readline.completion_proc = old_completion_proc
231
+
232
+ response.to_s
233
+ end
234
+
235
+ # Internal: Generate a lambda which retuns an array of autocompletion
236
+ # candidates.
237
+ #
238
+ # wordlist - Array containing Strings which are considered valid answers
239
+ # for autocompletion.
240
+ #
241
+ # Returns a lambda which returns an Array.
242
+ def autocomplete_lambda(wordlist)
243
+ lambda { |s| wordlist.grep(/^#{Regexp.escape(s)}/) }
244
+ end
245
+
179
246
  # Internal: Generate Regexps for any String in an array to be used in order
180
247
  # to match against user input. Strings are expected to indicate exact
181
248
  # matches desired.
@@ -1,3 +1,3 @@
1
1
  module ConsoleGlitter
2
- VERSION = '0.1.4'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console-glitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wuest
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-19 00:00:00.000000000 Z
11
+ date: 2014-05-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Tools for building nice looking CLI applications
14
14
  email: chris@chriswuest.com
@@ -19,7 +19,7 @@ files:
19
19
  - lib/console-glitter.rb
20
20
  - lib/console-glitter/ansi.rb
21
21
  - lib/console-glitter/version.rb
22
- homepage: http://github.com/cwuest/console-glitter
22
+ homepage: https://gitlab.com/wuest/console-glitter
23
23
  licenses:
24
24
  - MIT
25
25
  metadata: {}
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
39
  version: '0'
40
40
  requirements: []
41
41
  rubyforge_project:
42
- rubygems_version: 2.1.11
42
+ rubygems_version: 2.2.2
43
43
  signing_key:
44
44
  specification_version: 4
45
45
  summary: Tools for prettier CLI apps