Class: Bogo::Ui

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ self

Build new UI instance

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :app_name (String)

    name of application

  • :colors (TrueClass, FalseClass)

    enable/disable colors

  • :output_to (IO)

    IO to write



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))
  @options = args.to_smash
end

Instance Attribute Details

#application_nameString

Returns:

  • (String)


18
19
20
# File 'lib/bogo/ui.rb', line 18

def application_name
  @application_name
end

#auto_confirmTruthy, Falsey

Returns:

  • (Truthy, Falsey)


22
23
24
# File 'lib/bogo/ui.rb', line 22

def auto_confirm
  @auto_confirm
end

#auto_defaultTruthy, Falsey

Returns:

  • (Truthy, Falsey)


24
25
26
# File 'lib/bogo/ui.rb', line 24

def auto_default
  @auto_default
end

#colorizeTruthy, Falsey

Returns:

  • (Truthy, Falsey)


16
17
18
# File 'lib/bogo/ui.rb', line 16

def colorize
  @colorize
end

#optionsSmash (readonly)

Returns options.

Returns:

  • (Smash)

    options



26
27
28
# File 'lib/bogo/ui.rb', line 26

def options
  @options
end

#output_toIO (readonly)

Returns:

  • (IO)


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

Parameters:

  • question (String)
  • default (String)

Returns:

  • (String)


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

Parameters:

  • string (String)
  • args (Symbol)

Returns:

  • (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

Parameters:

  • question (String)


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

Parameters:

  • string (String)

Returns:

  • (String, NilClass)


118
119
120
121
122
123
124
# 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) ⇒ String

Format error string

Parameters:

  • string (String)

Returns:

  • (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

Parameters:

  • string (String)

Returns:

  • (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

Parameters:

  • string (String)

Returns:

  • (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

Output directly

Parameters:

  • string (String) (defaults to: '')

Returns:

  • (String)


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

Parameters:

  • string (String) (defaults to: '')

Returns:

  • (String)


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

Parameters:

  • inst (Object) (defaults to: nil)

    instance to attach table (for method call proxy)

Returns:



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

Parameters:

  • string (String)

Returns:

  • (String, NilClass)


107
108
109
110
111
112
# File 'lib/bogo/ui.rb', line 107

def verbose(string, *args)
  if(options[:verbose])
    info(string, :verbose, *args)
    string
  end
end

#warn(string, *args) ⇒ String

Format warning string

Parameters:

  • string (String)

Returns:

  • (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