Class: VimGolf::CLI::UI
- Inherits:
-
Object
- Object
- VimGolf::CLI::UI
- Defined in:
- lib/vimgolf/ui.rb
Instance Method Summary collapse
- #ask_question(message, options = {}) ⇒ Object
- #color_string(str, type) ⇒ Object
- #debug(name, message = nil) ⇒ Object
- #error(name, message = nil) ⇒ Object
- #info(name, message = nil) ⇒ Object
-
#initialize ⇒ UI
constructor
A new instance of UI.
- #print_envs(apps, default_env_name = nil, simple = false) ⇒ Object
- #print_exception(e) ⇒ Object
- #print_help(table) ⇒ Object
-
#print_log(strokes) ⇒ Object
Take list of keystrokes, pretty-print them.
- #set_color(string, *colors) ⇒ Object
- #warn(name, message = nil) ⇒ Object
Constructor Details
#initialize ⇒ UI
Returns a new instance of UI.
9 10 11 12 |
# File 'lib/vimgolf/ui.rb', line 9 def initialize super @hl = HighLine.new($stdin) end |
Instance Method Details
#ask_question(message, options = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/vimgolf/ui.rb', line 67 def ask_question(, = {}) begin = color_string(, [:type]) details = Proc.new do |q| q.readline = !$stdin.tty? end @hl.ask(, [:choices] || [], &details) rescue EOFError # be sure to exit, don't loop error "Argh! Forced quit due to EOF error. Please report this problem so we can fix it!" return :quit end end |
#color_string(str, type) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/vimgolf/ui.rb', line 81 def color_string(str, type) @hl.color( str, case type when :info then :green when :warn then :yellow when :error then :red when :debug then :cyan else nil end ) end |
#debug(name, message = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/vimgolf/ui.rb', line 55 def debug(name, = nil) return unless ENV["DEBUG"] if = .inspect unless .is_a?(String) say_status name, , :blue elsif name name = name.inspect unless name.is_a?(String) say name, :cyan end end |
#error(name, message = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/vimgolf/ui.rb', line 14 def error(name, = nil) begin orig_out, $stdout = $stdout, $stderr if say_status name, , :red elsif name say name, :red end ensure $stdout = orig_out end end |
#info(name, message = nil) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/vimgolf/ui.rb', line 35 def info(name, = nil) if say_status name, , :green elsif name say name, :green end end |
#print_envs(apps, default_env_name = nil, simple = false) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/vimgolf/ui.rb', line 94 def print_envs(apps, default_env_name = nil, simple = false) if simple envs = apps.map{ |a| a.environments } envs.flatten.map{|x| x.name}.uniq.each do |env| puts env end else apps.each do |app| puts "#{app.name} (#{app.account.name})" if app.environments.any? app.environments.each do |env| short_name = env.shorten_name_for(app) icount = env.instances_count iname = (icount == 1) ? "instance" : "instances" default_text = env.name == default_env_name ? " [default]" : "" puts " #{short_name}#{default_text} (#{icount} #{iname})" end else puts " (This application is not in any environments; you can make one at #{EY.config.endpoint})" end puts "" end end end |
#print_exception(e) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/vimgolf/ui.rb', line 123 def print_exception(e) if e..empty? || (e. == e.class.to_s) = nil else = e. end if ENV["DEBUG"] error(e.class, ) e.backtrace.each{|l| say(" "*3 + l) } else error( || e.class.to_s) end end |
#print_help(table) ⇒ Object
138 139 140 |
# File 'lib/vimgolf/ui.rb', line 138 def print_help(table) print_table(table, :ident => 2, :truncate => true, :colwidth => 20) end |
#print_log(strokes) ⇒ Object
Take list of keystrokes, pretty-print them
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vimgolf/ui.rb', line 44 def print_log(strokes) strokes.each do |stroke| if stroke.length == 1 # not a special character say stroke, color=nil, force_new_line=false else say stroke, color=:red, force_new_line=false end end say end |
#set_color(string, *colors) ⇒ Object
142 143 144 |
# File 'lib/vimgolf/ui.rb', line 142 def set_color(string, *colors) ($stdout.tty? || ENV['THOR_SHELL']) ? super : string end |
#warn(name, message = nil) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/vimgolf/ui.rb', line 27 def warn(name, = nil) if say_status name, , :yellow elsif name say name, :yellow end end |