colorer 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Domizio Demichelis
1
+ Copyright (c) 2010-2011 Domizio Demichelis
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -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.
@@ -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.6.1
1
+ 0.7.0
@@ -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.8.0"])
13
+ s.add_development_dependency('irt', [">= 1.0.0"])
14
14
 
15
15
  s.files = `git ls-files -z`.split("\0")
16
16
 
@@ -43,8 +43,12 @@ module Colorer
43
43
 
44
44
  extend self
45
45
 
46
- @color = RbConfig::CONFIG['host_os'] !~ /mswin|mingw/
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.join(';')
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 (Colorer.color && STDOUT.tty? && ENV['TERM'] && ENV['TERM'] != 'dumb')
82
- match(/^\e\[[\d;]+m.*\e\[0m$/) ?
83
- sub(/^(\e\[[\d;]+)/, '\1;' + codes) :
84
- sprintf("\e[0;%sm%s\e[0m", codes, self)
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
@@ -3,19 +3,26 @@ Colorer.def_basic_styles true, true
3
3
  desc "red"
4
4
  'red'.red
5
5
  puts _
6
- test_value_eql? "\e[0;31mred\e[0m"
6
+ _eql? "\e[0;31mred\e[0m"
7
7
 
8
8
  desc "red bold"
9
9
  'red bold'.red.bold
10
10
  puts _
11
- test_value_eql? "\e[0;31;1mred bold\e[0m"
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
- test_value_eql? "\e[0;31;1;4mred bold underline\e[0m"
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
- test_value_eql? "\e[0;31;1;4;7mred bold underline reversed\e[0m"
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
+
@@ -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
- test_value_eql? "\e[0;31;1;4merrorize\e[0m"
9
+ _eql? "\e[0;31;1;4merrorize\e[0m"
9
10
 
10
11
  desc "okeyze"
11
12
  'okeyize'.okeyze
12
13
  puts _
13
- test_value_eql? "\e[0;32;1mokeyize\e[0m"
14
+ _eql? "\e[0;32;1mokeyize\e[0m"
14
15
 
15
16
  desc "allows dummy styles"
16
17
  'dummy'.dummy
17
18
  puts _
18
- test_value_eql?( "dummy" )
19
+ _eql?( "dummy" )
@@ -1,3 +1,4 @@
1
1
  $:.unshift File.expand_path('../../lib', __FILE__)
2
+ Colorer.strict_ansi = true
2
3
 
3
4
  require 'colorer'
@@ -3,21 +3,21 @@ Colorer.def_basic_styles([:bold, :red], true)
3
3
  desc "red"
4
4
  'red'.red
5
5
  puts _
6
- test_value_eql? "\e[0;31mred\e[0m"
6
+ _eql? "\e[0;31mred\e[0m"
7
7
 
8
8
  desc "red bold"
9
9
  'red bold'.red.bold
10
10
  puts _
11
- test_value_eql? "\e[0;31;1mred bold\e[0m"
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
- test_value_eql? "\e[0;47monwhite\e[0m"
17
+ _eql? "\e[0;47monwhite\e[0m"
18
18
 
19
19
 
20
20
  desc "not underline"
21
21
  'not underline'.respond_to? :underline
22
- test_value_eql? false
22
+ _eql? false
23
23
 
@@ -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
- test_value_eql? "\e[0;1;7mMixed SGR parameters\e[0m"
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
- test_value_eql? "\e[0;32;7;4mBasic and custom definition\e[0m"
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: 5
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 1
10
- version: 0.6.1
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: 2010-12-25 00:00:00 -04:00
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: 63
29
+ hash: 23
30
30
  segments:
31
+ - 1
31
32
  - 0
32
- - 8
33
33
  - 0
34
- version: 0.8.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