class Bogo::Ui
CLI UI helper
Constants
- VERSION
Current library version
Attributes
@return [String]
@return [Truthy, Falsey]
@return [Truthy, Falsey]
@return [Truthy, Falsey]
@return [Smash] options
@return [IO]
Public Class Methods
Build new UI instance
@param args [Hash] @option args [String] :app_name name of application @option args [TrueClass, FalseClass] :colors enable/disable colors @option args [IO] :output_to IO to write @return [self]
# 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)) @options = args.to_smash end
Public Instance Methods
Prompt for question and receive answer
@param question [String] @param default [String] @return [String]
# 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
Colorize string
@param string [String] @param args [Symbol] @return [String]
# File lib/bogo/ui.rb, line 131 def color(string, *args) if(colorize) Paint[string, *args] else string end end
Confirm question. Requires user to provide Y or N answer
@param question [String]
# 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
Format debug string and output only if debug is set
@param string [String] @return [String, NilClass]
# File lib/bogo/ui.rb, line 118 def debug(string, *args) if(options[:debug]) output_method = args.include?(:nonewline) ? :print : :puts self.send(output_method, "#{color('[DEBUG]:', :white, :bold)} #{string}") string end end
Format error string
@param string [String] @return [String]
# 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
Format fatal string
@param string [String] @return [String]
# 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
Output information string
@param string [String] @return [String]
# 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
Output directly
@param string [String] @return [String]
# File lib/bogo/ui.rb, line 57 def print(string='') output_to.print string string end
Output directly
@param string [String] @return [String]
# File lib/bogo/ui.rb, line 48 def puts(string='') output_to.puts string string end
Create a new table
@param inst [Object] instance to attach table (for method call proxy) @return [Table]
# File lib/bogo/ui.rb, line 199 def table(inst=nil, &block) Table.new(self, inst, &block) end
Output info if verbose flag is set
@param string [String] @return [String, NilClass]
# File lib/bogo/ui.rb, line 107 def verbose(string, *args) if(options[:verbose]) info(string, :verbose, *args) string end end
Format warning string
@param string [String] @return [String]
# 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