Class: Hobo::Ui
Constant Summary collapse
- COLORS =
{ :debug => [ ], :info => [ ], :warning => [:yellow], :error => [:red], :success => [:green], :opt => [:green], :command => [:green], :special => [:blue], :title => [:green], :help_title => [:yellow], :description => [:bold] }
Instance Attribute Summary collapse
-
#interactive ⇒ Object
Returns the value of attribute interactive.
Instance Method Summary collapse
- #ask(question, opts = {}) ⇒ Object
- #ask_choice(question, choices, opts = {}) ⇒ Object
- #color(*args) ⇒ Object
- #color_scheme(scheme = nil) ⇒ Object
- #debug(message) ⇒ Object
- #error(message) ⇒ Object
- #info(message) ⇒ Object
-
#initialize(input = $stdin, output = $stdout, error = $stderr) ⇒ Ui
constructor
A new instance of Ui.
- #section(title) ⇒ Object
- #separator ⇒ Object
- #success(message) ⇒ Object
- #supports_color? ⇒ Boolean
- #title(message) ⇒ Object
- #use_color(opt = nil) ⇒ Object
- #warning(message) ⇒ Object
Methods included from Logging
Constructor Details
#initialize(input = $stdin, output = $stdout, error = $stderr) ⇒ Ui
Returns a new instance of Ui.
25 26 27 28 29 30 31 |
# File 'lib/hobo/ui.rb', line 25 def initialize input = $stdin, output = $stdout, error = $stderr HighLine.color_scheme = HighLine::ColorScheme.new COLORS @output_io = output @out = ::HighLine.new input, output @error = ::HighLine.new input, error use_color supports_color? end |
Instance Attribute Details
#interactive ⇒ Object
Returns the value of attribute interactive.
9 10 11 |
# File 'lib/hobo/ui.rb', line 9 def interactive @interactive end |
Instance Method Details
#ask(question, opts = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hobo/ui.rb', line 48 def ask question, opts = {} opts = { :validate => nil, :default => nil }.merge(opts) unless @interactive raise Hobo::NonInteractiveError.new(question) if opts[:default].nil? return opts[:default].to_s end question = "#{question} [#{opts[:default]}]" if opts[:default] question += ": " begin answer = @out.ask(question) do |q| q.validate = opts[:validate] if opts[:validate] q.readline end answer = answer.to_s answer.strip.empty? ? opts[:default].to_s : answer.strip rescue EOFError Hobo.ui.info "" "" end end |
#ask_choice(question, choices, opts = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/hobo/ui.rb', line 74 def ask_choice question, choices, opts = {} unless @interactive raise Hobo::NonInteractiveError.new(question) if opts[:default].nil? return opts[:default].to_s end question = "#{question} [#{opts[:default]}]" if opts[:default] question += ":" info question choice_map = {} choices.to_enum.with_index(1) do |choice, index| choice_map[index.to_s] = choice info "#{index}] #{choice}" end begin answer = @out.ask("? ") do |q| q.validate = lambda do |a| s = a.strip s.empty? || (choice_map.keys + choices).include?(s) end q.readline end answer = answer.to_s answer = choice_map[answer] if /^\d+$/.match(answer.strip) answer.strip.empty? ? opts[:default].to_s : answer.strip rescue EOFError Hobo.ui.info "" "" end end |
#color(*args) ⇒ Object
117 118 119 |
# File 'lib/hobo/ui.rb', line 117 def color *args @out.color *args end |
#color_scheme(scheme = nil) ⇒ Object
33 34 35 36 |
# File 'lib/hobo/ui.rb', line 33 def color_scheme scheme = nil HighLine.color_scheme = scheme if scheme HighLine.color_scheme end |
#debug(message) ⇒ Object
121 122 123 |
# File 'lib/hobo/ui.rb', line 121 def debug say @out, , :debug end |
#error(message) ⇒ Object
133 134 135 |
# File 'lib/hobo/ui.rb', line 133 def error say @error, , :error end |
#info(message) ⇒ Object
125 126 127 |
# File 'lib/hobo/ui.rb', line 125 def info say @out, , :info end |
#section(title) ⇒ Object
107 108 109 110 111 |
# File 'lib/hobo/ui.rb', line 107 def section title Hobo.ui.title title yield Hobo.ui.separator end |
#separator ⇒ Object
113 114 115 |
# File 'lib/hobo/ui.rb', line 113 def separator info(supports_color? ? "" : "\n") end |
#success(message) ⇒ Object
137 138 139 |
# File 'lib/hobo/ui.rb', line 137 def success say @out, , :success end |
#supports_color? ⇒ Boolean
38 39 40 41 |
# File 'lib/hobo/ui.rb', line 38 def supports_color? return @output_io.tty? unless Hobo.windows? return (ENV['ANSICON'] || ENV['TERM'] == 'xterm') && @output_io.tty? # ANSICON or MinTTY && output is TTY end |
#title(message) ⇒ Object
141 142 143 |
# File 'lib/hobo/ui.rb', line 141 def title say @out, , :title end |
#use_color(opt = nil) ⇒ Object
43 44 45 46 |
# File 'lib/hobo/ui.rb', line 43 def use_color opt = nil HighLine.use_color = opt unless opt.nil? HighLine.use_color? end |
#warning(message) ⇒ Object
129 130 131 |
# File 'lib/hobo/ui.rb', line 129 def warning say @error, , :warning end |