2010-10-03 21:00:33 +00:00
|
|
|
module Guard
|
2011-09-20 10:54:36 +00:00
|
|
|
|
|
|
|
# The UI class helps to format messages for the user.
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
module UI
|
|
|
|
class << self
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-06-20 10:13:28 +00:00
|
|
|
color_enabled = nil
|
|
|
|
|
2011-09-20 10:54:36 +00:00
|
|
|
# Show an info message.
|
|
|
|
#
|
|
|
|
# @param [String] message the message to show
|
|
|
|
# @param [Hash] options the options
|
|
|
|
# @option options [Boolean] reset whether to clean the output before
|
|
|
|
#
|
2011-09-20 13:07:29 +00:00
|
|
|
def info(message, options = { })
|
|
|
|
unless ENV['GUARD_ENV'] == 'test'
|
2010-10-03 21:00:33 +00:00
|
|
|
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
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 10:54:36 +00:00
|
|
|
# Show a red error message that is prefixed with ERROR.
|
|
|
|
#
|
|
|
|
# @param [String] message the message to show
|
|
|
|
# @param [Hash] options the options
|
|
|
|
# @option options [Boolean] reset whether to clean the output before
|
|
|
|
#
|
2011-09-20 13:07:29 +00:00
|
|
|
def error(message, options = { })
|
|
|
|
unless ENV['GUARD_ENV'] == 'test'
|
2010-11-25 23:57:08 +00:00
|
|
|
reset_line if options[:reset]
|
2011-07-25 21:28:54 +00:00
|
|
|
puts color('ERROR: ', :red) + message
|
2010-11-25 23:57:08 +00:00
|
|
|
end
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 10:54:36 +00:00
|
|
|
# Show a red deprecation message that is prefixed with DEPRECATION.
|
|
|
|
#
|
|
|
|
# @param [String] message the message to show
|
|
|
|
# @param [Hash] options the options
|
|
|
|
# @option options [Boolean] reset whether to clean the output before
|
|
|
|
#
|
2011-09-20 13:07:29 +00:00
|
|
|
def deprecation(message, options = { })
|
|
|
|
unless ENV['GUARD_ENV'] == 'test'
|
2011-07-02 08:01:45 +00:00
|
|
|
reset_line if options[:reset]
|
2011-07-25 21:28:54 +00:00
|
|
|
puts color('DEPRECATION: ', :red) + message
|
2010-11-25 23:57:08 +00:00
|
|
|
end
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 19:52:59 +00:00
|
|
|
# Show a debug message that is prefixed with DEBUG and a timestamp.
|
2011-09-20 10:54:36 +00:00
|
|
|
#
|
|
|
|
# @param [String] message the message to show
|
|
|
|
# @param [Hash] options the options
|
|
|
|
# @option options [Boolean] reset whether to clean the output before
|
|
|
|
#
|
2011-09-20 19:52:59 +00:00
|
|
|
def debug(message, options = { })
|
2011-09-20 13:07:29 +00:00
|
|
|
unless ENV['GUARD_ENV'] == 'test'
|
2010-11-11 10:02:29 +00:00
|
|
|
reset_line if options[:reset]
|
2011-07-25 21:28:54 +00:00
|
|
|
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
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 10:54:36 +00:00
|
|
|
# Reset a line.
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def reset_line
|
2011-06-19 18:24:47 +00:00
|
|
|
print(color_enabled? ? "\r\e[0m" : "\r\n")
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 10:54:36 +00:00
|
|
|
# Clear the output.
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def clear
|
2011-09-20 13:07:29 +00:00
|
|
|
system('clear;')
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 10:54:36 +00:00
|
|
|
private
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 10:54:36 +00:00
|
|
|
# Reset a color sequence.
|
|
|
|
#
|
2011-06-20 12:26:44 +00:00
|
|
|
# @deprecated
|
2011-09-20 10:54:36 +00:00
|
|
|
# @param [String] text the text
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
def reset_color(text)
|
2011-09-20 13:07:29 +00:00
|
|
|
deprecation('UI.reset_color(text) is deprecated, please use color(text, ' ') instead.')
|
|
|
|
color(text, '')
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 10:54:36 +00:00
|
|
|
# Checks if color output can be enabled.
|
|
|
|
#
|
|
|
|
# @return [Boolean] whether color is enabled or not
|
|
|
|
#
|
2011-05-06 21:35:09 +00:00
|
|
|
def color_enabled?
|
2011-06-20 10:13:28 +00:00
|
|
|
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
|
2011-05-06 21:35:09 +00:00
|
|
|
end
|
2011-06-20 10:13:28 +00:00
|
|
|
else
|
|
|
|
@color_enabled = true
|
2011-05-06 21:35:09 +00:00
|
|
|
end
|
|
|
|
end
|
2011-09-20 10:54:36 +00:00
|
|
|
|
2011-06-20 10:13:28 +00:00
|
|
|
@color_enabled
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Colorizes a text message. See the constant in the UI class for possible
|
|
|
|
# color_options parameters. You can pass optionally :bright, a foreground
|
|
|
|
# color and a background color.
|
2011-09-20 13:07:29 +00:00
|
|
|
#
|
|
|
|
# @example
|
2011-09-20 23:30:35 +00:00
|
|
|
#
|
2011-09-20 13:07:29 +00:00
|
|
|
# color('Hello World', :red, :bright)
|
|
|
|
#
|
|
|
|
# @param [String] the text to colorize
|
|
|
|
# @param [Array] color_options the color options
|
|
|
|
#
|
|
|
|
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
|
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-09-20 13:07:29 +00:00
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Brighten the color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_BRIGHT = '1'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Black foreground color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_BLACK = '30'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Red foreground color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_RED = '31'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Green foreground color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_GREEN = '32'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Yellow foreground color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_YELLOW = '33'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Blue foreground color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_BLUE = '34'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Magenta foreground color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_MAGENTA = '35'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Cyan foreground color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_CYAN = '36'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# White foreground color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_WHITE = '37'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Black background color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_BGBLACK = '40'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Red background color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_BGRED = '41'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Green background color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_BGGREEN = '42'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Yellow background color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_BGYELLOW = '43'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Blue background color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_BGBLUE = '44'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Magenta background color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_BGMAGENTA = '45'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Cyan background color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_BGCYAN = '46'
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# White background color
|
2011-09-20 13:07:29 +00:00
|
|
|
ANSI_ESCAPE_BGWHITE = '47'
|
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
|
|
|
end
|