Skip to content

Commit

Permalink
Kill term-ansicolor dep due to ambiguous constant
Browse files Browse the repository at this point in the history
This dependency has caused problems for many developers who have a class
named "Term", which is a very ambiguous word. This commit brings
Term::ANSIColor into Cucumber and nests it under the Cucumber namespace.
  • Loading branch information
Joe Cannati + Joe Fiorini authored and Joe Cannatti + Joe Fiorini committed Mar 24, 2011
1 parent 72b695d commit bad2318
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 24 deletions.
1 change: 0 additions & 1 deletion cucumber.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ for important information about this release. Happy cuking!
}

s.add_dependency 'gherkin', '>= 2.3.5'
s.add_dependency 'term-ansicolor', '>= 1.0.5'
s.add_dependency 'builder', '>= 2.1.2'
s.add_dependency 'diff-lcs', '>= 1.1.2'
s.add_dependency 'json', '>= 1.4.6'
Expand Down
3 changes: 2 additions & 1 deletion lib/cucumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'cucumber/cli/main'
require 'cucumber/broadcaster'
require 'cucumber/step_definitions'
require 'cucumber/term/ansicolor'

module Cucumber
class << self
Expand All @@ -24,4 +25,4 @@ def logger=(logger)
@log = logger
end
end
end
end
6 changes: 3 additions & 3 deletions lib/cucumber/ast/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,13 @@ def to_s(options = {}) #:nodoc:
options = {:color => true, :indent => 2, :prefixes => TO_S_PREFIXES}.merge(options)
io = StringIO.new

c = Term::ANSIColor.coloring?
Term::ANSIColor.coloring = options[:color]
c = Cucumber::Term::ANSIColor.coloring?
Cucumber::Term::ANSIColor.coloring = options[:color]
formatter = Formatter::Pretty.new(nil, io, options)
formatter.instance_variable_set('@indent', options[:indent])
TreeWalker.new(nil, [formatter]).visit_multiline_arg(self)

Term::ANSIColor.coloring = c
Cucumber::Term::ANSIColor.coloring = c
io.rewind
s = "\n" + io.read + (" " * (options[:indent] - 2))
s
Expand Down
4 changes: 2 additions & 2 deletions lib/cucumber/cli/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def parse!(args)
opts.on("-c", "--[no-]color",
"Whether or not to use ANSI color in the output. Cucumber decides",
"based on your platform and the output destination if not specified.") do |v|
Term::ANSIColor.coloring = v
Cucumber::Term::ANSIColor.coloring = v
end
opts.on("-d", "--dry-run", "Invokes formatters without executing the steps.",
"This also omits the loading of your support/env.rb file if it exists.") do
Expand All @@ -206,7 +206,7 @@ def parse!(args)
"Be careful if you choose to overwrite the originals.",
"Implies --dry-run --formatter pretty.") do |directory|
@options[:autoformat] = directory
Term::ANSIColor.coloring = false
Cucumber::Term::ANSIColor.coloring = false
@options[:dry_run] = true
@quiet = true
end
Expand Down
19 changes: 7 additions & 12 deletions lib/cucumber/formatter/ansicolor.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
begin
require 'term/ansicolor'
rescue LoadError
require 'rubygems'
require 'term/ansicolor'
end
require 'cucumber/platform'
require 'cucumber/term/ansicolor'

