dye 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,23 @@
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
+ ## Eclipse
22
+ .loadpath
23
+ .project
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010-2011 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,132 @@
1
+ = colorer
2
+
3
+ Easy ANSI code coloring for strings in libraries.
4
+
5
+ == Synopsis
6
+
7
+ require 'dye'
8
+
9
+ # you can use directly use Dye.dye for basic styles
10
+ Dye.dye "a red bold underlined text on white background", :red, :bold, :underline, :onwhite
11
+
12
+ # you can define custom styles in your classes or modules
13
+ module MyModule
14
+ extend self
15
+ CUSTOM_STYLES = { :errorize => [ :red, :bold, :underline ],
16
+ :mysgr => [ :red, 8 ] }
17
+ define_dye_method CUSTOM_STYLES
18
+ end
19
+
20
+ # use the custom styles
21
+ MyModule.dye "an error string", :errorize
22
+ MyModule.dye "my native (Select Graphic Rendition) string", :mysgr
23
+
24
+ # and use also the basic styles
25
+ MyModule.dye "red bold string", :red, :bold
26
+
27
+ # or use mixed custom and basic
28
+ MyModule.dye "red bold string", :mysgr, :bold
29
+
30
+ # you can eventually modify/add to the custom styles
31
+ CUSTOM_STYLES[:another] = [:green, :bold]
32
+ # and the style will be available right away
33
+ MyModule.dye "another style", :another
34
+
35
+ # you can also define the dye method as an instance method
36
+ any_instance.class.define_dye_method custom_styles
37
+
38
+ # and use it on the instance
39
+ any_instance.dye "any string", :any_style
40
+
41
+ # back to plain text
42
+ plain_text = Dye.strip_ansi(ansi_string)
43
+
44
+ === Feedback!!!
45
+
46
+ This is feedback-driven software. Just send me a line about you and/or what you think about it:
47
+ that will be a wonderful contribution that will help me to keep improving (and documenting) this software.
48
+
49
+ My email address is ddnexus at gmail.com ... waiting for your. Ciao.
50
+
51
+ == Features
52
+
53
+ * Does not define methods in String
54
+ * Allows you to define basic styles by just requiring the lib
55
+ * Allows you to easily add your own custom styles
56
+ * Allows extended (Select Graphic Rendition) parameters
57
+
58
+ === Difference with the Colorer gem
59
+
60
+ The Colorer gem is meant for using in your own application, it's a cool way to style string, but it
61
+ is not the perfect fit for libraries. Indeed it defines an instance method for each style, and that
62
+ might clash if another library defines the same style. That's not a problem for applications.
63
+
64
+ The Dye gem instead does not have the same problem, although its syntax is not so cool as the Colorer's one.
65
+
66
+ ==== Basic Styles List
67
+
68
+ * clear
69
+ * bold
70
+ * underline
71
+ * blinking
72
+ * reversed
73
+
74
+ * black
75
+ * red
76
+ * green
77
+ * yellow
78
+ * blue
79
+ * magenta
80
+ * cyan
81
+ * white
82
+
83
+ * onblack
84
+ * onred
85
+ * ongreen
86
+ * onyellow
87
+ * onblue
88
+ * onmagenta
89
+ * oncyan
90
+ * onwhite
91
+
92
+ === Custom Styles
93
+
94
+ You can define your own custom styles by aggregating any basic styles names,
95
+ besides can also add native SGR (Select Graphic Rendition) parameters (0..109) to any style::
96
+
97
+ custom_styles = { :errorize => [ :red, :bold, :underline ],
98
+ :okize => [ :green, :bold ],
99
+ :crazyize => [ :magenta, :onyellow, :bold, :underline ],
100
+ :mysgr => [ :red, 8 ] }
101
+ define_dye_method custom_styles
102
+
103
+ See http://en.wikipedia.org/wiki/ANSI_colors for a complete list of SGR codes.
104
+
105
+ === Strict ANSI
106
+
107
+ Some terminals don't parse composite SGR styles correctly, and need separate SGR for each.
108
+
109
+ puts "\e[7;31;46mSTRING\e[0m" # strict_ansi == true (may be difficult to parse)
110
+ puts "\e[7m\e[31m\e[46mSTRING\e[0m" # strict_ansi == false
111
+
112
+ On the other way most of the terminals that parse them correctly can parse also separate SGRs,
113
+ so Dye will output non strict ansi by default. If you want to have strict ansi you can do:
114
+
115
+ Dye.strict_ansi = true
116
+
117
+ or you can set the DYE_ANSI_STRICT environment variable for a system wide setting.
118
+
119
+ === Color
120
+
121
+ The color is true by defealut on a non-dumb tty terminal, anyway you can force it
122
+ by explicitly setting it:
123
+
124
+ Dye.color? #=> true/false by default depending on your terminal
125
+ Dye.color = true # force true
126
+ Dye.color? #=> true
127
+ Dye.color = false # force false
128
+ Dye.color? #=> false
129
+
130
+ == Copyright
131
+
132
+ Copyright (c) 2010-2011 Domizio Demichelis. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,60 @@
1
+ name = 'dye'
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 ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/dye.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ name = File.basename( __FILE__, '.gemspec' )
2
+ version = File.read(File.expand_path('../VERSION', __FILE__)).strip
3
+ require 'date'
4
+
5
+ Gem::Specification.new do |s|
6
+
7
+ s.authors = ["Domizio Demichelis"]
8
+ s.email = 'dd.nexus@gmail.com'
9
+ s.homepage = 'http://github.com/ddnexus/dye'
10
+ s.summary = 'Easy ANSI code coloring for strings in libraries'
11
+ s.description = 'Dye adds the basic ANSI styles to any string, allowing also to define your own stiles'
12
+
13
+ s.add_development_dependency('irt', [">= 1.0.0"])
14
+
15
+ s.files = `git ls-files -z`.split("\0")
16
+
17
+ s.name = name
18
+ s.version = version
19
+ s.date = Date.today.to_s
20
+
21
+ s.required_rubygems_version = ">= 1.3.6"
22
+ s.rdoc_options = ["--charset=UTF-8"]
23
+ s.require_paths = ["lib"]
24
+
25
+ end
26
+
data/lib/dye.rb ADDED
@@ -0,0 +1,100 @@
1
+ class Object
2
+
3
+ def define_dye_method(custom_styles={})
4
+ define_method(:dye) do |string, *names|
5
+ return string unless Dye.color?
6
+ codes = names.map do |name|
7
+ case
8
+ when custom_styles.has_key?(name)
9
+ Dye.sgr_names_to_codes *custom_styles[name]
10
+ when Dye::BASIC_SGR.has_key?(name)
11
+ Dye::BASIC_SGR[name]
12
+ else
13
+ raise Dye::UnknownSgrCode.new(name)
14
+ end
15
+ end.flatten
16
+ Dye.apply_codes string, *codes
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+
23
+ module Dye
24
+
25
+ extend self
26
+
27
+ VERSION = File.read(File.expand_path('../../VERSION', __FILE__)).strip
28
+
29
+ class UnknownSgrCode < Exception
30
+ def initialize(sgr)
31
+ super("#{sgr.inspect} is unknown")
32
+ end
33
+ end
34
+
35
+ # Select Graphic Rendition
36
+ BASIC_SGR = {
37
+ :clear => 0,
38
+ :bold => 1,
39
+ :underline => 4,
40
+ :blinking => 5,
41
+ :reversed => 7,
42
+
43
+ :black => 30,
44
+ :red => 31,
45
+ :green => 32,
46
+ :yellow => 33,
47
+ :blue => 34,
48
+ :magenta => 35,
49
+ :cyan => 36,
50
+ :white => 37,
51
+
52
+ :onblack => 40,
53
+ :onred => 41,
54
+ :ongreen => 42,
55
+ :onyellow => 43,
56
+ :onblue => 44,
57
+ :onmagenta => 45,
58
+ :oncyan => 46,
59
+ :onwhite => 47
60
+ }
61
+
62
+ @color = !!STDOUT.tty? && !!ENV['TERM'] && ENV['TERM'] != 'dumb'
63
+ attr_accessor :color
64
+ alias_method :color?, :color
65
+
66
+ @strict_ansi = !!ENV['DYE_STRICT_ANSI']
67
+ attr_accessor :strict_ansi
68
+
69
+ def dye(string, *sgr_names)
70
+ return string unless color?
71
+ apply_codes string, sgr_names_to_codes(*sgr_names)
72
+ end
73
+
74
+ def strip_ansi(string)
75
+ string.gsub(/\e\[[\d;]+m/, '')
76
+ end
77
+
78
+ def sgr_names_to_codes(*names)
79
+ names.map do |n|
80
+ next if n.nil?
81
+ code = n.is_a?(Symbol) ? BASIC_SGR[n] : n
82
+ raise UnknownSgrCode.new(n) unless code.is_a?(Integer) && (0..109).include?(code)
83
+ code
84
+ end
85
+ end
86
+
87
+ def apply_codes(string, *codes)
88
+ codes.compact!
89
+ return string if codes.empty?
90
+ if strict_ansi
91
+ string.match(/^\e\[[\d;]+m.*\e\[0m$/m) ?
92
+ string.sub(/^(\e\[[\d;]+)/, '\1;' + codes.join(';')) :
93
+ sprintf("\e[0;%sm%s\e[0m", codes.join(';'), string)
94
+ else
95
+ string.match(/^(?:\e\[\d+m)+.*\e\[0m$/m) ?
96
+ string.sub(/^((?:\e\[\d+m)+)/, '\1' + codes.map{|c| "\e[#{c}m" }.join) :
97
+ sprintf("\e[0m%s%s\e[0m", codes.map{|c| "\e[#{c}m" }.join, string)
98
+ end
99
+ end
100
+ end
data/test/basic.irt ADDED
@@ -0,0 +1,26 @@
1
+ desc "red"
2
+ Dye.dye 'red', :red
3
+ puts _
4
+ _eql? "\e[0;31mred\e[0m"
5
+
6
+ desc "red bold"
7
+ Dye.dye 'red bold', :red, :bold
8
+ puts _
9
+ _eql? "\e[0;31;1mred bold\e[0m"
10
+
11
+ desc "red bold underline"
12
+ Dye.dye 'red bold underline', :red, :bold, :underline
13
+ puts _
14
+ _eql? "\e[0;31;1;4mred bold underline\e[0m"
15
+
16
+ desc "red bold underline reversed"
17
+ Dye.dye 'red bold underline reversed', :red, :bold, :underline, :reversed
18
+ puts _
19
+ _eql? "\e[0;31;1;4;7mred bold underline reversed\e[0m"
20
+
21
+ desc "red bold with \\n"
22
+ Dye.dye "red bold with\n", :red, :bold
23
+ puts _
24
+ _eql? "\e[0;31;1mred bold with\n\e[0m"
25
+
26
+
data/test/custom.irt ADDED
@@ -0,0 +1,27 @@
1
+ custom_styles = { :dummy => nil,
2
+ :errorize => [ :red, :bold, :underline ],
3
+ :okeyze => [ :green, :bold ] }
4
+ self.class.define_dye_method custom_styles
5
+
6
+ desc "errorize"
7
+ dye 'errorize', :errorize
8
+ puts _
9
+ _eql? "\e[0;31;1;4merrorize\e[0m"
10
+
11
+ desc "okeyze"
12
+ dye 'okeyize', :okeyze
13
+ puts _
14
+ _eql? "\e[0;32;1mokeyize\e[0m"
15
+
16
+ desc "allows dummy styles"
17
+ dye 'dummy', :dummy
18
+ puts _
19
+ _eql?( "dummy" )
20
+
21
+ custom_styles[:added] = :blue
22
+
23
+ desc "add to custom styles"
24
+ dye 'added', :added
25
+ puts _
26
+ _eql?( "\e[0;34madded\e[0m" )
27
+
@@ -0,0 +1,4 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'dye'
3
+ Dye.strict_ansi = true
4
+
data/test/module.irt ADDED
@@ -0,0 +1,18 @@
1
+ module MyModule
2
+ extend self
3
+ define_dye_method
4
+ end
5
+
6
+ desc 'module call'
7
+ MyModule.dye 'My Module', :green, :bold
8
+ puts _
9
+ _eql?( "\e[0;32;1mMy Module\e[0m" )
10
+
11
+ class MyInstance; end
12
+
13
+ desc 'instance call'
14
+ obj = MyInstance.new
15
+ obj.class.define_dye_method
16
+ obj.dye 'My Instance', :reversed, :yellow
17
+ puts _
18
+ _eql?( "\e[0;7;33mMy Instance\e[0m" )
data/test/sgr.irt ADDED
@@ -0,0 +1,16 @@
1
+ custom_styles = { :mixed_sgr => [ :bold, 7 ] }
2
+
3
+ self.class.define_dye_method custom_styles
4
+
5
+ desc "Mixed SGR parameters"
6
+ dye "Mixed SGR parameters", :mixed_sgr
7
+ puts _
8
+ _eql? "\e[0;1;7mMixed SGR parameters\e[0m"
9
+
10
+ desc "Basic and custom definition"
11
+ custom_styles.merge!({:goo => [7], :guu => 4})
12
+ dye "Basic and custom definition", :green, :goo, :guu
13
+ puts _
14
+ _eql? "\e[0;32;7;4mBasic and custom definition\e[0m"
15
+
16
+
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dye
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Domizio Demichelis
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-31 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: irt
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: Dye adds the basic ANSI styles to any string, allowing also to define your own stiles
38
+ email: dd.nexus@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - dye.gemspec
52
+ - lib/dye.rb
53
+ - test/basic.irt
54
+ - test/custom.irt
55
+ - test/irt_helper.rb
56
+ - test/module.irt
57
+ - test/sgr.irt
58
+ has_rdoc: true
59
+ homepage: http://github.com/ddnexus/dye
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 23
82
+ segments:
83
+ - 1
84
+ - 3
85
+ - 6
86
+ version: 1.3.6
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.7
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Easy ANSI code coloring for strings in libraries
94
+ test_files: []
95
+