colorer 0.6.1 → 0.7.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/LICENSE +1 -1
- data/README.rdoc +38 -1
- data/Rakefile +60 -0
- data/VERSION +1 -1
- data/colorer.gemspec +1 -1
- data/lib/colorer.rb +42 -6
- data/test/basic.irt +11 -4
- data/test/custom.irt +4 -3
- data/test/irt_helper.rb +1 -0
- data/test/selected_basic.irt +4 -4
- data/test/sgr.irt +2 -2
- metadata +9 -8
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -13,6 +13,18 @@ Easy ANSI code coloring for strings.
|
|
13
13
|
"an error string".errorize
|
14
14
|
"my native (Select Graphic Rendition) string".mysgr
|
15
15
|
|
16
|
+
' reversed '.reversed.or('==== reversed ====')
|
17
|
+
|
18
|
+
Colorer.def_strip_ansi
|
19
|
+
plain_text = colored_string.strip_ansi
|
20
|
+
|
21
|
+
=== Feedback!!!
|
22
|
+
|
23
|
+
This is feedback-driven software. Just send me a line about you and/or what you think about IRT:
|
24
|
+
that will be a wonderful contribution that will help me to keep improving (and documenting) this software.
|
25
|
+
|
26
|
+
My email address is ddnexus at gmail.com ... waiting for your. Ciao.
|
27
|
+
|
16
28
|
== Features
|
17
29
|
|
18
30
|
* Does not pollute String of unwanted methods
|
@@ -88,6 +100,31 @@ You can also add native SGR (Select Graphic Rendition) parameters (0..109) to an
|
|
88
100
|
|
89
101
|
See http://en.wikipedia.org/wiki/ANSI_colors for a complete list
|
90
102
|
|
103
|
+
=== Strict ANSI
|
104
|
+
|
105
|
+
Some terminals don't parse composite SGR styles correctly, and need separate SGR for each.
|
106
|
+
|
107
|
+
puts "\e[7;31;46mSTRING\e[0m" # strict_ansi == true (may be difficult to parse)
|
108
|
+
puts "\e[7m\e[31m\e[46mSTRING\e[0m" # strict_ansi == false
|
109
|
+
|
110
|
+
On the other way most of the terminals that parse them correctly can parse also separate SGRs,
|
111
|
+
so Colorer will output non strict ansi by default. If you want to have strict ansi you can do:
|
112
|
+
|
113
|
+
Colorer.strict_ansi = true
|
114
|
+
|
115
|
+
or you can set the COLORER_ANSI_STRICT environment variable for a system wide setting.
|
116
|
+
|
117
|
+
=== Color
|
118
|
+
|
119
|
+
The color is true by defealut on a non-dumb tty terminal, anyway you can force it
|
120
|
+
by explicitly setting it:
|
121
|
+
|
122
|
+
Colorer.color? #=> true/false by default depending on your terminal
|
123
|
+
Colorer.color = true # force true
|
124
|
+
Colorer.color? #=> true
|
125
|
+
Colorer.color = false # force false
|
126
|
+
Colorer.color? #=> false
|
127
|
+
|
91
128
|
== Copyright
|
92
129
|
|
93
|
-
Copyright (c) 2010 Domizio Demichelis. See LICENSE for details.
|
130
|
+
Copyright (c) 2010-2011 Domizio Demichelis. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
name = 'colorer'
|
2
|
+
|
3
|
+
def ensure_clean(action, force=false)
|
4
|
+
if !force && ! `git status -s`.empty?
|
5
|
+
puts <<-EOS.gsub(/^ {6}/, '')
|
6
|
+
Rake task aborted: the working tree is dirty!
|
7
|
+
If you know what you are doing you can use \`rake #{action}[force]\`"
|
8
|
+
EOS
|
9
|
+
exit(1)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Install the gem"
|
14
|
+
task :install, :force do |t, args|
|
15
|
+
ensure_clean(:install, args.force)
|
16
|
+
orig_version = version = File.read('VERSION').strip
|
17
|
+
begin
|
18
|
+
commit_id = `git log -1 --format="%h" HEAD`.strip
|
19
|
+
version = "#{orig_version}.#{commit_id}"
|
20
|
+
File.open('VERSION', 'w') {|f| f.puts version }
|
21
|
+
gem_name = "#{name}-#{version}.gem"
|
22
|
+
sh %(gem build #{name}.gemspec)
|
23
|
+
sh %(gem install #{gem_name} --local)
|
24
|
+
puts <<-EOS.gsub(/^ {6}/, '')
|
25
|
+
|
26
|
+
*******************************************************************************
|
27
|
+
* NOTICE *
|
28
|
+
*******************************************************************************
|
29
|
+
* The version id of locally installed gems is comparable to a --pre version: *
|
30
|
+
* i.e. it is alphabetically ordered (not numerically ordered), besides it *
|
31
|
+
* includes the sah1 commit id which is not aphabetically ordered, so be sure *
|
32
|
+
* your application picks the version you really intend to use *
|
33
|
+
*******************************************************************************
|
34
|
+
|
35
|
+
EOS
|
36
|
+
ensure
|
37
|
+
remove_entry_secure gem_name, true
|
38
|
+
File.open('VERSION', 'w') {|f| f.puts orig_version }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
desc %(Remove all the "#{name}" installed gems and executables and install this version)
|
43
|
+
task :clean_install, :force do |t, args|
|
44
|
+
ensure_clean(:install, args.force)
|
45
|
+
sh %(gem uninstall #{name} --all --ignore-dependencies --executables)
|
46
|
+
Rake::Task['install'].invoke(args.force)
|
47
|
+
end
|
48
|
+
|
49
|
+
desc "Push the gem to rubygems.org"
|
50
|
+
task :push, :force do |t, args|
|
51
|
+
begin
|
52
|
+
ensure_clean(:push, args.force)
|
53
|
+
version = File.read('VERSION').strip
|
54
|
+
gem_name = "#{name}-#{version}.gem"
|
55
|
+
sh %(gem build #{name}.gemspec)
|
56
|
+
sh %(gem push #{gem_name})
|
57
|
+
ensure
|
58
|
+
remove_entry_secure gem_name, true
|
59
|
+
end
|
60
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.7.0
|
data/colorer.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.summary = 'Easy ANSI code coloring for strings'
|
11
11
|
s.description = 'Colorer adds the basic ANSI styles to any string, allowing also to define your own stiles'
|
12
12
|
|
13
|
-
s.add_development_dependency('irt', [">= 0.
|
13
|
+
s.add_development_dependency('irt', [">= 1.0.0"])
|
14
14
|
|
15
15
|
s.files = `git ls-files -z`.split("\0")
|
16
16
|
|
data/lib/colorer.rb
CHANGED
@@ -43,8 +43,12 @@ module Colorer
|
|
43
43
|
|
44
44
|
extend self
|
45
45
|
|
46
|
-
@color =
|
46
|
+
@color = !!STDOUT.tty? && !!ENV['TERM'] && ENV['TERM'] != 'dumb'
|
47
47
|
attr_accessor :color
|
48
|
+
alias_method :color?, :color
|
49
|
+
|
50
|
+
@strict_ansi = !!ENV['COLORER_STRICT_ANSI']
|
51
|
+
attr_accessor :strict_ansi
|
48
52
|
|
49
53
|
def def_basic_styles(basic=true, force=false)
|
50
54
|
define_styles basic_styles(basic), force
|
@@ -63,6 +67,7 @@ module Colorer
|
|
63
67
|
end
|
64
68
|
puts " #{caller[0]}"
|
65
69
|
end
|
70
|
+
check_or_define_or
|
66
71
|
styles.each_pair do |meth, style|
|
67
72
|
# allows dummy methods (useful for style overriding)
|
68
73
|
if style.nil?
|
@@ -74,14 +79,31 @@ module Colorer
|
|
74
79
|
code = s.is_a?(Symbol) ? BASIC_SGR[s] : s
|
75
80
|
raise UnknownSgrCode.new(s) unless code.is_a?(Integer) && (0..109).include?(code)
|
76
81
|
code
|
77
|
-
end
|
82
|
+
end
|
78
83
|
String.class_eval do
|
79
84
|
raise AlreadyDefinedMethod.new(meth, self) if !force && method_defined?(meth)
|
80
85
|
define_method(meth) do
|
81
|
-
return self unless
|
82
|
-
|
83
|
-
|
84
|
-
|
86
|
+
return self unless Colorer.color?
|
87
|
+
if Colorer.strict_ansi
|
88
|
+
match(/^\e\[[\d;]+m.*\e\[0m$/m) ?
|
89
|
+
sub(/^(\e\[[\d;]+)/, '\1;' + codes.join(';')) :
|
90
|
+
sprintf("\e[0;%sm%s\e[0m", codes.join(';'), self)
|
91
|
+
else
|
92
|
+
match(/^(?:\e\[\d+m)+.*\e\[0m$/m) ?
|
93
|
+
sub(/^((?:\e\[\d+m)+)/, '\1' + codes.map{|c| "\e[#{c}m" }.join) :
|
94
|
+
sprintf("\e[0m%s%s\e[0m", codes.map{|c| "\e[#{c}m" }.join, self)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# strips all ansi SGR codes from self
|
102
|
+
def def_strip_ansi
|
103
|
+
unless 'string'.respond_to?(:strip_ansi)
|
104
|
+
String.class_eval do
|
105
|
+
define_method(:strip_ansi) do
|
106
|
+
gsub(/\e\[[\d;]+m/, '')
|
85
107
|
end
|
86
108
|
end
|
87
109
|
end
|
@@ -102,4 +124,18 @@ module Colorer
|
|
102
124
|
styles
|
103
125
|
end
|
104
126
|
|
127
|
+
def check_or_define_or
|
128
|
+
# defifines the String#or method if not already defined
|
129
|
+
# the String#or will return self when #color? is true
|
130
|
+
# or an alternative string otherwise
|
131
|
+
# e.g.: ' reversed '.reversed.or('==== reversed ====')
|
132
|
+
unless 'string'.respond_to?(:or)
|
133
|
+
String.class_eval do
|
134
|
+
define_method(:or) do |alternative_string|
|
135
|
+
Colorer.color? ? self : alternative_string
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
105
141
|
end
|
data/test/basic.irt
CHANGED
@@ -3,19 +3,26 @@ Colorer.def_basic_styles true, true
|
|
3
3
|
desc "red"
|
4
4
|
'red'.red
|
5
5
|
puts _
|
6
|
-
|
6
|
+
_eql? "\e[0;31mred\e[0m"
|
7
7
|
|
8
8
|
desc "red bold"
|
9
9
|
'red bold'.red.bold
|
10
10
|
puts _
|
11
|
-
|
11
|
+
_eql? "\e[0;31;1mred bold\e[0m"
|
12
12
|
|
13
13
|
desc "red bold underline"
|
14
14
|
'red bold underline'.red.bold.underline
|
15
15
|
puts _
|
16
|
-
|
16
|
+
_eql? "\e[0;31;1;4mred bold underline\e[0m"
|
17
17
|
|
18
18
|
desc "red bold underline reversed"
|
19
19
|
'red bold underline reversed'.red.bold.underline.reversed
|
20
20
|
puts _
|
21
|
-
|
21
|
+
_eql? "\e[0;31;1;4;7mred bold underline reversed\e[0m"
|
22
|
+
|
23
|
+
desc "red bold with \\n"
|
24
|
+
"red bold with\n".red.bold
|
25
|
+
puts _
|
26
|
+
_eql? "\e[0;31;1mred bold with\n\e[0m"
|
27
|
+
|
28
|
+
|
data/test/custom.irt
CHANGED
@@ -1,18 +1,19 @@
|
|
1
1
|
Colorer.def_custom_styles :dummy => nil,
|
2
2
|
:errorize => [ :red, :bold, :underline ],
|
3
3
|
:okeyze => [ :green, :bold ]
|
4
|
+
Colorer.strict_ansi = true
|
4
5
|
|
5
6
|
desc "errorize"
|
6
7
|
'errorize'.errorize
|
7
8
|
puts _
|
8
|
-
|
9
|
+
_eql? "\e[0;31;1;4merrorize\e[0m"
|
9
10
|
|
10
11
|
desc "okeyze"
|
11
12
|
'okeyize'.okeyze
|
12
13
|
puts _
|
13
|
-
|
14
|
+
_eql? "\e[0;32;1mokeyize\e[0m"
|
14
15
|
|
15
16
|
desc "allows dummy styles"
|
16
17
|
'dummy'.dummy
|
17
18
|
puts _
|
18
|
-
|
19
|
+
_eql?( "dummy" )
|
data/test/irt_helper.rb
CHANGED
data/test/selected_basic.irt
CHANGED
@@ -3,21 +3,21 @@ Colorer.def_basic_styles([:bold, :red], true)
|
|
3
3
|
desc "red"
|
4
4
|
'red'.red
|
5
5
|
puts _
|
6
|
-
|
6
|
+
_eql? "\e[0;31mred\e[0m"
|
7
7
|
|
8
8
|
desc "red bold"
|
9
9
|
'red bold'.red.bold
|
10
10
|
puts _
|
11
|
-
|
11
|
+
_eql? "\e[0;31;1mred bold\e[0m"
|
12
12
|
|
13
13
|
desc "onwhite"
|
14
14
|
Colorer.def_basic_styles([:onwhite], true)
|
15
15
|
'onwhite'.onwhite
|
16
16
|
puts _
|
17
|
-
|
17
|
+
_eql? "\e[0;47monwhite\e[0m"
|
18
18
|
|
19
19
|
|
20
20
|
desc "not underline"
|
21
21
|
'not underline'.respond_to? :underline
|
22
|
-
|
22
|
+
_eql? false
|
23
23
|
|
data/test/sgr.irt
CHANGED
@@ -4,13 +4,13 @@ Colorer.def_custom_styles :mixed_sgr => [ :bold, 7 ]
|
|
4
4
|
desc "Mixed SGR parameters"
|
5
5
|
"Mixed SGR parameters".mixed_sgr
|
6
6
|
puts _
|
7
|
-
|
7
|
+
_eql? "\e[0;1;7mMixed SGR parameters\e[0m"
|
8
8
|
|
9
9
|
desc "Basic and custom definition"
|
10
10
|
Colorer.def_basic_styles true, true
|
11
11
|
Colorer.def_custom_styles({:goo => [7], :guu => 4}, 1)
|
12
12
|
"Basic and custom definition".green.goo.guu
|
13
13
|
puts _
|
14
|
-
|
14
|
+
_eql? "\e[0;32;7;4mBasic and custom definition\e[0m"
|
15
15
|
|
16
16
|
|
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: 3
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 7
|
9
|
+
- 0
|
10
|
+
version: 0.7.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:
|
18
|
+
date: 2011-01-17 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -26,12 +26,12 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
29
|
+
hash: 23
|
30
30
|
segments:
|
31
|
+
- 1
|
31
32
|
- 0
|
32
|
-
- 8
|
33
33
|
- 0
|
34
|
-
version: 0.
|
34
|
+
version: 1.0.0
|
35
35
|
type: :development
|
36
36
|
version_requirements: *id001
|
37
37
|
description: Colorer adds the basic ANSI styles to any string, allowing also to define your own stiles
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- .gitignore
|
47
47
|
- LICENSE
|
48
48
|
- README.rdoc
|
49
|
+
- Rakefile
|
49
50
|
- VERSION
|
50
51
|
- colorer.gemspec
|
51
52
|
- lib/colorer.rb
|