From 6ecb72c9cbbe1cefa17a5a33993a7f2e7841f1ab Mon Sep 17 00:00:00 2001 From: slavic Date: Sun, 24 Apr 2011 22:30:54 +0300 Subject: [PATCH] support color on windows --- Gemfile | 3 +++ lib/guard/ui.rb | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index a7126b6..180543c 100644 --- a/Gemfile +++ b/Gemfile @@ -12,3 +12,6 @@ if Config::CONFIG['target_os'] =~ /linux/i gem 'rb-inotify', '>= 0.5.1' gem 'libnotify', '~> 0.1.3' end +if Config::CONFIG['target_os'] =~ /mswin|mingw/i + gem 'win32console' +end diff --git a/lib/guard/ui.rb b/lib/guard/ui.rb index 94953db..587d6de 100644 --- a/lib/guard/ui.rb +++ b/lib/guard/ui.rb @@ -2,6 +2,29 @@ module Guard module UI 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 = {}) unless ENV["GUARD_ENV"] == "test" reset_line if options[:reset] @@ -24,7 +47,11 @@ module Guard end def reset_line - print "\r\e[0m" + if color_enabled? + print @new_line + "\e[0m" + else + print @new_line + end end def clear @@ -38,7 +65,11 @@ module Guard end 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