support color on windows

This commit is contained in:
slavic 2011-04-24 22:30:54 +03:00
parent cbe2cb353c
commit 6ecb72c9cb
2 changed files with 36 additions and 2 deletions

View File

@ -12,3 +12,6 @@ if Config::CONFIG['target_os'] =~ /linux/i
gem 'rb-inotify', '>= 0.5.1' gem 'rb-inotify', '>= 0.5.1'
gem 'libnotify', '~> 0.1.3' gem 'libnotify', '~> 0.1.3'
end end
if Config::CONFIG['target_os'] =~ /mswin|mingw/i
gem 'win32console'
end

View File

@ -2,6 +2,29 @@ module Guard
module UI module UI
class << self class << self
@color_enabled = true
@color_tested = false
@new_line = "\r"
def color_enabled?
if !@color_tested
@color_tested = true
if Config::CONFIG['target_os'] =~ /mswin|mingw/i
@new_line = "\r\n"
unless ENV['ANSICON']
begin
require 'rubygems' unless ENV['NO_RUBYGEMS']
require 'Win32/Console/ANSI'
rescue LoadError
@color_enabled = false
info "You must 'gem install win32console' to use color on Windows"
end
end
end
end
return @color_enabled
end
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]
@ -24,7 +47,11 @@ module Guard
end end
def reset_line def reset_line
print "\r\e[0m" if color_enabled?
print @new_line + "\e[0m"
else
print @new_line
end
end end
def clear def clear
@ -38,7 +65,11 @@ module Guard
end end
def color(text, color_code) def color(text, color_code)
"#{color_code}#{text}\e[0m" if color_enabled?
return "#{color_code}#{text}\e[0m"
else
return text
end
end end
end end