cape-cod 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -53,15 +53,21 @@ You can include cape-cod in you String class:
53
53
 
54
54
  puts 'This is BOLD'.bold
55
55
 
56
- puts 'and this is ITALIC'.fx(:italic) # You should probably avoid *italics* :\
56
+ puts 'and this is ITALIC'.fx(:italic) # You're not using this, right?
57
57
 
58
58
  puts 'Black n white'.fg(:black).on_white
59
59
 
60
60
  puts 'Magenta background'.bg(:magenta)
61
61
 
62
+ puts 'ZOMG THERE'S RGB AS WELL' background'.bg(220, 112, 234).fg(0xa30fd4)
63
+
62
64
  or use it like this:
63
65
 
64
- ```puts CapeCod.yellow('We all live in a yellow submarine!')```
66
+ puts CapeCod.yellow('We all live in a yellow submarine!')
67
+
68
+ puts CapeCod.fx :bold, 'BOOM!'
69
+
70
+ puts CapeCod.fg(255, 0, 255, 'Super magenta!')
65
71
 
66
72
  All the public instance methods are available as module methods.
67
73
 
data/lib/cape-cod.rb CHANGED
@@ -74,51 +74,50 @@ module CapeCod
74
74
  end
75
75
  end
76
76
 
77
- def foreground(color) # :nodoc:
78
- CapeCod.foreground(color, self)
77
+ def foreground(*color) # :nodoc:
78
+ CapeCod.foreground(*color, self)
79
79
  end
80
80
 
81
- def background(color) # :nodoc:
82
- CapeCod.background(color, self)
81
+ def background(*color) # :nodoc:
82
+ CapeCod.background(*color, self)
83
83
  end
84
84
 
85
85
  def effect(effect) # :nodoc:
86
86
  CapeCod.effect(effect, self)
87
87
  end
88
88
 
89
- alias_method :color, :foreground
90
- alias_method :fg, :foreground
91
- alias_method :bg, :background
92
- alias_method :fx, :effect
89
+ alias_method :fg, :foreground
90
+ alias_method :bg, :background
91
+ alias_method :fx, :effect
93
92
 
94
93
  class << self
95
94
 
96
95
  attr_accessor :enabled
96
+ alias_method :enabled?, :enabled
97
97
 
98
- def foreground(color, target) # :nodoc:
99
- apply_escape_sequence(color_code_for(color, :foreground), target)
98
+ def foreground(*color, target) # :nodoc:
99
+ apply_escape_sequence(color_code_for(*color, :foreground), target)
100
100
  end
101
101
 
102
- def background(color, target) # :nodoc:
103
- apply_escape_sequence(color_code_for(color, :background), target)
102
+ def background(*color, target) # :nodoc:
103
+ apply_escape_sequence(color_code_for(*color, :background), target)
104
104
  end
105
105
 
106
106
  def effect(effect, target) # :nodoc:
107
107
  apply_escape_sequence(effect_code_for(effect), target)
108
108
  end
109
109
 
110
- alias_method :color, :foreground
111
- alias_method :fg, :foreground
112
- alias_method :bg, :background
113
- alias_method :fx, :effect
110
+ alias_method :fg, :foreground
111
+ alias_method :bg, :background
112
+ alias_method :fx, :effect
114
113
 
115
114
  protected
116
115
 
117
116
  #
118
117
  # Returns the ANSI escape sequence for the given +color+.
119
118
  #
120
- def color_code_for(color, ground)
121
- Color.new(color, ground).ansi_code
119
+ def color_code_for(*color, ground)
120
+ Color.new(*color, ground).ansi_code
122
121
  end
123
122
 
124
123
  #
@@ -138,18 +137,30 @@ module CapeCod
138
137
  end
139
138
 
140
139
  #
141
- # Prepends the given +string+ with the ANSI escape sequence for the
140
+ # Prepends the given +target+ with the ANSI escape sequence for the
142
141
  # given escape +code+. In case string is not empty, also appends a
143
142
  # reset sequence.
144
143
  #
145
- def apply_escape_sequence(code, string)
146
- return string unless self.enabled
144
+ def apply_escape_sequence(code, target)
145
+ return target unless self.enabled?
147
146
 
148
- escape_sequence_for(code).tap do |s|
147
+ string = target.to_s
148
+
149
+ escape_sequence_for(code).tap do |s|
149
150
  unless string.nil? || string.empty?
