pastel 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +57 -13
  3. data/README.md +36 -29
  4. data/lib/pastel.rb +17 -19
  5. data/lib/pastel/alias_importer.rb +9 -7
  6. data/lib/pastel/ansi.rb +2 -2
  7. data/lib/pastel/color.rb +63 -23
  8. data/lib/pastel/color_parser.rb +24 -18
  9. data/lib/pastel/color_resolver.rb +3 -1
  10. data/lib/pastel/decorator_chain.rb +47 -7
  11. data/lib/pastel/delegator.rb +57 -26
  12. data/lib/pastel/detached.rb +41 -5
  13. data/lib/pastel/version.rb +2 -2
  14. metadata +27 -97
  15. data/.gitignore +0 -22
  16. data/.rspec +0 -3
  17. data/.travis.yml +0 -26
  18. data/Gemfile +0 -16
  19. data/Rakefile +0 -8
  20. data/appveyor.yml +0 -23
  21. data/assets/pastel_logo.png +0 -0
  22. data/assets/screenshot.png +0 -0
  23. data/benchmarks/nesting_speed.rb +0 -41
  24. data/benchmarks/speed.rb +0 -45
  25. data/examples/palette.rb +0 -14
  26. data/pastel.gemspec +0 -26
  27. data/spec/spec_helper.rb +0 -45
  28. data/spec/unit/alias_color_spec.rb +0 -24
  29. data/spec/unit/alias_importer_spec.rb +0 -29
  30. data/spec/unit/color/alias_color_spec.rb +0 -40
  31. data/spec/unit/color/code_spec.rb +0 -24
  32. data/spec/unit/color/colored_spec.rb +0 -15
  33. data/spec/unit/color/decorate_spec.rb +0 -79
  34. data/spec/unit/color/equal_spec.rb +0 -22
  35. data/spec/unit/color/lookup_spec.rb +0 -17
  36. data/spec/unit/color/new_spec.rb +0 -10
  37. data/spec/unit/color/strip_spec.rb +0 -56
  38. data/spec/unit/color/styles_spec.rb +0 -10
  39. data/spec/unit/color/valid_spec.rb +0 -19
  40. data/spec/unit/color_parser_spec.rb +0 -67
  41. data/spec/unit/decorate_dsl_spec.rb +0 -98
  42. data/spec/unit/decorator_chain_spec.rb +0 -47
  43. data/spec/unit/delegator_spec.rb +0 -38
  44. data/spec/unit/detach_spec.rb +0 -48
  45. data/spec/unit/new_spec.rb +0 -63
  46. data/spec/unit/respond_to_spec.rb +0 -17
  47. data/spec/unit/undecorate_spec.rb +0 -12
  48. data/tasks/console.rake +0 -11
  49. data/tasks/coverage.rake +0 -11
  50. data/tasks/spec.rake +0 -29
