lib/guard/ui.rb in guard-0.7.0 vs lib/guard/ui.rb in guard-0.8.0

- old
+ new

@@ -1,90 +1,90 @@ module Guard - module UI - ANSI_ESCAPE_BRIGHT = "1" - - 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" - + # The UI class helps to format messages for the user. + # + module UI class << self color_enabled = nil - def info(message, options = {}) - unless ENV["GUARD_ENV"] == "test" + # Show an info message. + # + # @param [String] message the message to show + # @option options [Boolean] reset whether to clean the output before + # + def info(message, options = { }) + unless ENV['GUARD_ENV'] == 'test' reset_line if options[:reset] puts color(message) if message != '' end end - def error(message, options={}) - unless ENV["GUARD_ENV"] == "test" + # Show a red error message that is prefixed with ERROR. + # + # @param [String] message the message to show + # @option options [Boolean] reset whether to clean the output before + # + def error(message, options = { }) + unless ENV['GUARD_ENV'] == 'test' reset_line if options[:reset] puts color('ERROR: ', :red) + message end end - def deprecation(message, options = {}) - unless ENV["GUARD_ENV"] == "test" + # Show a red deprecation message that is prefixed with DEPRECATION. + # + # @param [String] message the message to show + # @option options [Boolean] reset whether to clean the output before + # + def deprecation(message, options = { }) + unless ENV['GUARD_ENV'] == 'test' reset_line if options[:reset] puts color('DEPRECATION: ', :red) + message end end - def debug(message, options={}) - unless ENV["GUARD_ENV"] == "test" + # Show a debug message that is prefixed with DEBUG and a timestamp. + # + # @param [String] message the message to show + # @option options [Boolean] reset whether to clean the output before + # + def debug(message, options = { }) + unless ENV['GUARD_ENV'] == 'test' reset_line if options[:reset] puts color("DEBUG (#{Time.now.strftime('%T')}): ", :yellow) + message if ::Guard.options && ::Guard.options[:debug] end end + # Reset a line. + # def reset_line print(color_enabled? ? "\r\e[0m" : "\r\n") end + # Clear the output. + # def clear - system("clear;") + system('clear;') end - private + private + # Reset a color sequence. + # # @deprecated + # @param [String] text the text + # def reset_color(text) - deprecation('UI.reset_color(text) is deprecated, please use color(text, "") instead.') - color(text, "") + deprecation('UI.reset_color(text) is deprecated, please use color(text, ' ') instead.') + color(text, '') end - 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 - + # Checks if color output can be enabled. + # + # @return [Boolean] whether color is enabled or not + # def color_enabled? if @color_enabled.nil? if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i if ENV['ANSICON'] @color_enabled = true @@ -100,11 +100,89 @@ end else @color_enabled = true end end + @color_enabled end + # 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. + # + # @example + # + # 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 + end + + # Brighten the color + ANSI_ESCAPE_BRIGHT = '1' + + # Black foreground color + ANSI_ESCAPE_BLACK = '30' + + # Red foreground color + ANSI_ESCAPE_RED = '31' + + # Green foreground color + ANSI_ESCAPE_GREEN = '32' + + # Yellow foreground color + ANSI_ESCAPE_YELLOW = '33' + + # Blue foreground color + ANSI_ESCAPE_BLUE = '34' + + # Magenta foreground color + ANSI_ESCAPE_MAGENTA = '35' + + # Cyan foreground color + ANSI_ESCAPE_CYAN = '36' + + # White foreground color + ANSI_ESCAPE_WHITE = '37' + + # Black background color + ANSI_ESCAPE_BGBLACK = '40' + + # Red background color + ANSI_ESCAPE_BGRED = '41' + + # Green background color + ANSI_ESCAPE_BGGREEN = '42' + + # Yellow background color + ANSI_ESCAPE_BGYELLOW = '43' + + # Blue background color + ANSI_ESCAPE_BGBLUE = '44' + + # Magenta background color + ANSI_ESCAPE_BGMAGENTA = '45' + + # Cyan background color + ANSI_ESCAPE_BGCYAN = '46' + + # White background color + ANSI_ESCAPE_BGWHITE = '47' + end end