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,32 +36,33 @@ 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
begin if ENV['ANSICON']
require 'rubygems' unless ENV['NO_RUBYGEMS'] @color_enabled = true
require 'Win32/Console/ANSI' else
rescue LoadError begin
@color_enabled = false require 'rubygems' unless ENV['NO_RUBYGEMS']
info "You must 'gem install win32console' to use color on Windows" require 'Win32/Console/ANSI'
false @color_enabled = true
rescue LoadError
@color_enabled = false
info "You must 'gem install win32console' to use color on Windows"
end
end end
else
@color_enabled = true
end end
else
true
end end
@color_enabled
end end
end end