colorer 0.2.4 → 0.3.0
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/VERSION +1 -1
- data/lib/colorer.rb +30 -22
- data/test/sgr.irt +10 -2
- metadata +5 -5
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/colorer.rb
CHANGED
@@ -2,7 +2,17 @@ module Colorer
|
|
2
2
|
|
3
3
|
VERSION = File.read(File.expand_path('../../VERSION', __FILE__)).strip
|
4
4
|
|
5
|
-
class
|
5
|
+
class AlreadyDefinedMethod < Exception
|
6
|
+
def initialize(meth, klass)
|
7
|
+
super("already defined method '#{meth}' for #{klass}:#{klass.class}")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class UnknownSgrCode < Exception
|
12
|
+
def initialize(sgr)
|
13
|
+
super("#{sgr.inspect} is unknown")
|
14
|
+
end
|
15
|
+
end
|
6
16
|
|
7
17
|
# Select Graphic Rendition
|
8
18
|
BASIC_SGR = {
|
@@ -31,35 +41,33 @@ module Colorer
|
|
31
41
|
:onwhite => 47
|
32
42
|
}
|
33
43
|
|
44
|
+
extend self
|
45
|
+
|
34
46
|
@color = true
|
35
|
-
|
36
|
-
attr_accessor :color
|
47
|
+
attr_accessor :color
|
37
48
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
49
|
+
def add_sgr(string, sgr)
|
50
|
+
return string unless (Colorer.color && STDOUT.tty? && ENV['TERM'] && ENV['TERM'] != 'dumb')
|
51
|
+
code = sgr.is_a?(Symbol) ? Colorer::BASIC_SGR[sgr] : sgr
|
52
|
+
raise UnknownSgrCode.new(sgr) unless code.is_a? Integer
|
53
|
+
string = sprintf("\e[0m%s\e[0m", string) unless string =~ /^\e\[[\d;]+m.*\e\[0m$/
|
54
|
+
string.sub /^(\e\[[\d;]+)/, '\1;' + code.to_s
|
55
|
+
end
|
45
56
|
|
46
|
-
|
57
|
+
def define_styles(styles, force=false)
|
58
|
+
styles.each_pair do |meth, style|
|
47
59
|
String.class_eval do
|
48
|
-
if
|
49
|
-
Colorer::BASIC_SGR.each_pair do |
|
50
|
-
raise
|
51
|
-
|
52
|
-
define_method(meth) do
|
60
|
+
if meth == :basic && style
|
61
|
+
Colorer::BASIC_SGR.each_pair do |m, sgr|
|
62
|
+
raise AlreadyDefinedMethod.new(m, self) if !force && method_defined?(m)
|
63
|
+
define_method(m) do
|
53
64
|
Colorer.add_sgr(self, sgr)
|
54
65
|
end
|
55
66
|
end
|
56
67
|
else
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
define_method(meth) do
|
61
|
-
style.inject(self) { |str, sgr| Colorer.add_sgr(str, sgr) }
|
62
|
-
end
|
68
|
+
raise AlreadyDefinedMethod.new(meth, self) if !force && method_defined?(meth)
|
69
|
+
define_method(meth) do
|
70
|
+
style.inject(self) { |str, sgr| Colorer.add_sgr(str, sgr) }
|
63
71
|
end
|
64
72
|
end
|
65
73
|
end
|
data/test/sgr.irt
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
require 'colorer'
|
2
2
|
|
3
|
-
Colorer.define_styles :mixed_sgr => [ :bold,
|
3
|
+
Colorer.define_styles :mixed_sgr => [ :bold, 7 ]
|
4
4
|
|
5
5
|
|
6
6
|
desc "Mixed SGR parameters"
|
7
7
|
"Mixed SGR parameters".mixed_sgr
|
8
8
|
puts _
|
9
|
-
test_value_eql? "\e[0;1;
|
9
|
+
test_value_eql? "\e[0;1;7mMixed SGR parameters\e[0m"
|
10
|
+
|
11
|
+
desc "Basic and custom definition"
|
12
|
+
Colorer.define_styles({:basic => true, :goo => [7]}, 1)
|
13
|
+
"Basic and custom definition".green.goo
|
14
|
+
puts _
|
15
|
+
test_value_eql? "\e[0;32;7mBasic and custom definition\e[0m"
|
16
|
+
|
17
|
+
|
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: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Domizio Demichelis
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-10-17 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|