jcf-ansi_color 0.3.2 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,56 @@
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
+ ## Contributors
6
+
7
+ [Kacper Cieśla][comboy] improved print and puts loads with this [commit](http://github.com/comboy/ansi_color/commit/a66e3baf47aef2d3d60495c65a6a83153e7fa688). Thanks for your contributions [Kacper][comboy]!
8
+
9
+ ## Examples
10
+
11
+ require 'rubygems'
12
+ require 'ansi_color'
13
+
14
+ AnsiColor.print('plain string')
15
+ # => "plain string"
16
+
17
+ AnsiColor.puts('plain string')
18
+ # => "plain string\n"
19
+
20
+ AnsiColor.print('coloured string', :color => :red,
21
+ :background => :black,
22
+ :effects => [:blink, :bold])
23
+ # => "\e[31;40;5;1mcoloured string\e[0m"
24
+
25
+ AnsiColor.puts('coloured string', :color => :red,
26
+ :background => :black,
27
+ :effects => [:blink, :bold])
28
+ # => "\e[31;40;5;1mcoloured string\e[0m\n"
29
+
30
+ AnsiColor.red
31
+ # => 31
32
+
33
+ AnsiColor.red_background
34
+ # => 41
35
+
36
+ AnsiColor.blink
37
+ # => 5
38
+
39
+ ## Adding colour methods to String
40
+
41
+ ### This doesn't work properly!
42
+
43
+ Chaining methods works but isn't particularly ingenious.
44
+
45
+ require 'rubygems'
46
+ require 'ansi_color'
47
+
48
+ class String
49
+ include AnsiColor::String
50
+ end
51
+
52
+ 'coloured string'.red.bold.blink
53
+ # => "\e[5m\e[1m\e[31mcoloured string\e[0m\e[0m\e[0m"
54
+
55
+ [jcf]: http://github.com/jcf
56
+ [comboy]: http://github.com/comboy
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 3
4
- :patch: 2
3
+ :minor: 4
4
+ :patch: 1
data/lib/ansi_color.rb CHANGED
@@ -17,7 +17,7 @@ module AnsiColor
17
17
  :blink => 5,
18
18
  :inverse => 7
19
19
  }
