cape-cod 0.1.0 → 0.1.1
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 +29 -20
- data/lib/cape-cod.rb +138 -54
- data/lib/cape-cod/version.rb +2 -2
- metadata +8 -3
data/README.md
CHANGED
@@ -1,25 +1,29 @@
|
|
1
1
|
cape-cod
|
2
2
|
========
|
3
3
|
|
4
|
-
|
4
|
+
[]
|
5
|
+
(http://badge.fury.io/rb/cape-cod)
|
5
6
|
[]
|
6
7
|
(https://travis-ci.org/fuadsaud/cape-cod)
|
7
8
|
|
8
|
-
cape-cod makes it easy for you to append ANSI es<strong>
|
9
|
-
that coming; I know, I know, I'm a genius - to
|
9
|
+
cape-cod makes it easy for you to append ANSI es<strong>cape-cod</strong>es -
|
10
|
+
HAR! bet you didn't see that coming; I know, I know, I'm a genius - to your
|
11
|
+
strings.
|
10
12
|
|
11
13
|
### *Hey, but don't we have a plenty of gems that do exactly the same thing?*
|
12
14
|
|
13
|
-
**YES**. We can cite [colored](http://github.com/defunkt/colored)
|
14
|
-
[colorize](http://github.com/fazibear/colorize)
|
15
|
-
[term-ansicolor](http://github.com/flori/term-ansicolor),
|
16
|
-
|
17
|
-
|
15
|
+
**YES**. We can cite [colored](http://github.com/defunkt/colored),
|
16
|
+
[colorize](http://github.com/fazibear/colorize),
|
17
|
+
[term-ansicolor](http://github.com/flori/term-ansicolor),
|
18
|
+
[ANSI](http://github.com/rubyworks/ANSI) and so on...
|
19
|
+
They are really nice gems, and they solve the escape code problem in different
|
20
|
+
manners but they're all kind of abandoned, with a lot of lingering issues...
|
18
21
|
|
19
|
-
My point with this gem is to implement many of the possible ways of appending
|
20
|
-
codes to strings (monkey patching, blocks, etc) and let the user
|
21
|
-
I for instance prefer the colored's monkey patch
|
22
|
-
doesn't suit, so other options should be
|
22
|
+
My point with this gem is to implement many of the possible ways of appending
|
23
|
+
ANSI escape codes to strings (monkey patching, blocks, etc) and let the user
|
24
|
+
choose whatever he likes. I for instance prefer the colored's monkey patch
|
25
|
+
approach, but for some people it doesn't suit, so other options should be
|
26
|
+
offered.
|
23
27
|
|
24
28
|
Please contribute!
|
25
29
|
|
@@ -31,7 +35,7 @@ Add this line to your applications's gemfile:
|
|
31
35
|
|
32
36
|
then run:
|
33
37
|
|
34
|
-
```bundle```
|
38
|
+
```bundle install```
|
35
39
|
|
36
40
|
or simply:
|
37
41
|
|
@@ -43,18 +47,23 @@ or simply:
|
|
43
47
|
|
44
48
|
You can include cape-cod in you String class:
|
45
49
|
|
46
|
-
|
47
|
-
|
50
|
+
class String; include CapeCod end
|
51
|
+
|
52
|
+
puts "Praise R'hlor, for the night is dark and full of terrors".red
|
53
|
+
|
54
|
+
puts 'This is BOLD'.bold
|
55
|
+
|
56
|
+
puts 'and this is ITALIC'.fx(:italic) # You should probably avoid *italics* :\
|
57
|
+
|
58
|
+
puts 'Black n white'.fg(:black).on_white
|
48
59
|
|
49
|
-
|
50
|
-
```
|
60
|
+
puts 'Magenta background'.bg(:magenta)
|
51
61
|
|
52
62
|
or use it like this:
|
53
63
|
|
54
|
-
```
|
55
|
-
puts CapeCod.yellow('We all live in a yellow submarine!')
|
56
|
-
```
|
64
|
+
```puts CapeCod.yellow('We all live in a yellow submarine!')```
|
57
65
|
|
66
|
+
All the public instance methods are available as module methods.
|
58
67
|
|
59
68
|
## Contributing
|
60
69
|
|
data/lib/cape-cod.rb
CHANGED
@@ -7,75 +7,159 @@ module CapeCod
|
|
7
7
|
|
8
8
|
require 'cape-cod/version'
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
on_green: 42,
|
36
|
-
on_yellow: 43,
|
37
|
-
on_blue: 44,
|
38
|
-
on_magenta: 45,
|
39
|
-
on_cyan: 46,
|
40
|
-
on_white: 47,
|
41
|
-
}.freeze
|
10
|
+
@enabled = STDOUT.tty?
|
11
|
+
|
12
|
+
EFFECTS = {
|
13
|
+
reset: 0,
|
14
|
+
bold: 1,
|
15
|
+
dark: 2,
|
16
|
+
italic: 3,
|
17
|
+
underline: 4,
|
18
|
+
blink: 5,
|
19
|
+
rapid_blink: 6,
|
20
|
+
negative: 7,
|
21
|
+
concealed: 8,
|
22
|
+
strikethrough: 9
|
23
|
+
}.freeze
|
24
|
+
|
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
|
42
35
|
|
43
36
|
#
|
44
|
-
# Define helper methods for applying the escape codes
|
37
|
+
# Define helper methods for applying the escape codes.
|
45
38
|
#
|
46
|
-
|
47
|
-
|
48
|
-
|
39
|
+
COLORS.each do |color, _|
|
40
|
+
|
41
|
+
#
|
42
|
+
# Instance methods for background and foreground colors.
|
43
|
+
#
|
44
|
+
define_method color do
|
45
|
+
CapeCod.foreground(color, self)
|
49
46
|
end
|
50
47
|
|
51
|
-
|
48
|
+
define_method "on_#{color}".to_s do
|
49
|
+
CapeCod.background(color, self)
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Singleton methods for background and foreground colors.
|
54
|
+
#
|
55
|
+
define_singleton_method color do |obj = ''|
|
52
56
|
string = obj.to_s
|
53
|
-
return CapeCod.escape_sequence_for(code) if string.empty? unless block
|
54
57
|
|
55
|
-
string
|
58
|
+
foreground(color, string)
|
59
|
+
end
|
56
60
|
|
57
|
-
|
61
|
+
define_singleton_method "on_#{color}" do |obj = ''|
|
62
|
+
string = obj.to_s
|
63
|
+
|
64
|
+
background(color, string)
|
58
65
|
end
|
59
66
|
end
|
60
67
|
|
61
|
-
|
68
|
+
EFFECTS.each do |effect, _|
|
62
69
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
70
|
+
#
|
71
|
+
# Instance methods for effects.
|
72
|
+
#
|
73
|
+
define_method effect do
|
74
|
+
CapeCod.effect(effect, self)
|
75
|
+
end
|
76
|
+
|
77
|
+
#
|
78
|
+
# Singleton methods for effects.
|
79
|
+
#
|
80
|
+
define_singleton_method effect do |obj = ''|
|
81
|
+
string = obj.to_s
|
82
|
+
|
83
|
+
effect(effect, string)
|
84
|
+
end
|
68
85
|
end
|
69
86
|
|
70
|
-
#
|
71
|
-
|
72
|
-
|
73
|
-
#
|
74
|
-
def self.apply_escape_sequence(code, string)
|
75
|
-
sequence = escape_sequence_for(code)
|
87
|
+
def foreground(color) # :nodoc:
|
88
|
+
CapeCod.foreground(color, self)
|
89
|
+
end
|
76
90
|
|
77
|
-
|
91
|
+
def background(color) # :nodoc:
|
92
|
+
CapeCod.background(color, self)
|
93
|
+
end
|
94
|
+
|
95
|
+
def effect(effect) # :nodoc:
|
96
|
+
CapeCod.effect(effect, self)
|
97
|
+
end
|
78
98
|
|
79
|
-
|
99
|
+
alias_method :color, :foreground
|
100
|
+
alias_method :fg, :foreground
|
101
|
+
alias_method :bg, :background
|
102
|
+
alias_method :fx, :effect
|
103
|
+
|
104
|
+
class << self
|
105
|
+
|
106
|
+
attr_accessor :enabled
|
107
|
+
|
108
|
+
def foreground(color, target) # :nodoc:
|
109
|
+
apply_escape_sequence(color_code_for(color, :foreground), target)
|
110
|
+
end
|
111
|
+
|
112
|
+
def background(color, target) # :nodoc:
|
113
|
+
apply_escape_sequence(color_code_for(color, :background), target)
|
114
|
+
end
|
115
|
+
|
116
|
+
def effect(effect, target) # :nodoc:
|
117
|
+
apply_escape_sequence(effect_code_for(effect), target)
|
118
|
+
end
|
119
|
+
|
120
|
+
alias_method :color, :foreground
|
121
|
+
alias_method :fg, :foreground
|
122
|
+
alias_method :bg, :background
|
123
|
+
alias_method :fx, :effect
|
124
|
+
|
125
|
+
protected
|
126
|
+
|
127
|
+
#
|
128
|
+
# Returns the ANSI escape sequence for the given +color+.
|
129
|
+
#
|
130
|
+
def color_code_for(color, ground)
|
131
|
+
COLORS.fetch(color) + (ground == :foreground ? 30 : 40)
|
132
|
+
end
|
133
|
+
|
134
|
+
#
|
135
|
+
# Returns the ANSI escape sequence for the given +effect+.
|
136
|
+
#
|
137
|
+
def effect_code_for(effect)
|
138
|
+
EFFECTS.fetch(effect)
|
139
|
+
end
|
140
|
+
|
141
|
+
private
|
142
|
+
|
143
|
+
#
|
144
|
+
# Returns the ANSI escape sequence for a given escape +code+.
|
145
|
+
#
|
146
|
+
def escape_sequence_for(code)
|
147
|
+
"\e[#{code}m"
|
148
|
+
end
|
149
|
+
|
150
|
+
#
|
151
|
+
# Prepends the given +string+ with the ANSI escape sequence for the
|
152
|
+
# given escape +code+. In case string is not empty, also appends a
|
153
|
+
# reset sequence.
|
154
|
+
#
|
155
|
+
def apply_escape_sequence(code, string)
|
156
|
+
return string unless self.enabled
|
157
|
+
|
158
|
+
escape_sequence_for(code).tap do |s|
|
159
|
+
unless string.nil? || string.empty?
|
160
|
+
s << string << escape_sequence_for(effect_code_for(:reset))
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
80
164
|
end
|
81
165
|
end
|
data/lib/cape-cod/version.rb
CHANGED
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.1
|
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-04-
|
12
|
+
date: 2013-04-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -59,7 +59,12 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
-
description:
|
62
|
+
description: ! 'CapeCod offers you simple stupid way of colorizing and applying effects
|
63
|
+
to your
|
64
|
+
|
65
|
+
terminal output, by appending ANSI escape sequences to your strings.
|
66
|
+
|
67
|
+
'
|
63
68
|
email: fuadksd@gmail.com
|
64
69
|
executables: []
|
65
70
|
extensions: []
|