Class: Bogo::Ui
- Inherits:
-
Object
- Object
- Bogo::Ui
- Defined in:
- lib/bogo-ui/ui.rb,
lib/bogo-ui/table.rb,
lib/bogo-ui/version.rb
Overview
CLI UI helper
Defined Under Namespace
Classes: Table
Constant Summary collapse
- VERSION =
Current library version
Gem::Version.new('0.1.24')
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
30 31 32 33 34 35 36 37 |
# File 'lib/bogo-ui/ui.rb', line 30 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
13 14 15 |
# File 'lib/bogo-ui/ui.rb', line 13 def application_name @application_name end |
#auto_confirm ⇒ Truthy, Falsey
17 18 19 |
# File 'lib/bogo-ui/ui.rb', line 17 def auto_confirm @auto_confirm end |
#auto_default ⇒ Truthy, Falsey
19 20 21 |
# File 'lib/bogo-ui/ui.rb', line 19 def auto_default @auto_default end |
#colorize ⇒ Truthy, Falsey
11 12 13 |
# File 'lib/bogo-ui/ui.rb', line 11 def colorize @colorize end |
#options ⇒ Smash (readonly)
Returns options.
21 22 23 |
# File 'lib/bogo-ui/ui.rb', line 21 def end |
#output_to ⇒ IO (readonly)
15 16 17 |
# File 'lib/bogo-ui/ui.rb', line 15 def output_to @output_to end |
Instance Method Details
#ask(question, *args) ⇒ String Also known as: ask_question
Prompt for question and receive answer
139 140 141 142 143 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 |
# File 'lib/bogo-ui/ui.rb', line 139 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
126 127 128 129 130 131 132 |
# File 'lib/bogo-ui/ui.rb', line 126 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
183 184 185 186 187 188 |
# File 'lib/bogo-ui/ui.rb', line 183 def confirm(question) unless(auto_confirm) result = ask("#{question} (Y/N)", :valid => /[YyNn]/).downcase raise 'Confirmation declined!' unless result == 'y' end end |
#debug(string, *args) ⇒ String, NilClass
Format debug string and output only if debug is set
113 114 115 116 117 118 119 |
# File 'lib/bogo-ui/ui.rb', line 113 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
82 83 84 85 86 |
# File 'lib/bogo-ui/ui.rb', line 82 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
92 93 94 95 96 |
# File 'lib/bogo-ui/ui.rb', line 92 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
61 62 63 64 65 66 |
# File 'lib/bogo-ui/ui.rb', line 61 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
52 53 54 55 |
# File 'lib/bogo-ui/ui.rb', line 52 def print(string='') output_to.print string string end |
#puts(string = '') ⇒ String
Output directly
43 44 45 46 |
# File 'lib/bogo-ui/ui.rb', line 43 def puts(string='') output_to.puts string string end |
#table(inst = nil, &block) ⇒ Table
Create a new table
194 195 196 |
# File 'lib/bogo-ui/ui.rb', line 194 def table(inst=nil, &block) Table.new(self, inst, &block) end |
#verbose(string, *args) ⇒ String, NilClass
Output info if verbose flag is set
102 103 104 105 106 107 |
# File 'lib/bogo-ui/ui.rb', line 102 def verbose(string, *args) if([:verbose]) info(string, :verbose, *args) string end end |
#warn(string, *args) ⇒ String
Format warning string
72 73 74 75 76 |
# File 'lib/bogo-ui/ui.rb', line 72 def warn(string, *args) output_method = args.include?(:nonewline) ? :print : :puts self.send(output_method, "#{color('[WARN]:', :yellow, :bold)} #{string}") string end |