Fix issue #12. Test it without win32console.

This commit is contained in:
stereobooster 2011-06-20 03:13:28 -07:00
parent 61b0a7aa05
commit e4514ab298

View File

@ -2,6 +2,8 @@ module Guard
module UI module UI
class << self class << self
color_enabled = nil
def info(message, options = {}) def info(message, options = {})
unless ENV["GUARD_ENV"] == "test" unless ENV["GUARD_ENV"] == "test"
reset_line if options[:reset] reset_line if options[:reset]
@ -12,7 +14,7 @@ module Guard
def error(message, options = {}) def error(message, options = {})
unless ENV["GUARD_ENV"] == "test" unless ENV["GUARD_ENV"] == "test"
reset_line if options[:reset] reset_line if options[:reset]
puts "ERROR: #{message}" puts "#{color('ERROR:', ';31')} #{message}"
end end
end end
@ -24,11 +26,7 @@ module Guard
end end
def reset_line def reset_line
if color_enabled? print(color_enabled? ? "\r\e[0m" : "\r\n")
print "\r\e[0m"
else
print "\r\n"
end
end end
def clear def clear
@ -38,33 +36,34 @@ module Guard
private private
def reset_color(text) def reset_color(text)
color(text, "\e[0m") color(text, "")
end end
def color(text, color_code) def color(text, color_code)
if color_enabled? color_enabled? ? "\e[0#{color_code}m#{text}\e[0m" : text
return "#{color_code}#{text}\e[0m"
else
return text
end
end end
def color_enabled? def color_enabled?
@color_enabled ||= if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i if @color_enabled.nil?
unless ENV['ANSICON'] if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i
if ENV['ANSICON']
@color_enabled = true
else
begin begin
require 'rubygems' unless ENV['NO_RUBYGEMS'] require 'rubygems' unless ENV['NO_RUBYGEMS']
require 'Win32/Console/ANSI' require 'Win32/Console/ANSI'
@color_enabled = true
rescue LoadError rescue LoadError
@color_enabled = false @color_enabled = false
info "You must 'gem install win32console' to use color on Windows" info "You must 'gem install win32console' to use color on Windows"
false
end end
end end
else else
true @color_enabled = true
end end
end end
@color_enabled
end
end end
end end