Permalink
Browse files

support color on windows

  • Loading branch information...
1 parent cbe2cb3 commit 6ecb72c9cbbe1cefa17a5a33993a7f2e7841f1ab @stereobooster stereobooster committed Apr 24, 2011
Showing with 36 additions and 2 deletions.
  1. +3 −0 Gemfile
  2. +33 −2 lib/guard/ui.rb
View
@@ -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
View
@@ -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 @@ def debug(message, options = {})
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 @@ def reset_color(text)
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

0 comments on commit 6ecb72c

Please sign in to comment.