bogo-ui 0.1.4 → 0.1.6

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: d1d767eb2099b0f4ac0cbc40e21c2ea0bcd83814
4
- data.tar.gz: 1dfa7f6d9850e33fc991a96a7337a105104d9ef8
3
+ metadata.gz: 6c2b6c91279292d5f7b9eedf22c7f45e09311cf3
4
+ data.tar.gz: e66d24c3cace23c0bd65d3bc1761cd611d51e409
5
5
  SHA512:
6
- metadata.gz: f25fa2fcf0718ed7fa21f8b54d9481527a9f3ac583720fc4be5890f9e11d0eb3b2f58ccfade8324624b34268bcf8e4f085b9d8a25670c283b068ecff23fe648b
7
- data.tar.gz: 14567b0044081fdac3ab67c864e990287ccf4b06c0ea0aaefe03fd381819ef6f3e25042c95943bc702b850a05b8cc1d1d87f10edd06fc9d133b0ed94d024ac3b
6
+ metadata.gz: 16522be239637ea35788146691c56bdbdf67800f06e2affebf11218b903303552bbd6524e5ecdafc135b7ce69a93735ec1a3b24209c793f53cbb915813f4d178
7
+ data.tar.gz: e62846b1f7692904b970591f5ccb8a9db290243e426733054cc9a9b5d85a15b8b081da55a6fbf68ff6c3bc56f94eca390e6660afcb5986c16f60fbf0e40a24fa
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## v0.1.6
2
+ * Include `Ui#fatal` output method
3
+ * Add helper into `Ui` directly for building new tables
4
+ * Provide optional validation for answers provided to `Ui#question`
5
+ * Add `Ui#confirm` method
6
+ * Allow optional support for auto confirm and auto default
7
+
1
8
  ## v0.1.4
2
9
  * Add compat alias `#ask_question` -> `#ask`
3
10
  * Allow passing instance to `Ui::Table` to proxy method calls
data/lib/bogo-ui/table.rb CHANGED
@@ -84,8 +84,8 @@ module Bogo
84
84
  @table.buffer.rewind
85
85
  output = @table.buffer.read.split("\n")
86
86
  output.slice!(0, @printed_lines)
87
- @printed_lines = output.size
88
- ui.puts output.join("\n")
87
+ @printed_lines += output.size
88
+ ui.puts output.join("\n") unless output.empty?
89
89
  self
90
90
  end
91
91
 
data/lib/bogo-ui/ui.rb CHANGED
@@ -11,6 +11,12 @@ module Bogo
11
11
  attr_accessor :colorize
12
12
  # @return [String]
13
13
  attr_accessor :application_name
14
+ # @return [IO]
15
+ attr_reader :output_to
16
+ # @return [Truthy, Falsey]
17
+ attr_reader :auto_confirm
18
+ # @return [Truthy, Falsey]
19
+ attr_reader :auto_default
14
20
 
15
21
  # Build new UI instance
16
22
  #
@@ -20,9 +26,15 @@ module Bogo
20
26
  # @option args [IO] :output_to IO to write
21
27
  # @return [self]
22
28
  def initialize(args={})
23
- @application_name = args.fetch(:app_name)
29
+ @application_name = args.fetch(:app_name, 'App')
24
30
  @colorize = args.fetch(:colors, true)
25
31
  @output_to = args.fetch(:output_to, $stdout)
32
+ @auto_confirm = args.fetch(:auto_confirm,
33
+ args.fetch(:yes, false)
34
+ )
35
+ @auto_default = args.fetch(:auto_default,
36
+ args.fetch(:defaults, false)
37
+ )
26
38
  end
27
39
 
28
40
  # Output directly
@@ -30,7 +42,7 @@ module Bogo
30
42
  # @param string [String]
31
43
  # @return [String]
32
44
  def puts(string='')
33
- @output_to.puts string
45
+ output_to.puts string
34
46
  string
35
47
  end
36
48
 
@@ -39,7 +51,7 @@ module Bogo
39
51
  # @param string [String]
40
52
  # @return [String]
41
53
  def print(string='')
42
- @output_to.print string
54
+ output_to.print string
43
55
  string
44
56
  end
45
57
 
@@ -73,6 +85,16 @@ module Bogo
73
85
  string
74
86
  end
75
87
 
88
+ # Format fatal string
89
+ #
90
+ # @param string [String]
91
+ # @return [String]
92
+ def fatal(string, *args)
93
+ output_method = args.include?(:nonewline) ? :print : :puts
94
+ self.send(output_method, "#{color('[FATAL]:', :red, :bold)} #{string}")
95
+ string
96
+ end
97
+
76
98
  # Colorize string
77
99
  #
78
100
  # @param string [String]
@@ -91,26 +113,59 @@ module Bogo
91
113
  # @param question [String]
92
114
  # @param default [String]
93
115
  # @return [String]
94
- def ask(question, default=nil)
95
- string = question.dup
96
- if(default)
97
- string << " [#{default}]"
98
- end
99
- result = nil
100
- until(result)
101
- info "#{string}: ", :nonewline
102
- result = $stdin.gets.strip
103
- if(result.empty? && default)
104
- result = default
116
+ def ask(question, *args)
117
+ opts = (args.detect{|x| x.is_a?(Hash)} || {}).to_smash
118
+ default = args.detect{|x| x.is_a?(String)} || opts[:default]
119
+ if(auto_default && default)
120
+ default
121
+ else
122
+ valid = opts[:valid]
123
+ string = question.dup
124
+ if(default)
125
+ string << " [#{default}]"
105
126
  end
106
- if(result.empty?)
107
- error 'Please provide a value'
108
- result = nil
127
+ result = nil
128
+ until(result)
129
+ info "#{string}: ", :nonewline
130
+ result = $stdin.gets.strip
131
+ if(result.empty? && default)
132
+ result = default
133
+ end
134
+ if(valid)
135
+ case valid
136
+ when Array
137
+ result = nil unless valid.include?(result)
138
+ when Regexp
139
+ result = nil unless result =~ valid
140
+ end
141
+ end
142
+ if(result.empty?)
143
+ error 'Please provide a valid value'
144
+ result = nil
145
+ end
109
146
  end
147
+ result
110
148
  end
111
- result
112
149
  end
113
150
  alias_method :ask_question, :ask
114
151
 
152
+ # Confirm question. Requires user to provide Y or N answer
153
+ #
154
+ # @param question [String]
155
+ def confirm(question)
156
+ unless(auto_confirm)
157
+ result = ask("#{question} (Y/N)", :valid => /[YyNn]/).downcase
158
+ raise 'Confirmation declined!' unless result == 'y'
159
+ end
160
+ end
161
+
162
+ # Create a new table
163
+ #
164
+ # @param inst [Object] instance to attach table (for method call proxy)
165
+ # @return [Table]
166
+ def table(inst=nil, &block)
167
+ Table.new(self, inst, &block)
168
+ end
169
+
115
170
  end
116
171
  end
@@ -1,6 +1,6 @@
1
1
  module Bogo
2
2
  class Ui
3
3
  # Current library version
4
- VERSION = Gem::Version.new('0.1.4')
4
+ VERSION = Gem::Version.new('0.1.6')
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bogo-ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Roberts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-30 00:00:00.000000000 Z
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bogo