colorer 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/colorer.rb +30 -22
  3. data/test/sgr.irt +10 -2
  4. metadata +5 -5
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4
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 Exception < ::Exception; end
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
- class << self
36
- attr_accessor :color
47
+ attr_accessor :color
37
48
 
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
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
- def define_styles(styles, force=false)
57
+ def define_styles(styles, force=false)
58
+ styles.each_pair do |meth, style|
47
59
  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
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
- 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
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, 8 ]
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;8mMixed SGR parameters\e[0m"
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: 31
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 4
10
- version: 0.2.4
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-09-25 00:00:00 -04:00
18
+ date: 2010-10-17 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency