Class: Hem::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
- #editor(initial_text) ⇒ Object
- #error(message) ⇒ Object
- #info(message) ⇒ Object
-
#initialize(input = $stdin, output = $stdout, error = $stderr) ⇒ Ui
constructor
A new instance of Ui.
- #output(message) ⇒ Object
- #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 32 |
# File 'lib/hem/ui.rb', line 25 def initialize input = $stdin, output = $stdout, error = $stderr require 'highline' 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/hem/ui.rb', line 9 def interactive @interactive end |
Instance Method Details
#ask(question, opts = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/hem/ui.rb', line 49 def ask question, opts = {} opts = { :validate => nil, :default => nil, :echo => true }.merge(opts) unless @interactive raise Hem::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.echo = opts[:echo] q.readline end answer = answer.to_s answer.strip.empty? ? opts[:default].to_s : answer.strip rescue EOFError Hem.ui.info "" "" end end |
#ask_choice(question, choices, opts = {}) ⇒ Object
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 106 107 108 |
# File 'lib/hem/ui.rb', line 77 def ask_choice question, choices, opts = {} unless @interactive raise Hem::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 Hem.ui.info "" "" end end |
#color(*args) ⇒ Object
141 142 143 |
# File 'lib/hem/ui.rb', line 141 def color *args @out.color *args end |
#color_scheme(scheme = nil) ⇒ Object
34 35 36 37 |
# File 'lib/hem/ui.rb', line 34 def color_scheme scheme = nil HighLine.color_scheme = scheme if scheme HighLine.color_scheme end |
#debug(message) ⇒ Object
145 146 147 |
# File 'lib/hem/ui.rb', line 145 def debug say @out, , :debug end |
#editor(initial_text) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/hem/ui.rb', line 110 def editor initial_text editor = Hem.user_config.editor.nil? ? ENV['EDITOR'] : Hem.user_config.editor if editor.nil? raise Hem::UndefinedEditorError.new end tmp = Tempfile.new('hem_tmp') begin tmp.write initial_text tmp.close system([editor, tmp.path].join(' ')) tmp.open return tmp.read rescue Exception => e raise Hem::Error.new e. ensure tmp.close tmp.unlink end end |
#error(message) ⇒ Object
157 158 159 |
# File 'lib/hem/ui.rb', line 157 def error say @error, , :error end |
#info(message) ⇒ Object
149 150 151 |
# File 'lib/hem/ui.rb', line 149 def info say @out, , :info end |
#output(message) ⇒ Object
165 166 167 |
# File 'lib/hem/ui.rb', line 165 def output say @out, , nil end |
#section(title) ⇒ Object
131 132 133 134 135 |
# File 'lib/hem/ui.rb', line 131 def section title Hem.ui.title title yield Hem.ui.separator end |
#separator ⇒ Object
137 138 139 |
# File 'lib/hem/ui.rb', line 137 def separator info(supports_color? ? "" : "\n") end |
#success(message) ⇒ Object
161 162 163 |
# File 'lib/hem/ui.rb', line 161 def success say @out, , :success end |
#supports_color? ⇒ Boolean
39 40 41 42 |
# File 'lib/hem/ui.rb', line 39 def supports_color? return @output_io.tty? unless Hem.windows? return (ENV['ANSICON'] || ENV['TERM'] == 'xterm') && @output_io.tty? # ANSICON or MinTTY && output is TTY end |
#title(message) ⇒ Object
169 170 171 |
# File 'lib/hem/ui.rb', line 169 def title say @out, , :title end |
#use_color(opt = nil) ⇒ Object
44 45 46 47 |
# File 'lib/hem/ui.rb', line 44 def use_color opt = nil HighLine.use_color = opt unless opt.nil? HighLine.use_color? end |
#warning(message) ⇒ Object
153 154 155 |
# File 'lib/hem/ui.rb', line 153 def warning say @error, , :warning end |