rainbow 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/Changelog +0 -0
  2. data/LICENSE +0 -0
  3. data/README +11 -0
  4. data/lib/rainbow.rb +96 -0
  5. data/test/rainbow_test.rb +40 -0
  6. metadata +57 -0
File without changes
data/LICENSE ADDED
File without changes
data/README ADDED
@@ -0,0 +1,11 @@
1
+ == Rainbow
2
+
3
+ === About
4
+
5
+ It's!
6
+
7
+ === Usage
8
+
9
+ require 'rainbow'
10
+ puts "this is red".foreground(:red)
11
+
@@ -0,0 +1,96 @@
1
+
2
+ module Sickill
3
+ module Rainbow
4
+
5
+ TERM_COLORS = {
6
+ :black => 0,
7
+ :red => 1,
8
+ :green => 2,
9
+ :yellow => 3,
10
+ :blue => 4,
11
+ :magenta => 5,
12
+ :cyan => 6,
13
+ :white => 7,
14
+ :default => 9,
15
+ }
16
+
17
+ TERM_EFFECTS = {
18
+ :reset => 0,
19
+ :bright => 1,
20
+ :italic => 3,
21
+ :underline => 4,
22
+ :blink => 5,
23
+ :inverse => 7,
24
+ :hide => 8,
25
+ }
26
+
27
+ # Sets foreground color if this text.
28
+ def foreground(color)
29
+ color = color.to_sym
30
+ validate_color(color)
31
+ wrap_with_code(TERM_COLORS[color] + 30)
32
+ end
33
+ alias_method :color, :foreground
34
+ alias_method :colour, :foreground
35
+
36
+
37
+ # Sets background color of this text.
38
+ def background(color)
39
+ color = color.to_sym
40
+ validate_color(color)
41
+ wrap_with_code(TERM_COLORS[color] + 40)
42
+ end
43
+
44
+ # Resets terminal to default colors/backgrounds.
45
+ #
46
+ # It shouldn't be needed to use this method because all methods append terminal reset code to end of string.
47
+ def reset
48
+ wrap_with_code(TERM_EFFECTS[:reset])
49
+ end
50
+
51
+ # Turns on bright/bold for this text.
52
+ def bright
53
+ wrap_with_code(TERM_EFFECTS[:bright])
54
+ end
55
+
56
+ # Turns on italic style for this text (not well supported by terminal emulators).
57
+ def italic
58
+ wrap_with_code(TERM_EFFECTS[:italic])
59
+ end
60
+
61
+ # Turns on underline decoration for this text.
62
+ def underline
63
+ wrap_with_code(TERM_EFFECTS[:underline])
64
+ end
65
+
66
+ # Turns on blinking attribute for this text (not well supported by terminal emulators).
67
+ def blink
68
+ wrap_with_code(TERM_EFFECTS[:blink])
69
+ end
70
+
71
+ # Inverses current foreground/background colors.
72
+ def inverse
73
+ wrap_with_code(TERM_EFFECTS[:inverse])
74
+ end
75
+
76
+ # Hides this text (set its color to the same as background).
77
+ def hide
78
+ wrap_with_code(TERM_EFFECTS[:hide])
79
+ end
80
+
81
+ protected
82
+ def wrap_with_code(code) #:nodoc:
83
+ out = self
84
+ match = out.match(/^(\e\[([\d;]+)m)*/)
85
+ out.insert(match.end(0), "\e[#{code}m")
86
+ out.concat("\e[0m") unless out =~ /\e\[0m$/
87
+ out
88
+ end
89
+
90
+ def validate_color(color) #:nodoc:
91
+ raise ArgumentError.new("Unknown color, valid colors: #{TERM_COLORS.keys.join(', ')}") unless TERM_COLORS.keys.include?(color)
92
+ end
93
+ end
94
+ end
95
+
96
+ String.send(:include, Sickill::Rainbow)
@@ -0,0 +1,40 @@
1
+ require 'test/unit'
2
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/rainbow')
3
+
4
+ class RainbowTest < Test::Unit::TestCase #:nodoc:
5
+ def test_color
6
+ assert_equal "\e[31mhello\e[0m", "hello".color(:red)
7
+ end
8
+
9
+ def test_foreground
10
+ assert_equal "hello".color(:red), "hello".foreground(:red)
11
+ end
12
+
13
+ def test_colour_alias
14
+ assert_equal "hello".color(:red), "hello".colour(:red)
15
+ end
16
+
17
+ def test_background
18
+ assert_equal "\e[42mhello\e[0m", "hello".background(:green)
19
+ end
20
+
21
+ def test_color_and_background
22
+ assert_equal "\e[31m\e[42mhello\e[0m", "hello".color(:red).background(:green)
23
+ end
24
+
25
+ def test_bright
26
+ assert_equal "\e[1mhello\e[0m", "hello".bright
27
+ end
28
+
29
+ def test_bright_and_color
30
+ assert_equal "\e[1m\e[31mhello\e[0m", "hello".bright.color(:red)
31
+ end
32
+
33
+ def test_bright_and_background
34
+ assert_equal "\e[1m\e[44mhello\e[0m", "hello".bright.background(:blue)
35
+ end
36
+
37
+ def test_color_override
38
+ assert_equal "\e[31m\e[34m\e[33mhello\e[0m", "hello".color(:red).color(:blue).color(:yellow)
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rainbow
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Kulik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-21 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: ParseConfig provides simple parsing of standard *nix style config files.
17
+ email: sikkill@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - Changelog
27
+ - LICENSE
28
+ - lib/rainbow.rb
29
+ - test/rainbow_test.rb
30
+ has_rdoc: true
31
+ homepage: http://www.5dollarwhitebox.org/
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project: rainbow
52
+ rubygems_version: 1.2.0
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: Rainbow provides simple parsing of standard *nix style config files.
56
+ test_files: []
57
+