Class: Bogo::Ui
- Inherits:
-
Object
- Object
- Bogo::Ui
- Defined in:
- lib/bogo/ui.rb,
lib/bogo/ui/table.rb,
lib/bogo/ui/version.rb
Overview
CLI UI helper
Defined Under Namespace
Classes: ConfirmationDeclined, Table
Constant Summary collapse
- VERSION =
Current library version
Gem::Version.new('0.4.1')
Instance Attribute Summary collapse
- #application_name ⇒ String
- #auto_confirm ⇒ Truthy, Falsey
- #auto_default ⇒ Truthy, Falsey
- #colorize ⇒ Truthy, Falsey
-
#options ⇒ Smash
readonly
Options.
- #output_to ⇒ IO readonly
Instance Method Summary collapse
-
#ask(question, *args) ⇒ String
(also: #ask_question)
Prompt for question and receive answer.
-
#color(string, *args) ⇒ String
Colorize string.
-
#confirm(question) ⇒ Object
Confirm question.
-
#debug(string, *args) ⇒ String, NilClass
Format debug string and output only if debug is set.
-
#error(string, *args) ⇒ String
Format error string.
-
#fatal(string, *args) ⇒ String
Format fatal string.
-
#info(string, *args) ⇒ String
Output information string.
-
#initialize(args = {}) ⇒ self
constructor
Build new UI instance.
-
#print(string = '') ⇒ String
Output directly.
-
#puts(string = '') ⇒ String
Output directly.
-
#table(inst = nil, &block) ⇒ Table
Create a new table.
-
#verbose(string, *args) ⇒ String, NilClass
Output info if verbose flag is set.
-
#warn(string, *args) ⇒ String
Format warning string.
Constructor Details
#initialize(args = {}) ⇒ self
Build new UI instance
35 36 37 38 39 40 41 42 |
# File 'lib/bogo/ui.rb', line 35 def initialize(args={}) @application_name = args.fetch(:app_name, 'App') @colorize = args.fetch(:colors, true) @output_to = args.fetch(:output_to, $stdout) @auto_confirm = args.fetch(:auto_confirm, args.fetch(:yes, false)) @auto_default = args.fetch(:auto_default, args.fetch(:defaults, false)) = args.to_smash end |
Instance Attribute Details
#application_name ⇒ String
18 19 20 |
# File 'lib/bogo/ui.rb', line 18 def application_name @application_name end |
#auto_confirm ⇒ Truthy, Falsey
22 23 24 |
# File 'lib/bogo/ui.rb', line 22 def auto_confirm @auto_confirm end |
#auto_default ⇒ Truthy, Falsey
24 25 26 |
# File 'lib/bogo/ui.rb', line 24 def auto_default @auto_default end |
#colorize ⇒ Truthy, Falsey
16 17 18 |
# File 'lib/bogo/ui.rb', line 16 def colorize @colorize end |
#options ⇒ Smash (readonly)
Returns options.
26 27 28 |
# File 'lib/bogo/ui.rb', line 26 def end |
#output_to ⇒ IO (readonly)
20 21 22 |
# File 'lib/bogo/ui.rb', line 20 def output_to @output_to end |
Instance Method Details
#ask(question, *args) ⇒ String Also known as: ask_question
Prompt for question and receive answer
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/bogo/ui.rb', line 144 def ask(question, *args) opts = (args.detect{|x| x.is_a?(Hash)} || {}).to_smash default = args.detect{|x| x.is_a?(String)} || opts[:default] if(auto_default && default) default else valid = opts[:valid] string = question.dup if(default) default_string = !default.to_s.empty? && opts[:hide_default] ? '*****' : default string << " [#{default_string}]" end result = nil until(result) info "#{string}: ", :nonewline result = opts[:no_echo] ? $stdin.noecho(&:gets).strip : $stdin.gets.strip puts "\n" if opts[:no_echo] if(result.to_s.empty? && default) result = default.to_s end if(valid) case valid when Array result = nil unless valid.include?(result) when Regexp result = nil unless result =~ valid end end if(result.to_s.empty?) error 'Please provide a valid value' result = nil end end result end end |
#color(string, *args) ⇒ String
Colorize string
131 132 133 134 135 136 137 |
# File 'lib/bogo/ui.rb', line 131 def color(string, *args) if(colorize) Paint[string, *args] else string end end |
#confirm(question) ⇒ Object
Confirm question. Requires user to provide Y or N answer
188 189 190 191 192 193 |
# File 'lib/bogo/ui.rb', line 188 def confirm(question) unless(auto_confirm) result = ask("#{question} (Y/N)", :valid => /[YyNn]/).downcase raise ConfirmationDeclined.new 'Confirmation declined!' unless result == 'y' end end |
#debug(string, *args) ⇒ String, NilClass
Format debug string and output only if debug is set
118 119 120 121 122 123 124 |
# File 'lib/bogo/ui.rb', line 118 def debug(string, *args) if([:debug]) output_method = args.include?(:nonewline) ? :print : :puts self.send(output_method, "#{color('[DEBUG]:', :white, :bold)} #{string}") string end end |
#error(string, *args) ⇒ String
Format error string
87 88 89 90 91 |
# File 'lib/bogo/ui.rb', line 87 def error(string, *args) output_method = args.include?(:nonewline) ? :print : :puts self.send(output_method, "#{color('[ERROR]:', :red, :bold)} #{string}") string end |
#fatal(string, *args) ⇒ String
Format fatal string
97 98 99 100 101 |
# File 'lib/bogo/ui.rb', line 97 def fatal(string, *args) output_method = args.include?(:nonewline) ? :print : :puts self.send(output_method, "#{color('[FATAL]:', :red, :bold)} #{string}") string end |
#info(string, *args) ⇒ String
Output information string
66 67 68 69 70 71 |
# File 'lib/bogo/ui.rb', line 66 def info(string, *args) output_method = args.include?(:nonewline) ? :print : :puts o_color = args.include?(:verbose) ? :yellow : :green self.send(output_method, "#{color("[#{application_name}]:", o_color)} #{string}") string end |
#print(string = '') ⇒ String
Output directly
57 58 59 60 |
# File 'lib/bogo/ui.rb', line 57 def print(string='') output_to.print string string end |
#puts(string = '') ⇒ String
Output directly
48 49 50 51 |
# File 'lib/bogo/ui.rb', line 48 def puts(string='') output_to.puts string string end |
#table(inst = nil, &block) ⇒ Table
Create a new table
199 200 201 |
# File 'lib/bogo/ui.rb', line 199 def table(inst=nil, &block) Table.new(self, inst, &block) end |
#verbose(string, *args) ⇒ String, NilClass
Output info if verbose flag is set
107 108 109 110 111 112 |
# File 'lib/bogo/ui.rb', line 107 def verbose(string, *args) if([:verbose]) info(string, :verbose, *args) string end end |
#warn(string, *args) ⇒ String
Format warning string
77 78 79 80 81 |
# File 'lib/bogo/ui.rb', line 77 def warn(string, *args) output_method = args.include?(:nonewline) ? :print : :puts self.send(output_method, "#{color('[WARN]:', :yellow, :bold)} #{string}") string end |