Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Add --color option to disable ANSI color output
Browse files Browse the repository at this point in the history
  • Loading branch information
wingrunr21 committed Aug 8, 2013
1 parent 73cdf74 commit 4460a6c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/strainer.rb
Expand Up @@ -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'
17 changes: 12 additions & 5 deletions lib/strainer/cli.rb
Expand Up @@ -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

Expand All @@ -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'
Expand Down
44 changes: 44 additions & 0 deletions lib/strainer/shell.rb
@@ -0,0 +1,44 @@
#
# Copyright 2013, Stafford Brunk <sbrunk@customink.com>
# 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
3 changes: 0 additions & 3 deletions lib/strainer/ui.rb
Expand Up @@ -127,6 +127,3 @@ def deprecated(message)
end
end
end

# Include this module in Thor's shell
Thor::Base.shell.send(:include, Strainer::UI)

0 comments on commit 4460a6c

Please sign in to comment.