colorer 0.2.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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+
23
+ ## Eclipse
24
+ .loadpath
25
+ .project
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Domizio Demichelis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,82 @@
1
+ = colorer
2
+
3
+ Easy ANSI code coloring for strings.
4
+
5
+ == Synopsis
6
+
7
+ require 'colorer'
8
+ Colorer.define_styles :basic => true,
9
+ :errorize => [ :red, :bold, :underline ],
10
+ :mysgr => [ :red, 8 ]
11
+
12
+ "a red bold underlined text on white background".red.bold.underline.onwhite
13
+ "an error string".errorize
14
+ "my native (Select Graphic Rendition) string".mysgr
15
+
16
+ == Features
17
+
18
+ * Does not pollute String of unwanted methods
19
+ * Allows you to define basic styles with one line of code
20
+ * Allows you to easily add your own custom styles
21
+ * Allows extended (Select Graphic Rendition) parameters
22
+
23
+ === Basic Styles
24
+
25
+ You can define the basic styles for any string:
26
+
27
+ Colorer.define_styles :basic => true
28
+ 'red bold string'.red.bold
29
+
30
+ * clear
31
+ * bold
32
+ * underline
33
+ * blinking
34
+ * reversed
35
+
36
+ * black
37
+ * red
38
+ * green
39
+ * yellow
40
+ * blue
41
+ * magenta
42
+ * cyan
43
+ * white
44
+
45
+ * onblack
46
+ * onred
47
+ * ongreen
48
+ * onyellow
49
+ * onblue
50
+ * onmagenta
51
+ * oncyan
52
+ * onwhite
53
+
54
+ === Custom Styles
55
+
56
+ You can define your own custom styles:
57
+
58
+ Colorer.define_styles :errorize => [ :red, :bold, :underline ],
59
+ :okize => [ :green, :bold ],
60
+ :crazyize => [ :magenta, :onyellow, :bold, :underline ]
61
+
62
+ error_string.errorize
63
+ # same as
64
+ error_string.red.bold.underline
65
+ ok_string.okeyze
66
+ # same as
67
+ ok_string.green.bold
68
+ crazy_string.crazyize
69
+ # same as
70
+ crazy_string.magenta.onyellow.bold.underline
71
+
72
+ === SGR Styles
73
+
74
+ You can also add native SGR (Select Graphic Rendition) parameters (0..109) to any style:
75
+
76
+ Colorer.define_styles :mysgr => [ :red, 8 ]
77
+
78
+ See http://en.wikipedia.org/wiki/ANSI_colors for a complete list
79
+
80
+ == Copyright
81
+
82
+ Copyright (c) 2010 Domizio Demichelis. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "colorer"
8
+ gem.summary = %Q{Easy ANSI code coloring for strings}
9
+ gem.description = %Q{Colorer adds the basic ANSI styles to any string, allowing also to define your own stiles}
10
+ gem.email = "dd.nexus@gmail.com"
11
+ gem.homepage = "http://github.com/ddnexus/colorer"
12
+ gem.authors = ["Domizio Demichelis"]
13
+ #gem.add_development_dependency "irt", ">= 0.7.1"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "colorer #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.2
data/colorer.gemspec ADDED
@@ -0,0 +1,48 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{colorer}
8
+ s.version = "0.2.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Domizio Demichelis"]
12
+ s.date = %q{2010-09-17}
13
+ s.description = %q{Colorer adds the basic ANSI styles to any string, allowing also to define your own stiles}
14
+ s.email = %q{dd.nexus@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "colorer.gemspec",
27
+ "lib/colorer.rb",
28
+ "test/basic.irt",
29
+ "test/custom.irt",
30
+ "test/sgr.irt"
31
+ ]
32
+ s.homepage = %q{http://github.com/ddnexus/colorer}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{Easy ANSI code coloring for strings}
37
+
38
+ if s.respond_to? :specification_version then
39
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ else
44
+ end
45
+ else
46
+ end
47
+ end
48
+
data/lib/colorer.rb ADDED
@@ -0,0 +1,69 @@
1
+ module Colorer
2
+
3
+ VERSION = File.read(File.expand_path('../../VERSION', __FILE__)).strip
4
+
5
+ class Exception < ::Exception; end
6
+
7
+ # Select Graphic Rendition
8
+ BASIC_SGR = {
9
+ :clear => 0,
10
+ :bold => 1,
11
+ :underline => 4,
12
+ :blinking => 5,
13
+ :reversed => 7,
14
+
15
+ :black => 30,
16
+ :red => 31,
17
+ :green => 32,
18
+ :yellow => 33,
19
+ :blue => 34,
20
+ :magenta => 35,
21
+ :cyan => 36,
22
+ :white => 37,
23
+
24
+ :onblack => 40,
25
+ :onred => 41,
26
+ :ongreen => 42,
27
+ :onyellow => 43,
28
+ :onblue => 44,
29
+ :onmagenta => 45,
30
+ :oncyan => 46,
31
+ :onwhite => 47
32
+ }
33
+
34
+ @color = true
35
+ class << self
36
+ attr_accessor :color
37
+
38
+ def add_sgr(string, sgr)
39
+ return string unless (Colorer.color && STDOUT.tty? && ENV['TERM'] && ENV['TERM'] != 'dumb')
40
+ string = sprintf("\e[0m%s\e[0m", string) unless string =~ /^\e\[[\d;]+m.*\e\[0m$/
41
+ sgr = Colorer::BASIC_SGR[sgr] if sgr.is_a?(Symbol)
42
+ raise Exception, "undefined SGR code '#{sgr}'" if sgr.nil?
43
+ string.sub /^(\e\[[\d;]+)/, '\1;' + sgr.to_s
44
+ end
45
+
46
+ def define_styles(styles, force=false)
47
+ String.class_eval do
48
+ if styles.delete(:basic)
49
+ Colorer::BASIC_SGR.each_pair do |meth, sgr|
50
+ raise Exception, "already defined method '#{meth}' for #{self}:#{self.class}" \
51
+ if !force && instance_methods.include?(meth.to_s)
52
+ define_method(meth) do
53
+ Colorer.add_sgr(self, sgr)
54
+ end
55
+ end
56
+ else
57
+ styles.each_pair do |meth, style|
58
+ raise Exception, "already defined method '#{meth}' for #{self}:#{self.class}" \
59
+ if !force && instance_methods.include?(meth.to_s)
60
+ define_method(meth) do
61
+ style.inject(self) { |str, sgr| Colorer.add_sgr(str, sgr) }
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ end
data/test/basic.irt ADDED
@@ -0,0 +1,26 @@
1
+ require 'colorer'
2
+
3
+ Colorer.define_styles :basic => 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 "red bold underline"
16
+ 'red bold underline'.red.bold.underline
17
+ puts _
18
+ test_value_eql? "\e[0;31;1;4mred bold underline\e[0m"
19
+
20
+ desc "red bold underline reversed"
21
+ 'red bold underline reversed'.red.bold.underline.reversed
22
+ puts _
23
+ test_value_eql? "\e[0;31;1;4;7mred bold underline reversed\e[0m"
24
+
25
+
26
+
data/test/custom.irt ADDED
@@ -0,0 +1,14 @@
1
+ require 'colorer'
2
+
3
+ Colorer.define_styles :errorize => [ :red, :bold, :underline ],
4
+ :okeyze => [ :green, :bold ]
5
+
6
+ desc "errorize"
7
+ 'errorize'.errorize
8
+ puts _
9
+ test_value_eql? "\e[0;31;1;4merrorize\e[0m"
10
+
11
+ desc "okeyze"
12
+ 'okeyize'.okeyze
13
+ puts _
14
+ test_value_eql? "\e[0;32;1mokeyize\e[0m"
data/test/sgr.irt ADDED
@@ -0,0 +1,9 @@
1
+ require 'colorer'
2
+
3
+ Colorer.define_styles :mixed_sgr => [ :bold, 8 ]
4
+
5
+
6
+ desc "Mixed SGR parameters"
7
+ "Mixed SGR parameters".mixed_sgr
8
+ puts _
9
+ test_value_eql? "\e[0;1;8mMixed SGR parameters\e[0m"
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: colorer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 2
10
+ version: 0.2.2
11
+ platform: ruby
12
+ authors:
13
+ - Domizio Demichelis
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-17 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Colorer adds the basic ANSI styles to any string, allowing also to define your own stiles
23
+ email: dd.nexus@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - LICENSE
30
+ - README.rdoc
31
+ files:
32
+ - .document
33
+ - .gitignore
34
+ - LICENSE
35
+ - README.rdoc
36
+ - Rakefile
37
+ - VERSION
38
+ - colorer.gemspec
39
+ - lib/colorer.rb
40
+ - test/basic.irt
41
+ - test/custom.irt
42
+ - test/sgr.irt
43
+ has_rdoc: true
44
+ homepage: http://github.com/ddnexus/colorer
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.7
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Easy ANSI code coloring for strings
77
+ test_files: []
78
+