Glitz 0.1.0

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 (5) hide show
  1. data/LICENSE +19 -0
  2. data/README.rdoc +51 -0
  3. data/lib/glitz.rb +94 -0
  4. data/lib/glitz/version.rb +3 -0
  5. metadata +72 -0
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Mattt Thompson (http://mattt.me)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,51 @@
1
+ = Glitz adds ANSI-colorized glamour to your terminal output
2
+
3
+ == Example
4
+
5
+ require 'glitz'
6
+
7
+ include Glitz
8
+
9
+ puts "Roses are Red".foreground(:red)
10
+ puts "Editors are White on Black".foreground(:white).background(:black)
11
+ puts "If you eat " + "Chunky Bacon".bold.underline
12
+ puts "Beware the dreaded " + "Blink Attack!".foreground(:white).background(:magenta).blink
13
+
14
+ == Demo
15
+
16
+ If your terminal supports ANSI colors, execute the command below to check out the output from the example
17
+
18
+ $ echo | curl https://raw.github.com/gist/1217524/9222c1e0f788b852ec277f213fe47b122ef3836f/poem.txt
19
+
20
+ == Usage
21
+
22
+ Colors and effects can be chained, and added in any order. You can specify one foreground color and one background color (setting a new value will overwrite the previous one). Effects like bold and underline, on the other hand, combine together.
23
+
24
+ === Supported Values
25
+
26
+ * Colors: <tt>[:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]</tt>
27
+ * Effects: <tt>[:bold, :underline, :blink, :reverse, :concealed]</tt>
28
+
29
+ == License
30
+
31
+ Glitz is licensed under the MIT License:
32
+
33
+ Copyright (c) 2011 Mattt Thompson (http://mattt.me)
34
+
35
+ Permission is hereby granted, free of charge, to any person obtaining a copy
36
+ of this software and associated documentation files (the "Software"), to deal
37
+ in the Software without restriction, including without limitation the rights
38
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
39
+ copies of the Software, and to permit persons to whom the Software is
40
+ furnished to do so, subject to the following conditions:
41
+
42
+ The above copyright notice and this permission notice shall be included in
43
+ all copies or substantial portions of the Software.
44
+
45
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
48
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
50
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
51
+ THE SOFTWARE.
data/lib/glitz.rb ADDED
@@ -0,0 +1,94 @@
1
+ module Glitz
2
+ COLORS = {
3
+ :black => 0,
4
+ :red => 1,
5
+ :green => 2,
6
+ :yellow => 3,
7
+ :blue => 4,
8
+ :magenta => 5,
9
+ :cyan => 6,
10
+ :white => 7
11
+ }
12
+
13
+ EFFECTS = {
14
+ :bold => 1,
15
+ :underline => 4,
16
+ :blink => 5,
17
+ :reverse => 7,
18
+ :concealed => 8,
19
+ }
20
+
21
+ def self.included(klass)
22
+ String.instance_eval do
23
+ extend ClassMethods
24
+ include InstanceMethods
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ def ansi_colors
30
+ COLORS.keys
31
+ end
32
+
33
+ def ansi_effects
34
+ EFFECTS.keys
35
+ end
36
+ end
37
+
38
+ module InstanceMethods
39
+ def colorize(options = {})
40
+ ColoredString.new(self).colorize(options)
41
+ end
42
+
43
+ def uncolorize
44
+ self
45
+ end
46
+
47
+ EFFECTS.keys.each do |effect|
48
+ define_method(effect) do
49
+ colorize(:effects => effect)
50
+ end
51
+ end
52
+
53
+ def foreground(color)
54
+ colorize(:foreground => color)
55
+ end
56
+
57
+ def background(color)
58
+ colorize(:background => color)
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ class ColoredString
65
+ include InstanceMethods
66
+
67
+ def initialize(string)
68
+ @string = string
69
+ @effects = []
70
+ end
71
+
72
+ def colorize(options = {})
73
+ @foreground = COLORS[options.delete(:foreground)] + 30 if options.key? :foreground
74
+ @background = COLORS[options.delete(:background)] + 40 if options.key? :background
75
+ @effects += Array(options.delete(:effects)).collect{|s| EFFECTS[s]} if options.key? :effects
76
+
77
+ self
78
+ end
79
+
80
+ def uncolorize
81
+ @string
82
+ end
83
+
84
+ def to_s
85
+ "\033[#{[@effects, @foreground, @background].flatten.compact.uniq.join(';')}m#{@string}\033[0m"
86
+ end
87
+
88
+ alias :to_str :to_s
89
+
90
+ def method_missing(method_name, *args, &block)
91
+ self.to_s.send(method_name, *args, &block)
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,3 @@
1
+ module Glitz
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Glitz
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Mattt Thompson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-18 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Glitz adds ANSI-colorized glamour to your terminal output
23
+ email: m@mattt.me
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/glitz/version.rb
32
+ - lib/glitz.rb
33
+ - LICENSE
34
+ - README.rdoc
35
+ has_rdoc: true
36
+ homepage: http://github.com/mattt/glitz
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 23
59
+ segments:
60
+ - 1
61
+ - 3
62
+ - 6
63
+ version: 1.3.6
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.6.2
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Glitz
71
+ test_files: []
72
+