Class: Yetty::Ui
- Inherits:
-
Object
- Object
- Yetty::Ui
- Defined in:
- lib/yetty/ui.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#ask(question, default = nil) ⇒ String
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
15 16 17 18 |
# File 'lib/yetty/ui.rb', line 15 def initialize(args={}) @colorize = args.fetch(:colors, true) @output_to = args.fetch(:output_to, $stdout) end |
Instance Attribute Details
#colorize ⇒ Truthy, Falsey
7 8 9 |
# File 'lib/yetty/ui.rb', line 7 def colorize @colorize end |
Instance Method Details
#ask(question, default = nil) ⇒ String
Prompt for question and receive answer
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/yetty/ui.rb', line 82 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
73 74 75 |
# File 'lib/yetty/ui.rb', line 73 def color(string, *args) Paint[string, *args] end |
#error(string, *args) ⇒ String
Format error string
62 63 64 65 66 |
# File 'lib/yetty/ui.rb', line 62 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
42 43 44 45 46 |
# File 'lib/yetty/ui.rb', line 42 def info(string, *args) output_method = args.include?(:nonewline) ? :print : :puts self.send(output_method, "#{color('[Yetty]:', :green)} #{string}") string end |
#print(string = '') ⇒ String
Output directly
33 34 35 36 |
# File 'lib/yetty/ui.rb', line 33 def print(string='') @output_to.print string string end |
#puts(string = '') ⇒ String
Output directly
24 25 26 27 |
# File 'lib/yetty/ui.rb', line 24 def puts(string='') @output_to.puts string string end |
#warn(string, *args) ⇒ String
Format warning string
52 53 54 55 56 |
# File 'lib/yetty/ui.rb', line 52 def warn(string, *args) output_method = args.include?(:nonewline) ? :print : :puts self.send(output_method, "#{color('[WARN]:', :yellow, :bold)} #{string}") string end |