Skip to content

Commit

Permalink
Colorize output with --color
Browse files Browse the repository at this point in the history
Displays all violation in red
  • Loading branch information
justincampbell committed Feb 5, 2013
1 parent 9afd1be commit 652c2f8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/cane/cli/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ def add_cane_options
"Use all processors. Slower on small projects, faster on large.",
cast: ->(x) { x }

add_option %w(--color),
"Colorize output", default: false

parser.separator ""
end

Expand Down
2 changes: 1 addition & 1 deletion lib/cane/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(spec)
end

def run
outputter.print formatter.new(violations)
outputter.print formatter.new(violations, opts)

violations.length <= opts.fetch(:max_violations)
end
Expand Down
16 changes: 13 additions & 3 deletions lib/cane/violation_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ module Cane
# Computes a string to be displayed as output from an array of violations
# computed by the checks.
class ViolationFormatter
attr_reader :violations
attr_reader :violations, :options

def initialize(violations)
def initialize(violations, options = {})
@violations = violations.map do |v|
v.merge(file_and_line: v[:line] ?
"%s:%i" % v.values_at(:file, :line) :
v[:file]
)
end

@options = options
end

def to_s
return "" if violations.empty?

violations.group_by {|x| x[:description] }.map do |d, vs|
string = violations.group_by {|x| x[:description] }.map do |d, vs|
format_group_header(d, vs) +
format_violations(vs)
end.join("\n") + "\n\n" + totals + "\n\n"

colorize(string)
end

protected
Expand Down Expand Up @@ -54,6 +58,12 @@ def format_violation(v, column_widths)
}.join(' ').strip
end

def colorize(string)
return string unless options[:color]

"\e[31m#{string}\e[0m"
end

def totals
"Total Violations: #{violations.length}"
end
Expand Down
6 changes: 6 additions & 0 deletions spec/cane_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def violations
Cane.task_runner(parallel: true).should == Parallel
end

it 'colorizes output' do
output, exitstatus = run("--color --abc-max 0")

output.should include("\e[31m")
end

after do
if Object.const_defined?(class_name)
Object.send(:remove_const, class_name)
Expand Down
11 changes: 11 additions & 0 deletions spec/violation_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@ def violation(description)
result = described_class.new(violations).to_s
result.should include("Total Violations: 2")
end

it 'does not colorize output by default' do
result = described_class.new([violation("FAIL")]).to_s
result.should_not include("\e[31m")
end

it 'colorizes output when passed color: true' do
result = described_class.new([violation("FAIL")], color: true).to_s
result.should include("\e[31m")
result.should include("\e[0m")
end
end

0 comments on commit 652c2f8

Please sign in to comment.