colorb 0.0.1

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.
Files changed (2) hide show
  1. data/lib/colorb.rb +102 -0
  2. metadata +64 -0
@@ -0,0 +1,102 @@
1
+ #--
2
+ # Copyleft meh. [http://meh.doesntexist.org | meh@paranoici.org]
3
+ #
4
+ # colorb is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU Affero General Public License as published
6
+ # by the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # colorb is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU Affero General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Affero General Public License
15
+ # along with colorb. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+
18
+ require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
19
+
20
+ class String
21
+ Colors = {
22
+ :default => 9,
23
+
24
+ :black => 0,
25
+ :red => 1,
26
+ :green => 2,
27
+ :yellow => 3,
28
+ :blue => 4,
29
+ :magenta => 5,
30
+ :cyan => 6,
31
+ :white => 7
32
+ }
33
+
34
+ Extra = {
35
+ :default => 0,
36
+
37
+ :bold => 1,
38
+ :underline => 4,
39
+ :blink => 5,
40
+ :standout => 7
41
+ }
42
+
43
+ (Colors.keys + Extra.keys).each {|name|
44
+ remove_method name rescue nil
45
+ }
46
+
47
+ attr_reader :foreground, :background, :flags
48
+
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
+ return __old_method_missing(id, *args, &block) unless Colors[name] || Extra[name]
55
+
56
+ if Colors[name]
57
+ if id.to_s.end_with?('?')
58
+ @foreground == name
59
+ else
60
+ if !foreground
61
+ @foreground = name
62
+ else
63
+ @background = name
64
+ end
65
+ end
66
+ elsif Extra[name]
67
+ @flags ||= []
68
+
69
+ if id.to_s.end_with?('?')
70
+ @flags.member?(name)
71
+ else
72
+ @flags << name
73
+ end
74
+ end
75
+
76
+ String.colorify(self, @foreground, @background, @flags)
77
+ end
78
+
79
+ def self.colorify (string, foreground, background, flags)
80
+ result = string.clone
81
+
82
+ result.sub!(/^/, [
83
+ String.color!(foreground),
84
+ String.color!(background, true),
85
+ [flags].flatten.compact.map {|f|
86
+ String.extra!(f)
87
+ }
88
+ ].flatten.compact.join(''))
89
+
90
+ result.sub!(/$/, String.extra!(:default))
91
+
92
+ result
93
+ end
94
+
95
+ def self.color! (name, bg=false)
96
+ "\e[#{Colors[name] + (bg ? 40 : 30)}m" if Colors[name]
97
+ end
98
+
99
+ def self.extra! (name)
100
+ "\e[#{Extra[name]}m" if Extra[name]
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: colorb
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - meh.
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-11 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: meh@paranoici.org
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/colorb.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/meh/colorb
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.7
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Colorify strings
63
+ test_files: []
64
+