Class: Bogo::Ui

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

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



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

Instance Attribute Details

#application_nameString

Returns:

  • (String)


13
14
15
# File 'lib/bogo-ui/ui.rb', line 13

def application_name
  @application_name
end

#auto_confirmTruthy, Falsey

Returns:

  • (Truthy, Falsey)


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

def auto_confirm
  @auto_confirm
end

#auto_defaultTruthy, Falsey

Returns:

  • (Truthy, Falsey)


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

def auto_default
  @auto_default
end

#colorizeTruthy, Falsey

Returns:

  • (Truthy, Falsey)


11
12
13
# File 'lib/bogo-ui/ui.rb', line 11

def colorize
  @colorize
end

#optionsSmash (readonly)

Returns options.

Returns:

  • (Smash)

    options



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

def options
  @options
end

#output_toIO (readonly)

Returns:

  • (IO)


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

Parameters:

  • question (String)
  • default (String)

Returns:

  • (String)


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

Parameters:

  • string (String)
  • args (Symbol)

Returns:

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

Parameters:

  • question (String)


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

Parameters:

  • string (String)

Returns:

  • (String, NilClass)


113
114
115
116
117
118
119
# File 'lib/bogo-ui/ui.rb', line 113

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)


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

Parameters:

  • string (String)

Returns:

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

Parameters:

  • string (String)

Returns:

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

Output directly

Parameters:

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

Returns:

  • (String)


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

Parameters:

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

Returns:

  • (String)


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

Parameters:

  • inst (Object) (defaults to: nil)

    instance to attach table (for method call proxy)

Returns:



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

Parameters:

  • string (String)

Returns:

  • (String, NilClass)


102
103
104
105
106
107
# File 'lib/bogo-ui/ui.rb', line 102

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)


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