Class: Bogo::Ui
- Inherits:
-
Object
- Object
- Bogo::Ui
- 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
-
#ask(question, default = nil) ⇒ String
(also: #ask_question)
Prompt for question and receive answer.
-
#color(string, *args) ⇒ String
Colorize string.
-
#error(string, *args) ⇒ String
Format error string.
-
#info(string, *args) ⇒ String
Output information string.
-
#initialize(args = {}) ⇒ self
constructor
Build new UI instance.
-
#print(string = '') ⇒ String
Output directly.
-
#puts(string = '') ⇒ String
Output directly.
-
#warn(string, *args) ⇒ String
Format warning string.
Constructor Details
#initialize(args = {}) ⇒ self
Build new UI instance
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_name ⇒ String
13 14 15 |
# File 'lib/bogo-ui/ui.rb', line 13 def application_name @application_name end |
#colorize ⇒ 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
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
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
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
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 |
#print(string = '') ⇒ String
Output directly
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
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
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 |