colorer 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +13 -2
- data/VERSION +1 -1
- data/lib/colorer.rb +19 -7
- data/test/selected_basic.irt +25 -0
- metadata +5 -4
data/README.rdoc
CHANGED
@@ -22,10 +22,19 @@ Easy ANSI code coloring for strings.
|
|
22
22
|
|
23
23
|
=== Basic Styles
|
24
24
|
|
25
|
-
You can define the basic styles for any string:
|
25
|
+
You can define the basic styles for any string by using the reserved name :basic:
|
26
26
|
|
27
|
+
# all basic styles
|
27
28
|
Colorer.define_styles :basic => true
|
28
|
-
|
29
|
+
"a string".green.bold.reversed.underline...
|
30
|
+
|
31
|
+
# a few basic styles
|
32
|
+
Colorer.define_styles :basic => [:bold, :reversed]
|
33
|
+
"a string".bold.reversed
|
34
|
+
|
35
|
+
# one basic style
|
36
|
+
Colorer.define_styles :basic => :red
|
37
|
+
"a string".red
|
29
38
|
|
30
39
|
* clear
|
31
40
|
* bold
|
@@ -69,6 +78,8 @@ You can define your own custom styles:
|
|
69
78
|
# same as
|
70
79
|
crazy_string.magenta.onyellow.bold.underline
|
71
80
|
|
81
|
+
Notice: you cannot use :basic as a custom name, since it is the reserved name used to define the basic styles.
|
82
|
+
|
72
83
|
=== SGR Styles
|
73
84
|
|
74
85
|
You can also add native SGR (Select Graphic Rendition) parameters (0..109) to any style:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/lib/colorer.rb
CHANGED
@@ -47,20 +47,32 @@ module Colorer
|
|
47
47
|
attr_accessor :color
|
48
48
|
|
49
49
|
def define_styles(styles, force=false)
|
50
|
-
|
50
|
+
if basic = styles.delete(:basic)
|
51
|
+
basic_styles = {}
|
52
|
+
case basic
|
53
|
+
when TrueClass
|
54
|
+
basic_styles = BASIC_SGR
|
55
|
+
when Array
|
56
|
+
basic.each{|k| basic_styles[k] = BASIC_SGR[k]}
|
57
|
+
when Symbol
|
58
|
+
basic_styles[basic] = BASIC_SGR[basic]
|
59
|
+
end
|
60
|
+
styles = basic_styles.merge(styles)
|
61
|
+
end
|
51
62
|
styles.each_pair do |meth, style|
|
52
63
|
style = [style] unless style.is_a?(Array)
|
53
64
|
codes = style.map do |s|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
65
|
+
code = s.is_a?(Symbol) ? BASIC_SGR[s] : s
|
66
|
+
raise UnknownSgrCode.new(s) unless code.is_a?(Integer) && (0..109).include?(code)
|
67
|
+
code
|
68
|
+
end.join(';')
|
58
69
|
String.class_eval do
|
59
70
|
raise AlreadyDefinedMethod.new(meth, self) if !force && method_defined?(meth)
|
60
71
|
define_method(meth) do
|
61
72
|
return self unless (Colorer.color && STDOUT.tty? && ENV['TERM'] && ENV['TERM'] != 'dumb')
|
62
|
-
|
63
|
-
|
73
|
+
match(/^\e\[[\d;]+m.*\e\[0m$/) ?
|
74
|
+
sub(/^(\e\[[\d;]+)/, '\1;' + codes) :
|
75
|
+
sprintf("\e[0;%sm%s\e[0m", codes, self)
|
64
76
|
end
|
65
77
|
end
|
66
78
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'colorer'
|
2
|
+
|
3
|
+
Colorer.define_styles({:basic => [:bold, :red]}, true)
|
4
|
+
|
5
|
+
desc "red"
|
6
|
+
'red'.red
|
7
|
+
puts _
|
8
|
+
test_value_eql? "\e[0;31mred\e[0m"
|
9
|
+
|
10
|
+
desc "red bold"
|
11
|
+
'red bold'.red.bold
|
12
|
+
puts _
|
13
|
+
test_value_eql? "\e[0;31;1mred bold\e[0m"
|
14
|
+
|
15
|
+
desc "onwhite"
|
16
|
+
Colorer.define_styles({:basic => :onwhite}, true)
|
17
|
+
'onwhite'.onwhite
|
18
|
+
puts _
|
19
|
+
test_value_eql? "\e[0;47monwhite\e[0m"
|
20
|
+
|
21
|
+
|
22
|
+
desc "not underline"
|
23
|
+
'not underline'.respond_to? :underline
|
24
|
+
test_value_eql? false
|
25
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: colorer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Domizio Demichelis
|
@@ -51,6 +51,7 @@ files:
|
|
51
51
|
- lib/colorer.rb
|
52
52
|
- test/basic.irt
|
53
53
|
- test/custom.irt
|
54
|
+
- test/selected_basic.irt
|
54
55
|
- test/sgr.irt
|
55
56
|
has_rdoc: true
|
56
57
|
homepage: http://github.com/ddnexus/colorer
|