diff --git a/lib/strainer.rb b/lib/strainer.rb index 567a529..246433f 100644 --- a/lib/strainer.rb +++ b/lib/strainer.rb @@ -100,5 +100,5 @@ def logfile_path require_relative 'strainer/runner' require_relative 'strainer/sandbox' require_relative 'strainer/strainerfile' -require_relative 'strainer/ui' +require_relative 'strainer/shell' require_relative 'strainer/version' diff --git a/lib/strainer/cli.rb b/lib/strainer/cli.rb index c9b7972..1b690ac 100644 --- a/lib/strainer/cli.rb +++ b/lib/strainer/cli.rb @@ -45,6 +45,12 @@ def initialize(*args) # Set the Strainer path if it's specified Strainer.sandbox_path = @options[:sandbox] if @options[:sandbox] + # Use Strainer::Shell as the primary output shell + Thor::Base.shell = Strainer::Shell + + # Set whether color output is enabled + Thor::Base.shell.enable_colors = @options[:color] + # Unfreeze the options Hash from Thor @options = options.dup @@ -54,11 +60,12 @@ def initialize(*args) # global options map ['-v', '--version'] => :version - class_option :cookbooks_path, :type => :string, :aliases => '-p', :desc => 'The path to the cookbook store', :banner => 'PATH' - class_option :config, :type => :string, :aliases => '-c', :desc => 'The path to the knife.rb/client.rb config' - class_option :strainer_file, :type => :string, :aliases => '-s', :desc => 'The path to the Strainer file to run against', :banner => 'FILE', :default => Strainer::Strainerfile::DEFAULT_FILENAME - class_option :sandbox, :type => :string, :aliases => '-s', :desc => 'The sandbox path (defaults to a temporary directory)', :default => Dir.mktmpdir - class_option :debug, :type => :boolean, :aliases => '-d', :desc => 'Show debugging log output', :default => false + class_option :cookbooks_path, :type => :string, :aliases => '-p', :desc => 'The path to the cookbook store', :banner => 'PATH' + class_option :config, :type => :string, :aliases => '-c', :desc => 'The path to the knife.rb/client.rb config' + class_option :strainer_file, :type => :string, :aliases => '-s', :desc => 'The path to the Strainer file to run against', :banner => 'FILE', :default => Strainer::Strainerfile::DEFAULT_FILENAME + class_option :sandbox, :type => :string, :aliases => '-s', :desc => 'The sandbox path (defaults to a temporary directory)', :default => Dir.mktmpdir + class_option :debug, :type => :boolean, :aliases => '-d', :desc => 'Show debugging log output', :default => false + class_option :color, :type => :boolean, :aliases => '-co', :desc => 'Enable color in Strainer output', :default => true # strainer test *COOKBOOKS method_option :except, :type => :array, :aliases => '-e', :desc => 'Strainerfile labels to ignore' diff --git a/lib/strainer/shell.rb b/lib/strainer/shell.rb new file mode 100644 index 0000000..6734c26 --- /dev/null +++ b/lib/strainer/shell.rb @@ -0,0 +1,44 @@ +# +# Copyright 2013, Stafford Brunk +# Copyright 2013, CustomInk, LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require_relative 'ui' + +module Strainer + class Shell < Thor::Shell::Color + include UI + + class << self + attr_accessor :enable_colors + end + + # Should output have ANSI colors applied? + def color_enabled? + !self.class.enable_colors + end + + # Set ANSI colors for the given string only if + # colors are enabled for this shell + # + # @param [String] + # message to set colors on + # @param [Symbol] colors + # the colors to apply + def set_color(string, *colors) + color_enabled? ? string : super + end + end +end diff --git a/lib/strainer/ui.rb b/lib/strainer/ui.rb index af9b571..1837ed6 100644 --- a/lib/strainer/ui.rb +++ b/lib/strainer/ui.rb @@ -127,6 +127,3 @@ def deprecated(message) end end end - -# Include this module in Thor's shell -Thor::Base.shell.send(:include, Strainer::UI)