colored 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +24 -0
  2. data/lib/colored.rb +87 -0
  3. data/test/test_color.rb +32 -0
  4. metadata +48 -0
data/README ADDED
@@ -0,0 +1,24 @@
1
+ cute.
2
+
3
+ usage:
4
+ >> puts "this is red".red
5
+
6
+ >> puts "this is red with a blue background (read: ugly)".red_on_blue
7
+
8
+ >> puts "this is red with an underline".red.underline
9
+
10
+ >> puts "this is really bold and really blue".bold.blue
11
+
12
+ >> logger.debug "hey this is broken!".red_on_yellow # in rails
13
+
14
+ >> puts Color.red "This is red" # but this part is mostly untested
15
+
16
+ Windows users:
17
+ You will need the Win32 Console Ansi gem. Get it:
18
+
19
+ $ sudo gem install win32console-1.0.0 --source require.errtheblog.com
20
+
21
+ (We're trying to make it official. Hang in there.)
22
+
23
+ >> chris[at]ozmm[dot]org
24
+ => http://errtheblog.com/
@@ -0,0 +1,87 @@
1
+ require 'Win32/Console/ANSI' if PLATFORM =~ /win32/
2
+
3
+ #
4
+ # cute.
5
+ #
6
+ # usage:
7
+ #
8
+ # >> "this is red".red
9
+ #
10
+ # >> "this is red with a blue background (read: ugly)".red_on_blue
11
+ #
12
+ # >> "this is red with an underline".red.underline
13
+ #
14
+ # >> "this is really bold and really blue".bold.blue
15
+ #
16
+ # >> Colored.red "This is red" # but this part is mostly untested
17
+ #
18
+ module Colored
19
+ extend self
20
+
21
+ 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
+ }
31
+
32
+ EXTRAS = {
33
+ 'clear' => 0,
34
+ 'bold' => 1,
35
+ 'underline' => 4,
36
+ 'reverse' => 7
37
+ }
38
+
39
+ COLORS.each do |color, value|
40
+ define_method(color) do
41
+ colorize(self, :foreground => color)
42
+ end
43
+
44
+ define_method("on_#{color}") do
45
+ colorize(self, :background => color)
46
+ end
47
+
48
+ COLORS.each do |highlight, value|
49
+ next if color == highlight
50
+ define_method("#{color}_on_#{highlight}") do
51
+ colorize(self, :foreground => color, :background => highlight)
52
+ end
53
+ end
54
+ end
55
+
56
+ EXTRAS.each do |extra, value|
57
+ next if extra == 'clear'
58
+ define_method(extra) do
59
+ colorize(self, :extra => extra)
60
+ end
61
+ end
62
+
63
+ def colorize(string, options = {})
64
+ colored = ''
65
+ colored = [color(options[:foreground]), color("on_#{options[:background]}"), extra(options[:extra])].compact * ''
66
+ colored << string
67
+ colored << extra(:clear)
68
+ end
69
+
70
+ def colors
71
+ @@colors ||= COLORS.keys.sort
72
+ end
73
+
74
+ def extra(extra_name)
75
+ extra_name = extra_name.to_s
76
+ "\e[#{EXTRAS[extra_name]}m" if EXTRAS[extra_name]
77
+ end
78
+
79
+ def color(color_name)
80
+ background = color_name.to_s =~ /on_/
81
+ color_name = color_name.to_s.sub('on_', '')
82
+ return unless color_name && COLORS[color_name]
83
+ "\e[#{COLORS[color_name] + (background ? 10 : 0)}m"
84
+ end
85
+ end unless Object.const_defined? :Colored
86
+
87
+ String.send(:include, Colored)
@@ -0,0 +1,32 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/color'
3
+
4
+ class TestColor < Test::Unit::TestCase
5
+ def test_one_color
6
+ assert_equal "\e[31mred\e[0m", "red".red
7
+ end
8
+
9
+ def test_two_colors
10
+ assert_equal "\e[34m\e[31mblue\e[0m\e[0m", "blue".red.blue
11
+ end
12
+
13
+ def test_background_color
14
+ assert_equal "\e[43mon yellow\e[0m", "on yellow".on_yellow
15
+ end
16
+
17
+ def test_hot_color_on_color_action
18
+ assert_equal "\e[31m\e[44mred on blue\e[0m", "red on blue".red_on_blue
19
+ end
20
+
21
+ def test_modifier
22
+ assert_equal "\e[1mway bold\e[0m", "way bold".bold
23
+ end
24
+
25
+ def test_modifiers_stack
26
+ assert_equal "\e[4m\e[1munderlined bold\e[0m\e[0m", "underlined bold".bold.underline
27
+ end
28
+
29
+ def test_modifiers_stack_with_colors
30
+ assert_equal "\e[36m\e[4m\e[1mcyan underlined bold\e[0m\e[0m\e[0m", "cyan underlined bold".bold.underline.cyan
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: colored
5
+ version: !ruby/object:Gem::Version
6
+ version: "1.0"
7
+ date: 2006-11-09 00:00:00 -08:00
8
+ summary: Add some color to your life.
9
+ require_paths:
10
+ - lib
11
+ email: chris[at]ozmm[dot]org
12
+ homepage: http://errtheblog.com/
13
+ rubyforge_project:
14
+ description: Add some color to your life.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Chris Wanstrath
31
+ files:
32
+ - README
33
+ - lib/colored.rb
34
+ - test/test_color.rb
35
+ test_files: []
36
+
37
+ rdoc_options: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ requirements: []
46
+
47
+ dependencies: []
48
+