Add yardoc to the UI class.

This commit is contained in:
Michael Kessler 2011-09-20 12:54:36 +02:00
parent dddc2ad369
commit ab91117ed7

View File

@ -1,4 +1,7 @@
module Guard module Guard
# The UI class helps to format messages for the user.
#
module UI module UI
ANSI_ESCAPE_BRIGHT = "1" ANSI_ESCAPE_BRIGHT = "1"
@ -25,6 +28,12 @@ module Guard
color_enabled = nil color_enabled = nil
# 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
#
def info(message, options = {}) def info(message, options = {})
unless ENV["GUARD_ENV"] == "test" unless ENV["GUARD_ENV"] == "test"
reset_line if options[:reset] reset_line if options[:reset]
@ -32,13 +41,25 @@ module Guard
end end
end end
def error(message, options={}) # 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
#
def error(message, options = {})
unless ENV["GUARD_ENV"] == "test" unless ENV["GUARD_ENV"] == "test"
reset_line if options[:reset] reset_line if options[:reset]
puts color('ERROR: ', :red) + message puts color('ERROR: ', :red) + message
end end
end end
# 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
#
def deprecation(message, options = {}) def deprecation(message, options = {})
unless ENV["GUARD_ENV"] == "test" unless ENV["GUARD_ENV"] == "test"
reset_line if options[:reset] reset_line if options[:reset]
@ -46,43 +67,69 @@ module Guard
end end
end end
def debug(message, options={}) # Show a debug message that is prefixed with DEBUG and a timestampe.
#
# @param [String] message the message to show
# @param [Hash] options the options
# @option options [Boolean] reset whether to clean the output before
#
def debug(message, options={ })
unless ENV["GUARD_ENV"] == "test" unless ENV["GUARD_ENV"] == "test"
reset_line if options[:reset] reset_line if options[:reset]
puts color("DEBUG (#{Time.now.strftime('%T')}): ", :yellow) + message if ::Guard.options && ::Guard.options[:debug] puts color("DEBUG (#{Time.now.strftime('%T')}): ", :yellow) + message if ::Guard.options && ::Guard.options[:debug]
end end
end end
# Reset a line.
#
def reset_line def reset_line
print(color_enabled? ? "\r\e[0m" : "\r\n") print(color_enabled? ? "\r\e[0m" : "\r\n")
end end
# Clear the output.
#
def clear def clear
system("clear;") system("clear;")
end end
private private
# Reset a color sequence.
#
# @deprecated # @deprecated
# @param [String] text the text
#
def reset_color(text) def reset_color(text)
deprecation('UI.reset_color(text) is deprecated, please use color(text, "") instead.') deprecation('UI.reset_color(text) is deprecated, please use color(text, "") instead.')
color(text, "") color(text, "")
end end
# Colorizes a text message.
#
# @example
# color("Hello World", :red, :bright)
#
# @param [String] the text to colorize
# @param [Array] color_options
#
def color(text, *color_options) def color(text, *color_options)
color_code = "" color_code = ""
color_options.each do |color_option| color_options.each do |color_option|
color_option = color_option.to_s color_option = color_option.to_s
if color_option != "" if color_option != ""
if !(color_option =~ /\d+/) if !(color_option =~ /\d+/)
color_option = const_get("ANSI_ESCAPE_#{color_option.upcase}") color_option = const_get("ANSI_ESCAPE_#{ color_option.upcase }")
end end
color_code += ";" + color_option color_code += ";" + color_option
end end
end end
color_enabled? ? "\e[0#{color_code}m#{text}\e[0m" : text color_enabled? ? "\e[0#{ color_code }m#{ text }\e[0m" : text
end end
# Checks if color output can be enabled.
#
# @return [Boolean] whether color is enabled or not
#
def color_enabled? def color_enabled?
if @color_enabled.nil? if @color_enabled.nil?
if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i
@ -102,6 +149,7 @@ module Guard
@color_enabled = true @color_enabled = true
end end
end end
@color_enabled @color_enabled
end end