jcf-ansi_color 0.3.2

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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 James Conroy-Finn
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,7 @@
1
+ = ansi_color
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 James Conroy-Finn. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "ansi_color"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "james@logi.cl"
10
+ gem.homepage = "http://github.com/jcf/ansi_color"
11
+ gem.authors = ["James Conroy-Finn"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "ansi_color #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 3
4
+ :patch: 2
@@ -0,0 +1,12 @@
1
+ module AnsiColor
2
+ class Effects < Array
3
+ def initialize(array)
4
+ # This should probably be validated
5
+ valid_effects = array.select { |effect| EFFECTS.include?(effect) }
6
+ super(valid_effects)
7
+ end
8
+ def to_codes!
9
+ self.map! { |code| EFFECTS[code] }.join(';')
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,59 @@
1
+ module AnsiColor
2
+ class Helpers
3
+ def self.code_from_name(name)
4
+ FOREGROUND_COLOURS[name] || raise(InvalidColorName, "#{name} is not a colour")
5
+ end
6
+
7
+ def self.name_from_code(code)
8
+ FOREGROUND_COLOURS.index(code.to_i) || raise(InvalidColorCode, "#{code} is not a colour code")
9
+ end
10
+
11
+ def self.code_from_background_name(name)
12
+ BACKGROUND_COLOURS[name] || raise(InvalidColorName, "#{name} is not a background colour")
13
+ end
14
+
15
+ def self.name_from_background_code(code)
16
+ BACKGROUND_COLOURS.index(code.to_i) || raise(InvalidColorCode, "#{code} is not a background colour code")
17
+ end
18
+
19
+ def self.build_open_tag(args={})
20
+ # X;FG;BG;Bm
21
+ return if args.size == 0
22
+ options = {
23
+ :color => nil,
24
+ :background => nil,
25
+ :effects => nil
26
+ }
27
+
28
+ args.each do |k, v|
29
+ unless options.keys.include?(k)
30
+ valid_options = options.keys.join(', ')
31
+ raise ArgumentError, "#{k} is not a valid argument. Valid options are #{valid_options}"
32
+ end
33
+ end
34
+
35
+ options.merge!(args)
36
+
37
+ fg = code_from_name(options[:color]) unless options[:color].nil?
38
+ bg = code_from_background_name(options[:background]) unless options[:background].nil?
39
+
40
+ # options.reject! { |key, value| key == :color || key == :background }
41
+
42
+ if effects = options[:effects]
43
+ effects = Array(effects).flatten
44
+ if (effects - EFFECTS.keys).empty?
45
+ effects = Effects.new(effects).to_codes!
46
+ else
47
+ raise InvalidEffect, "The only valid effects are #{EFFECTS.keys.join(', ')}"
48
+ end
49
+ end
50
+
51
+ tag = [fg || 0, bg, effects].compact.join(';')
52
+ "#{E + tag}m"
53
+ end
54
+
55
+ def self.reset
56
+ RESET
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,30 @@
1
+ module AnsiColor
2
+ class Rainbow
3
+ def initialize
4
+ puts "\n\n== build_open_tag FOREGROUND"
5
+ FOREGROUND_COLOURS.each do |fg_name, fg_code|
6
+ open = Helpers::build_open_tag(:color => fg_name)
7
+ print "#{open}#{fg_name}#{Helpers::reset}\n"
8
+ end
9
+
10
+ puts "\n\n== build_open_tag BACKGROUND"
11
+ BACKGROUND_COLOURS.each do |bg_name, bg_code|
12
+ open = Helpers::build_open_tag(:background => bg_name)
13
+ print "#{open}#{bg_name}#{Helpers::reset}\n"
14
+ end
15
+
16
+ puts "\n\n== build_open_tag FOREGROUND, BACKGROUND & EFFECT"
17
+ FOREGROUND_COLOURS.each do |fg_name, fg_code|
18
+ BACKGROUND_COLOURS.each do |bg_name, bg_code|
19
+ open = Helpers::build_open_tag(:color => fg_name, :background => bg_name)
20
+ print "#{open}#{fg_name} #{bg_name}#{Helpers::reset}\n"
21
+ EFFECTS.each do |effect_name, effect_code|
22
+ open = Helpers::build_open_tag(:color => fg_name, :background => bg_name, :effects => effect_name)
23
+ print "#{open}#{fg_name} #{bg_name} #{effect_name}#{Helpers::reset}\n"
24
+ end
25
+ print "\n"
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
data/lib/ansi_color.rb ADDED
@@ -0,0 +1,54 @@
1
+ $:.push File.dirname(__FILE__)
2
+ require "ansi_color/helpers"
3
+ require "ansi_color/effects"
4
+ require "ansi_color/rainbow"
5
+
6
+ module AnsiColor
7
+ class InvalidColorName < StandardError; end
8
+ class InvalidColorCode < StandardError; end
9
+ class InvalidEffect < StandardError; end
10
+
11
+ E = "\033["
12
+ RESET = "#{E}0m"
13
+ EFFECTS = {
14
+ :normal => 0,
15
+ :bold => 1,
16
+ :underscore => 4,
17
+ :blink => 5,
18
+ :inverse => 7
19
+ }
20
+ FOREGROUND_COLOURS = {
21
+ :black => 30,
22
+ :red => 31,
23
+ :green => 32,
24
+ :yellow => 33,
25
+ :blue => 34,
26
+ :magenta => 35,
27
+ :cyan => 36,
28
+ :white => 37
29
+ }
30
+ BACKGROUND_COLOURS = {
31
+ :black => 40,
32
+ :red => 41,
33
+ :green => 42,
34
+ :yellow => 43,
35
+ :blue => 44,
36
+ :magenta => 45,
37
+ :cyan => 46,
38
+ :white => 47
39
+ }
40
+
41
+ FOREGROUND_COLOURS.each do |name, code|
42
+ define_method(name) { code }
43
+ end
44
+
45
+ BACKGROUND_COLOURS.each do |name, code|
46
+ define_method("#{name}_background") { code }
47
+ end
48
+
49
+ def ansi_tag(string, options={})
50
+ return string if options.empty?
51
+ open_tag = Helpers::build_open_tag(options)
52
+ open_tag + string + Helpers::reset
53
+ end
54
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Effects do
4
+
5
+ it "ignores invalid effects on initialize" do
6
+ effects = [:bold, :blink, :does_not_exist]
7
+ effects = Effects.new(effects)
8
+ effects.should == [:bold, :blink]
9
+ end
10
+
11
+ it "converts an array of effects to codes" do
12
+ effects = [:bold, :blink]
13
+ effects = Effects.new(effects)
14
+ effects.to_codes!.should == "#{EFFECTS[:bold]};#{EFFECTS[:blink]}"
15
+ end
16
+
17
+ end
@@ -0,0 +1,142 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Helpers do
4
+
5
+ describe "handling arguments" do
6
+ it "shouldn't accept invalid options" do
7
+ lambda {
8
+ Helpers.build_open_tag(:invalid_key => 'something')
9
+ }.should raise_error(ArgumentError)
10
+ # raise ArgumentError, "#{k} is not a valid argument. Valid options are #{valid_options}"
11
+ end
12
+
13
+ it "shouldn't error with valid options" do
14
+ lambda {
15
+ Helpers.build_open_tag(:color => 'color',
16
+ :background => 'background',
17
+ :effects => 'effects')
18
+ }.should_not raise_error(ArgumentError)
19
+ end
20
+
21
+ it "returns when build_open_tag is called with no args" do
22
+ Helpers.build_open_tag.should be_nil
23
+ end
24
+
25
+ it "raises an InvalidEffect when effect is invalid" do
26
+ lambda {
27
+ Helpers.build_open_tag(:effects => :rubbish)
28
+ }.should raise_error(InvalidEffect)
29
+ end
30
+
31
+ it "raises an InvalidColorName when effect is invalid" do
32
+ lambda {
33
+ Helpers.build_open_tag(:color => 'invalid')
34
+ }.should raise_error(InvalidColorName)
35
+ end
36
+ end
37
+
38
+ describe "informative errors" do
39
+ it "should raise an InvalidColorCode when color name does not exist" do
40
+ lambda {
41
+ Helpers.name_from_code('invalid')
42
+ }.should raise_error(InvalidColorCode)
43
+ end
44
+
45
+ it "should raise an InvalidColorCode when background color name does not exist" do
46
+ lambda {
47
+ Helpers.name_from_background_code('invalid')
48
+ }.should raise_error(InvalidColorCode)
49
+ end
50
+
51
+ it "should raise an InvalidColorName when color name does not exist" do
52
+ lambda {
53
+ Helpers.code_from_name('invalid')
54
+ }.should raise_error(InvalidColorName)
55
+ end
56
+
57
+ it "should raise an InvalidColorName when color name does not exist" do
58
+ lambda {
59
+ Helpers.code_from_background_name('invalid')
60
+ }.should raise_error(InvalidColorName)
61
+ end
62
+
63
+ it "should raise an ArgumentError when an effect isn't a symbol or an array" do
64
+ lambda {
65
+ Helpers.build_open_tag(:effects => 'invalid')
66
+ }.should raise_error(InvalidEffect)
67
+ end
68
+ end
69
+
70
+ describe "should build tags" do
71
+ it "color" do
72
+ Helpers::FOREGROUND_COLOURS.each do |name, code|
73
+ Helpers.build_open_tag(:color => name).should == "\e[#{code}m"
74
+ end
75
+ end
76
+
77
+ it "background" do
78
+ Helpers::BACKGROUND_COLOURS.each do |name, code|
79
+ Helpers.build_open_tag(:background => name).should == "\e[0;#{code}m"
80
+ end
81
+ end
82
+
83
+ it "colour and background" do
84
+ Helpers::FOREGROUND_COLOURS.each do |fg_name, fg_code|
85
+ Helpers::BACKGROUND_COLOURS.each do |bg_name, bg_code|
86
+ Helpers.build_open_tag(:color => fg_name, :background => bg_name).should == "\e[#{fg_code};#{bg_code}m"
87
+ end
88
+ end
89
+ end
90
+
91
+ it "takes a symbol as an effect" do
92
+ Helpers.build_open_tag(:effects => :bold).should == "#{Helpers::E}0;1m"
93
+ end
94
+
95
+ it "takes an array of effects" do
96
+ Helpers.build_open_tag(:effects => [:bold, :blink]).should == "#{Helpers::E}0;1;5m"
97
+ end
98
+ end
99
+
100
+ describe "code from name" do
101
+ Helpers::FOREGROUND_COLOURS.each do |name, code|
102
+ it "#{name} returns #{code}" do
103
+ Helpers.send(name).should == code
104
+ end
105
+ end
106
+
107
+ Helpers::BACKGROUND_COLOURS.each do |name, code|
108
+ it "#{name}_background returns #{code}" do
109
+ Helpers.send("#{name}_background").should == code
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "finds colour codes from names" do
115
+ Helpers::FOREGROUND_COLOURS.each do |name, code|
116
+ it "code_from_name(:#{name}) returns #{code}" do
117
+ Helpers.code_from_name(name).should == code
118
+ end
119
+ end
120
+
121
+ Helpers::BACKGROUND_COLOURS.each do |name, code|
122
+ it "code_from_background_name(:#{name}) returns #{code}" do
123
+ Helpers.code_from_background_name(name).should == code
124
+ end
125
+ end
126
+ end
127
+
128
+ describe "finds colour codes from names" do
129
+ Helpers::FOREGROUND_COLOURS.each do |name, code|
130
+ it "name_from_code(#{code}) returns #{name}" do
131
+ Helpers.name_from_code(code).should == name
132
+ end
133
+ end
134
+
135
+ Helpers::BACKGROUND_COLOURS.each do |name, code|
136
+ it "name_from_background_code(#{code}) returns #{name}" do
137
+ Helpers.name_from_background_code(code).should == name
138
+ end
139
+ end
140
+ end
141
+
142
+ end
@@ -0,0 +1,4 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Rainbow do
4
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe AnsiColor do
4
+ describe "wrapping strings" do
5
+
6
+ it "returns the original string with no options" do
7
+ tag = ansi_tag('james')
8
+ tag.should == 'james'
9
+ end
10
+
11
+ it "red and bold" do
12
+ tag = ansi_tag('james', :color => :red, :effects => :bold)
13
+ tag.should == "#{Helpers::E}31;1mjames#{Helpers::E}0m"
14
+ end
15
+
16
+ it "blue on white blinking" do
17
+ tag = ansi_tag('james', :color => :blue,
18
+ :background => :white,
19
+ :effects => :blink)
20
+ tag.should == "#{Helpers::E}34;47;5mjames#{Helpers::E}0m"
21
+ end
22
+
23
+ end
24
+ end
25
+
26
+ # describe String do
27
+ #
28
+ # describe "styles" do
29
+ #
30
+ # Helpers::EFFECTS.each do |name, code|
31
+ # it "#{name}" do
32
+ # "test string".send(name).should == "#{Helpers::E}#{code}mtest string#{Helpers::RESET}"
33
+ # # puts "#{name}".send(name)
34
+ # end
35
+ # end
36
+ #
37
+ # end
38
+ #
39
+ # describe "foreground colours" do
40
+ #
41
+ # Helpers::FOREGROUND_COLOURS.each do |name, code|
42
+ # it "#{name}" do
43
+ # "test string".send(name).should == "#{Helpers::E}#{code}mtest string#{Helpers::RESET}"
44
+ # # puts "#{name}".send(name)
45
+ # end
46
+ # end
47
+ #
48
+ # end
49
+ #
50
+ # describe "background colours" do
51
+ #
52
+ # Helpers::BACKGROUND_COLOURS.each do |name, code|
53
+ # it "#{name}" do
54
+ # "test string".send("#{name}_background").should == "#{Helpers::E}0;#{code}mtest string#{Helpers::RESET}"
55
+ # # puts "#{name}_background".send("#{name}_background")
56
+ # end
57
+ # end
58
+ #
59
+ # end
60
+ #
61
+ # describe "colour on background" do
62
+ #
63
+ # Helpers::FOREGROUND_COLOURS.each do |fg_name, fg_code|
64
+ # Helpers::BACKGROUND_COLOURS.each do |bg_name, bg_code|
65
+ # it "#{fg_name} on #{bg_name}" do
66
+ # "test string".send("#{fg_name}_on_#{bg_name}").should == "#{Helpers::E}#{fg_code};#{bg_code}mtest string#{Helpers::RESET}"
67
+ # # puts "#{fg_name}_on_#{bg_name}".send("#{fg_name}_on_#{bg_name}")
68
+ # end
69
+ # end
70
+ # end
71
+ #
72
+ # end
73
+ #
74
+ # end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,10 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'ansi_color'
6
+ include AnsiColor
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jcf-ansi_color
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ platform: ruby
6
+ authors:
7
+ - James Conroy-Finn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-18 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: james@logi.cl
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/ansi_color.rb
31
+ - lib/ansi_color/effects.rb
32
+ - lib/ansi_color/helpers.rb
33
+ - lib/ansi_color/rainbow.rb
34
+ - spec/ansi_color/effects_spec.rb
35
+ - spec/ansi_color/helpers_spec.rb
36
+ - spec/ansi_color/rainbow_spec.rb
37
+ - spec/ansi_color_spec.rb
38
+ - spec/spec.opts
39
+ - spec/spec_helper.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/jcf/ansi_color
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: TODO
66
+ test_files:
67
+ - spec/ansi_color/effects_spec.rb
68
+ - spec/ansi_color/helpers_spec.rb
69
+ - spec/ansi_color/rainbow_spec.rb
70
+ - spec/ansi_color_spec.rb
71
+ - spec/spec_helper.rb