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.4')

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



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

def initialize(args={})
  @application_name = args.fetch(:app_name)
  @colorize = args.fetch(:colors, true)
  @output_to = args.fetch(:output_to, $stdout)
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

#colorizeTruthy, Falsey

Returns:

  • (Truthy, Falsey)


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

def colorize
  @colorize
end

Instance Method Details

#ask(question, default = nil) ⇒ String Also known as: ask_question

Prompt for question and receive answer

Parameters:

  • question (String)
  • default (String) (defaults to: nil)

Returns:

  • (String)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/bogo-ui/ui.rb', line 94

def ask(question, default=nil)
  string = question.dup
  if(default)
    string << " [#{default}]"
  end
  result = nil
  until(result)
    info "#{string}: ", :nonewline
    result = $stdin.gets.strip
    if(result.empty? && default)
      result = default
    end
    if(result.empty?)
      error 'Please provide a value'
      result = nil
    end
  end
  result
end

#color(string, *args) ⇒ String

Colorize string

Parameters:

  • string (String)
  • args (Symbol)

Returns:

  • (String)


81
82
83
84
85
86
87
# File 'lib/bogo-ui/ui.rb', line 81

def color(string, *args)
  if(colorize)
    Paint[string, *args]
  else
    string
  end
end

#error(string, *args) ⇒ String

Format error string

Parameters:

  • string (String)

Returns:

  • (String)


70
71
72
73
74
# File 'lib/bogo-ui/ui.rb', line 70

def error(string, *args)
  output_method = args.include?(:nonewline) ? :print : :puts
  self.send(output_method, "#{color('[ERROR]:', :red, :bold)} #{string}")
  string
end

#info(string, *args) ⇒ String

Output information string

Parameters:

  • string (String)

Returns:

  • (String)


50
51
52
53
54
# File 'lib/bogo-ui/ui.rb', line 50

def info(string, *args)
  output_method = args.include?(:nonewline) ? :print : :puts
  self.send(output_method, "#{color("[#{application_name}]:", :green)} #{string}")
  string
end

Output directly

Parameters:

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

Returns:

  • (String)


41
42
43
44
# File 'lib/bogo-ui/ui.rb', line 41

def print(string='')
  @output_to.print string
  string
end

#puts(string = '') ⇒ String

Output directly

Parameters:

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

Returns:

  • (String)


32
33
34
35
# File 'lib/bogo-ui/ui.rb', line 32

def puts(string='')
  @output_to.puts string
  string
end

#warn(string, *args) ⇒ String

Format warning string

Parameters:

  • string (String)

Returns:

  • (String)


60
61
62
63
64
# File 'lib/bogo-ui/ui.rb', line 60

def warn(string, *args)
  output_method = args.include?(:nonewline) ? :print : :puts
  self.send(output_method, "#{color('[WARN]:', :yellow, :bold)} #{string}")
  string
end