150
151
  s << string << escape_sequence_for(effect_code_for(:reset))
151
152
  end
152
153
  end
153
154
  end
155
+
156
+ def ensure_windows_dependencies!
157
+ if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
158
+ require 'Win32/Console/ANSI'
159
+ end
160
+ rescue LoadError
161
+ self.enabled = false
162
+ end
154
163
  end
164
+
165
+ ensure_windows_dependencies!
155
166
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module CapeCod
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.3'
5
5
  end
@@ -4,19 +4,33 @@ require 'spec_helper'
4
4
 
5
5
  describe CapeCod do
6
6
  describe CapeCod::Color do
7
- context 'when instantiated with valid args' do
8
- it 'produces proper color representation' do
9
- color_from_symbol = CapeCod::Color.new(:yellow, :foreground).ansi_code
10
- color_from_hex = CapeCod::Color.new(0xffff00, :foreground).ansi_code
11
- color_from_rgb = CapeCod::Color.new(255, 255, 0, :foreground).ansi_code
7
+ context 'instantiated with valid args' do
8
+ context 'passed a three integer RGB representation' do
9
+ it 'produces proper color representation' do
10
+ expect(
11
+ CapeCod::Color.new(255, 255, 0, :foreground).ansi_code
12
+ ).to eql '38;5;210'
13
+ end
14
+ end
15
+
16
+ context 'passed an RGB integer representation' do
17
+ it 'produces proper color representation' do
18
+ expect(
19
+ CapeCod::Color.new(0xffff00, :foreground).ansi_code
20
+ ).to eql '38;5;210'
21
+ end
22
+ end
12
23
 
13
- expect(color_from_symbol).to eql '33'
14
- expect(color_from_hex).to eql '38;5;210'
15
- expect(color_from_rgb).to eql '38;5;210'
24
+ context 'passed a color name' do
25
+ it 'produces proper color representation' do
26
+ expect(
27
+ CapeCod::Color.new(:yellow, :foreground).ansi_code
28
+ ).to eql '33'
29
+ end
16
30
  end
17
31
  end
18
32
 
19
- context 'when instantiated with invalid args' do
33
+ context 'instantiated with invalid args' do
20
34
  it 'fails initialization' do