20
- FOREGROUND_COLOURS = {
20
+ FOREGROUND_COLORS = {
21
21
  :black => 30,
22
22
  :red => 31,
23
23
  :green => 32,
@@ -27,7 +27,7 @@ module AnsiColor
27
27
  :cyan => 36,
28
28
  :white => 37
29
29
  }
30
- BACKGROUND_COLOURS = {
30
+ BACKGROUND_COLORS = {
31
31
  :black => 40,
32
32
  :red => 41,
33
33
  :green => 42,
@@ -38,17 +38,41 @@ module AnsiColor
38
38
  :white => 47
39
39
  }
40
40
 
41
- FOREGROUND_COLOURS.each do |name, code|
42
- define_method(name) { code }
41
+ class << self
42
+ FOREGROUND_COLORS.each do |name, code|
43
+ define_method(name) { code }
44
+ end
45
+
46
+ BACKGROUND_COLORS.each do |name, code|
47
+ define_method("#{name}_background") { code }
48
+ end
49
+
50
+ EFFECTS.each do |name, code|
51
+ define_method(name) { code }
52
+ end
43
53
  end
44
54
 
45
- BACKGROUND_COLOURS.each do |name, code|
46
- define_method("#{name}_background") { code }
55
+ def self.print(*args)
56
+ if args.last.is_a?(Hash)
57
+ options = args.pop
58
+ open_tag = Helpers::build_open_tag(options)
59
+ string = args.map { |arg| arg.to_s }.join
60
+ super(open_tag + string + Helpers::reset)
61
+ else
62
+ super(*args)
63
+ end
47
64
  end
48
65
 
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
66
+ def self.puts(*args)
67
+ if args.last.is_a?(Hash)
68
+ options = args.pop
69
+ open_tag = Helpers::build_open_tag(options)
70
+ string = args.map { |arg| arg.to_s }.join("\n")
71
+ super(open_tag + string + Helpers::reset)
72
+ else
73
+ super(*args)
74
+ end
53
75
  end
54
- end
76
+ end
77
+
78
+ require "ansi_color/string"
@@ -1,19 +1,19 @@
1
1
  module AnsiColor
2
2
  class Helpers
3
3
  def self.code_from_name(name)
4
- FOREGROUND_COLOURS[name] || raise(InvalidColorName, "#{name} is not a colour")
4
+ FOREGROUND_COLORS[name] || raise(InvalidColorName, "#{name} is not a colour")
5
5
  end
6
6
 
7
7
  def self.name_from_code(code)
8
- FOREGROUND_COLOURS.index(code.to_i) || raise(InvalidColorCode, "#{code} is not a colour code")
8
+ FOREGROUND_COLORS.index(code.to_i) || raise(InvalidColorCode, "#{code} is not a colour code")
9
9
  end
10
10
 
11
11
  def self.code_from_background_name(name)
12
- BACKGROUND_COLOURS[name] || raise(InvalidColorName, "#{name} is not a background colour")
12
+ BACKGROUND_COLORS[name] || raise(InvalidColorName, "#{name} is not a background colour")
13
13
  end
14
14
 
15
15
  def self.name_from_background_code(code)
16
- BACKGROUND_COLOURS.index(code.to_i) || raise(InvalidColorCode, "#{code} is not a background colour code")
16
+ BACKGROUND_COLORS.index(code.to_i) || raise(InvalidColorCode, "#{code} is not a background colour code")
17
17
  end
18
18
 
19
19
  def self.build_open_tag(args={})
@@ -37,8 +37,6 @@ module AnsiColor
37
37
  fg = code_from_name(options[:color]) unless options[:color].nil?
38
38
  bg = code_from_background_name(options[:background]) unless options[:background].nil?
39
39
 
40
- # options.reject! { |key, value| key == :color || key == :background }
41
-
42
40
  if effects = options[:effects]
43
41
  effects = Array(effects).flatten
44
42
  if (effects - EFFECTS.keys).empty?
@@ -2,20 +2,20 @@ module AnsiColor
2
2
  class Rainbow
3
3
  def initialize
4
4
  puts "\n\n== build_open_tag FOREGROUND"
5
- FOREGROUND_COLOURS.each do |fg_name, fg_code|
5
+ FOREGROUND_COLORS.each do |fg_name, fg_code|
6
6
  open = Helpers::build_open_tag(:color => fg_name)
7
7
  print "#{open}#{fg_name}#{Helpers::reset}\n"
8
8
  end
9
9
 
10
10
  puts "\n\n== build_open_tag BACKGROUND"
11
- BACKGROUND_COLOURS.each do |bg_name, bg_code|
11
+ BACKGROUND_COLORS.each do |bg_name, bg_code|
12
12
  open = Helpers::build_open_tag(:background => bg_name)
13
13
  print "#{open}#{bg_name}#{Helpers::reset}\n"
14
14
  end
15
15
 
16
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|
17
+ FOREGROUND_COLORS.each do |fg_name, fg_code|
18
+ BACKGROUND_COLORS.each do |bg_name, bg_code|
19
19
  open = Helpers::build_open_tag(:color => fg_name, :background => bg_name)
20
20
  print "#{open}#{fg_name} #{bg_name}#{Helpers::reset}\n"
21
21
  EFFECTS.each do |effect_name, effect_code|
@@ -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
@@ -69,20 +69,20 @@ describe Helpers do
69
69
 
70
70
  describe "should build tags" do
71
71
  it "color" do
72
- Helpers::FOREGROUND_COLOURS.each do |name, code|
72
+ Helpers::FOREGROUND_COLORS.each do |name, code|
73
73
  Helpers.build_open_tag(:color => name).should == "\e[#{code}m"
74
74
  end
75
75
  end
76
76
 
77
77
  it "background" do
78
- Helpers::BACKGROUND_COLOURS.each do |name, code|
78
+ Helpers::BACKGROUND_COLORS.each do |name, code|
79
79
  Helpers.build_open_tag(:background => name).should == "\e[0;#{code}m"
80
80
  end
81
81
  end
82
82
 
83
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|
84
+ Helpers::FOREGROUND_COLORS.each do |fg_name, fg_code|
85
+ Helpers::BACKGROUND_COLORS.each do |bg_name, bg_code|
86
86
  Helpers.build_open_tag(:color => fg_name, :background => bg_name).should == "\e[#{fg_code};#{bg_code}m"
87
87
  end
88
88
  end
@@ -97,28 +97,14 @@ describe Helpers do
97
97
  end
98
98
  end
99
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
100
  describe "finds colour codes from names" do
115
- Helpers::FOREGROUND_COLOURS.each do |name, code|
101
+ Helpers::FOREGROUND_COLORS.each do |name, code|
116
102
  it "code_from_name(:#{name}) returns #{code}" do
117
103
  Helpers.code_from_name(name).should == code
118
104
  end
119
105
  end
120
106
 
121
- Helpers::BACKGROUND_COLOURS.each do |name, code|
107
+ Helpers::BACKGROUND_COLORS.each do |name, code|
122
108
  it "code_from_background_name(:#{name}) returns #{code}" do
123
109
  Helpers.code_from_background_name(name).should == code
124
110
  end
@@ -126,13 +112,13 @@ describe Helpers do
126
112
  end
127
113
 
128
114
  describe "finds colour codes from names" do
129
- Helpers::FOREGROUND_COLOURS.each do |name, code|
115
+ Helpers::FOREGROUND_COLORS.each do |name, code|
130
116
  it "name_from_code(#{code}) returns #{name}" do
131
117
  Helpers.name_from_code(code).should == name
132
118
  end
133
119
  end
134
120
 
135
- Helpers::BACKGROUND_COLOURS.each do |name, code|
121
+ Helpers::BACKGROUND_COLORS.each do |name, code|
136
122
  it "name_from_background_code(#{code}) returns #{name}" do
137
123
  Helpers.name_from_background_code(code).should == name
138
124
  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
@@ -1,74 +1,108 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe AnsiColor do
4
- describe "wrapping strings" do
4
+ describe "color codes from names" do
5
+ FOREGROUND_COLORS.each do |name, code|
6
+ it "#{name} returns #{code}" do
7
+ AnsiColor.send(name).should == code
8
+ end
9
+ end
5
10
 
6
- it "returns the original string with no options" do
7
- tag = ansi_tag('james')
8
- tag.should == 'james'
11
+ BACKGROUND_COLORS.each do |name, code|
12
+ it "#{name}_background returns #{code}" do
13
+ AnsiColor.send("#{name}_background").should == code
14
+ end
9
15
  end
10
16
 
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"
17
+ EFFECTS.each do |name, code|
18
+ it "#{name} returns #{code}" do
19
+ AnsiColor.send("#{name}").should == code
20
+ end
14
21
  end
15
22
 
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"
23
+ describe "print" do
24
+ it "print the original string with no options" do
25
+ capture_stdout do |stdout|
26
+ AnsiColor.print('james')
27
+ stdout.string.should == 'james'
28
+ end
29
+ end
30
+
31
+ it "print non string objects with no options" do
32
+ capture_stdout do |stdout|
33
+ AnsiColor.print(234)
34
+ stdout.string.should == '234'
35
+ end
36
+ end
37
+
38
+ it "red and bold on non string object" do
39
+ capture_stdout do |stdout|
40
+ AnsiColor.print(42, :color => :red, :effects => :bold)
41
+ stdout.string.should == "#{E}31;1m42#{E}0m"
42
+ end
43
+ end
44
+
45
+ it "print many args with no options" do
46
+ capture_stdout do |stdout|
47
+ AnsiColor.print(1, ' string ', 2)
48
+ stdout.string.should == "1 string 2"
49
+ end
50
+ end
51
+
52
+ it "print many args in red and bold" do
53
+ capture_stdout do |stdout|
54
+ AnsiColor.print(1, ' string ', 2, :color => :red, :effects => :bold)
55
+ stdout.string.should == "#{E}31;1m1 string 2#{E}0m"
56
+ end
57
+ end
58
+
59
+ it "red and bold" do
60
+ capture_stdout do |stdout|
61
+ AnsiColor.print('james', :color => :red, :effects => :bold)
62
+ stdout.string.should == "#{E}31;1mjames#{E}0m"
63
+ end
64
+ end
65
+
66
+ it "blue on white blinking" do
67
+ capture_stdout do |stdout|
68
+ AnsiColor.print('james', :color => :blue,
69
+ :background => :white,
70
+ :effects => :blink)
71
+ stdout.string.should == "#{E}34;47;5mjames#{E}0m"
72
+ end
73
+ end
21
74
  end
22
75
 
76
+ describe "puts" do
77
+ it "print the original string with no options" do
78
+ capture_stdout do |stdout|
79
+ AnsiColor.puts('james')
80
+ stdout.string.should == "james\n"
81
+ end
82
+ end
83
+
84
+ it "print some number with no options" do
85
+ capture_stdout do |stdout|
86
+ AnsiColor.puts(243)
87
+ stdout.string.should == "243\n"
88
+ end
89
+ end
90
+
91
+ it "red and bold" do
92
+ capture_stdout do |stdout|
93
+ AnsiColor.puts('james', :color => :red, :effects => :bold)
94
+ stdout.string.should == "#{E}31;1mjames#{E}0m\n"
95
+ end
96
+ end
97
+
98
+ it "blue on white blinking" do
99
+ capture_stdout do |stdout|
100
+ AnsiColor.puts('james', :color => :blue,
101
+ :background => :white,
102
+ :effects => :blink)
103
+ stdout.string.should == "#{E}34;47;5mjames#{E}0m\n"
104
+ end
105
+ end
106
+ end
23
107
  end
24
108
  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_helper.rb CHANGED
@@ -1,4 +1,12 @@
1
- require 'spec'
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ require 'stringio'
2
10
 
3
11
  $LOAD_PATH.unshift(File.dirname(__FILE__))
4
12
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -8,3 +16,13 @@ include AnsiColor
8
16
  Spec::Runner.configure do |config|
9
17
 
10
18
  end
19
+
20
+ def capture_stdout(&block)
21
+ begin
22
+ orig_stream, $stdout = $stdout, StringIO.new
23
+ block.call($stdout)
24
+ ensure
25
+ s, $stdout = $stdout.string, orig_stream
26
+ s
27
+ end
28
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jcf-ansi_color
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Conroy-Finn
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-18 00:00:00 -07:00
12
+ date: 2009-04-26 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -21,19 +21,21 @@ extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
23
  - LICENSE
24
- - README.rdoc
24
+ - README.md
25
25
  files:
26
26
  - LICENSE
27
- - README.rdoc
27
+ - README.md
28
28
  - Rakefile
29
29
  - VERSION.yml
30
30
  - lib/ansi_color.rb
31
31
  - lib/ansi_color/effects.rb
32
32
  - lib/ansi_color/helpers.rb
33
33
  - lib/ansi_color/rainbow.rb
34
+ - lib/ansi_color/string.rb
34
35
  - spec/ansi_color/effects_spec.rb
35
36
  - spec/ansi_color/helpers_spec.rb
36
37
  - spec/ansi_color/rainbow_spec.rb
38
+ - spec/ansi_color/string_spec.rb
37
39
  - spec/ansi_color_spec.rb
38
40
  - spec/spec.opts
39
41
  - spec/spec_helper.rb
@@ -67,5 +69,6 @@ test_files:
67
69
  - spec/ansi_color/effects_spec.rb
68
70
  - spec/ansi_color/helpers_spec.rb
69
71
  - spec/ansi_color/rainbow_spec.rb
72
+ - spec/ansi_color/string_spec.rb
70
73
  - spec/ansi_color_spec.rb
71
74
  - spec/spec_helper.rb
data/README.rdoc DELETED
@@ -1,7 +0,0 @@
1
- = ansi_color
2
-
3
- Description goes here.
4
-
5
- == Copyright
6
-
7
- Copyright (c) 2009 James Conroy-Finn. See LICENSE for details.