colorb 0.0.6.1 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/colorb.rb +58 -50
  2. metadata +3 -4
@@ -40,72 +40,80 @@ class String
40
40
  :standout => 7
41
41
  }
42
42
 
43
- (Colors.keys + Extra.keys).each {|name|
44
- remove_method name rescue nil
45
- }
46
-
47
43
  attr_accessor :foreground, :background, :flags
48
44
 
49
- alias __old_method_missing method_missing
50
-
51
- def method_missing (id, *args, &block)
52
- name = id.to_s.match(/^(.+?)[!?]?$/)[1].to_sym
53
-
54
- result = (self.frozen? || !id.to_s.end_with?('!')) ? self.dup : self
55
-
56
- return __old_method_missing(id, *args, &block) unless Colors[name] || Extra[name]
45
+ Colors.keys.each {|name|
46
+ define_method name do
47
+ color(name)
48
+ end
57
49
 
58
- if Colors[name]
59
- if id.to_s.end_with?('?')
60
- result.foreground == name
61
- else
62
- if !result.foreground
63
- result.foreground = name
64
- else
65
- result.background = name
66
- end
67
- end
68
- elsif Extra[name]
69
- result.flags ||= []
50
+ define_method "#{name}!" do
51
+ color!(name)
52
+ end
53
+ }
70
54
 
71
- if id.to_s.end_with?('?')
72
- result.flags.member?(name)
73
- else
74
- result.flags << name
75
- end
55
+ Extra.keys.each {|name|
56
+ define_method name do
57
+ extra(name)
76
58
  end
77
59
 
78
- result.lazy? ? result : result.colorify!
79
- end
60
+ define_method "#{name}!" do
61
+ extra!(name)
62
+ end
63
+ }
80
64
 
81
65
  def color (code, second=nil)
82
- return method_missing(code.to_sym) if code.is_a?(Symbol) || code.is_a?(String)
83
-
84
- self.dup.color!(code, seond)
66
+ self.dup.color!(code, second)
85
67
  end
86
68
 
87
69
  def color! (code, second=nil)
88
- if code < 16
89
- if code > 7
90
- (self.flags ||= []) << (!self.foreground ? :bold : :blink)
91
- end
70
+ string = (self.frozen?) ? self.dup : self
92
71
 
93
- if !self.foreground
94
- self.foreground = code - (code > 7 ? 7 : 0)
72
+ if code.is_a?(Symbol) || code.is_a?(String)
73
+ name = code.to_sym
74
+
75
+ if !string.foreground
76
+ string.foreground = name
95
77
  else
96
- self.background = code - (code > 7 ? 7 : 0)
78
+ string.background = name
97
79
  end
80
+
81
+ string.lazy? ? string : string.colorify!
98
82
  else
99
- if !self.foreground
100
- self.foreground = code
83
+ if code < 16
84
+ if code > 7
85
+ (string.flags ||= []) << (!string.foreground ? :bold : :blink)
86
+ end
87
+
88
+ if !string.foreground
89
+ string.foreground = code - (code > 7 ? 7 : 0)
90
+ else
91
+ string.background = code - (code > 7 ? 7 : 0)
92
+ end
101
93
  else
102
- self.background = code
94
+ if !string.foreground
95
+ string.foreground = code
96
+ else
97
+ string.background = code
98
+ end
103
99
  end
104
100
  end
105
101
 
106
- self.color(second) if second
102
+ string.color!(second) if second
103
+
104
+ string.lazy? ? string : string.colorify!
105
+ end
106
+
107
+ def extra (name)
108
+ self.dup.extra!(name)
109
+ end
110
+
111
+ def extra! (name)
112
+ string = (self.frozen?) ? self.dup : self
113
+
114
+ (string.flags ||= []) << name
107
115
 
108
- self.lazy? ? self : self.colorify!
116
+ string.lazy? ? string : string.colorify!
109
117
  end
110
118
 
111
119
  def lazy; @lazy = true; self end
@@ -118,9 +126,9 @@ class String
118
126
  def self.colorify (string, foreground, background, flags)
119
127
  return string if ENV['NO_COLORS'] && !ENV['NO_COLORS'].empty?
120
128
 
121
- result = string.dup
129
+ string = string.dup
122
130
 
123
- result.sub!(/^/, [
131
+ string.sub!(/^/, [
124
132
  String.color!(foreground),
125
133
  String.color!(background, true),
126
134
  [flags].flatten.compact.uniq.map {|f|
@@ -128,9 +136,9 @@ class String
128
136
  }
129
137
  ].flatten.compact.join(''))
130
138
 
131
- result.sub!(/$/, String.extra!(:clean))
139
+ string.sub!(/$/, String.extra!(:clean))
132
140
 
133
- result
141
+ string
134
142
  end
135
143
 
136
144
  def self.color! (what, bg=false)
metadata CHANGED
@@ -5,9 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 6
9
- - 1
10
- version: 0.0.6.1
8
+ - 7
9
+ version: 0.0.7
11
10
  platform: ruby
12
11
  authors:
13
12
  - meh.
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-02-05 00:00:00 +01:00
17
+ date: 2011-02-10 00:00:00 +01:00
19
18
  default_executable:
20
19
  dependencies: []
21
20