colorful 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ -f Fuubar
2
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm gemset use 1.9.2@colorful --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in colorful.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/colorful.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "colorful/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "colorful"
7
+ s.version = Colorful::VERSION
8
+ s.authors = ["Nick Karpenske"]
9
+ s.email = ["randland@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Terminal Color Gem}
12
+ s.description = %q{Provides string extensions for terminal color output}
13
+
14
+ s.rubyforge_project = "colorful"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ s.add_development_dependency "fuubar"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,3 @@
1
+ module Colorful
2
+ VERSION = "0.0.1"
3
+ end
data/lib/colorful.rb ADDED
@@ -0,0 +1,102 @@
1
+ require "colorful/version"
2
+
3
+ module Colorful
4
+ COLORS = { :black => 0,
5
+ :red => 1,
6
+ :green => 2,
7
+ :yellow => 3,
8
+ :blue => 4,
9
+ :magenta => 5,
10
+ :cyan => 6,
11
+ :white => 7,
12
+ :default => 9 }
13
+
14
+ EFFECTS = { :reset => 0,
15
+ :bright => 1,
16
+ :bold => 1,
17
+ :italic => 3,
18
+ :underline => 4,
19
+ :blink => 5,
20
+ :inverse => 7,
21
+ :hide => 8 }
22
+ end
23
+
24
+ class String
25
+ Colorful::COLORS.each do |color, code|
26
+ define_method color do
27
+ inject_ansi_code 30 + code
28
+ end
29
+
30
+ define_method "#{color}_background" do
31
+ inject_ansi_code 40 + code
32
+ end
33
+
34
+ Colorful::COLORS.each do |bg_color, bg_code|
35
+ define_method "#{color}_on_#{bg_color}" do
36
+ inject_ansi_code 30 + code, 40 + bg_code
37
+ end
38
+ end
39
+ end
40
+
41
+ Colorful::EFFECTS.each do |effect, code|
42
+ define_method effect do
43
+ inject_ansi_code code
44
+ end
45
+
46
+ define_method "no_#{effect}" do
47
+ inject_ansi_code 20 + code
48
+ end
49
+ end
50
+
51
+ def foreground(*args)
52
+ code = ansi_color_code *args
53
+ inject_ansi_code 38, 5, code
54
+ end
55
+ alias :fg :foreground
56
+ alias :color :foreground
57
+ alias :colour :foreground
58
+
59
+ def background(*args)
60
+ code = ansi_color_code *args
61
+ inject_ansi_code 48, 5, code
62
+ end
63
+ alias :bg :background
64
+ alias :on :background
65
+
66
+ private
67
+ def ansi_color_code *args
68
+ if args.size == 3
69
+ ansi_rgb_color *args
70
+ else
71
+ str = args[0].to_s.sub(/^(#|_)/, '')
72
+ if str.size == 3
73
+ red, green, blue = str[0] * 2, str[1] * 2, str[2] * 2
74
+ elsif string.size == 6
75
+ red, green, blue = str[0,2], str[2,2], str[4,2]
76
+ else
77
+ raise 'Malformed color string'
78
+ end
79
+ ansi_rgb_color red.to_i(16), green.to_i(16), blue.to_i(16)
80
+ end
81
+ end
82
+
83
+ def ansi_rgb_color *args
84
+ red, green, blue = args
85
+ args.inject('') do |m,c|
86
+ m << (c / 256.0 * 6.0).to_i.to_s
87
+ end.to_i(6) + 16
88
+ end
89
+
90
+ def inject_ansi_code *codes
91
+ result = self
92
+ codes.each do |code|
93
+ if result =~ /^\e\[[0-9;]*m/
94
+ terminator = result.index('m')
95
+ result = result.insert(terminator, ";#{code}")
96
+ else
97
+ result = "\e[#{code}m#{result}\e[m"
98
+ end
99
+ end
100
+ result
101
+ end
102
+ end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ def color_codes
4
+ { :black => 0,
5
+ :red => 1,
6
+ :green => 2,
7
+ :yellow => 3,
8
+ :blue => 4,
9
+ :magenta => 5,
10
+ :cyan => 6,
11
+ :white => 7,
12
+ :default => 9 }
13
+ end
14
+
15
+ def effect_codes
16
+ { :reset => 0,
17
+ :bright => 1,
18
+ :bold => 1,
19
+ :italic => 3,
20
+ :underline => 4,
21
+ :blink => 5,
22
+ :inverse => 7,
23
+ :hide => 8 }
24
+ end
25
+
26
+ describe String do
27
+ let(:string) { "This is a test"}
28
+
29
+ context 'colors' do
30
+ color_codes.each do |color, code|
31
+
32
+ describe "##{color}" do
33
+ subject { string.send color }
34
+ it { should == "\e[3#{code}m#{string}\e[m" }
35
+ end
36
+
37
+ describe "##{color}_background" do
38
+ subject { string.send "#{color}_background" }
39
+ it { should == "\e[4#{code}m#{string}\e[m" }
40
+ end
41
+
42
+ color_codes.each do |bg_color, bg_code|
43
+ describe "##{color}_on_#{bg_color}" do
44
+ subject { string.send "#{color}_on_#{bg_color}" }
45
+ it { should == "\e[3#{code};4#{bg_code}m#{string}\e[m" }
46
+ end
47
+
48
+ describe "##{color}.#{bg_color}_background" do
49
+ subject { string.send("#{color}").send("#{bg_color}_background") }
50
+ it { should == "\e[3#{code};4#{bg_code}m#{string}\e[m" }
51
+ end
52
+ end
53
+ end
54
+
55
+ describe "#.ansi_rgb_color" do
56
+ let(:r) { 0 }
57
+ let(:g) { 0 }
58
+ let(:b) { 0 }
59
+
60
+ subject {string.send :ansi_rgb_color, r,g,b}
61
+
62
+ context 'black' do
63
+ it { should == 16 }
64
+ end
65
+
66
+ context 'red' do
67
+ let(:r) { 255 }
68
+ it { should == 196 }
69
+ end
70
+
71
+ context 'green' do
72
+ let(:g) { 255 }
73
+ it { should == 46 }
74
+ end
75
+
76
+ context 'blue' do
77
+ let(:b) { 255 }
78
+ it { should == 21 }
79
+ end
80
+
81
+ context 'white' do
82
+ let(:r) { 255 }
83
+ let(:g) { 255 }
84
+ let(:b) { 255 }
85
+ it { should == 231 }
86
+ end
87
+ end
88
+
89
+ describe "#foreground" do
90
+ context "RGB values" do
91
+ context "black" do
92
+ subject { string.foreground(0,0,0) }
93
+ let(:code) { '16' }
94
+ it { should == "\e[38;5;#{code}m#{string}\e[m" }
95
+ end
96
+ context "blue" do
97
+ subject { string.foreground(0,0,255) }
98
+ let(:code) { '21' }
99
+ it { should == "\e[38;5;#{code}m#{string}\e[m" }
100
+ end
101
+ context "green" do
102
+ subject { string.foreground(0,255,0) }
103
+ let(:code) { '46' }
104
+ it { should == "\e[38;5;#{code}m#{string}\e[m" }
105
+ end
106
+ context "red" do
107
+ subject { string.foreground(255,0,0) }
108
+ let(:code) { '196' }
109
+ it { should == "\e[38;5;#{code}m#{string}\e[m" }
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ context 'effects' do
116
+ effect_codes.each do |effect, code|
117
+ describe "##{effect}" do
118
+ subject { string.send effect }
119
+ it { should == "\e[#{code}m#{string}\e[m" }
120
+ end
121
+
122
+ describe "#no_#{effect}" do
123
+ subject { string.send "no_#{effect}" }
124
+ it { should == "\e[2#{code}m#{string}\e[m" }
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'colorful'
5
+
6
+ RSpec.configure do |config|
7
+ end
8
+
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: colorful
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nick Karpenske
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-14 00:00:00.000000000 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &2153679300 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *2153679300
26
+ - !ruby/object:Gem::Dependency
27
+ name: fuubar
28
+ requirement: &2153678880 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *2153678880
37
+ description: Provides string extensions for terminal color output
38
+ email:
39
+ - randland@gmail.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - .rspec
46
+ - .rvmrc
47
+ - Gemfile
48
+ - Rakefile
49
+ - colorful.gemspec
50
+ - lib/colorful.rb
51
+ - lib/colorful/version.rb
52
+ - spec/colorful_spec.rb
53
+ - spec/spec_helper.rb
54
+ has_rdoc: true
55
+ homepage: ''
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project: colorful
75
+ rubygems_version: 1.6.2
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Terminal Color Gem
79
+ test_files:
80
+ - spec/colorful_spec.rb
81
+ - spec/spec_helper.rb