21
35
  expect {
22
36
  CapeCod::Color.new(:not_a_color, :foreground).ansi_code
@@ -5,80 +5,197 @@ require 'spec_helper'
5
5
  describe CapeCod do
6
6
  it('has a version') { expect(CapeCod::VERSION).to be_a String }
7
7
 
8
- context 'when disabled' do
9
- before do
10
- class String; include CapeCod end
8
+ context 'disabled' do
9
+ before(:all) { CapeCod.enabled = false }
11
10
 
12
- CapeCod.enabled = false
11
+ context 'using singleton methods' do
12
+ it('does nothing') { expect(CapeCod.bold).to be_empty }
13
13
  end
14
14
 
15
- it 'does nothing' do
16
- expect(CapeCod.bold).to be_empty
17
- expect(CapeCod.red('some text')).to eql('some text')
18
- expect('some text'.red.bold.fx(:italic).bg(:cyan)).to eql('some text')
15
+ context 'using instance methods' do
16
+ before do
17
+ class StringWithCapeCodIncluded < String; include CapeCod end
18
+ end
19
+
20
+ let(:target) { StringWithCapeCodIncluded.new('some text') }
21
+
22
+ it 'does nothing' do
23
+ expect(target.red.bold.fx(:italic).bg(:cyan)).to eql target
24
+ end
19
25
  end
20
26
  end
21
27
 
22
- context 'when enabled' do
23
- before(:all) do CapeCod.enabled = true end
28
+ context 'enabled' do
29
+ before(:all) { CapeCod.enabled = true }
24
30
 
25
- it 'generates proper escape sequences' do
26
- expect(CapeCod.reset).to eq("\e[0m")
27
- end
31
+ let(:target) { 'some text' }
32
+
33
+ context 'using singleton methods' do
34
+ let(:reset) { %(\e[0m) }
35
+ let(:bold) { %(\e[1m) }
36
+ let(:italic) { %(\e[3m) }
37
+ let(:red) { %(\e[31m) }
38
+ let(:on_red) { %(\e[41m) }
39
+ let(:rgb_red_fg) { %(\e[38;5;180m) }
40
+ let(:rgb_red_bg) { %(\e[48;5;180m) }
41
+
42
+ it { should respond_to :fx }
43
+ it { should respond_to :fg }
44
+ it { should respond_to :bg }
28
45
 
29
- context 'when using singleton methods' do
30
46
  context 'no params given' do
31
- it 'returns the escape sequence' do
32
- expect(CapeCod.red).to eq("\e[31m")
33
- expect(CapeCod.on_red).to eq("\e[41m")
34
- expect(CapeCod.bold).to eq("\e[1m")
47
+ it 'returns the effect escape sequence' do
48
+ expect(CapeCod.bold).to eq(bold)
49
+ end
50
+
51
+ it 'returns the foreground color escape sequence' do
52
+ expect(CapeCod.red).to eq(red)
53
+ end
54
+
55
+ it 'returns the background color escape sequence' do
56
+ expect(CapeCod.on_red).to eq(on_red)
35
57
  end
36
58
  end
37
59
 
38
- context 'when object param given' do
39
- let(:obj) { ['foo', 10, :bar] }
60
+ context 'object param given' do
61
+ let(:target) { ['foo', 10, :bar] }
62
+
63
+ it 'returns a new object' do
64
+ expect(CapeCod.bold(target).object_id).to_not eql(target.object_id)
65
+ end
66
+
67
+ context 'using direct method' do
68
+ it "escapes the object with the effect's sequence" do
69
+ expect(CapeCod.bold(target)).to eq("#{bold}#{target}#{reset}")
70
+ end
71
+
72
+ it "escapes the object with the foreground color's sequence" do
73
+ expect(CapeCod.red(target)).to eq("#{red}#{target}#{reset}")
74
+ end
75
+
76
+ it "escapes the object with the background color's sequence" do
77
+ expect(CapeCod.on_red(target)).to eq("#{on_red}#{target}#{reset}")
78
+ end
79
+ end
40
80
 
41
- it 'prepends the escape sequence and append a reset' do
81
+ context 'passing a symbol with color/effect name' do
82
+ it "escapes the object with the effect's sequence" do
83
+ expect(CapeCod.fx(:bold, target)).to eq("#{bold}#{target}#{reset}")
84
+ end
85
+
86
+ context 'colorization' do
87
+ context 'using color names' do
88
+ it "escapes the object with the foreground color's sequence" do
89
+ expect(CapeCod.fg(:red, target)).to(
90
+ eq("#{red}#{target}#{reset}"))
91
+ end
92
+
93
+ it "escapes the object with the background color's sequence" do
94
+ expect(CapeCod.bg(:red, target)).to(
95
+ eq("#{on_red}#{target}#{reset}"))
96
+ end
97
+ end
98
+
99
+ context 'using RGB' do
100
+ context 'passing three integers' do
101
+ it "escapes the object with the foreground color's sequence" do
102
+ expect(CapeCod.fg(255, 0, 0, target)).to(
103
+ eq("#{rgb_red_fg}#{target}#{reset}"))
104
+ end
105
+
106
+ it "escapes the object with the background color's sequence" do
107
+ expect(CapeCod.bg(255, 0, 0, target)).to(
108
+ eq("#{rgb_red_bg}#{target}#{reset}"))
109
+ end
110
+ end
111
+
112
+ context 'passing a single integer (hex representation)' do
113
+ it "escapes the object with the foreground color's sequence" do
114
+ expect(CapeCod.fg(0xff0000, target)).to(
115
+ eq("#{rgb_red_fg}#{target}#{reset}"))
116
+ end
117
+
118
+ it "escapes the object with the background color's sequence" do
119
+ expect(CapeCod.bg(0xff0000, target)).to(
120
+ eq("#{rgb_red_bg}#{target}#{reset}"))
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
42
126
 
43
- expect(CapeCod.red(obj)).to eq("\e[31m#{obj.to_s}\e[0m")
44
- expect(CapeCod.on_red(obj)).to eq("\e[41m#{obj.to_s}\e[0m")
45
- expect(CapeCod.bold(obj)).to eq("\e[1m#{obj.to_s}\e[0m")
127
+ it 'works properly with multiple calls passing the same object' do
128
+ expect(
129
+ CapeCod.red(CapeCod.italic(CapeCod.bold(CapeCod.on_red(target))))
130
+ ).to eql(
131
+ ''.tap do |s|
132
+ s << "#{red}#{italic}#{bold}#{on_red}"
133
+ s << "#{target}"
134
+ s << "#{reset}#{reset}#{reset}#{reset}"
135
+ end
136
+ )
46
137
  end
47
138
  end
48
139
  end
49
140
 
50
- context 'when using instance methods' do
51
- before { class String; include CapeCod end }
141
+ context 'using instance methods' do
142
+ before(:all) { class String; include CapeCod end }
52
143
 
53
- let(:string) { 'foo bar baz' }
54
- let(:bold) { "\e[1m#{string}\e[0m" }
55
- let(:red) { "\e[31m#{string}\e[0m" }
56
- let(:on_yellow) { "\e[43m#{string}\e[0m" }
57
- let(:r_on_y) { "\e[43m\e[31mfoo bar baz\e[0m\e[0m" }
58
- let(:r_on_y_b) { "\e[1m\e[43m\e[31mfoo bar baz\e[0m\e[0m\e[0m" }
144
+ it('provides the "fx" alias') { String.public_method_defined? :fx }
145
+ it('provides the "fg" alias') { String.public_method_defined? :fg }
146
+ it('provides the "bg" alias') { String.public_method_defined? :bg }
59
147
 
148
+ context 'using direct methods' do
149
+ it 'returns a new string' do
150
+ expect(target.red.object_id).to_not eql(target.object_id)
151
+ end
60
152
 
61
- it 'returns a new string with the proper escape codes applied' do
153
+ it 'behaves the same way as singleton methods for foreground colors' do
154
+ expect(target.red).to eql(CapeCod.red(target))
155
+ end
62
156
 
63
- expect(string.bold).to_not eql(string)
157
+ it 'behaves the same way as singleton methods for background colors' do
158
+ expect(target.on_red).to eql(CapeCod.on_red(target))
159
+ end
64
160
 
65
- expect(string.bold).to eql(bold)
66
- expect(string.red).to eql(red)
67
- expect(string.on_yellow).to eql(on_yellow)
161
+ it 'behaves the same way as singleton methods for effects' do
162
+ expect(target.bold).to eql(CapeCod.bold(target))
163
+ end
68
164
 
69
- expect(string.effect(:bold)).to eql(bold)
70
- expect(string.foreground(:red)).to eql(red)
71
- expect(string.background(:yellow)).to eql(on_yellow)
72
- expect(string.fx(:bold)).to eql(bold)
73
- expect(string.fg(:red)).to eql(red)
74
- expect(string.bg(:yellow)).to eql(on_yellow)
165
+ it 'behaves the same way as singleton methods for chained calls' do
166
+ expect(
167
+ target.on_red.bold.italic.red
168
+ ).to eql(
169
+ CapeCod.red(CapeCod.italic(CapeCod.bold(CapeCod.on_red(target))))
170
+ )
171
+ end
172
+ end
75
173
 
76
- expect(string.red.on_yellow).to eql(r_on_y)
77
- expect(string.fg(:red).bg(:yellow)).to eql(r_on_y)
174
+ context 'passing a symbol with color/effect name' do
175
+ it 'returns a new string' do
176
+ expect(target.red.object_id).to_not eql(target.object_id)
177
+ end
78
178
 
79
- expect(string.red.on_yellow.bold).to eql(r_on_y_b)
80
- expect(string.fg(:red).bg(:yellow).effect(:bold)).to eql(r_on_y_b)
179
+ it 'behaves the same way as singleton methods for foreground colors' do
180
+ expect(target.foreground(:red)).to eql(CapeCod.red(target))
181
+ end
182
+
183
+ it 'behaves the same way as singleton methods for background colors' do
184
+ expect(target.background(:red)).to eql(CapeCod.on_red(target))
185
+ end
186
+
187
+ it 'behaves the same way as singleton methods for effects' do
188
+ expect(target.effect(:bold)).to eql(CapeCod.bold(target))
189
+ end
190
+
191
+ it 'behaves the same way as singleton methods for chained calls' do
192
+ expect(
193
+ target.bg(:red).fx(:bold).fx(:italic).fg(:red)
194
+ ).to eql(
195
+ CapeCod.red(CapeCod.italic(CapeCod.bold(CapeCod.on_red(target))))
196
+ )
197
+ end
198
+ end
81
199
  end
82
200
  end
83
- end
84
201
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cape-cod
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-19 00:00:00.000000000 Z
12
+ date: 2013-05-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -43,6 +43,38 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: guard
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: guard-rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
46
78
  description: ! 'CapeCod offers you simple stupid way of colorizing and applying effects
47
79
  to your
48
80