class Bogo::Ui

CLI UI helper

Constants

VERSION

Current library version

Attributes

application_name[RW]

@return [String]

auto_confirm[RW]

@return [Truthy, Falsey]

auto_default[RW]

@return [Truthy, Falsey]

colorize[RW]

@return [Truthy, Falsey]

options[R]

@return [Smash] options

output_to[R]

@return [IO]

Public Class Methods

new(args={}) click to toggle source

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

ask(question, *args) click to toggle source

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
Also aliased as: ask_question
ask_question(question, *args)
Alias for: ask
color(string, *args) click to toggle source

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) click to toggle source

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
debug(string, *args) click to toggle source

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
error(string, *args) click to toggle source

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
fatal(string, *args) click to toggle source

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
info(string, *args) click to toggle source

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
print(string='') click to toggle source

Output directly

@param string [String] @return [String]

puts(string='') click to toggle source

Output directly

@param string [String] @return [String]

# File lib/bogo/ui.rb, line 48
def puts(string='')
  output_to.puts string
  string
end
table(inst=nil, &block) click to toggle source

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
verbose(string, *args) click to toggle source

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
warn(string, *args) click to toggle source

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