mikowitz-color 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +9 -0
- data/LICENSE +20 -0
- data/README.rdoc +32 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/lib/color.rb +66 -0
- data/spec/color_spec.rb +100 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/term_test.rb +8 -0
- metadata +86 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Michael Berkowitz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
= color
|
2
|
+
|
3
|
+
=== current version : 0.1.1
|
4
|
+
|
5
|
+
http://github.com/mikowitz/color
|
6
|
+
|
7
|
+
== Description
|
8
|
+
|
9
|
+
A simple, flexible gem that provides an open-ended API to print colored and styled output in a terminal.
|
10
|
+
|
11
|
+
== Usage examples
|
12
|
+
|
13
|
+
The following are all methods that <code>Color</code> understands, and should give you an idea of what is possible.
|
14
|
+
|
15
|
+
Color.blue("this will be printed as blue text")
|
16
|
+
Color.on_red("this will be printed on a red background")
|
17
|
+
Color.bold("this will be bold")
|
18
|
+
Color.underline_blue_on_yellow("this will be underlined blue text on a yellow background")
|
19
|
+
|
20
|
+
The order of elements in the method name is also unimportant. For example
|
21
|
+
|
22
|
+
Color.bold_underline_blue_on_yellow("This should be formatted the same as the line below")
|
23
|
+
|
24
|
+
will look the same as
|
25
|
+
|
26
|
+
Color.on_yellow_bold_blue_underline("This should be formatted the same as the line above")
|
27
|
+
|
28
|
+
Check out <code>"spec/term_test.rb"</code> and <code>`rake proof`</code> if you don't believe me.
|
29
|
+
|
30
|
+
== Copyright
|
31
|
+
|
32
|
+
Copyright (c) 2009 Michael Berkowitz. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "mikowitz-color"
|
8
|
+
gem.summary = %Q{A small gem to provide colored/styled output in the terminal.}
|
9
|
+
gem.description = %Q{A simple, flexible gem that provides an open-ended API to print colored and styled output in a terminal.}
|
10
|
+
gem.email = "michael.berkowitz@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/mikowitz/color"
|
12
|
+
gem.authors = ["Michael Berkowitz"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
gem.add_development_dependency "yard"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
spec.spec_opts = ["-cfs"]
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
+
spec.libs << 'lib' << 'spec'
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "'Proof is the bottom line for everyone' -- Paul Simon"
|
36
|
+
task :proof do
|
37
|
+
system "ruby spec/term_test.rb"
|
38
|
+
end
|
39
|
+
|
40
|
+
task :spec => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :spec
|
43
|
+
|
44
|
+
begin
|
45
|
+
require 'yard'
|
46
|
+
YARD::Rake::YardocTask.new
|
47
|
+
rescue LoadError
|
48
|
+
task :yardoc do
|
49
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
50
|
+
end
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.2
|
data/lib/color.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
module Color
|
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
|
+
COLORS.default = 0
|
13
|
+
|
14
|
+
FORMATS = {
|
15
|
+
"bold" => 1,
|
16
|
+
"underline" => 4
|
17
|
+
}
|
18
|
+
FORMATS.default = 0
|
19
|
+
|
20
|
+
def self.io; $stderr; end
|
21
|
+
|
22
|
+
def self.method_missing(method, string, newline=true)
|
23
|
+
io.write prepare_string(string, *parse_method_name(method))
|
24
|
+
io.flush
|
25
|
+
puts if newline
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.prepare_string(string, foreground=nil, background=nil, formatting=[])
|
29
|
+
[ prepare_foreground_color(foreground),
|
30
|
+
prepare_background_color(background),
|
31
|
+
prepare_formatting(*formatting),
|
32
|
+
string,
|
33
|
+
"\e[0m"
|
34
|
+
].join("")
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.parse_method_name(name)
|
38
|
+
name = name.to_s.split("_")
|
39
|
+
if x = name.index("on")
|
40
|
+
name.delete("on")
|
41
|
+
background = name.delete_at(x)
|
42
|
+
end
|
43
|
+
foreground = name.find{|b| COLORS.keys.include?(b) }
|
44
|
+
formatting = name.reject{|b| COLORS.keys.include?(b) }
|
45
|
+
[foreground, background, formatting]
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def self.prepare_foreground_color(color=nil)
|
50
|
+
handle_color(3, color)
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.prepare_background_color(color=nil)
|
54
|
+
handle_color(4, color)
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.prepare_formatting(*formats)
|
58
|
+
return "" if formats.empty?
|
59
|
+
formats.map{|f| "\e[#{FORMATS[f]}m"}.join("")
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.handle_color(lead, color=nil)
|
63
|
+
return "" unless color
|
64
|
+
"\e[#{lead}#{COLORS[color]}m"
|
65
|
+
end
|
66
|
+
end
|
data/spec/color_spec.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Color" do
|
4
|
+
describe "parse_method_name" do
|
5
|
+
it "should correctly parse a single color" do
|
6
|
+
Color.parse_method_name(:cyan).should == ["cyan", nil, []]
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should correctly parse a single background color" do
|
10
|
+
Color.parse_method_name(:on_red).should == [nil, "red", []]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should correctly parse a single formatting option" do
|
14
|
+
Color.parse_method_name(:bold).should == [nil, nil, ["bold"]]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should correctly parse a foreground color and background color" do
|
18
|
+
Color.parse_method_name(:blue_on_green).should == ["blue", "green", []]
|
19
|
+
Color.parse_method_name(:on_magenta_yellow).should == ["yellow", "magenta", []]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should correctly parse a foreground color and single formatting option" do
|
23
|
+
Color.parse_method_name(:red_bold).should == ["red", nil, ["bold"]]
|
24
|
+
Color.parse_method_name(:underline_green).should == ["green", nil, ["underline"]]
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should correctly parse a background color and two formatting options" do
|
28
|
+
Color.parse_method_name(:on_red_bold_underline).should == [nil, "red", ["bold", "underline"]]
|
29
|
+
Color.parse_method_name(:underline_on_red_bold).should == [nil, "red", ["underline", "bold"]]
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should correctly parse a foreground color, background color and formatting option" do
|
33
|
+
Color.parse_method_name(:blue_on_cyan_bold).should == ["blue", "cyan", ["bold"]]
|
34
|
+
Color.parse_method_name(:underline_red_on_green).should == ["red", "green", ["underline"]]
|
35
|
+
Color.parse_method_name(:on_magenta_bold_black).should == ["black", "magenta", ["bold"]]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "prepare_foreground_color" do
|
40
|
+
it "should correctly convert 'blue' into terminal output" do
|
41
|
+
Color.send(:prepare_foreground_color, "blue").should == "\e[34m"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should correctly handle a nil value" do
|
45
|
+
Color.send(:prepare_foreground_color, nil).should == ""
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should correctly handle a non-existent color" do
|
49
|
+
Color.send(:prepare_foreground_color, "turquoise").should == "\e[30m"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "prepare_background_color" do
|
54
|
+
it "should correctly convert 'magenta' into terminal output" do
|
55
|
+
Color.send(:prepare_background_color, "magenta").should == "\e[45m"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should correctly handle a nil value" do
|
59
|
+
Color.send(:prepare_background_color, nil).should == ""
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should correctly handle a non-existent color" do
|
63
|
+
Color.send(:prepare_background_color, "pink").should == "\e[40m"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "prepare_formatting" do
|
68
|
+
it "should correctly convert ['bold'] into terminal output" do
|
69
|
+
Color.send(:prepare_formatting, "bold").should == "\e[1m"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should correctly convert ['bold', 'underline'] into terminal output" do
|
73
|
+
Color.send(:prepare_formatting, "bold", "underline").should == "\e[1m\e[4m"
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should correctly convert [] into terminal output" do
|
77
|
+
Color.send(:prepare_formatting, *[]).should == ""
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should correctly handle ['fake_formatting_option']" do
|
81
|
+
Color.send(:prepare_formatting, "fake_formatting_option").should == "\e[0m"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "prepare_string" do
|
86
|
+
it "should return the correct value for the parameter set ('hello', 'blue', nil, [])" do
|
87
|
+
Color.prepare_string("hello", "blue", nil, []).should == "\e[34mhello\e[0m"
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should return the correct value for the parameter set ('hello', nil, 'red, [])" do
|
91
|
+
Color.prepare_string("hello", nil, "red", []).should == "\e[41mhello\e[0m"
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should return the correct value for the parameter set ('hello', 'green', 'red, ['bold])" do
|
95
|
+
Color.prepare_string("hello", "green", "red", ["bold"]).should == "\e[32m\e[41m\e[1mhello\e[0m"
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/term_test.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
## EXPERIMENTATION
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
4
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
+
require 'color'
|
6
|
+
|
7
|
+
Color.bold_underline_blue_on_yellow("This should be formatted the same as the line below")
|
8
|
+
Color.on_yellow_bold_blue_underline("This should be formatted the same as the line above")
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mikowitz-color
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Berkowitz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-08 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: yard
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: A simple, flexible gem that provides an open-ended API to print colored and styled output in a terminal.
|
36
|
+
email: michael.berkowitz@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/color.rb
|
52
|
+
- spec/color_spec.rb
|
53
|
+
- spec/spec_helper.rb
|
54
|
+
- spec/term_test.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/mikowitz/color
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.3.5
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: A small gem to provide colored/styled output in the terminal.
|
83
|
+
test_files:
|
84
|
+
- spec/color_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- spec/term_test.rb
|