cape-cod 0.1.1 → 0.1.2
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 +4 -3
- data/lib/cape-cod.rb +3 -13
- data/lib/cape-cod/color.rb +81 -0
- data/lib/cape-cod/version.rb +1 -1
- data/spec/cape-cod/color_spec.rb +35 -0
- data/spec/cape-cod_spec.rb +84 -0
- data/spec/spec_helper.rb +5 -0
- metadata +13 -21
data/README.md
CHANGED
@@ -65,10 +65,11 @@ or use it like this:
|
|
65
65
|
|
66
66
|
All the public instance methods are available as module methods.
|
67
67
|
|
68
|
+
## License
|
69
|
+
|
70
|
+
Please refer to [LICENSE.md](LICENSE.md).
|
71
|
+
|
68
72
|
## Contributing
|
69
73
|
|
70
74
|
Check the guidelines at [CONTRIBUTING.md](CONTRIBUTING.md).
|
71
75
|
|
72
|
-
## License
|
73
|
-
|
74
|
-
Please refer to [LICENSE.md](LICENSE.md).
|
data/lib/cape-cod.rb
CHANGED
@@ -6,6 +6,7 @@ $LOAD_PATH.unshift File.expand_path(
|
|
6
6
|
module CapeCod
|
7
7
|
|
8
8
|
require 'cape-cod/version'
|
9
|
+
require 'cape-cod/color'
|
9
10
|
|
10
11
|
@enabled = STDOUT.tty?
|
11
12
|
|
@@ -22,21 +23,10 @@ module CapeCod
|
|
22
23
|
strikethrough: 9
|
23
24
|
}.freeze
|
24
25
|
|
25
|
-
COLORS = {
|
26
|
-
black: 0,
|
27
|
-
red: 1,
|
28
|
-
green: 2,
|
29
|
-
yellow: 3,
|
30
|
-
blue: 4,
|
31
|
-
magenta: 5,
|
32
|
-
cyan: 6,
|
33
|
-
white: 7,
|
34
|
-
}.freeze
|
35
|
-
|
36
26
|
#
|
37
27
|
# Define helper methods for applying the escape codes.
|
38
28
|
#
|
39
|
-
|
29
|
+
Color::CODES.each do |color, _|
|
40
30
|
|
41
31
|
#
|
42
32
|
# Instance methods for background and foreground colors.
|
@@ -128,7 +118,7 @@ module CapeCod
|
|
128
118
|
# Returns the ANSI escape sequence for the given +color+.
|
129
119
|
#
|
130
120
|
def color_code_for(color, ground)
|
131
|
-
|
121
|
+
Color.new(color, ground).ansi_code
|
132
122
|
end
|
133
123
|
|
134
124
|
#
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CapeCod
|
4
|
+
class Color
|
5
|
+
|
6
|
+
#
|
7
|
+
# The ANSI color codes.
|
8
|
+
#
|
9
|
+
CODES = {
|
10
|
+
black: 0,
|
11
|
+
red: 1,
|
12
|
+
green: 2,
|
13
|
+
yellow: 3,
|
14
|
+
blue: 4,
|
15
|
+
magenta: 5,
|
16
|
+
cyan: 6,
|
17
|
+
white: 7,
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
#
|
21
|
+
# Returns the ANSI domain code for the given RGB color packed
|
22
|
+
# into an Integer.
|
23
|
+
#
|
24
|
+
def self.hex_to_ansi(hex)
|
25
|
+
(6 * ((hex >> 16 & 0xff) / 256.0)).to_i * 36 +
|
26
|
+
(6 * ((hex >> 8 & 0xff) / 256.0)).to_i * 6 +
|
27
|
+
(6 * ((hex & 0xff) / 256.0)).to_i
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Initializes a the color.
|
32
|
+
#
|
33
|
+
# +color+ may be either a single Integer representation of a RGB
|
34
|
+
# color, a Symbol with the color name (valid color names are listed
|
35
|
+
# in the COLORS hash), or three integers representing the RGB channels.
|
36
|
+
# +ground+ is either :background or :foreground.
|
37
|
+
#
|
38
|
+
def initialize(*color, ground)
|
39
|
+
unless [:foreground, :background].include? ground
|
40
|
+
raise ArgumentError, 'color must be either foreground or background.'
|
41
|
+
end
|
42
|
+
|
43
|
+
if color.empty? || color.size > 3 || color.size == 2
|
44
|
+
raise ArgumentError,
|
45
|
+
"wrong number of arguments (#{color.size + 1} for 2|4)."
|
46
|
+
elsif color.size == 3
|
47
|
+
color = [(color[0] << 16) | (color[1] << 8) | color[2]]
|
48
|
+
elsif color.first.is_a?(Integer) && color.first < 0
|
49
|
+
raise ArgumentError, 'hex code must be positive.'
|
50
|
+
elsif color.first.is_a?(Symbol) && !CODES.has_key?(color.first)
|
51
|
+
raise ArgumentError, %(invalid color name "#{color.first}".)
|
52
|
+
end
|
53
|
+
|
54
|
+
@ground = ground
|
55
|
+
@color = color.first
|
56
|
+
end
|
57
|
+
|
58
|
+
#
|
59
|
+
# Returns a string representing the ANSI escape code for this color.
|
60
|
+
#
|
61
|
+
def ansi_code
|
62
|
+
case @color
|
63
|
+
when Symbol then code_from_name
|
64
|
+
when Integer then code_from_hex
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def code_from_name
|
71
|
+
CODES[@color].+(@ground == :foreground ? 30 : 40).to_s
|
72
|
+
end
|
73
|
+
|
74
|
+
def code_from_hex
|
75
|
+
ground_code = @ground == :foreground ? 38 : 48
|
76
|
+
color_code = self.class.hex_to_ansi(@color)
|
77
|
+
|
78
|
+
"#{ground_code};5;#{color_code}"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/lib/cape-cod/version.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe CapeCod do
|
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
|
12
|
+
|
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'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when instantiated with invalid args' do
|
20
|
+
it 'fails initialization' do
|
21
|
+
expect {
|
22
|
+
CapeCod::Color.new(:not_a_color, :foreground).ansi_code
|
23
|
+
}.to raise_error(ArgumentError)
|
24
|
+
|
25
|
+
expect {
|
26
|
+
CapeCod::Color.new(:yellow, :not_a_ground).ansi_code
|
27
|
+
}.to raise_error(ArgumentError)
|
28
|
+
|
29
|
+
expect {
|
30
|
+
CapeCod::Color.new(155, 155, :foreground).ansi_code
|
31
|
+
}.to raise_error(ArgumentError)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe CapeCod do
|
6
|
+
it('has a version') { expect(CapeCod::VERSION).to be_a String }
|
7
|
+
|
8
|
+
context 'when disabled' do
|
9
|
+
before do
|
10
|
+
class String; include CapeCod end
|
11
|
+
|
12
|
+
CapeCod.enabled = false
|
13
|
+
end
|
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')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when enabled' do
|
23
|
+
before(:all) do CapeCod.enabled = true end
|
24
|
+
|
25
|
+
it 'generates proper escape sequences' do
|
26
|
+
expect(CapeCod.reset).to eq("\e[0m")
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when using singleton methods' do
|
30
|
+
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")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'when object param given' do
|
39
|
+
let(:obj) { ['foo', 10, :bar] }
|
40
|
+
|
41
|
+
it 'prepends the escape sequence and append a reset' do
|
42
|
+
|
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")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when using instance methods' do
|
51
|
+
before { class String; include CapeCod end }
|
52
|
+
|
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" }
|
59
|
+
|
60
|
+
|
61
|
+
it 'returns a new string with the proper escape codes applied' do
|
62
|
+
|
63
|
+
expect(string.bold).to_not eql(string)
|
64
|
+
|
65
|
+
expect(string.bold).to eql(bold)
|
66
|
+
expect(string.red).to eql(red)
|
67
|
+
expect(string.on_yellow).to eql(on_yellow)
|
68
|
+
|
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)
|
75
|
+
|
76
|
+
expect(string.red.on_yellow).to eql(r_on_y)
|
77
|
+
expect(string.fg(:red).bg(:yellow)).to eql(r_on_y)
|
78
|
+
|
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)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/spec/spec_helper.rb
ADDED
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.
|
4
|
+
version: 0.1.2
|
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-
|
12
|
+
date: 2013-05-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -27,22 +27,6 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: pry
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ! '>='
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: '0'
|
38
|
-
type: :development
|
39
|
-
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ! '>='
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: '0'
|
46
30
|
- !ruby/object:Gem::Dependency
|
47
31
|
name: rspec
|
48
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -73,10 +57,15 @@ files:
|
|
73
57
|
- README.md
|
74
58
|
- Rakefile
|
75
59
|
- LICENSE.md
|
60
|
+
- lib/cape-cod/color.rb
|
76
61
|
- lib/cape-cod/version.rb
|
77
62
|
- lib/cape-cod.rb
|
63
|
+
- spec/cape-cod/color_spec.rb
|
64
|
+
- spec/cape-cod_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
78
66
|
homepage: http://github.com/fuadsaud/cape-cod
|
79
|
-
licenses:
|
67
|
+
licenses:
|
68
|
+
- MIT
|
80
69
|
post_install_message:
|
81
70
|
rdoc_options: []
|
82
71
|
require_paths:
|
@@ -86,7 +75,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
75
|
requirements:
|
87
76
|
- - ! '>='
|
88
77
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
78
|
+
version: 1.9.3
|
90
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
80
|
none: false
|
92
81
|
requirements:
|
@@ -99,5 +88,8 @@ rubygems_version: 1.8.23
|
|
99
88
|
signing_key:
|
100
89
|
specification_version: 3
|
101
90
|
summary: Make your strings look fancy with ANSI escape codes.
|
102
|
-
test_files:
|
91
|
+
test_files:
|
92
|
+
- spec/cape-cod/color_spec.rb
|
93
|
+
- spec/cape-cod_spec.rb
|
94
|
+
- spec/spec_helper.rb
|
103
95
|
has_rdoc: false
|