lib/vimgolf/ui.rb in vimgolf-0.3.0 vs lib/vimgolf/ui.rb in vimgolf-0.4.0
- old
+ new
@@ -1,9 +1,16 @@
+require 'highline'
+
module VimGolf
class CLI
class UI < Thor::Base.shell
+ def initialize
+ super
+ @hl = HighLine.new($stdin)
+ end
+
def error(name, message = nil)
begin
orig_out, $stdout = $stdout, $stderr
if message
say_status name, message, :red
@@ -41,26 +48,35 @@
name = name.inspect unless name.is_a?(String)
say name, :cyan
end
end
- def ask(message, password = false)
+ def ask_question(message, options = {})
begin
- require 'highline'
- @hl ||= HighLine.new($stdin)
- if not $stdin.tty?
- @hl.ask(message)
- elsif password
- @hl.ask(message) {|q| q.echo = "*" }
- else
- @hl.ask(message) {|q| q.readline = true }
+ message = color_string(message, options[:type])
+ details = Proc.new do |q|
+ q.readline = !$stdin.tty?
end
+ @hl.ask(message, options[:choices] || [], &details)
rescue EOFError
return ''
end
end
+ 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
+
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
@@ -111,6 +127,6 @@
($stdout.tty? || ENV['THOR_SHELL']) ? super : string
end
end
end
-end
\ No newline at end of file
+end