paint 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,2 @@
1
+ === 0.8 ===
2
+ * Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT LICENSE
2
+
3
+ Copyright (c) 2011 Jan Lelis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,155 @@
1
+ Paint manages terminal colors and effects for you. It combines the strengths of gems like term-ansicolor or rainbow into a simple to use and flexible colorizer.
2
+
3
+ == Features
4
+ * No string extensions (suitable for library development)
5
+ * Supports setting 256 colors (for capable terminals)
6
+ * Supports setting any effects (although most terminals won't support it)
7
+ * Simple to use
8
+ * Custom shortcuts can be defined and flexibly mixed in
9
+
10
+ == TODO next version (beginning of july)
11
+ * Fall-back mode for non-256-color terminals (Paint.mode)
12
+ * Detection of those old terminals
13
+
14
+ == Setup
15
+
16
+ Install with:
17
+
18
+ gem install paint
19
+
20
+ In Ruby do:
21
+
22
+ require 'paint'
23
+
24
+ == Usage
25
+
26
+ The only method you need to know to get started is: <tt>Paint.[]</tt>
27
+
28
+ The first argument given to Paint.[] is the string to colorize (if the object is not a string, to_s will be called on the object). The other arguments describe how to modify/colorize the string. Let's learn by example:
29
+
30
+ Paint['Ruby', :red] # sets ansi color red
31
+ Paint['Ruby', :red, :bright] # also applies bright/bold effect
32
+ Paint['Ruby', :bright, :red] # does the same as above
33
+ Paint['Ruby', :red, :bright, :underline] # effects can often be combined
34
+ Paint['Ruby', :red, :blue] # the second color you define is for background
35
+ Paint['Ruby', nil, :blue] # pass a nil before a color to ignore foreground and only set background color
36
+ Paint['Ruby', [100, 255, 5]] # you can define rgb colors that map to one of 256 colors. Only supported on 256-color terminals, of course
37
+ Paint['Ruby', "gold", "snow"] # Paint supports rgb.txt color names, note that the arguments are strings (:yellow != "yellow")!
38
+ Paint['Ruby', "#123456"] # html like definitions are possible.
39
+ Paint['Ruby', "fff"] # another html hex definition
40
+ Paint['Ruby'] # don't pass any argument to get one of eight random ansi foreground colors
41
+ Paint['Ruby', :inverse] # swaps fore- and background
42
+ Paint['Ruby', :italic, :encircle, :rapid_blink, :overline] # probably not supported effects
43
+
44
+ If you pass multiple colors, the first one is taken as foreground color and the second one defines the background color (all others will be ignored). To only change the background color, you have to pass a <tt>nil</tt> first. Effects can be passed in any order.
45
+
46
+ To see more examples, checkout the specs.
47
+
48
+ == More details about terminal colors and effects
49
+
50
+ Terminal colors/effects are set by {ansi escape sequences}[http://en.wikipedia.org/wiki/ANSI_escape_code]. These are strings that look like this: <tt>\e[X;X;X;X;X]m</tt> where X are integers with some meaning. For example, 0 means reset, 31 means red foreground and 41 red background. When you tell Paint to use one of the eight ansi base colors as foreground color, it just inserts a number between 30 and 37 in the sequence. The following colors are available:
51
+
52
+ :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, (:default)
53
+
54
+ When combined with the <tt>:bright</tt> (= <tt>:bold</tt>) effect, the color in the terminal emulator often differs a little bit.
55
+
56
+ Through special sequences it's also possible to set 256-colors, instead of 8, which is also supported by many - but not all - terminals. Paint automatically translates given rgb colors to a suitable color of the 256 available colors.
57
+
58
+ When using the <tt>Paint.[]</tt> method, Paint wraps the given string between the calculated escape sequence and an reset sequence (<tt>"\e[0m"</tt>). You can directly get only the escape sequence by using the <tt>Paint.color</tt> method.
59
+
60
+ === Effects (without colors)
61
+
62
+ Also see {en.wikipedia.org/wiki/ANSI_escape_code}[http://en.wikipedia.org/wiki/ANSI_escape_code]:
63
+
64
+ ==== Often supported
65
+
66
+ 0) :reset, :nothing
67
+ 1) :bright, :bold
68
+ 4) :underline
69
+ 7) :inverse, :negative
70
+ 8) :conceal, :hide
71
+ 22) :clean
72
+ 24) :underline_off
73
+ 26) :inverse_off, :positive
74
+ 27) :conceal_off, :show, :reveal
75
+
76
+ ==== Not widely supported
77
+
78
+ 2) :faint
79
+ 3) :italic
80
+ 5) :blink, :slow_blink
81
+ 6) :rapid_blink
82
+ 9) :crossed, :crossed_out
83
+ 10) :default_font, :font0
84
+ 11-19) :font1, :font2, :font3, :font4, :font5, :font6, :font7, :font8, :font9
85
+ 20) :fraktur
86
+ 21) :bright_off, :bold_off, :double_underline
87
+ 23) :italic_off, :fraktur_off
88
+ 25) :blink_off
89
+ 29) :crossed_off, :crossed_out_off
90
+ 51) :frame
91
+ 52) :encircle
92
+ 53) :overline
93
+ 54) :frame_off, :encircle_off
94
+ 55) :overline_off
95
+
96
+ == Shortcuts
97
+
98
+ Now for the fancy part: Color shortcuts for your gems and scripts! Note: You don't have to use them (and only stick to <tt>Paint.[]</tt>) ;)
99
+
100
+ It's easy: Just setup a hash of symbol keys and escape string values at: <tt>Paint::SHORTCUTS[:your_namespace]</tt>, for example:
101
+
102
+ Paint::SHORTCUTS[:example] = {
103
+ :white => Paint.color(:black),
104
+ :red => Paint.color(:red, :bright),
105
+ :title => Paint.color(:underline),
106
+ }
107
+
108
+ The methods become rubymagically available in a <tt>Paint</tt> child model (via method_missing):
109
+
110
+ Paint::Example.red 'Ruby' # => "\e[31;1mRuby\e[0m"
111
+ Paint::Example.white # => "\e[37m"
112
+
113
+ As you can see, the helper methods look useful and can take either one (wrap string) or none (only color) arguments ...but they aren't really <em>short</em> yet.
114
+
115
+ === Inlcuding action
116
+
117
+ Just include them:
118
+
119
+ include Paint::Example
120
+ red # => "\e[31;1m"
121
+ white 'Ruby' # => "\e[30m"
122
+
123
+ All shortcuts, defined in your shortcut namespace at this time, are now (privately) available in your current namespace (without using a method_missing implementation).
124
+
125
+ Furthermore, there are two variations of this approach:
126
+
127
+ include Paint::Example::String
128
+ "Ruby".title # => "\e[4mRuby\e[0m"
129
+ 5.red # => "\e[31;1m5\e[0m"
130
+
131
+ In this case, <tt>self</tt> will be converted to a string and wrapped with the specific color code. Note, that the helper methods doesn't take any arguments when using this method.
132
+
133
+ The third way allows you to get a single color helper method to avoid cluttering namespaces:
134
+
135
+ include Paint::Example::Prefix::ExampleName
136
+ "Ruby".example_name(:red) # => "\e[31;1mRuby\e[0m"
137
+
138
+ === Utilities
139
+
140
+ There are some helper methods available. You can get a <tt>p</tt> like alternative for calling <tt>Paint.[]</tt>:
141
+
142
+ require 'paint/pa'
143
+ pa "Ruby", :red, :underline # same as puts Paint["Ruby", :red, :underline]
144
+
145
+ Another one is <tt>Paint.unpaint</tt>, which removes any ansi colors:
146
+
147
+ Paint.unpaint( Paint['J-_-L', :red, :bright] ).should == 'J-_-L'
148
+
149
+ == Credits
150
+
151
+ Mainly influenced by rainbow[https://github.com/sickill/rainbow] and {term-ansicolor}[https://github.com/flori/term-ansicolor].
152
+
153
+ Copyright (c) 2011 Jan Lelis, http://rbjl.net, released under the MIT license.
154
+
155
+ J-_-L
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ GEMSPEC = 'paint.gemspec'
2
+
3
+ require 'rake'
4
+ #require 'rake/rdoctask'
5
+ require 'fileutils'
6
+ require 'rspec/core/rake_task'
7
+
8
+ task :default => :spec
9
+ task :test => :spec
10
+
11
+ RSpec::Core::RakeTask.new(:spec) do |t|
12
+ t.rspec_opts = [
13
+ '--color',
14
+ '-r ' + File.expand_path( File.join( 'spec', 'spec_helper') ),
15
+ ]
16
+ end
17
+
18
+ def gemspec
19
+ @gemspec ||= eval(File.read(GEMSPEC), binding, GEMSPEC)
20
+ end
21
+
22
+ desc "Build the gem"
23
+ task :gem => :gemspec do
24
+ sh "gem build #{GEMSPEC}"
25
+ FileUtils.mkdir_p 'pkg'
26
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
27
+ end
28
+
29
+ desc "Install the gem locally"
30
+ task :install => :gem do
31
+ sh %{gem install pkg/#{gemspec.name}-#{gemspec.version} --no-rdoc --no-ri}
32
+ end
33
+
34
+ desc "Generate the gemspec"
35
+ task :generate do
36
+ puts gemspec.to_ruby
37
+ end
38
+
39
+ desc "Validate the gemspec"
40
+ task :gemspec do
41
+ gemspec.validate
42
+ end
43
+
44
+ #Rake::RDocTask.new do |rdoc|
45
+ # require File.expand_path( File.join( 'lib', 'paint') )
46
+ #
47
+ # rdoc.rdoc_dir = 'doc'
48
+ # rdoc.title = "paint #{Paint::VERSION}"
49
+ # rdoc.rdoc_files.include('README*')
50
+ # rdoc.rdoc_files.include('lib/**/*.rb')
51
+ #end
data/lib/paint.rb ADDED
@@ -0,0 +1,175 @@
1
+ require 'paint/version'
2
+ require 'paint/shortcuts'
3
+ require 'paint/util'
4
+
5
+ module Paint
6
+ autoload :RGB_COLORS, 'paint/rgb_colors'
7
+
8
+ # Important purpose
9
+ NOTHING = "\e[0m"
10
+
11
+ # Basic colors (often, the color differs when using the bright effect)
12
+ # Final color will be 30 + value for foreground and 40 + value for background
13
+ ANSI_COLORS = {
14
+ :black => 0,
15
+ :red => 1,
16
+ :green => 2,
17
+ :yellow => 3,
18
+ :blue => 4,
19
+ :magenta => 5,
20
+ :cyan => 6,
21
+ :white => 7,
22
+ :default => 9,
23
+ }
24
+
25
+ # Terminal effects - most of them are not supported ;)
26
+ # See http://en.wikipedia.org/wiki/ANSI_escape_code
27
+ ANSI_EFFECTS = {
28
+ :reset => 0, :nothing => 0, # usually supported
29
+ :bright => 1, :bold => 1, # usually supported
30
+ :faint => 2,
31
+ :italic => 3,
32
+ :underline => 4, # usually supported
33
+ :blink => 5, :slow_blink => 5,
34
+ :rapid_blink => 6,
35
+ :inverse => 7, :swap => 7, # usually supported
36
+ :conceal => 8, :hide => 9,
37
+ :default_font => 10,
38
+ :font0 => 10, :font1 => 11, :font2 => 12, :font3 => 13, :font4 => 14,
39
+ :font5 => 15, :font6 => 16, :font7 => 17, :font8 => 18, :font9 => 19,
40
+ :fraktur => 20,
41
+ :bright_off => 21, :bold_off => 21, :double_underline => 21,
42
+ :clean => 22,
43
+ :italic_off => 23, :fraktur_off => 23,
44
+ :underline_off => 24,
45
+ :blink_off => 25,
46
+ :inverse_off => 26, :positive => 26,
47
+ :conceal_off => 27, :show => 27, :reveal => 27,
48
+ :crossed_off => 29, :crossed_out_off => 29,
49
+ :frame => 51,
50
+ :encircle => 52,
51
+ :overline => 53,
52
+ :frame_off => 54, :encircle_off => 54,
53
+ :overline_off => 55,
54
+ }
55
+
56
+ class << self
57
+ # Takes a string and color options and colorizes the string
58
+ # See README.rdoc for details
59
+ def [](string, *args)
60
+ color(*args) + string.to_s + NOTHING
61
+ end
62
+
63
+ # Sometimes, you only need the color
64
+ # Used by []
65
+ def color(*options)
66
+ mix = []
67
+ color_seen = false
68
+
69
+ if options.empty?
70
+ mix << random(false) # random foreground color
71
+ else
72
+ options.each{ |option|
73
+ case option
74
+ when Symbol
75
+ if ANSI_EFFECTS.keys.include?(option)
76
+ mix << effect(option)
77
+ elsif ANSI_COLORS.keys.include?(option)
78
+ mix << simple(option, color_seen)
79
+ color_seen = true
80
+ else
81
+ raise ArgumentError, "Unknown color or effect: #{ option }"
82
+ end
83
+
84
+ when Array
85
+ if option.size == 3 && option.all?{ |n| n.is_a? Numeric }
86
+ mix << rgb(*(option + [color_seen])) # 1.8 workaround
87
+ color_seen = true
88
+ else
89
+ raise ArgumentError, "Array argument must contain 3 numerals"
90
+ end
91
+
92
+ when ::String
93
+ if option =~ /^#?(?:[0-9a-f]{3}){1,2}$/
94
+ mix << hex(option, color_seen)
95
+ color_seen = true
96
+ else
97
+ mix << name(option, color_seen)
98
+ color_seen = true
99
+ end
100
+
101
+ when Numeric
102
+ integer = option.to_i
103
+ color_seen = true if (30..49).include?(integer)
104
+ mix << integer
105
+
106
+ when nil
107
+ color_seen = true
108
+
109
+ else
110
+ raise ArgumentError, "Invalid argument: #{ option.inspect }"
111
+
112
+ end
113
+ }
114
+ end
115
+
116
+ wrap(*mix)
117
+ end
118
+
119
+ # Adds ansi sequence
120
+ def wrap(*ansi_codes)
121
+ "\e[" + ansi_codes*";" + "m"
122
+ end
123
+
124
+ # Creates simple ansi color by looking it up on Paint::ANSI_COLORS
125
+ def simple(color_name, background = false)
126
+ (background ? 40 : 30) + ANSI_COLORS[color_name]
127
+ end
128
+
129
+ # Creates a 256-compatible color from rgb values
130
+ def rgb(red, green, blue, background = false)
131
+ "#{background ? 48 : 38};5;#{rgb_value(red, green, blue)}"
132
+ end
133
+
134
+ # Creates 256-compatible color from a html-like color string
135
+ def hex(string, background = false)
136
+ string.tr! '#',''
137
+ rgb(
138
+ *(if string.size == 6
139
+ # string.chars.each_cons(2).map{ |hex_color| hex_color.join.to_i(16) }
140
+ [string[0,2].to_i(16), string[2,2].to_i(16), string[4,2].to_i(16)]
141
+ else
142
+ string.chars.map{ |hex_color_half| (hex_color_half*2).to_i(16) }
143
+ end + [background]) # 1.8 workaround
144
+ )
145
+ end
146
+
147
+ # Creates a 256-color from a name found in Paint::RGB_COLORS (based on rgb.txt)
148
+ def name(color_name, background = false)
149
+ if color_code = RGB_COLORS[color_name]
150
+ "#{background ? 48 : 38};5;#{color_code}"
151
+ end
152
+ end
153
+
154
+ # Creates a random ansi color
155
+ def random(background = false)
156
+ (background ? 40 : 30) + rand(8)
157
+ end
158
+
159
+ # Creates the specified effect by looking it up in Paint::ANSI_COLORS
160
+ def effect(effect_name)
161
+ ANSI_EFFECTS[effect_name]
162
+ end
163
+
164
+ private
165
+
166
+ # Gets nearest supported color, heavily inspired by the rainbow gem
167
+ def rgb_value(red, green, blue)
168
+ [16, *[red, green, blue].zip([36, 6, 1]).map{ |color, mod|
169
+ (6 * (color.to_f / 256)).to_i * mod
170
+ }].inject(:+)
171
+ end
172
+ end
173
+ end
174
+
175
+ # J-_-L
data/lib/paint/pa.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'paint'
2
+
3
+ module Kernel
4
+ private
5
+
6
+ # A helper method similar to puts for printing a String on STDOUT
7
+ # Passes all arguments to Paint.[]
8
+ def pa(*args)
9
+ puts Paint[*args]
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Paint
2
+ # A list of color names, based on X11's rgb.txt
3
+ # Can be used with Paint.[] by passing a string containing the color name
4
+ # See Paint::Util::update_rgb_colors for generating
5
+ RGB_COLORS = {"snow"=>231, "ghost white"=>231, "GhostWhite"=>231, "white smoke"=>231, "WhiteSmoke"=>231, "gainsboro"=>231, "floral white"=>231, "FloralWhite"=>231, "old lace"=>231, "OldLace"=>231, "linen"=>231, "antique white"=>231, "AntiqueWhite"=>231, "papaya whip"=>230, "PapayaWhip"=>230, "blanched almond"=>230, "BlanchedAlmond"=>230, "bisque"=>230, "peach puff"=>230, "PeachPuff"=>230, "navajo white"=>230, "NavajoWhite"=>230, "moccasin"=>230, "cornsilk"=>231, "ivory"=>231, "lemon chiffon"=>230, "LemonChiffon"=>230, "seashell"=>231, "honeydew"=>231, "mint cream"=>231, "MintCream"=>231, "azure"=>231, "alice blue"=>231, "AliceBlue"=>231, "lavender"=>231, "lavender blush"=>231, "LavenderBlush"=>231, "misty rose"=>231, "MistyRose"=>231, "white"=>231, "black"=>16, "dark slate gray"=>59, "DarkSlateGray"=>59, "dark slate grey"=>59, "DarkSlateGrey"=>59, "dim gray"=>102, "DimGray"=>102, "dim grey"=>102, "DimGrey"=>102, "slate gray"=>109, "SlateGray"=>109, "slate grey"=>109, "SlateGrey"=>109, "light slate gray"=>109, "LightSlateGray"=>109, "light slate grey"=>109, "LightSlateGrey"=>109, "gray"=>188, "grey"=>188, "light grey"=>188, "LightGrey"=>188, "light gray"=>188, "LightGray"=>188, "midnight blue"=>18, "MidnightBlue"=>18, "navy"=>19, "navy blue"=>19, "NavyBlue"=>19, "cornflower blue"=>111, "CornflowerBlue"=>111, "dark slate blue"=>61, "DarkSlateBlue"=>61, "slate blue"=>104, "SlateBlue"=>104, "medium slate blue"=>105, "MediumSlateBlue"=>105, "light slate blue"=>141, "LightSlateBlue"=>141, "medium blue"=>20, "MediumBlue"=>20, "royal blue"=>69, "RoyalBlue"=>69, "blue"=>21, "dodger blue"=>39, "DodgerBlue"=>39, "deep sky blue"=>45, "DeepSkyBlue"=>45, "sky blue"=>153, "SkyBlue"=>153, "light sky blue"=>153, "LightSkyBlue"=>153, "steel blue"=>74, "SteelBlue"=>74, "light steel blue"=>189, "LightSteelBlue"=>189, "light blue"=>195, "LightBlue"=>195, "powder blue"=>195, "PowderBlue"=>195, "pale turquoise"=>195, "PaleTurquoise"=>195, "dark turquoise"=>44, "DarkTurquoise"=>44, "medium turquoise"=>80, "MediumTurquoise"=>80, "turquoise"=>86, "cyan"=>51, "light cyan"=>231, "LightCyan"=>231, "cadet blue"=>109, "CadetBlue"=>109, "medium aquamarine"=>115, "MediumAquamarine"=>115, "aquamarine"=>122, "dark green"=>28, "DarkGreen"=>28, "dark olive green"=>65, "DarkOliveGreen"=>65, "dark sea green"=>151, "DarkSeaGreen"=>151, "sea green"=>72, "SeaGreen"=>72, "medium sea green"=>78, "MediumSeaGreen"=>78, "light sea green"=>43, "LightSeaGreen"=>43, "pale green"=>157, "PaleGreen"=>157, "spring green"=>48, "SpringGreen"=>48, "lawn green"=>118, "LawnGreen"=>118, "green"=>46, "chartreuse"=>118, "medium spring green"=>49, "MediumSpringGreen"=>49, "green yellow"=>191, "GreenYellow"=>191, "lime green"=>77, "LimeGreen"=>77, "yellow green"=>149, "YellowGreen"=>149, "forest green"=>34, "ForestGreen"=>34, "olive drab"=>106, "OliveDrab"=>106, "dark khaki"=>186, "DarkKhaki"=>186, "khaki"=>229, "pale goldenrod"=>229, "PaleGoldenrod"=>229, "light goldenrod yellow"=>230, "LightGoldenrodYellow"=>230, "light yellow"=>231, "LightYellow"=>231, "yellow"=>226, "gold"=>226, "light goldenrod"=>229, "LightGoldenrod"=>229, "goldenrod"=>214, "dark goldenrod"=>178, "DarkGoldenrod"=>178, "rosy brown"=>181, "RosyBrown"=>181, "indian red"=>174, "IndianRed"=>174, "saddle brown"=>130, "SaddleBrown"=>130, "sienna"=>131, "peru"=>179, "burlywood"=>223, "beige"=>231, "wheat"=>230, "sandy brown"=>216, "SandyBrown"=>216, "tan"=>187, "chocolate"=>172, "firebrick"=>160, "brown"=>124, "dark salmon"=>216, "DarkSalmon"=>216, "salmon"=>216, "light salmon"=>216, "LightSalmon"=>216, "orange"=>214, "dark orange"=>214, "DarkOrange"=>214, "coral"=>209, "light coral"=>217, "LightCoral"=>217, "tomato"=>209, "orange red"=>202, "OrangeRed"=>202, "red"=>196, "hot pink"=>212, "HotPink"=>212, "deep pink"=>199, "DeepPink"=>199, "pink"=>224, "light pink"=>224, "LightPink"=>224, "pale violet red"=>211, "PaleVioletRed"=>211, "maroon"=>168, "medium violet red"=>163, "MediumVioletRed"=>163, "violet red"=>163, "VioletRed"=>163, "magenta"=>201, "violet"=>219, "plum"=>219, "orchid"=>213, "medium orchid"=>170, "MediumOrchid"=>170, "dark orchid"=>134, "DarkOrchid"=>134, "dark violet"=>128, "DarkViolet"=>128, "blue violet"=>135, "BlueViolet"=>135, "purple"=>129, "medium purple"=>141, "MediumPurple"=>141, "thistle"=>225, "snow1"=>231, "snow2"=>231, "snow3"=>188, "snow4"=>145, "seashell1"=>231, "seashell2"=>231, "seashell3"=>188, "seashell4"=>145, "AntiqueWhite1"=>231, "AntiqueWhite2"=>230, "AntiqueWhite3"=>188, "AntiqueWhite4"=>144, "bisque1"=>230, "bisque2"=>224, "bisque3"=>187, "bisque4"=>138, "PeachPuff1"=>230, "PeachPuff2"=>224, "PeachPuff3"=>187, "PeachPuff4"=>138, "NavajoWhite1"=>230, "NavajoWhite2"=>223, "NavajoWhite3"=>187, "NavajoWhite4"=>138, "LemonChiffon1"=>230, "LemonChiffon2"=>230, "LemonChiffon3"=>187, "LemonChiffon4"=>144, "cornsilk1"=>231, "cornsilk2"=>230, "cornsilk3"=>188, "cornsilk4"=>144, "ivory1"=>231, "ivory2"=>231, "ivory3"=>188, "ivory4"=>145, "honeydew1"=>231, "honeydew2"=>231, "honeydew3"=>188, "honeydew4"=>145, "LavenderBlush1"=>231, "LavenderBlush2"=>231, "LavenderBlush3"=>188, "LavenderBlush4"=>145, "MistyRose1"=>231, "MistyRose2"=>224, "MistyRose3"=>188, "MistyRose4"=>138, "azure1"=>231, "azure2"=>231, "azure3"=>188, "azure4"=>145, "SlateBlue1"=>141, "SlateBlue2"=>105, "SlateBlue3"=>104, "SlateBlue4"=>61, "RoyalBlue1"=>69, "RoyalBlue2"=>69, "RoyalBlue3"=>68, "RoyalBlue4"=>25, "blue1"=>21, "blue2"=>21, "blue3"=>20, "blue4"=>19, "DodgerBlue1"=>39, "DodgerBlue2"=>39, "DodgerBlue3"=>32, "DodgerBlue4"=>25, "SteelBlue1"=>117, "SteelBlue2"=>117, "SteelBlue3"=>74, "SteelBlue4"=>67, "DeepSkyBlue1"=>45, "DeepSkyBlue2"=>45, "DeepSkyBlue3"=>38, "DeepSkyBlue4"=>31, "SkyBlue1"=>153, "SkyBlue2"=>117, "SkyBlue3"=>110, "SkyBlue4"=>67, "LightSkyBlue1"=>195, "LightSkyBlue2"=>153, "LightSkyBlue3"=>152, "LightSkyBlue4"=>103, "SlateGray1"=>195, "SlateGray2"=>189, "SlateGray3"=>152, "SlateGray4"=>103, "LightSteelBlue1"=>195, "LightSteelBlue2"=>189, "LightSteelBlue3"=>152, "LightSteelBlue4"=>103, "LightBlue1"=>195, "LightBlue2"=>195, "LightBlue3"=>152, "LightBlue4"=>109, "LightCyan1"=>231, "LightCyan2"=>195, "LightCyan3"=>188, "LightCyan4"=>109, "PaleTurquoise1"=>195, "PaleTurquoise2"=>195, "PaleTurquoise3"=>152, "PaleTurquoise4"=>109, "CadetBlue1"=>159, "CadetBlue2"=>159, "CadetBlue3"=>116, "CadetBlue4"=>73, "turquoise1"=>51, "turquoise2"=>51, "turquoise3"=>44, "turquoise4"=>37, "cyan1"=>51, "cyan2"=>51, "cyan3"=>44, "cyan4"=>37, "DarkSlateGray1"=>159, "DarkSlateGray2"=>159, "DarkSlateGray3"=>116, "DarkSlateGray4"=>73, "aquamarine1"=>122, "aquamarine2"=>122, "aquamarine3"=>115, "aquamarine4"=>72, "DarkSeaGreen1"=>194, "DarkSeaGreen2"=>194, "DarkSeaGreen3"=>151, "DarkSeaGreen4"=>108, "SeaGreen1"=>85, "SeaGreen2"=>85, "SeaGreen3"=>79, "SeaGreen4"=>72, "PaleGreen1"=>157, "PaleGreen2"=>157, "PaleGreen3"=>114, "PaleGreen4"=>71, "SpringGreen1"=>48, "SpringGreen2"=>48, "SpringGreen3"=>42, "SpringGreen4"=>35, "green1"=>46, "green2"=>46, "green3"=>40, "green4"=>34, "chartreuse1"=>118, "chartreuse2"=>118, "chartreuse3"=>112, "chartreuse4"=>70, "OliveDrab1"=>191, "OliveDrab2"=>191, "OliveDrab3"=>149, "OliveDrab4"=>106, "DarkOliveGreen1"=>192, "DarkOliveGreen2"=>192, "DarkOliveGreen3"=>150, "DarkOliveGreen4"=>107, "khaki1"=>229, "khaki2"=>229, "khaki3"=>186, "khaki4"=>143, "LightGoldenrod1"=>229, "LightGoldenrod2"=>229, "LightGoldenrod3"=>186, "LightGoldenrod4"=>143, "LightYellow1"=>231, "LightYellow2"=>230, "LightYellow3"=>188, "LightYellow4"=>144, "yellow1"=>226, "yellow2"=>226, "yellow3"=>184, "yellow4"=>142, "gold1"=>226, "gold2"=>220, "gold3"=>184, "gold4"=>136, "goldenrod1"=>220, "goldenrod2"=>220, "goldenrod3"=>178, "goldenrod4"=>136, "DarkGoldenrod1"=>220, "DarkGoldenrod2"=>220, "DarkGoldenrod3"=>178, "DarkGoldenrod4"=>136, "RosyBrown1"=>224, "RosyBrown2"=>224, "RosyBrown3"=>181, "RosyBrown4"=>138, "IndianRed1"=>210, "IndianRed2"=>210, "IndianRed3"=>167, "IndianRed4"=>131, "sienna1"=>215, "sienna2"=>209, "sienna3"=>173, "sienna4"=>130, "burlywood1"=>223, "burlywood2"=>223, "burlywood3"=>180, "burlywood4"=>137, "wheat1"=>230, "wheat2"=>230, "wheat3"=>187, "wheat4"=>138, "tan1"=>215, "tan2"=>215, "tan3"=>179, "tan4"=>137, "chocolate1"=>208, "chocolate2"=>208, "chocolate3"=>172, "chocolate4"=>130, "firebrick1"=>203, "firebrick2"=>203, "firebrick3"=>160, "firebrick4"=>124, "brown1"=>203, "brown2"=>203, "brown3"=>167, "brown4"=>124, "salmon1"=>216, "salmon2"=>216, "salmon3"=>173, "salmon4"=>131, "LightSalmon1"=>216, "LightSalmon2"=>216, "LightSalmon3"=>180, "LightSalmon4"=>137, "orange1"=>214, "orange2"=>214, "orange3"=>178, "orange4"=>136, "DarkOrange1"=>208, "DarkOrange2"=>208, "DarkOrange3"=>172, "DarkOrange4"=>130, "coral1"=>210, "coral2"=>209, "coral3"=>173, "coral4"=>131, "tomato1"=>209, "tomato2"=>209, "tomato3"=>167, "tomato4"=>130, "OrangeRed1"=>202, "OrangeRed2"=>202, "OrangeRed3"=>166, "OrangeRed4"=>124, "red1"=>196, "red2"=>196, "red3"=>160, "red4"=>124, "DebianRed"=>197, "DeepPink1"=>199, "DeepPink2"=>199, "DeepPink3"=>162, "DeepPink4"=>125, "HotPink1"=>212, "HotPink2"=>211, "HotPink3"=>175, "HotPink4"=>132, "pink1"=>224, "pink2"=>218, "pink3"=>181, "pink4"=>138, "LightPink1"=>224, "LightPink2"=>218, "LightPink3"=>181, "LightPink4"=>138, "PaleVioletRed1"=>218, "PaleVioletRed2"=>211, "PaleVioletRed3"=>175, "PaleVioletRed4"=>132, "maroon1"=>206, "maroon2"=>205, "maroon3"=>163, "maroon4"=>126, "VioletRed1"=>205, "VioletRed2"=>205, "VioletRed3"=>168, "VioletRed4"=>125, "magenta1"=>201, "magenta2"=>201, "magenta3"=>164, "magenta4"=>127, "orchid1"=>219, "orchid2"=>213, "orchid3"=>176, "orchid4"=>133, "plum1"=>225, "plum2"=>225, "plum3"=>182, "plum4"=>139, "MediumOrchid1"=>213, "MediumOrchid2"=>177, "MediumOrchid3"=>170, "MediumOrchid4"=>97, "DarkOrchid1"=>171, "DarkOrchid2"=>171, "DarkOrchid3"=>134, "DarkOrchid4"=>91, "purple1"=>135, "purple2"=>135, "purple3"=>92, "purple4"=>55, "MediumPurple1"=>183, "MediumPurple2"=>141, "MediumPurple3"=>140, "MediumPurple4"=>97, "thistle1"=>231, "thistle2"=>225, "thistle3"=>188, "thistle4"=>139, "gray0"=>16, "grey0"=>16, "gray1"=>16, "grey1"=>16, "gray2"=>16, "grey2"=>16, "gray3"=>16, "grey3"=>16, "gray4"=>16, "grey4"=>16, "gray5"=>16, "grey5"=>16, "gray6"=>16, "grey6"=>16, "gray7"=>16, "grey7"=>16, "gray8"=>16, "grey8"=>16, "gray9"=>16, "grey9"=>16, "gray10"=>16, "grey10"=>16, "gray11"=>16, "grey11"=>16, "gray12"=>16, "grey12"=>16, "gray13"=>16, "grey13"=>16, "gray14"=>16, "grey14"=>16, "gray15"=>16, "grey15"=>16, "gray16"=>16, "grey16"=>16, "gray17"=>59, "grey17"=>59, "gray18"=>59, "grey18"=>59, "gray19"=>59, "grey19"=>59, "gray20"=>59, "grey20"=>59, "gray21"=>59, "grey21"=>59, "gray22"=>59, "grey22"=>59, "gray23"=>59, "grey23"=>59, "gray24"=>59, "grey24"=>59, "gray25"=>59, "grey25"=>59, "gray26"=>59, "grey26"=>59, "gray27"=>59, "grey27"=>59, "gray28"=>59, "grey28"=>59, "gray29"=>59, "grey29"=>59, "gray30"=>59, "grey30"=>59, "gray31"=>59, "grey31"=>59, "gray32"=>59, "grey32"=>59, "gray33"=>59, "grey33"=>59, "gray34"=>102, "grey34"=>102, "gray35"=>102, "grey35"=>102, "gray36"=>102, "grey36"=>102, "gray37"=>102, "grey37"=>102, "gray38"=>102, "grey38"=>102, "gray39"=>102, "grey39"=>102, "gray40"=>102, "grey40"=>102, "gray41"=>102, "grey41"=>102, "gray42"=>102, "grey42"=>102, "gray43"=>102, "grey43"=>102, "gray44"=>102, "grey44"=>102, "gray45"=>102, "grey45"=>102, "gray46"=>102, "grey46"=>102, "gray47"=>102, "grey47"=>102, "gray48"=>102, "grey48"=>102, "gray49"=>102, "grey49"=>102, "gray50"=>102, "grey50"=>102, "gray51"=>145, "grey51"=>145, "gray52"=>145, "grey52"=>145, "gray53"=>145, "grey53"=>145, "gray54"=>145, "grey54"=>145, "gray55"=>145, "grey55"=>145, "gray56"=>145, "grey56"=>145, "gray57"=>145, "grey57"=>145, "gray58"=>145, "grey58"=>145, "gray59"=>145, "grey59"=>145, "gray60"=>145, "grey60"=>145, "gray61"=>145, "grey61"=>145, "gray62"=>145, "grey62"=>145, "gray63"=>145, "grey63"=>145, "gray64"=>145, "grey64"=>145, "gray65"=>145, "grey65"=>145, "gray66"=>145, "grey66"=>145, "gray67"=>188, "grey67"=>188, "gray68"=>188, "grey68"=>188, "gray69"=>188, "grey69"=>188, "gray70"=>188, "grey70"=>188, "gray71"=>188, "grey71"=>188, "gray72"=>188, "grey72"=>188, "gray73"=>188, "grey73"=>188, "gray74"=>188, "grey74"=>188, "gray75"=>188, "grey75"=>188, "gray76"=>188, "grey76"=>188, "gray77"=>188, "grey77"=>188, "gray78"=>188, "grey78"=>188, "gray79"=>188, "grey79"=>188, "gray80"=>188, "grey80"=>188, "gray81"=>188, "grey81"=>188, "gray82"=>188, "grey82"=>188, "gray83"=>188, "grey83"=>188, "gray84"=>231, "grey84"=>231, "gray85"=>231, "grey85"=>231, "gray86"=>231, "grey86"=>231, "gray87"=>231, "grey87"=>231, "gray88"=>231, "grey88"=>231, "gray89"=>231, "grey89"=>231, "gray90"=>231, "grey90"=>231, "gray91"=>231, "grey91"=>231, "gray92"=>231, "grey92"=>231, "gray93"=>231, "grey93"=>231, "gray94"=>231, "grey94"=>231, "gray95"=>231, "grey95"=>231, "gray96"=>231, "grey96"=>231, "gray97"=>231, "grey97"=>231, "gray98"=>231, "grey98"=>231, "gray99"=>231, "grey99"=>231, "gray100"=>231, "grey100"=>231, "dark grey"=>145, "DarkGrey"=>145, "dark gray"=>145, "DarkGray"=>145, "dark blue"=>19, "DarkBlue"=>19, "dark cyan"=>37, "DarkCyan"=>37, "dark magenta"=>127, "DarkMagenta"=>127, "dark red"=>124, "DarkRed"=>124, "light green"=>157, "LightGreen"=>157}
6
+ end
7
+
8
+ # J-_-L
@@ -0,0 +1,82 @@
1
+ module Paint
2
+ # Hash for defining color/effect shortcuts
3
+ # See README for details
4
+ SHORTCUTS = {
5
+ # :example => { # would create a Paint::Example constant...
6
+ # :light_red => "\e[31;1m", # with a method .light_red
7
+ # }
8
+ }
9
+ SHORTCUTS.default = {}
10
+
11
+ class << self
12
+ # Paint::SomeModule --> Paint::SHORTCUTS[:some_module]
13
+ def const_missing(mod_name)
14
+ # get shortcuts
15
+ shortcuts = SHORTCUTS[mod_name.to_s.gsub(/[A-Z]/,'_\0').downcase[1..-1].to_sym] || []
16
+
17
+ # create module
18
+ class_eval "module #{mod_name}; end"
19
+ mod = const_get(mod_name)
20
+ eigen_mod = class << mod; self; end # 1.8
21
+
22
+ # define direct behaviour, class methods
23
+ # mod.define_singleton_method :method_missing do |color_name, *args|
24
+ eigen_mod.send:define_method, :method_missing do |color_name, *args|
25
+ if color_code = shortcuts[color_name]
26
+ if args.empty? then color_code else color_code + Array(args).join + NOTHING end
27
+ else
28
+ nil
29
+ end
30
+ end
31
+
32
+ eigen_mod.send:define_method, :respond_to? do |color_name, *args|
33
+ shortcuts.include?(color_name) || super(color_name, *args)
34
+ end
35
+
36
+ # define include behaviour, instance methods
37
+ eigen_mod.send:define_method, :included do |_|
38
+ shortcuts.each{ |color_name, color_code|
39
+ define_method color_name do |*args|
40
+ if args.empty? then color_code else color_code + Array(args).join + NOTHING end
41
+ end
42
+ }
43
+ private *shortcuts.keys unless shortcuts.empty?
44
+ end
45
+
46
+ # include variations, defined in child modules
47
+ mod.class_eval "module String; end"
48
+ string = mod.const_get(:String)
49
+ eigen_string = class << string; self; end # 1.8
50
+ eigen_string.send:define_method, :included do |_|
51
+ shortcuts.each{ |color_name, color_code|
52
+ define_method color_name do
53
+ color_code + to_s + NOTHING
54
+ end
55
+ }
56
+ end
57
+
58
+ # OK, let's take it one level further ;)
59
+ mod.class_eval "module Prefix; end"
60
+ prefix_prefix = mod.const_get(:Prefix)
61
+ eigen_prefix_prefix = class << prefix_prefix; self; end # 1.8
62
+ eigen_prefix_prefix.send:define_method, :const_missing do |prefix_name|
63
+ class_eval "module #{prefix_name}; end"
64
+ prefix = const_get(prefix_name)
65
+ eigen_prefix = class << prefix; self; end # 1.8
66
+
67
+ eigen_prefix.send:define_method, :included do |_|
68
+ define_method prefix_name.to_s.gsub(/[A-Z]/,'_\0').downcase[1..-1].to_sym do |color_name|
69
+ shortcuts[color_name] && shortcuts[color_name] + to_s + NOTHING
70
+ end
71
+ end
72
+
73
+ prefix
74
+ end
75
+
76
+ # :)
77
+ mod
78
+ end
79
+ end
80
+ end
81
+
82
+ # J-_-L
data/lib/paint/util.rb ADDED
@@ -0,0 +1,35 @@
1
+ module Paint
2
+ # These helpers add functionality you sometimes need when working with terminal colors
3
+ class << self
4
+ # Removes any color and effect strings
5
+ def unpaint(string)
6
+ string.gsub(/\e\[(?:[0-9];?)+m/, '')
7
+ end
8
+
9
+ # Tries to print all 256 colors
10
+ def rainbow
11
+ (0..256).each{ |color|
12
+ print Paint[' ',48,5,color] # print empty bg color field
13
+ }
14
+ puts
15
+ end
16
+
17
+ # Updates color names
18
+ def update_rgb_colors(path = '/etc/X11/rgb.txt')
19
+ if File.file?(path)
20
+ Paint::RGB_COLORS.clear
21
+
22
+ File.open(path, 'r') do |file|
23
+ file.each_line{ |line|
24
+ line.chomp =~ /(\d+)\s+(\d+)\s+(\d+)\s+(.*)$/
25
+ Paint::RGB_COLORS[$4] = Paint.send:rgb_value, $1, $2, $3 if $4
26
+ }
27
+ end
28
+ else
29
+ raise ArgumentError, "Not a valid file: #{path}"
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ # J-_-L
@@ -0,0 +1,3 @@
1
+ module Paint
2
+ VERSION = '0.8.0'
3
+ end
data/paint.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'rubygems' unless defined? Gem
3
+ require File.dirname(__FILE__) + "/lib/paint/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "paint"
7
+ s.version = Paint::VERSION
8
+ s.authors = ["Jan Lelis"]
9
+ s.email = "mail@janlelis.de"
10
+ s.homepage = "https://github.com/janlelis/paint"
11
+ s.summary = "Yet another terminal colors gem"
12
+ s.description = "Yet another terminal colors gem / no string extensions / 256 color support / effect support / define custom shortcuts"
13
+ s.required_ruby_version = '>= 1.8.7'
14
+ s.files = Dir.glob(%w[{lib,test,spec}/**/*.rb bin/* [A-Z]*.{txt,rdoc} ext/**/*.{rb,c}]) + %w{Rakefile paint.gemspec .gemtest}
15
+ s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
16
+ s.license = 'MIT'
17
+ s.add_development_dependency 'rspec'
18
+ s.add_development_dependency 'rspec-core'
19
+ s.add_development_dependency 'rake'
20
+
21
+ len = s.homepage.size
22
+ s.post_install_message = \
23
+ (" ┌── " + "info ".ljust(len-2,'%') + "─┐\n" +
24
+ " J-_-L │ " + s.homepage + " │\n" +
25
+ " ├── " + "usage ".ljust(len-2,'%') + "─┤\n" +
26
+ " │ " + "require 'paint'".ljust(len,' ') + " │\n" +
27
+ " │ " + "puts Paint['J-_-L', :red] # \e[31mJ-_-L\e[0m".ljust(len,' ') + " │\n" +
28
+ " └─" + '─'*len + "─┘").gsub('%', '─') # 1.8 workaround
29
+ end
@@ -0,0 +1,77 @@
1
+ describe 'Paint.color' do
2
+ it 'only returns a the color escape sequnce and is directly used by Paint.[] with all paramenters except the first; see there fore specs' do end
3
+ end
4
+
5
+ describe 'Paint.simple' do
6
+ it 'returns ansi code number for one of the eight ansi base colors' do
7
+ Paint.simple(:red).should == 31
8
+ end
9
+
10
+ it 'returns background ansi code number for one of the eight ansi base colors if second parameter is true' do
11
+ Paint.simple(:red, true).should == 41
12
+ end
13
+ end
14
+
15
+ describe 'Paint.rgb' do
16
+ it 'returns ansi code sequence for one of 256 colors' do
17
+ Paint.rgb(1,2,3).should == '38;5;16'
18
+ end
19
+
20
+ it 'returns background ansi code sequence for one of 256 colors if last parameter is true' do
21
+ Paint.rgb(1, 2, 3, true).should == '48;5;16'
22
+ end
23
+ end
24
+
25
+ describe 'Paint.hex' do
26
+ it 'returns ansi code sequence for one of 256 colors' do
27
+ Paint.hex("#fff").should == "38;5;231"
28
+ end
29
+
30
+ it 'returns background ansi code sequence for one of 256 colors if second parameter is true' do
31
+ Paint.hex("123456", true).should == "48;5;24"
32
+ end
33
+ end
34
+
35
+ describe 'Paint.name' do
36
+ it 'returns ansi code sequence for one of 256 colors' do
37
+ Paint.name("gold").should == "38;5;226"
38
+ end
39
+
40
+ it 'returns background ansi code sequence for one of 256 colors if second parameter is true' do
41
+ Paint.name("gold", true).should == "48;5;226"
42
+ end
43
+ end
44
+
45
+ describe 'Paint.random' do
46
+ it 'returns ansi code for one of the eight ansi base colors' do
47
+ (30...38) === Paint.random.should
48
+ end
49
+ end
50
+
51
+ describe 'Paint.effect' do
52
+ it 'returns ansi code for effect using EFFECTS hash' do
53
+ Paint.effect(:bright).should == 1
54
+ end
55
+ end
56
+
57
+ describe 'Paint.wrap' do
58
+ it 'wraps an ansi color code (array of integers) into an ansi escape sequence' do
59
+ Paint.wrap(31, 1).should == "\e[31;1m"
60
+ end
61
+ end
62
+
63
+ # util.rb
64
+
65
+ describe 'Paint.unpaint' do
66
+ it 'removes any ansi color escape sequences in the string' do
67
+ Paint.unpaint( Paint['J-_-L', :red, :bright] ).should == 'J-_-L'
68
+ end
69
+ end
70
+
71
+ describe 'Paint.rainbow' do
72
+ it 'prints all available 256 colors' do end
73
+ end
74
+
75
+ describe 'Paint.update_rgb_colors' do
76
+ it 'updates the Paint::RGB_COLORS hash using rgb.txt (takes path to it as argument' do end
77
+ end
@@ -0,0 +1,55 @@
1
+ # TODO fix include specs, probably failing because of some rspec voodoo
2
+
3
+ describe 'Paint::SHORTCUTS' do
4
+ before do
5
+ Paint::SHORTCUTS[:example] = {
6
+ :white => Paint.color(:black),
7
+ :red => Paint.color(:red, :bright),
8
+ :title => Paint.color(:underline),
9
+ }
10
+ end
11
+
12
+ context 'Paint::Example.method_missing' do
13
+ it 'returns a color defined in the SHORTCUTS hash under the :example key' do
14
+ Paint::Example.red == "\e[31m"
15
+ end
16
+
17
+ it 'returns a color defined in the SHORTCUTS hash under the :some_module key; method takes string to colorize' do
18
+ Paint::Example.red 'J-_-L' == "\e[31;1mJ-_-L\e[0m"
19
+ end
20
+ end
21
+
22
+ context 'include Paint::Example' do
23
+ include Paint::Example
24
+ #red.should == "\e[31;1m"
25
+ #white( 'Ruby' ).should "\e[30m"
26
+ end
27
+
28
+
29
+ context 'include Paint::Example::String' do
30
+ it 'adds shortcuts methods that colorize self' do
31
+ class MyString < String # could also have used original String
32
+ include Paint::Example::String
33
+ end
34
+
35
+ # MyString.new("J-_-L").red.should == "\e[31;1mJ-_-L\e[0m"
36
+ end
37
+
38
+ it 'adds shortcuts methods that colorize self (also works for non-String classes by calling to_s)' do
39
+ Paint::SHORTCUTS[:example][:gold] = Paint.color "gold"
40
+ class Integer
41
+ include Paint::Example::String
42
+ end
43
+ #123.red.should == "\e[38;5;226m123\e[0m"
44
+ end
45
+ end
46
+
47
+ context 'include Paint::Example::Prefix::ExampleName' do
48
+ it 'sets a single color helper method to avoid cluttering namespaces' do
49
+ class Object
50
+ include Paint::Example::Prefix::ExampleName
51
+ end
52
+ #"Ruby".example_name(:red).should == "\e[31;1mRuby\e[0m"
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,79 @@
1
+ describe 'Paint.[]' do
2
+ context '(with no options)' do
3
+ it 'colorizes using a random ansi foreground color' do
4
+ Paint['J-_-L'].should =~ /\e\[3\dmJ-_-L\e\[0m/
5
+ end
6
+ end
7
+
8
+ context '(with one color)' do
9
+ it 'understands a simple symbol color and use it as foreground color' do
10
+ Paint['J-_-L', :yellow].should == "\e[33mJ-_-L\e[0m"
11
+ end
12
+
13
+ it 'understands an array as rgb color definition and use it as foreground color' do
14
+ Paint['J-_-L', [255, 200, 0]].should == "\e[38;5;220mJ-_-L\e[0m"
15
+ end
16
+
17
+ it 'understands a hex string (with #, 6 digits) as rgb color definition and use it as foreground color' do
18
+ Paint['J-_-L', "#123456"].should == "\e[38;5;24mJ-_-L\e[0m"
19
+ end
20
+
21
+ it 'understands a hex string (no #, 6 digits) as rgb color definition and use it as foreground color' do
22
+ Paint['J-_-L', "123456"].should == "\e[38;5;24mJ-_-L\e[0m"
23
+ end
24
+
25
+ it 'understands a hex string (with #, 3 digits) as rgb color definition and use it as foreground color' do
26
+ Paint['J-_-L', "#fff"].should == "\e[38;5;231mJ-_-L\e[0m"
27
+ end
28
+
29
+ it 'understands a hex string (no #, 3 digits) as rgb color definition and use it as foreground color' do
30
+ Paint['J-_-L', "fff"].should == "\e[38;5;231mJ-_-L\e[0m"
31
+ end
32
+
33
+ it 'understands a non-hex string as rgb color name (rgb.txt) and use it as foreground color' do
34
+ Paint['J-_-L', "medium purple"].should == "\e[38;5;141mJ-_-L\e[0m"
35
+ end
36
+ end
37
+
38
+ context '(with two colors)' do
39
+ it 'interprets the first color as foreground color and the second one as background color' do
40
+ Paint['J-_-L', :yellow, :red].should == "\e[33;41mJ-_-L\e[0m"
41
+ end
42
+
43
+ it 'interprets the first color as foreground color and the second one as background color (rgb)' do
44
+ Paint['J-_-L', '#424242', [42, 142, 242]].should == "\e[38;5;59;48;5;39mJ-_-L\e[0m"
45
+ end
46
+
47
+ it 'sets only a background color, if first color is nil' do
48
+ Paint['J-_-L', nil, [42, 142, 242]].should == "\e[48;5;39mJ-_-L\e[0m"
49
+ end
50
+ end
51
+
52
+ context '(with effects)' do
53
+ it 'passes effects' do
54
+ Paint['J-_-L', :bright].should == "\e[1mJ-_-L\e[0m"
55
+ end
56
+
57
+ it 'passes effects, mixed with colors' do
58
+ Paint['J-_-L', :yellow, :bright].should == "\e[33;1mJ-_-L\e[0m"
59
+ end
60
+
61
+ it 'passes effects, mixed with colors, order does not matter' do
62
+ Paint['J-_-L', :bright, :yellow].should == "\e[1;33mJ-_-L\e[0m"
63
+ end
64
+
65
+ it 'passes multiple effects' do
66
+ Paint['J-_-L', :yellow, :red, :bright, :underline, :inverse].should == "\e[33;41;1;4;7mJ-_-L\e[0m"
67
+ end
68
+ end
69
+
70
+ context '(with plain integers)' do
71
+ it 'passes integers to final escape sequence' do
72
+ Paint['J-_-L', 31, 1, 42].should == "\e[31;1;42mJ-_-L\e[0m"
73
+ end
74
+
75
+ it 'passes integers to final escape sequence (mixed with normal arguments)' do
76
+ Paint['J-_-L', :red, :bright, 42, :underline].should == "\e[31;1;42;4mJ-_-L\e[0m"
77
+ end
78
+ end
79
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/../lib/paint'
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paint
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.8.0
6
+ platform: ruby
7
+ authors:
8
+ - Jan Lelis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-29 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec-core
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: rake
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id003
48
+ description: Yet another terminal colors gem / no string extensions / 256 color support / effect support / define custom shortcuts
49
+ email: mail@janlelis.de
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - README.rdoc
56
+ - LICENSE.txt
57
+ files:
58
+ - lib/paint/rgb_colors.rb
59
+ - lib/paint/util.rb
60
+ - lib/paint/shortcuts.rb
61
+ - lib/paint/pa.rb
62
+ - lib/paint/version.rb
63
+ - lib/paint.rb
64
+ - spec/spec_helper.rb
65
+ - spec/paint_methods_spec.rb
66
+ - spec/paint_spec.rb
67
+ - spec/paint_shortcuts_spec.rb
68
+ - LICENSE.txt
69
+ - README.rdoc
70
+ - CHANGELOG.rdoc
71
+ - Rakefile
72
+ - paint.gemspec
73
+ - .gemtest
74
+ homepage: https://github.com/janlelis/paint
75
+ licenses:
76
+ - MIT
77
+ post_install_message: " \xE2\x94\x8C\xE2\x94\x80\xE2\x94\x80 info \xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x90\n J-_-L \xE2\x94\x82 https://github.com/janlelis/paint \xE2\x94\x82\n \xE2\x94\x9C\xE2\x94\x80\xE2\x94\x80 usage \xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\xA4\n \xE2\x94\x82 require 'paint' \xE2\x94\x82\n \xE2\x94\x82 puts Paint['J-_-L', :red] # \e[31mJ-_-L\e[0m \xE2\x94\x82\n \xE2\x94\x94\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x80\xE2\x94\x98"
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 1.8.7
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.1
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Yet another terminal colors gem
101
+ test_files: []
102
+