From 845fa14e71d985ff6775d122ff02cbce47def0a7 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Sun, 1 Feb 2009 14:17:07 -0800 Subject: [PATCH] Factored out a logger for the command line to tell the user what is happening. --- lib/compass.rb | 5 ++++- lib/compass/exec.rb | 1 + lib/compass/logger.rb | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 lib/compass/logger.rb diff --git a/lib/compass.rb b/lib/compass.rb index 12aca135..d82db208 100644 --- a/lib/compass.rb +++ b/lib/compass.rb @@ -25,7 +25,10 @@ module Compass def base_directory File.expand_path(File.join(File.dirname(__FILE__), '..')) end - module_function :base_directory + def lib_directory + File.expand_path(File.join(File.dirname(__FILE__))) + end + module_function :base_directory, :lib_directory end require File.join(File.dirname(__FILE__), 'compass', 'frameworks') diff --git a/lib/compass/exec.rb b/lib/compass/exec.rb index 97f6e0e9..3198851c 100644 --- a/lib/compass/exec.rb +++ b/lib/compass/exec.rb @@ -1,6 +1,7 @@ require 'optparse' require 'rubygems' require 'haml' +require File.join(Compass.lib_directory, 'compass', 'logger') module Compass module Exec diff --git a/lib/compass/logger.rb b/lib/compass/logger.rb new file mode 100644 index 00000000..eb0a184e --- /dev/null +++ b/lib/compass/logger.rb @@ -0,0 +1,34 @@ +module Compass + class Logger + + DEFAULT_ACTIONS = [:directory, :exists, :remove, :create, :overwrite] + + attr_accessor :actions, :options + + def initialize(*actions) + self.options = actions.last.is_a?(Hash) ? actions.pop : {} + @actions = DEFAULT_ACTIONS.dup + @actions += actions + end + + # Record an action that has occurred + def record(action, *arguments) + log "#{action_padding(action)}#{action} #{arguments.join(' ')}" + end + + # Emit a log message + def log(msg) + puts msg + end + + # add padding to the left of an action that was performed. + def action_padding(action) + ' ' * [(max_action_length - action.to_s.length), 0].max + end + + # the maximum length of all the actions known to the logger. + def max_action_length + @max_action_length ||= actions.inject(0){|memo, a| [memo, a.to_s.length].max} + end + end +end \ No newline at end of file