Skip to content

Commit e2f6441

Browse files
committed
Colorizes text output
`csscss --color` (the default) and `csscss --no-color` are now available. Fixes zmoazeni#2
1 parent 62edd09 commit e2f6441

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ PATH
22
remote: .
33
specs:
44
csscss (0.1.0)
5+
colorize
56
parslet (~> 1.5)
67

78
GEM
89
remote: https://rubygems.org/
910
specs:
1011
blankslate (2.1.2.4)
12+
colorize (0.5.8)
1113
columnize (0.3.6)
1214
debugger (1.5.0)
1315
columnize (>= 0.3.1)

csscss.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ Gem::Specification.new do |gem|
1818
gem.require_paths = ["lib"]
1919

2020
gem.add_dependency "parslet", "~> 1.5"
21+
gem.add_dependency "colorize"
2122
end

lib/csscss.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require "optparse"
33
require "json"
44

5+
require "colorize"
56
require "parslet"
67
require "csscss/parslet_optimizations"
78

lib/csscss/cli.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module Csscss
22
class CLI
33
def initialize(argv)
4-
@argv = argv
4+
@argv = argv
55
@verbose = false
6+
@color = true
67
@minimum = 3
78
end
89

@@ -30,7 +31,7 @@ def execute
3031
if @json
3132
puts JSONReporter.new(combined_redundancies).report
3233
else
33-
puts Reporter.new(combined_redundancies).report(@verbose)
34+
puts Reporter.new(combined_redundancies).report(verbose:@verbose, color:true)
3435
end
3536

3637
rescue Parslet::ParseFailed => e
@@ -48,6 +49,10 @@ def parse(argv)
4849
@verbose = v
4950
end
5051

52+
opts.on("--[no-]color", "Colorizes output") do |c|
53+
@color = c
54+
end
55+
5156
opts.on("-n", "--num N", Integer, "Print matches with at least this many rules. Defaults to 3") do |n|
5257
@minimum = n
5358
end

lib/csscss/reporter.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,28 @@ def initialize(redundancies)
44
@redundancies = redundancies
55
end
66

7-
def report(verbose = false)
7+
def report(options = {})
8+
verbose = options.fetch(:verbose, false)
9+
should_color = options.fetch(:color, true)
10+
811
io = StringIO.new
912
@redundancies.each do |selector_groups, declarations|
10-
selector_groups = selector_groups.map {|selectors| "{#{selectors}}" }
13+
selector_groups = selector_groups.map {|selectors| maybe_color("{#{selectors}}", :red, should_color) }
1114
last_selector = selector_groups.pop
1215
count = declarations.size
13-
io.puts %Q(#{selector_groups.join(", ")} and #{last_selector} share #{count} rule#{"s" if count > 1})
16+
io.puts %Q(#{selector_groups.join(", ")} AND #{last_selector} share #{maybe_color(count, :red, should_color)} rule#{"s" if count > 1})
1417
if verbose
15-
declarations.each {|dec| io.puts " - #{dec}" }
18+
declarations.each {|dec| io.puts(maybe_color(" - #{dec}", :yellow, should_color)) }
1619
end
1720
end
1821

1922
io.rewind
2023
io.read
2124
end
25+
26+
private
27+
def maybe_color(string, color, condition)
28+
condition ? string.to_s.colorize(color) : string
29+
end
2230
end
2331
end

test/csscss/reporter_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ module Csscss
1212
})
1313

1414
expected =<<-EXPECTED
15-
{.foo} and {.bar} share 2 rules
16-
{h1, h2}, {.foo} and {.baz} share 1 rule
17-
{h1, h2} and {.bar} share 1 rule
15+
{.foo} AND {.bar} share 2 rules
16+
{h1, h2}, {.foo} AND {.baz} share 1 rule
17+
{h1, h2} AND {.bar} share 1 rule
1818
EXPECTED
19-
reporter.report.must_equal expected
19+
reporter.report(color:false).must_equal expected
2020

2121
expected =<<-EXPECTED
22-
{.foo} and {.bar} share 2 rules
22+
{.foo} AND {.bar} share 2 rules
2323
- width: 1px
2424
- border: black
25-
{h1, h2}, {.foo} and {.baz} share 1 rule
25+
{h1, h2}, {.foo} AND {.baz} share 1 rule
2626
- display: none
27-
{h1, h2} and {.bar} share 1 rule
27+
{h1, h2} AND {.bar} share 1 rule
2828
- position: relative
2929
EXPECTED
30-
reporter.report(true).must_equal expected
30+
reporter.report(verbose:true, color:false).must_equal expected
3131
end
3232
end
3333
end

0 commit comments

Comments
 (0)