From e4514ab298c1a8f5486dcf8f7475305fa59ac652 Mon Sep 17 00:00:00 2001 From: stereobooster Date: Mon, 20 Jun 2011 03:13:28 -0700 Subject: [PATCH 1/3] Fix issue #12. Test it without win32console. --- lib/guard/ui.rb | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/lib/guard/ui.rb b/lib/guard/ui.rb index 2a260fd..d9b9991 100644 --- a/lib/guard/ui.rb +++ b/lib/guard/ui.rb @@ -2,6 +2,8 @@ module Guard module UI class << self + color_enabled = nil + def info(message, options = {}) unless ENV["GUARD_ENV"] == "test" reset_line if options[:reset] @@ -12,7 +14,7 @@ module Guard def error(message, options = {}) unless ENV["GUARD_ENV"] == "test" reset_line if options[:reset] - puts "ERROR: #{message}" + puts "#{color('ERROR:', ';31')} #{message}" end end @@ -24,11 +26,7 @@ module Guard end def reset_line - if color_enabled? - print "\r\e[0m" - else - print "\r\n" - end + print(color_enabled? ? "\r\e[0m" : "\r\n") end def clear @@ -38,32 +36,33 @@ module Guard private def reset_color(text) - color(text, "\e[0m") + color(text, "") end def color(text, color_code) - if color_enabled? - return "#{color_code}#{text}\e[0m" - else - return text - end + color_enabled? ? "\e[0#{color_code}m#{text}\e[0m" : text end def color_enabled? - @color_enabled ||= if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i - 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" - false + if @color_enabled.nil? + if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i + if ENV['ANSICON'] + @color_enabled = true + else + begin + require 'rubygems' unless ENV['NO_RUBYGEMS'] + require 'Win32/Console/ANSI' + @color_enabled = true + rescue LoadError + @color_enabled = false + info "You must 'gem install win32console' to use color on Windows" + end end + else + @color_enabled = true end - else - true end + @color_enabled end end From 3464d59cb739fcd3ff7214e80d94cd8972304bc1 Mon Sep 17 00:00:00 2001 From: stereobooster Date: Mon, 20 Jun 2011 05:26:44 -0700 Subject: [PATCH 2/3] more color support for UI --- lib/guard/ui.rb | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/guard/ui.rb b/lib/guard/ui.rb index d9b9991..7d20871 100644 --- a/lib/guard/ui.rb +++ b/lib/guard/ui.rb @@ -1,5 +1,24 @@ module Guard module UI + + ANSI_ESCAPE_BLACK = "30" + ANSI_ESCAPE_RED = "31" + ANSI_ESCAPE_GREEN = "32" + ANSI_ESCAPE_YELLOW = "33" + ANSI_ESCAPE_BLUE = "34" + ANSI_ESCAPE_MAGENTA = "35" + ANSI_ESCAPE_CYAN = "36" + ANSI_ESCAPE_WHITE = "37" + + ANSI_ESCAPE_BGBLACK = "40" + ANSI_ESCAPE_BGRED = "41" + ANSI_ESCAPE_BGGREEN = "42" + ANSI_ESCAPE_BGYELLOW = "43" + ANSI_ESCAPE_BGBLUE = "44" + ANSI_ESCAPE_BGMAGENTA = "45" + ANSI_ESCAPE_BGCYAN = "46" + ANSI_ESCAPE_BGWHITE = "47" + class << self color_enabled = nil @@ -7,14 +26,14 @@ module Guard def info(message, options = {}) unless ENV["GUARD_ENV"] == "test" reset_line if options[:reset] - puts reset_color(message) if message != '' + puts color(message) if message != '' end end def error(message, options = {}) unless ENV["GUARD_ENV"] == "test" reset_line if options[:reset] - puts "#{color('ERROR:', ';31')} #{message}" + puts color('ERROR:', :red) + message end end @@ -35,11 +54,22 @@ module Guard private + # @deprecated def reset_color(text) color(text, "") end - def color(text, color_code) + def color(text, *color_options) + color_code = "" + color_options.each do |color_option| + color_option = color_option.to_s + if color_option != "" + if !(color_option =~ /\d+/) + color_option = const_get("ANSI_ESCAPE_#{color_option.upcase}") + end + color_code += ";" + color_option + end + end color_enabled? ? "\e[0#{color_code}m#{text}\e[0m" : text end From 2b6d524cfb5e79be99dac76f011e2445bb70139a Mon Sep 17 00:00:00 2001 From: stereobooster Date: Mon, 20 Jun 2011 05:33:08 -0700 Subject: [PATCH 3/3] add const ANSI_ESCAPE_BRIGHT = "1" --- lib/guard/ui.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/guard/ui.rb b/lib/guard/ui.rb index 7d20871..d68f4ed 100644 --- a/lib/guard/ui.rb +++ b/lib/guard/ui.rb @@ -1,6 +1,8 @@ module Guard module UI + ANSI_ESCAPE_BRIGHT = "1" + ANSI_ESCAPE_BLACK = "30" ANSI_ESCAPE_RED = "31" ANSI_ESCAPE_GREEN = "32"