@@ -1,22 +0,0 @@
1
- # encoding: utf-8
2
-
3
- RSpec.describe Pastel::Color, '#==' do
4
- it "is true with the same enabled and eachline attributes" do
5
- expect(Pastel::Color.new(enabled: false, eachline: "\n")).
6
- to eq(Pastel::Color.new(enabled: false, eachline: "\n"))
7
- end
8
-
9
- it "is false with different enabled attribute" do
10
- expect(Pastel::Color.new(enabled: true, eachline: "\n")).
11
- not_to eq(Pastel::Color.new(enabled: false, eachline: "\n"))
12
- end
13
-
14
- it "is false with different eachline attribute" do
15
- expect(Pastel::Color.new(enabled: false, eachline: "\n")).
16
- not_to eq(Pastel::Color.new(enabled: false, eachline: "\r\n"))
17
- end
18
-
19
- it "is false with non-color" do
20
- expect(Pastel::Color.new(enabled: true)).not_to eq(:other)
21
- end
22
- end
@@ -1,17 +0,0 @@
1
- # encoding: utf-8
2
-
3
- RSpec.describe Pastel::Color, '#lookup' do
4
- it "looksup colors" do
5
- color = described_class.new(enabled: true)
6
- expect(color.lookup(:red, :on_green, :bold)).to eq("\e[31;42;1m")
7
- end
8
-
9
- it "caches color lookups" do
10
- color = described_class.new(enabled: true)
11
- allow(color).to receive(:code).and_return([31])
12
- color.lookup(:red, :on_green)
13
- color.lookup(:red, :on_green)
14
- color.lookup(:red, :on_green)
15
- expect(color).to have_received(:code).once
16
- end
17
- end
@@ -1,10 +0,0 @@
1
- # encoding: utf-8
2
-
3
- RSpec.describe Pastel::Color, '::new' do
4
- it "allows to disable coloring" do
5
- color = described_class.new(enabled: false)
6
-
7
- expect(color.enabled?).to eq(false)
8
- expect(color.decorate("Unicorn", :red)).to eq("Unicorn")
9
- end
10
- end
@@ -1,56 +0,0 @@
1
- # encoding: utf-8
2
-
3
- RSpec.describe Pastel::Color, '.strip' do
4
-
5
- subject(:color) { described_class.new(enabled: true) }
6
-
7
- it 'strips ansi color from string' do
8
- string = "This is a \e[1m\e[34mbold blue text\e[0m"
9
- expect(color.strip(string)).to eq('This is a bold blue text')
10
- end
11
-
12
- it "strips partial ansi color" do
13
- string = "foo\e[1mbar"
14
- expect(color.strip(string)).to eq('foobar')
15
- end
16
-
17
- it 'preserves movement characters' do
18
- # [176A - move cursor up n lines
19
- expect(color.strip("foo\e[176Abar")).to eq("foo\e[176Abar")
20
- end
21
-
22
- it 'strips reset/setfg/setbg/italics/strike/underline sequence' do
23
- string = "\x1b[0;33;49;3;9;4mfoo\x1b[0m"
24
- expect(color.strip(string)).to eq("foo")
25
- end
26
-
27
- it 'strips octal in encapsulating brackets' do
28
- string = "\[\033[01;32m\]u@h \[\033[01;34m\]W $ \[\033[00m\]"
29
- expect(color.strip(string)).to eq('[]u@h []W $ []')
30
- end
31
-
32
- it 'strips octal codes without brackets' do
33
- string = "\033[01;32mu@h \033[01;34mW $ \033[00m"
34
- expect(color.strip(string)).to eq('u@h W $ ')
35
- end
36
-
37
- it 'strips octal with multiple colors' do
38
- string = "\e[3;0;0;mfoo\e[8;50;0m"
39
- expect(color.strip(string)).to eq('foo')
40
- end
41
-
42
- it "strips multiple colors delimited by :" do
43
- string = "\e[31:44:4mfoo\e[0m"
44
- expect(color.strip(string)).to eq('foo')
45
- end
46
-
47
- it 'strips control codes' do
48
- string = "WARN. \x1b[1m&\x1b[0m ERR. \x1b[7m&\x1b[0m"
49
- expect(color.strip(string)).to eq('WARN. & ERR. &')
50
- end
51
-
52
- it 'strips escape bytes' do
53
- string = "This is a \e[1m\e[34mbold blue text\e[0m"
54
- expect(color.strip(string)).to eq("This is a bold blue text")
55
- end
56
- end
@@ -1,10 +0,0 @@
1
- # coding: utf-8
2
-
3
- RSpec.describe Pastel::Color, '#styles' do
4
-
5
- subject(:color) { described_class.new(enabled: true) }
6
-
7
- it "exposes all available style ANSI codes" do
8
- expect(color.styles[:red]).to eq(31)
9
- end
10
- end
@@ -1,19 +0,0 @@
1
- # encoding: utf-8
2
-
3
- RSpec.describe Pastel::Color, '.valid?' do
4
- it "detects valid colors" do
5
- color = described_class.new
6
- expect(color.valid?(:red, :on_green, :bold)).to eq(true)
7
- end
8
-
9
- it "detects valid color aliases" do
10
- color = described_class.new
11
- color.alias_color(:funky, :red)
12
- expect(color.valid?(:funky)).to eq(true)
13
- end
14
-
15
- it "detects invalid color" do
16
- color = described_class.new
17
- expect(color.valid?(:red, :unknown)).to eq(false)
18
- end
19
- end
@@ -1,67 +0,0 @@
1
- # encoding: utf-8
2
-
3
- RSpec.describe Pastel::ColorParser, '::parse' do
4
- subject(:parser) { described_class }
5
-
6
- it "parses string with no color" do
7
- expect(parser.parse("foo")).to eq([{text: 'foo'}])
8
- end
9
-
10
- it "parses simple color" do
11
- expect(parser.parse("\e[32mfoo\e[0m")).to eq([
12
- {foreground: :green, text: 'foo'}
13
- ])
14
- end
15
-
16
- it "parses simple color and style" do
17
- expect(parser.parse("\e[32;1mfoo\e[0m")).to eq([
18
- {foreground: :green, style: :bold, text: 'foo'}
19
- ])
20
- end
21
-
22
- it "parses chained colors in shorthand syntax" do
23
- expect(parser.parse("\e[32;44mfoo\e[0m")).to eq([
24
- {foreground: :green, background: :on_blue, text: 'foo'}
25
- ])
26
- end
27
-
28
- it "parses chained colors in regular syntax" do
29
- expect(parser.parse("\e[32m\e[44mfoo\e[0m")).to eq([
30
- {foreground: :green, background: :on_blue, text: 'foo'}
31
- ])
32
- end
33
-
34
- it "parses many colors" do
35
- expect(parser.parse("\e[32mfoo\e[0m \e[31mbar\e[0m")).to eq([
36
- {foreground: :green, text: 'foo'},
37
- {text: ' '},
38
- {foreground: :red, text: 'bar'}
39
- ])
40
- end
41
-
42
- it "parses nested colors with one reset" do
43
- expect(parser.parse("\e[32mfoo\e[31mbar\e[0m")).to eq([
44
- {foreground: :green, text: 'foo'},
45
- {foreground: :red, text: 'bar'}
46
- ])
47
- end
48
-
49
- it "parses nested colors with two resets" do
50
- expect(parser.parse("\e[32mfoo\e[31mbar\e[0m\e[0m")).to eq([
51
- {foreground: :green, text: 'foo'},
52
- {foreground: :red, text: 'bar'}
53
- ])
54
- end
55
-
56
- it "parses unrest color" do
57
- expect(parser.parse("\e[32mfoo")).to eq([
58
- {foreground: :green, text: 'foo'}
59
- ])
60
- end
61
-
62
- it "parses malformed control sequence" do
63
- expect(parser.parse("\eA foo bar ESC\e")).to eq([
64
- {text: "\eA foo bar ESC\e"}
65
- ])
66
- end
67
- end
@@ -1,98 +0,0 @@
1
- # encoding: utf-8
2
-
3
- RSpec.describe Pastel, 'coloring dsl' do
4
-
5
- subject(:pastel) { described_class.new(enabled: true) }
6
-
7
- it "colors string" do
8
- expect(pastel.red("unicorn")).to eq("\e[31municorn\e[0m")
9
- end
10
-
11
- it "allows to specify variable number of arguments" do
12
- expect(pastel.red("unicorn", "running")).to eq("\e[31municornrunning\e[0m")
13
- end
14
-
15
- it "combines colored strings with regular ones" do
16
- expect(pastel.red("Unicorns") + ' will rule ' + pastel.green('the World!')).
17
- to eq("\e[31mUnicorns\e[0m will rule \e[32mthe World!\e[0m")
18
- end
19
-
20
- it "composes two color strings " do
21
- expect(pastel.red.on_green("unicorn")).to eq("\e[31;42municorn\e[0m")
22
- end
23
-
24
- it "composes three color strings" do
25
- expect(pastel.red.on_green.underline("unicorn")).
26
- to eq("\e[31;42;4municorn\e[0m")
27
- end
28
-
29
- it "combines colored composed strings with regular ones" do
30
- expect(pastel.red.on_green("Unicorns") + ' will rule ' +
31
- pastel.green.on_red('the World!')).
32
- to eq("\e[31;42mUnicorns\e[0m will rule \e[32;41mthe World!\e[0m")
33
- end
34
-
35
- it "allows one level nesting" do
36
- expect(pastel.red("Unicorn" + pastel.blue("rule!"))).
37
- to eq("\e[31mUnicorn\e[34mrule!\e[0m\e[0m")
38
- end
39
-
40
- it "allows to nest mixed styles" do
41
- expect(pastel.red("Unicorn" + pastel.green.on_yellow.underline('running') + '!')).
42
- to eq("\e[31mUnicorn\e[32;43;4mrunning\e[0m\e[31m!\e[0m")
43
- end
44
-
45
- it "allows for deep nesting" do
46
- expect(pastel.red('r' + pastel.green('g' + pastel.yellow('y') + 'g') + 'r')).
47
- to eq("\e[31mr\e[32mg\e[33my\e[0m\e[32mg\e[0m\e[31mr\e[0m")
48
- end
49
-
50
- it "allows for variable nested arguments" do
51
- expect(pastel.red('r', pastel.green('g'), 'r')).
52
- to eq("\e[31mr\e[32mg\e[0m\e[31mr\e[0m")
53
- end
54
-
55
- it "nests color foreground & background" do
56
- expect(pastel.on_red('foo', pastel.green('bar'), 'foo')).
57
- to eq("\e[41mfoo\e[32mbar\e[0m\e[41mfoo\e[0m")
58
- end
59
-
60
- it "allows to nest styles within block" do
61
- string = pastel.red.on_green('Unicorns' +
62
- pastel.green.on_red('will ', 'dominate' + pastel.yellow('the world!')))
63
-
64
- expect(pastel.red.on_green('Unicorns') do
65
- green.on_red('will ', 'dominate') do
66
- yellow('the world!')
67
- end
68
- end).to eq(string)
69
- end
70
-
71
- it "doesn't decorate nil" do
72
- expect(pastel.red(nil)).to eq('')
73
- end
74
-
75
- it "doesn't apply styles to empty string" do
76
- expect(pastel.red('')).to eq('')
77
- end
78
-
79
- it "applies styles to empty with width more than 1" do
80
- expect(pastel.red(' ')).to eq("\e[31m \e[0m")
81
- end
82
-
83
- it "applies color only once" do
84
- expect(pastel.red.red.red("unicorn")).to eq(pastel.red("unicorn"))
85
- end
86
-
87
- it "raises error when chained with unrecognized color" do
88
- expect {
89
- pastel.unknown.on_red('unicorn')
90
- }.to raise_error(Pastel::InvalidAttributeNameError)
91
- end
92
-
93
- it "raises error when doesn't recognize color" do
94
- expect {
95
- pastel.unknown('unicorn')
96
- }.to raise_error(Pastel::InvalidAttributeNameError)
97
- end
98
- end
@@ -1,47 +0,0 @@
1
- # coding: utf-8
2
-
3
- RSpec.describe Pastel::DecoratorChain do
4
- it "is enumerable" do
5
- expect(described_class.new).to be_a(Enumerable)
6
- end
7
-
8
- it "is equatable" do
9
- expect(described_class.new).to be_a(Equatable)
10
- end
11
-
12
- describe ".each" do
13
- it "yields each decorator" do
14
- first = double('first')
15
- second = double('second')
16
- chain = described_class.new.add(first).add(second)
17
- yielded = []
18
-
19
- expect {
20
- chain.each { |decorator| yielded << decorator }
21
- }.to change { yielded }.from([]).to([first, second])
22
- end
23
- end
24
-
25
- describe ".==" do
26
- it "is equivalent with the same decorator" do
27
- expect(described_class.new.add(:foo).add(:bar)).
28
- to eq(described_class.new.add(:foo).add(:bar))
29
- end
30
-
31
- it "is not equivalent with different decorator" do
32
- expect(described_class.new.add(:foo).add(:bar)).
33
- not_to eq(described_class.new.add(:foo).add(:baz))
34
- end
35
-
36
- it "is not equivalent to another type" do
37
- expect(described_class.new.add(:foo).add(:bar)).
38
- not_to eq(:other)
39
- end
40
- end
41
-
42
- describe ".inspect" do
43
- it "displays object information" do
44
- expect(described_class.new.inspect).to match(/decorators=\[\]/)
45
- end
46
- end
47
- end
@@ -1,38 +0,0 @@
1
- # coding: utf-8
2
-
3
- RSpec.describe Pastel::Delegator do
4
-
5
- it "returns delegator for color without argument" do
6
- pastel = Pastel.new(enabled: true)
7
- expect(pastel.red).to be_a(Pastel::Delegator)
8
- end
9
-
10
- describe ".inspect" do
11
- it "inspects delegator styles chain" do
12
- chain = ['red', 'on_green']
13
- delegator = described_class.new(:resolver, chain)
14
- allow(delegator).to receive(:styles).and_return({red: 31, on_green: 42})
15
- expect(delegator.inspect).to eq("#<Pastel @styles=[\"red\", \"on_green\"]>")
16
- end
17
- end
18
-
19
- describe ".respond_to_missing?" do
20
- context 'for a method defined on' do
21
- it "returns true" do
22
- resolver = double(:resolver)
23
- chain = double(:chain)
24
- decorator = described_class.new(resolver, chain)
25
- expect(decorator.method(:styles)).not_to be_nil
26
- end
27
- end
28
-
29
- context "for an undefined method " do
30
- it "returns false" do
31
- resolver = double(:resolver, color: true)
32
- chain = double(:chain)
33
- decorator = described_class.new(resolver, chain)
34
- expect { decorator.method(:unknown) }.to raise_error(NameError)
35
- end
36
- end
37
- end
38
- end
@@ -1,48 +0,0 @@
1
- # coding: utf-8
2
-
3
- RSpec.describe Pastel, '.detach' do
4
-
5
- subject(:pastel) { described_class.new(enabled: true) }
6
-
7
- it "creates detached instance" do
8
- error = pastel.red.bold.detach
9
- expect(error).to be_a(Pastel::Detached)
10
- end
11
-
12
- it "ensures instance is immutable" do
13
- error = pastel.red.detach
14
- expect(error.frozen?).to be(true)
15
- end
16
-
17
- it "detaches colors combination" do
18
- error = pastel.red.bold.detach
19
- expect(error.call('unicorn')).to eq("\e[31;1municorn\e[0m")
20
- expect(error.call('error')).to eq("\e[31;1merror\e[0m")
21
- end
22
-
23
- it "allows array like access" do
24
- error = pastel.red.bold.detach
25
- expect(error['unicorn']).to eq("\e[31;1municorn\e[0m")
26
- end
27
-
28
- it "allows alternative call invocation" do
29
- error = pastel.red.bold.detach
30
- expect(error.('unicorn')).to eq("\e[31;1municorn\e[0m")
31
- end
32
-
33
- it "calls detached colors with no arguments" do
34
- warning = pastel.yellow.detach
35
- expect(warning.call('')).to eq('')
36
- end
37
-
38
- it "inspects detached colors" do
39
- warning = pastel.yellow.bold.detach
40
- expect(warning.inspect).to eq('#<Pastel::Detached styles=[:yellow, :bold]>')
41
- end
42
-
43
- it "accepts multiple strings" do
44
- error = pastel.red.bold.detach
45
- expect(error.call('Unicorns', ' run ', 'wild')).
46
- to eq("\e[31;1mUnicorns run wild\e[0m")
47
- end
48
- end