master/lib/guard/ui.rb

111 lines
2.8 KiB
Ruby
Raw Normal View History

2010-10-03 21:00:33 +00:00
module Guard
module UI
2011-06-20 12:26:44 +00:00
2011-06-20 12:33:08 +00:00
ANSI_ESCAPE_BRIGHT = "1"
2011-06-20 12:26:44 +00:00
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"
2010-10-03 21:00:33 +00:00
class << self
color_enabled = nil
2010-10-03 21:00:33 +00:00
def info(message, options = {})
unless ENV["GUARD_ENV"] == "test"
reset_line if options[:reset]
2011-06-20 12:26:44 +00:00
puts color(message) if message != ''
2010-10-03 21:00:33 +00:00
end
end
2010-11-11 10:02:29 +00:00
def error(message, options = {})
unless ENV["GUARD_ENV"] == "test"
reset_line if options[:reset]
puts color('ERROR: ', :red) + message
end
2010-10-03 21:00:33 +00:00
end
def deprecation(message, options = {})
unless ENV["GUARD_ENV"] == "test"
reset_line if options[:reset]
puts color('DEPRECATION: ', :red) + message
end
end
2010-11-11 10:02:29 +00:00
def debug(message, options = {})
2010-10-10 10:38:25 +00:00
unless ENV["GUARD_ENV"] == "test"
2010-11-11 10:02:29 +00:00
reset_line if options[:reset]
puts color("DEBUG (#{Time.now.strftime('%T')}): ", :yellow) + message if ::Guard.options && ::Guard.options[:debug]
2010-10-10 10:38:25 +00:00
end
end
2010-10-03 21:00:33 +00:00
def reset_line
print(color_enabled? ? "\r\e[0m" : "\r\n")
2010-10-03 21:00:33 +00:00
end
2010-10-03 21:00:33 +00:00
def clear
system("clear;")
end
2010-10-10 10:38:25 +00:00
private
2011-06-20 12:26:44 +00:00
# @deprecated
2010-10-03 21:00:33 +00:00
def reset_color(text)
deprecation('UI.reset_color(text) is deprecated, please use color(text, "") instead.')
color(text, "")
2010-10-03 21:00:33 +00:00
end
2011-06-20 12:26:44 +00:00
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
2010-10-03 21:00:33 +00:00
end
def color_enabled?
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
end
@color_enabled
2010-10-03 21:00:33 +00:00
end
2010-10-03 21:00:33 +00:00
end
end
end