comboy-ansi_color 0.4.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 +20 -0
- data/README.md +62 -0
- data/Rakefile +48 -0
- data/VERSION.yml +4 -0
- data/ansi_color.gemspec +42 -0
- data/lib/ansi_color.rb +89 -0
- data/lib/ansi_color/effects.rb +12 -0
- data/lib/ansi_color/helpers.rb +57 -0
- data/lib/ansi_color/rainbow.rb +30 -0
- data/lib/ansi_color/string.rb +29 -0
- data/spec/ansi_color/effects_spec.rb +17 -0
- data/spec/ansi_color/helpers_spec.rb +128 -0
- data/spec/ansi_color/rainbow_spec.rb +4 -0
- data/spec/ansi_color/string_spec.rb +41 -0
- data/spec/ansi_color_spec.rb +103 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +19 -0
- metadata +79 -0
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.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# ansi_color
|
|
2
|
+
|
|
3
|
+
Add some simple methods that allow you to colourise and style your Strings with lovely Ruby syntactic sugar.
|
|
4
|
+
|
|
5
|
+
## Examples
|
|
6
|
+
|
|
7
|
+
require 'rubygems'
|
|
8
|
+
require 'ansi_color'
|
|
9
|
+
|
|
10
|
+
AnsiColor.print('plain string')
|
|
11
|
+
# => "plain string"
|
|
12
|
+
|
|
13
|
+
AnsiColor.puts('plain string')
|
|
14
|
+
# => "plain string\n"
|
|
15
|
+
|
|
16
|
+
AnsiColor.print('coloured string', :color => :red,
|
|
17
|
+
:background => :black,
|
|
18
|
+
:effects => [:blink, :bold])
|
|
19
|
+
# => "\e[31;40;5;1mcoloured string\e[0m"
|
|
20
|
+
|
|
21
|
+
AnsiColor.puts('coloured string', :color => :red,
|
|
22
|
+
:background => :black,
|
|
23
|
+
:effects => [:blink, :bold])
|
|
24
|
+
# => "\e[31;40;5;1mcoloured string\e[0m\n"
|
|
25
|
+
|
|
26
|
+
AnsiColor.red
|
|
27
|
+
# => 31
|
|
28
|
+
|
|
29
|
+
AnsiColor.red_background
|
|
30
|
+
# => 41
|
|
31
|
+
|
|
32
|
+
AnsiColor.blink
|
|
33
|
+
# => 5
|
|
34
|
+
|
|
35
|
+
## Having AnsiColor options in your puts
|
|
36
|
+
|
|
37
|
+
require 'rubygems'
|
|
38
|
+
require 'ansi_color'
|
|
39
|
+
|
|
40
|
+
include AnsiColor::StdOut
|
|
41
|
+
|
|
42
|
+
puts "hello", :color => :blue
|
|
43
|
+
print "indeed", :effect => :bold
|
|
44
|
+
|
|
45
|
+
# or even shorter version
|
|
46
|
+
puts "ruby is so coll", :red
|
|
47
|
+
|
|
48
|
+
## Adding colour methods to String
|
|
49
|
+
|
|
50
|
+
### This doesn't work properly!
|
|
51
|
+
|
|
52
|
+
Chaining methods works but isn't particularly ingenious.
|
|
53
|
+
|
|
54
|
+
require 'rubygems'
|
|
55
|
+
require 'ansi_color'
|
|
56
|
+
|
|
57
|
+
class String
|
|
58
|
+
include AnsiColor::String
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
'coloured string'.red.bold.blink
|
|
62
|
+
# => "\e[5m\e[1m\e[31mcoloured string\e[0m\e[0m\e[0m"
|
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
data/ansi_color.gemspec
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = %q{ansi_color}
|
|
5
|
+
s.version = "0.4.2"
|
|
6
|
+
|
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
8
|
+
s.authors = ["James Conroy-Finn","Kacper Ciesla"]
|
|
9
|
+
s.date = %q{2009-05-12}
|
|
10
|
+
s.email = %q{james@logi.cl}
|
|
11
|
+
s.extra_rdoc_files = [
|
|
12
|
+
"LICENSE",
|
|
13
|
+
"README.md"
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
s.files = [
|
|
17
|
+
"spec", "spec/ansi_color_spec.rb", "spec/ansi_color", "spec/ansi_color/effects_spec.rb", "spec/ansi_color/helpers_spec.rb", "spec/ansi_color/rainbow_spec.rb", "spec/ansi_color/string_spec.rb", "spec/spec_helper.rb", "spec/spec.opts", "VERSION.yml", "LICENSE", "lib", "lib/ansi_color", "lib/ansi_color/effects.rb", "lib/ansi_color/rainbow.rb", "lib/ansi_color/string.rb", "lib/ansi_color/helpers.rb", "lib/ansi_color.rb", "Rakefile", "ansi_color.gemspec", "README.md"]
|
|
18
|
+
|
|
19
|
+
s.has_rdoc = true
|
|
20
|
+
s.homepage = %q{http://github.com/jcf/ansi_color}
|
|
21
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
|
22
|
+
s.require_paths = ["lib"]
|
|
23
|
+
s.rubygems_version = %q{1.3.1}
|
|
24
|
+
s.summary = %q{TODO}
|
|
25
|
+
s.test_files = [
|
|
26
|
+
"spec/ansi_color/effects_spec.rb",
|
|
27
|
+
"spec/ansi_color/helpers_spec.rb",
|
|
28
|
+
"spec/ansi_color/rainbow_spec.rb",
|
|
29
|
+
"spec/ansi_color_spec.rb",
|
|
30
|
+
"spec/spec_helper.rb"
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
if s.respond_to? :specification_version then
|
|
34
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
35
|
+
s.specification_version = 2
|
|
36
|
+
|
|
37
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
38
|
+
else
|
|
39
|
+
end
|
|
40
|
+
else
|
|
41
|
+
end
|
|
42
|
+
end
|
data/lib/ansi_color.rb
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
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
|
+
:italics => 3,
|
|
17
|
+
:underscore => 4,
|
|
18
|
+
:blink => 5,
|
|
19
|
+
:inverse => 7
|
|
20
|
+
}
|
|
21
|
+
FOREGROUND_COLORS = {
|
|
22
|
+
:black => 30,
|
|
23
|
+
:red => 31,
|
|
24
|
+
:green => 32,
|
|
25
|
+
:yellow => 33,
|
|
26
|
+
:blue => 34,
|
|
27
|
+
:magenta => 35,
|
|
28
|
+
:cyan => 36,
|
|
29
|
+
:white => 37,
|
|
30
|
+
:default => 39
|
|
31
|
+
|
|
32
|
+
}
|
|
33
|
+
BACKGROUND_COLORS = {
|
|
34
|
+
:black => 40,
|
|
35
|
+
:red => 41,
|
|
36
|
+
:green => 42,
|
|
37
|
+
:yellow => 43,
|
|
38
|
+
:blue => 44,
|
|
39
|
+
:magenta => 45,
|
|
40
|
+
:cyan => 46,
|
|
41
|
+
:white => 47,
|
|
42
|
+
:white => 49
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module StdOut
|
|
46
|
+
|
|
47
|
+
def print(*args)
|
|
48
|
+
if [Hash,Symbol].include? args.last.class
|
|
49
|
+
options = args.pop
|
|
50
|
+
options = {:color => options} if options.kind_of? Symbol
|
|
51
|
+
open_tag = Helpers::build_open_tag(options)
|
|
52
|
+
super(open_tag + args.map{|a| a.to_s}.join + Helpers::reset)
|
|
53
|
+
else
|
|
54
|
+
super(*args)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def puts(*args)
|
|
59
|
+
if [Hash,Symbol].include? args.last.class
|
|
60
|
+
options = args.pop
|
|
61
|
+
options = {:color => options} if options.kind_of? Symbol
|
|
62
|
+
open_tag = Helpers::build_open_tag(options)
|
|
63
|
+
super(open_tag + args.map{|a| a.to_s}.join("\n") + Helpers::reset)
|
|
64
|
+
else
|
|
65
|
+
super(*args)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
class << self
|
|
72
|
+
FOREGROUND_COLORS.each do |name, code|
|
|
73
|
+
define_method(name) { code }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
BACKGROUND_COLORS.each do |name, code|
|
|
77
|
+
define_method("#{name}_background") { code }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
EFFECTS.each do |name, code|
|
|
81
|
+
define_method(name) { code }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
include StdOut
|
|
85
|
+
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
require "ansi_color/string"
|
|
@@ -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,57 @@
|
|
|
1
|
+
module AnsiColor
|
|
2
|
+
class Helpers
|
|
3
|
+
def self.code_from_name(name)
|
|
4
|
+
FOREGROUND_COLORS[name] || raise(InvalidColorName, "#{name} is not a colour")
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def self.name_from_code(code)
|
|
8
|
+
FOREGROUND_COLORS.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_COLORS[name] || raise(InvalidColorName, "#{name} is not a background colour")
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.name_from_background_code(code)
|
|
16
|
+
BACKGROUND_COLORS.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
|
+
if effects = options[:effects]
|
|
41
|
+
effects = Array(effects).flatten
|
|
42
|
+
if (effects - EFFECTS.keys).empty?
|
|
43
|
+
effects = Effects.new(effects).to_codes!
|
|
44
|
+
else
|
|
45
|
+
raise InvalidEffect, "The only valid effects are #{EFFECTS.keys.join(', ')}"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
tag = [fg || 0, bg, effects].compact.join(';')
|
|
50
|
+
"#{E + tag}m"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.reset
|
|
54
|
+
RESET
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module AnsiColor
|
|
2
|
+
class Rainbow
|
|
3
|
+
def initialize
|
|
4
|
+
puts "\n\n== build_open_tag FOREGROUND"
|
|
5
|
+
FOREGROUND_COLORS.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_COLORS.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_COLORS.each do |fg_name, fg_code|
|
|
18
|
+
BACKGROUND_COLORS.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
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module AnsiColor
|
|
2
|
+
module String
|
|
3
|
+
FOREGROUND_COLORS.each do |name, code|
|
|
4
|
+
define_method name do
|
|
5
|
+
"#{E}#{code}m#{self}#{RESET}"
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
BACKGROUND_COLORS.each do |name, code|
|
|
10
|
+
define_method "#{name}_background" do
|
|
11
|
+
"#{E}0;#{code}m#{self}#{RESET}"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
FOREGROUND_COLORS.each do |fg_name, fg_code|
|
|
16
|
+
BACKGROUND_COLORS.each do |bg_name, bg_code|
|
|
17
|
+
define_method "#{fg_name}_on_#{bg_name}" do
|
|
18
|
+
"#{E}#{fg_code};#{bg_code}m#{self}#{RESET}"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
EFFECTS.each do |name, code|
|
|
24
|
+
define_method name do
|
|
25
|
+
"#{E}#{code}m#{self}#{E}0m"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
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,128 @@
|
|
|
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_COLORS.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_COLORS.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_COLORS.each do |fg_name, fg_code|
|
|
85
|
+
Helpers::BACKGROUND_COLORS.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 "finds colour codes from names" do
|
|
101
|
+
Helpers::FOREGROUND_COLORS.each do |name, code|
|
|
102
|
+
it "code_from_name(:#{name}) returns #{code}" do
|
|
103
|
+
Helpers.code_from_name(name).should == code
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
Helpers::BACKGROUND_COLORS.each do |name, code|
|
|
108
|
+
it "code_from_background_name(:#{name}) returns #{code}" do
|
|
109
|
+
Helpers.code_from_background_name(name).should == code
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
describe "finds colour codes from names" do
|
|
115
|
+
Helpers::FOREGROUND_COLORS.each do |name, code|
|
|
116
|
+
it "name_from_code(#{code}) returns #{name}" do
|
|
117
|
+
Helpers.name_from_code(code).should == name
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
Helpers::BACKGROUND_COLORS.each do |name, code|
|
|
122
|
+
it "name_from_background_code(#{code}) returns #{name}" do
|
|
123
|
+
Helpers.name_from_background_code(code).should == name
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|
2
|
+
|
|
3
|
+
class String
|
|
4
|
+
include AnsiColor::String
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
describe String do
|
|
8
|
+
describe "styles" do
|
|
9
|
+
EFFECTS.each do |name, code|
|
|
10
|
+
it "#{name}" do
|
|
11
|
+
"test string".send(name).should == "#{E}#{code}mtest string#{RESET}"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
describe "foreground colors" do
|
|
17
|
+
FOREGROUND_COLORS.each do |name, code|
|
|
18
|
+
it "#{name}" do
|
|
19
|
+
"test string".send(name).should == "#{E}#{code}mtest string#{RESET}"
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
describe "background colors" do
|
|
25
|
+
BACKGROUND_COLORS.each do |name, code|
|
|
26
|
+
it "#{name}" do
|
|
27
|
+
"test string".send("#{name}_background").should == "#{E}0;#{code}mtest string#{RESET}"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
describe "color on background" do
|
|
33
|
+
FOREGROUND_COLORS.each do |fg_name, fg_code|
|
|
34
|
+
BACKGROUND_COLORS.each do |bg_name, bg_code|
|
|
35
|
+
it "#{fg_name} on #{bg_name}" do
|
|
36
|
+
"test string".send("#{fg_name}_on_#{bg_name}").should == "#{E}#{fg_code};#{bg_code}mtest string#{RESET}"
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
|
|
3
|
+
describe AnsiColor do
|
|
4
|
+
describe "print" do
|
|
5
|
+
it "print the original string with no options" do
|
|
6
|
+
string = catch_stdout { AnsiColor.print('james') }
|
|
7
|
+
string.should == 'james'
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "print non string objects with no options" do
|
|
11
|
+
string = catch_stdout { AnsiColor.print(234) }
|
|
12
|
+
string.should == '234'
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "red and bold on non string object" do
|
|
16
|
+
string = catch_stdout { AnsiColor.print(42, :color => :red, :effects => :bold) }
|
|
17
|
+
string.should == "#{E}31;1m42#{E}0m"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "print many args with no options" do
|
|
21
|
+
str = catch_stdout { AnsiColor.print(1,'w0t',2) }
|
|
22
|
+
str.should == "1w0t2"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "print many args in red and bold" do
|
|
26
|
+
str = catch_stdout { AnsiColor.print(1,'w0t',2, :color => :red, :effects => :bold) }
|
|
27
|
+
str.should == "#{E}31;1m1w0t2#{E}0m"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "red and bold" do
|
|
31
|
+
string = catch_stdout { AnsiColor.print('james', :color => :red, :effects => :bold) }
|
|
32
|
+
string.should == "#{E}31;1mjames#{E}0m"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "blue on white blinking" do
|
|
36
|
+
tag = catch_stdout {
|
|
37
|
+
AnsiColor.print('james', :color => :blue,
|
|
38
|
+
:background => :white,
|
|
39
|
+
:effects => :blink)
|
|
40
|
+
}
|
|
41
|
+
tag.should == "#{E}34;47;5mjames#{E}0m"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "if symbol is a last parameter treat it as color" do
|
|
45
|
+
s1 = catch_stdout { AnsiColor.print('comboy', :color => :red) }
|
|
46
|
+
s2 = catch_stdout { AnsiColor.print('comboy', :red) }
|
|
47
|
+
s1.should == s2
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
describe "puts" do
|
|
52
|
+
it "print the original string with no options" do
|
|
53
|
+
string = catch_stdout { AnsiColor.puts('james') }
|
|
54
|
+
string.should == "james\n"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "print some number with no options" do
|
|
58
|
+
string = catch_stdout { AnsiColor.puts(243) }
|
|
59
|
+
string.should == "243\n"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it "red and bold" do
|
|
63
|
+
string = catch_stdout { AnsiColor.puts('james', :color => :red, :effects => :bold) }
|
|
64
|
+
string.should == "#{E}31;1mjames#{E}0m\n"
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it "blue on white blinking" do
|
|
68
|
+
string = catch_stdout {
|
|
69
|
+
AnsiColor.puts('james', :color => :blue,
|
|
70
|
+
:background => :white,
|
|
71
|
+
:effects => :blink)
|
|
72
|
+
}
|
|
73
|
+
string.should == "#{E}34;47;5mjames#{E}0m\n"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it "if symbol is a last parameter treat it as color" do
|
|
77
|
+
s1 = catch_stdout { AnsiColor.puts('comboy', :color => :red) }
|
|
78
|
+
s2 = catch_stdout { AnsiColor.puts('comboy', :red) }
|
|
79
|
+
s1.should == s2
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
describe "color codes from names" do
|
|
85
|
+
FOREGROUND_COLORS.each do |name, code|
|
|
86
|
+
it "#{name} returns #{code}" do
|
|
87
|
+
AnsiColor.send(name).should == code
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
BACKGROUND_COLORS.each do |name, code|
|
|
92
|
+
it "#{name}_background returns #{code}" do
|
|
93
|
+
AnsiColor.send("#{name}_background").should == code
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
EFFECTS.each do |name, code|
|
|
98
|
+
it "#{name} returns #{code}" do
|
|
99
|
+
AnsiColor.send("#{name}").should == code
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
data/spec/spec.opts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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
|
|
11
|
+
|
|
12
|
+
def catch_stdout
|
|
13
|
+
stdout = $>
|
|
14
|
+
fake = StringIO.new ''
|
|
15
|
+
$> = fake
|
|
16
|
+
yield
|
|
17
|
+
$> = stdout
|
|
18
|
+
fake.string
|
|
19
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: comboy-ansi_color
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.4.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- James Conroy-Finn
|
|
8
|
+
- Kacper Ciesla
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
|
|
13
|
+
date: 2009-05-12 00:00:00 -07:00
|
|
14
|
+
default_executable:
|
|
15
|
+
dependencies: []
|
|
16
|
+
|
|
17
|
+
description:
|
|
18
|
+
email: james@logi.cl
|
|
19
|
+
executables: []
|
|
20
|
+
|
|
21
|
+
extensions: []
|
|
22
|
+
|
|
23
|
+
extra_rdoc_files:
|
|
24
|
+
- LICENSE
|
|
25
|
+
- README.md
|
|
26
|
+
files:
|
|
27
|
+
- spec
|
|
28
|
+
- spec/ansi_color_spec.rb
|
|
29
|
+
- spec/ansi_color
|
|
30
|
+
- spec/ansi_color/effects_spec.rb
|
|
31
|
+
- spec/ansi_color/helpers_spec.rb
|
|
32
|
+
- spec/ansi_color/rainbow_spec.rb
|
|
33
|
+
- spec/ansi_color/string_spec.rb
|
|
34
|
+
- spec/spec_helper.rb
|
|
35
|
+
- spec/spec.opts
|
|
36
|
+
- VERSION.yml
|
|
37
|
+
- LICENSE
|
|
38
|
+
- lib
|
|
39
|
+
- lib/ansi_color
|
|
40
|
+
- lib/ansi_color/effects.rb
|
|
41
|
+
- lib/ansi_color/rainbow.rb
|
|
42
|
+
- lib/ansi_color/string.rb
|
|
43
|
+
- lib/ansi_color/helpers.rb
|
|
44
|
+
- lib/ansi_color.rb
|
|
45
|
+
- Rakefile
|
|
46
|
+
- ansi_color.gemspec
|
|
47
|
+
- README.md
|
|
48
|
+
has_rdoc: true
|
|
49
|
+
homepage: http://github.com/jcf/ansi_color
|
|
50
|
+
post_install_message:
|
|
51
|
+
rdoc_options:
|
|
52
|
+
- --charset=UTF-8
|
|
53
|
+
require_paths:
|
|
54
|
+
- lib
|
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: "0"
|
|
60
|
+
version:
|
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: "0"
|
|
66
|
+
version:
|
|
67
|
+
requirements: []
|
|
68
|
+
|
|
69
|
+
rubyforge_project:
|
|
70
|
+
rubygems_version: 1.2.0
|
|
71
|
+
signing_key:
|
|
72
|
+
specification_version: 2
|
|
73
|
+
summary: TODO
|
|
74
|
+
test_files:
|
|
75
|
+
- spec/ansi_color/effects_spec.rb
|
|
76
|
+
- spec/ansi_color/helpers_spec.rb
|
|
77
|
+
- spec/ansi_color/rainbow_spec.rb
|
|
78
|
+
- spec/ansi_color_spec.rb
|
|
79
|
+
- spec/spec_helper.rb
|