if Cucumber::IRONRUBY
begin
Expand All @@ -17,11 +12,11 @@
if Cucumber::WINDOWS_MRI
unless ENV['ANSICON']
STDERR.puts %{*** WARNING: You must use ANSICON 1.31 or higher (http://adoxa.110mb.com/ansicon) to get coloured output on Windows}
Term::ANSIColor.coloring = false
Cucumber::Term::ANSIColor.coloring = false
end
end

Term::ANSIColor.coloring = false if !STDOUT.tty? && !ENV.has_key?("AUTOTEST")
Cucumber::Term::ANSIColor.coloring = false if !STDOUT.tty? && !ENV.has_key?("AUTOTEST")

module Cucumber
module Formatter
Expand Down Expand Up @@ -57,11 +52,11 @@ module Formatter
# (If you're on Windows, use SET instead of export).
# To see what colours and effects are available, just run this in your shell:
#
# ruby -e "require 'rubygems'; require 'term/ansicolor'; puts Term::ANSIColor.attributes"
# ruby -e "require 'rubygems'; require 'term/ansicolor'; puts Cucumber::Term::ANSIColor.attributes"
#
# Although not listed, you can also use <tt>grey</tt>
module ANSIColor
include Term::ANSIColor
include Cucumber::Term::ANSIColor

ALIASES = Hash.new do |h,k|
if k.to_s =~ /(.*)_param/
Expand Down Expand Up @@ -108,7 +103,7 @@ def self.define_grey #:nodoc:
when 0
raise "Your terminal doesn't support colours"
when 1
::Term::ANSIColor.coloring = false
::Cucumber::Term::ANSIColor.coloring = false
alias grey white
when 2..8
alias grey white
Expand All @@ -130,7 +125,7 @@ def self.define_grey #:nodoc:

def self.define_real_grey #:nodoc:
def grey(m) #:nodoc:
if ::Term::ANSIColor.coloring?
if ::Cucumber::Term::ANSIColor.coloring?
"\e[90m#{m}\e[0m"
else
m
Expand Down
2 changes: 1 addition & 1 deletion lib/cucumber/formatter/pretty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def table_cell_value(value, status)
cell_text = escape_cell(value.to_s || '')
padded = cell_text + (' ' * (width - cell_text.unpack('U*').length))
prefix = cell_prefix(status)
@io.print(' ' + format_string("#{prefix}#{padded}", status) + ::Term::ANSIColor.reset(" |"))
@io.print(' ' + format_string("#{prefix}#{padded}", status) + ::Cucumber::Term::ANSIColor.reset(" |"))
@io.flush
end

Expand Down
118 changes: 118 additions & 0 deletions lib/cucumber/term/ansicolor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
module Cucumber
module Term
# The ANSIColor module can be used for namespacing and mixed into your own
# classes.
module ANSIColor
# :stopdoc:
ATTRIBUTES = [
[ :clear , 0 ],
[ :reset , 0 ], # synonym for :clear
[ :bold , 1 ],
[ :dark , 2 ],
[ :italic , 3 ], # not widely implemented
[ :underline , 4 ],
[ :underscore , 4 ], # synonym for :underline
[ :blink , 5 ],
[ :rapid_blink , 6 ], # not widely implemented
[ :negative , 7 ], # no reverse because of String#reverse
[ :concealed , 8 ],
[ :strikethrough, 9 ], # not widely implemented
[ :black , 30 ],
[ :red , 31 ],
[ :green , 32 ],
[ :yellow , 33 ],
[ :blue , 34 ],
[ :magenta , 35 ],
[ :cyan , 36 ],
[ :white , 37 ],
[ :on_black , 40 ],
[ :on_red , 41 ],
[ :on_green , 42 ],
[ :on_yellow , 43 ],
[ :on_blue , 44 ],
[ :on_magenta , 45 ],
[ :on_cyan , 46 ],
[ :on_white , 47 ],
]

ATTRIBUTE_NAMES = ATTRIBUTES.transpose.first
# :startdoc:

# Returns true, if the coloring function of this module
# is switched on, false otherwise.
def self.coloring?
@coloring
end

# Turns the coloring on or off globally, so you can easily do
# this for example:
# Cucumber::Term::ANSIColor::coloring = STDOUT.isatty
def self.coloring=(val)
@coloring = val
end
self.coloring = true

ATTRIBUTES.each do |c, v|
eval %Q{
def #{c}(string = nil)
result = ''
result << "\e[#{v}m" if Cucumber::Term::ANSIColor.coloring?
if block_given?
result << yield
elsif string
result << string
elsif respond_to?(:to_str)
result << to_str
else
return result #only switch on
end
result << "\e[0m" if Cucumber::Term::ANSIColor.coloring?
result
end
}
end

# Regular expression that is used to scan for ANSI-sequences while
# uncoloring strings.
COLORED_REGEXP = /\e\[(?:[34][0-7]|[0-9])?m/


def self.included(klass)
if version_is_greater_than_18? and klass == String
ATTRIBUTES.delete(:clear)
ATTRIBUTE_NAMES.delete(:clear)
end
end

# Returns an uncolored version of the string, that is all
# ANSI-sequences are stripped from the string.
def uncolored(string = nil) # :yields:
if block_given?
yield.gsub(COLORED_REGEXP, '')
elsif string
string.gsub(COLORED_REGEXP, '')
elsif respond_to?(:to_str)
to_str.gsub(COLORED_REGEXP, '')
else
''
end
end

module_function

# Returns an array of all Cucumber::Term::ANSIColor attributes as symbols.
def attributes
ATTRIBUTE_NAMES
end
extend self

private

def version_is_greater_than_18?
version = RUBY_VERSION.split('.')
version.map! &:to_i
version[0] >= 1 && version[1] > 8
end
end
end
end
4 changes: 2 additions & 2 deletions spec/cucumber/cli/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,12 @@ def reset_config
end

it "should accept --color option" do
Term::ANSIColor.should_receive(:coloring=).with(true)
Cucumber::Term::ANSIColor.should_receive(:coloring=).with(true)
config.parse!(['--color'])
end

it "should accept --no-color option" do
Term::ANSIColor.should_receive(:coloring=).with(false)
Cucumber::Term::ANSIColor.should_receive(:coloring=).with(false)
config = Configuration.new(StringIO.new)
config.parse!(['--no-color'])
end
Expand Down
2 changes: 1 addition & 1 deletion spec/cucumber/formatter/ansicolor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Formatter
end

it "should not generate ansi codes when colors are disabled" do
::Term::ANSIColor.coloring = false
::Cucumber::Term::ANSIColor.coloring = false
passed("foo").should == "foo"
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/cucumber/formatter/progress_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Formatter
describe Progress do

before(:each) do
Term::ANSIColor.coloring = false
Cucumber::Term::ANSIColor.coloring = false
@out = StringIO.new
progress = Cucumber::Formatter::Progress.new(mock("step mother"), @out, {})
@visitor = Cucumber::Ast::TreeWalker.new(nil, [progress])
Expand Down

0 comments on commit bad2318

Please sign in to comment.