lib/guard/ui.rb in guard-1.8.3 vs lib/guard/ui.rb in guard-2.0.0.pre

- old
+ new

@@ -1,111 +1,116 @@ require 'lumberjack' +require 'guard/ui/colors' + module Guard # The UI class helps to format messages for the user. Everything that is logged # through this class is considered either as an error message or a diagnostic # message and is written to standard error ($stderr). # # If your Guard plugin does some output that is piped into another process for further # processing, please just write it to STDOUT with `puts`. # module UI + include Colors class << self # Get the Guard::UI logger instance # def logger @logger ||= begin - options = self.options.dup - Lumberjack::Logger.new(options.delete(:device) || $stderr, options) + opts = options.marshal_dump + Lumberjack::Logger.new(opts.delete(:device) { $stderr }, opts) end end # Get the logger options # # @return [Hash] the logger options # def options - @options ||= { :level => :info, :template => ':time - :severity - :message', :time_format => '%H:%M:%S' } + @options ||= ::Guard::Options.new(level: :info, template: ':time - :severity - :message', time_format: '%H:%M:%S') end # Set the logger options # # @param [Hash] options the logger options # @option options [Symbol] level the log level # @option options [String] template the logger template # @option options [String] time_format the time format # def options=(options) - @options = options + @options = ::Guard::Options.new(options) end # Show an info message. # # @param [String] message the message to show # @option options [Boolean] reset whether to clean the output before # @option options [String] plugin manually define the calling plugin # - def info(message, options = { }) + def info(message, options = {}) filter(options[:plugin]) do |plugin| reset_line if options[:reset] - self.logger.info(message, plugin) + logger.info(message, plugin) end end # Show a yellow warning message that is prefixed with WARNING. # # @param [String] message the message to show # @option options [Boolean] reset whether to clean the output before # @option options [String] plugin manually define the calling plugin # - def warning(message, options = { }) + def warning(message, options = {}) filter(options[:plugin]) do |plugin| reset_line if options[:reset] - self.logger.warn(color(message, :yellow), plugin) + logger.warn(color(message, :yellow), plugin) end end # 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 # @option options [String] plugin manually define the calling plugin # - def error(message, options = { }) + def error(message, options = {}) filter(options[:plugin]) do |plugin| reset_line if options[:reset] - self.logger.error(color(message, :red), plugin) + logger.error(color(message, :red), plugin) end end # Show a red deprecation message that is prefixed with DEPRECATION. # It has a log level of `warn`. # # @param [String] message the message to show # @option options [Boolean] reset whether to clean the output before # @option options [String] plugin manually define the calling plugin # - def deprecation(message, options = { }) + def deprecation(message, options = {}) + return unless ::Guard.options.show_deprecations + filter(options[:plugin]) do |plugin| reset_line if options[:reset] - self.logger.warn(color(message, :yellow), plugin) + logger.warn(color(message, :yellow), plugin) end end # 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 # @option options [String] plugin manually define the calling plugin # - def debug(message, options = { }) + def debug(message, options = {}) filter(options[:plugin]) do |plugin| reset_line if options[:reset] - self.logger.debug(color(message, :yellow), plugin) + logger.debug(color(message, :yellow), plugin) end end # Reset a line. # @@ -114,11 +119,11 @@ end # Clear the output if clearable. # def clear(options = {}) - if ::Guard.options[:clear] && (@clearable || options[:force]) + if ::Guard.options.clear && (@clearable || options[:force]) @clearable = false system('clear;') end end @@ -140,12 +145,12 @@ if plugins.empty? && groups.empty? plugins = ::Guard.scope[:plugins] || [] groups = ::Guard.scope[:groups] || [] end - scope_message ||= plugins.join(',') unless plugins.empty? - scope_message ||= groups.join(',') unless groups.empty? + scope_message ||= plugins.map(&:title).join(', ') unless plugins.empty? + scope_message ||= groups.map(&:title).join(', ') unless groups.empty? scope_message ||= 'all' info "#{ action } #{ scope_message }" end @@ -157,12 +162,12 @@ # @param [String] plugin the calling plugin name # @yield When the message should be logged # @yieldparam [String] param the calling plugin name # def filter(plugin) - only = self.options[:only] - except = self.options[:except] + only = options.only + except = options.except plugin = plugin || calling_plugin_name if (!only && !except) || (only && only.match(plugin)) || (except && !except.match(plugin)) yield plugin end @@ -177,20 +182,10 @@ def calling_plugin_name(depth = 2) name = /(guard\/[a-z_]*)(\/[a-z_]*)?.rb:/i.match(caller[depth]) name ? name[1].split('/').map { |part| part.split(/[^a-z0-9]/i).map { |word| word.capitalize }.join }.join('::') : 'Guard' end - # 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, '') - end - # Checks if color output can be enabled. # # @return [Boolean] whether color is enabled or not # def color_enabled? @@ -240,59 +235,8 @@ 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