rainbow 1.1.4 → 1.99.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 820cb8349ec300d38fa0b456dfec3d4fe3399314
4
+ data.tar.gz: bc326589c9e418bb2dc1d13989d9931b8a7d548e
5
+ SHA512:
6
+ metadata.gz: 7f487f330dd0cc1401f5b1172ac5e2eca94f54371f02ab79f5d12a5c23c2c3ee5938a9036875b53575721ecae9d33257676f3400f042894678d9beffe68c0df9
7
+ data.tar.gz: c0b10c5d741321d8743f5b0a35feb3cc9ba3128dd5798462a6b89c273a6d3b725a1062a1fe725a87a2f394b7dd34e78189b9eb0304c8776efd2806ee2ce6413f
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ bundler_args: --without guard
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1.0
8
+ - rbx
9
+ - jruby
10
+
11
+ script: "bundle exec rake"
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rainbow.gemspec
4
+ gemspec
5
+
6
+ gem 'coveralls', :require => false
7
+ gem 'mime-types', '< 2.0.0', :platforms => [:ruby_18]
8
+
9
+ group :guard do
10
+ gem 'guard'
11
+ gem 'guard-rspec'
12
+ end
13
+
14
+ platform :rbx do
15
+ gem 'rubysl'
16
+ gem 'rubysl-json'
17
+ end
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec, :cmd => 'rspec --color' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/README.markdown CHANGED
@@ -1,75 +1,118 @@
1
- Rainbow
2
- =======
1
+ # Rainbow
3
2
 
4
- ![build status](https://secure.travis-ci.org/sickill/rainbow.png)
3
+ [![Build Status](https://travis-ci.org/sickill/rainbow.png?branch=master)](https://travis-ci.org/sickill/rainbow)
4
+ [![Code Climate](https://codeclimate.com/github/sickill/rainbow.png)](https://codeclimate.com/github/sickill/rainbow)
5
+ [![Coverage Status](https://coveralls.io/repos/sickill/rainbow/badge.png)](https://coveralls.io/r/sickill/rainbow)
5
6
 
6
- About
7
- -----
7
+ Rainbow is a ruby gem for colorizing printed text on ANSI terminals.
8
8
 
9
- Rainbow extends ruby String class adding methods to wrap the string with [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code).
9
+ It provides a string wrapper object that allows for wrapping a string with
10
+ [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code). These codes
11
+ when printed in a terminal change text attributes like text color, background,
12
+ intensity etc.
10
13
 
11
- Features
12
- --------
14
+ ## Example
13
15
 
14
- Rainbow adds following methods to String class:
16
+ ```ruby
17
+ require 'rainbow'
15
18
 
16
- * foreground(color) (with _color_ and _colour_ aliases)
17
- * background(color)
18
- * reset
19
- * bright
20
- * italic (not well supported by terminal emulators).
21
- * underline
22
- * blink
23
- * inverse
24
- * hide.
19
+ puts Rainbow("this is red").color(:red) + " and " + Rainbow("this on yellow bg").background(:yellow) + " and " + Rainbow("even bright underlined!").underline.bright
20
+ ```
25
21
 
26
- Each of those methods returns string wrapped with some ANSI codes so you can chain calls as in example above.
22
+ ## API
27
23
 
28
- Color can be one of following symbols:
24
+ Following methods are available on a string wrapper object:
29
25
 
30
- :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, :default
26
+ * `foreground(color)` (with `color` and `colour` aliases)
27
+ * `background(color)`
28
+ * `bright`
29
+ * `underline`
30
+ * `blink`
31
+ * `inverse`
32
+ * `hide`
33
+ * `italic` (not well supported by terminal emulators).
31
34
 
32
- If you have 256-colors capable terminal you can also specify color in RGB which will find the nearest match from 256 colors palette:
35
+ All of the methods return wrapped string so you can chain method calls as in
36
+ the above example.
33
37
 
34
- "Jolacz".color(115, 23, 98)
35
- "Jolacz".color("#FFC482")
36
- "Jolacz".color("FFC482")
38
+ ### String mixin
37
39
 
38
- For support on Windows, you should install the following gems:
40
+ If you prefer not to wrap every string you want to colorize with `Rainbow()`
41
+ you can include all the rainbow methods in a String class directly by requiring
42
+ `rainbow/string`:
39
43
 
40
- gem install windows-pr win32console
44
+ ```ruby
45
+ require 'rainbow/string'
41
46
 
42
- If the gems aren't installed strings are simply returned unaltered.
47
+ puts "this is red".color(:red) + " and " + "this on yellow bg".background(:yellow) + " and " + "even bright underlined!".underline.bright
48
+ ```
43
49
 
44
- Rainbow can be disabled globally by setting:
50
+ This way of using Rainbow is not recommended though as it pollutes String's
51
+ public interface with methods that are presentation specific.
45
52
 
46
- Sickill::Rainbow.enabled = false
53
+ NOTE: the mixin is included in String by default in rainbow 1.99 to not break
54
+ backwards compatibility. It won't be included by default in rainbow 2.0.
47
55
 
48
- It will be disabled by default if it detects that STDOUT is not a TTY.
56
+ ### Color specification
49
57
 
50
- Installation
51
- ------------
58
+ Both `foreground/color/colour` and `background` accept color specified in any
59
+ of the following ways:
52
60
 
53
- gem install rainbow
61
+ * color number (where 1 is red, 2 is green and so on):
62
+ `Rainbow("hello").foreground(1)`
54
63
 
55
- Usage
56
- -----
64
+ * color name as a symbol (available: :black, :red, :green, :yellow, :blue,
65
+ :magenta, :cyan, :white):
66
+ `Rainbow("hello").foreground(:yellow)`
57
67
 
58
- require 'rainbow'
59
- puts "this is red".foreground(:red) + " and " + "this on yellow bg".background(:yellow) + " and " + "even bright underlined!".underline.bright
68
+ * RGB triplet as separate values in the range 0-255:
69
+ `Rainbow("hello").foreground(115, 23, 98)`
60
70
 
61
- Rails Usage
62
- -----------
71
+ * RGB triplet as a hex string:
72
+ `Rainbow("hello").foreground("FFC482")` or `Rainbow("hello").foreground("#FFC482")`
63
73
 
64
- You're probably wanting to add colour to your logs. To do so you must explicity enable rainbow because it will detect that STDOUT (ie: the log file) is not a TTY.
65
- To make things easy, create the file `config/initializers/rainbow.rb` and include the following:
74
+ When you specify a color with a RGB triplet rainbow finds the nearest match
75
+ from 256 colors palette. Note that it requires a 256-colors capable terminal to
76
+ display correctly.
66
77
 
67
- require 'rainbow'
68
- Sickill::Rainbow.enabled = true
78
+ ## Windows support
69
79
 
70
- Authors
71
- -------
80
+ For Windows support, you should install the following gems:
72
81
 
73
- * Marcin Kulik
74
- * Xavier Nayrac
82
+ ```ruby
83
+ gem install windows-pr win32console
84
+ ```
75
85
 
86
+ If the above gems aren't installed then all strings are returned unmodified.
87
+
88
+ ## Configuration
89
+
90
+ Rainbow can be enabled/disabled globally by setting:
91
+
92
+ ```ruby
93
+ Rainbow.enabled = true/false
94
+ ```
95
+
96
+ When disabled all the methods return an unmodified string
97
+ (`Rainbow("hello").color(:red) == "hello"`).
98
+
99
+ It's enabled by default, unless STDOUT/STDERR is not a TTY or a terminal is
100
+ dumb.
101
+
102
+ ## Installation
103
+
104
+ Add it to your Gemfile:
105
+
106
+ ```ruby
107
+ gem 'rainbow'
108
+ ```
109
+
110
+ Or just install it via rubygems:
111
+
112
+ ```ruby
113
+ gem install rainbow
114
+ ```
115
+
116
+ ## Authors
117
+
118
+ [Marcin Kulik](http://ku1ik.com/) and [great open-source contributors](https://github.com/sickill/rainbow/graphs/contributors).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/lib/rainbow.rb CHANGED
@@ -1,108 +1,26 @@
1
- require 'rbconfig'
2
- require File.join(File.dirname(__FILE__), 'ansi_color')
3
-
4
- module Sickill
5
- module Rainbow
6
- class << self; attr_accessor :enabled; end
7
- @enabled = STDOUT.tty? && ENV['TERM'] != 'dumb' || ENV['CLICOLOR_FORCE'] == '1'
8
-
9
- TERM_COLORS = {
10
- :black => 0,
11
- :red => 1,
12
- :green => 2,
13
- :yellow => 3,
14
- :blue => 4,
15
- :magenta => 5,
16
- :cyan => 6,
17
- :white => 7,
18
- :default => 9,
19
- }
20
-
21
- TERM_EFFECTS = {
22
- :reset => 0,
23
- :bright => 1,
24
- :italic => 3,
25
- :underline => 4,
26
- :blink => 5,
27
- :inverse => 7,
28
- :hide => 8,
29
- }
30
-
31
- # Sets foreground color of this text.
32
- def foreground(*color)
33
- wrap_with_code(AnsiColor.new(:foreground, *color).code)
34
- end
35
- alias_method :color, :foreground
36
- alias_method :colour, :foreground
37
-
38
-
39
- # Sets background color of this text.
40
- def background(*color)
41
- wrap_with_code(AnsiColor.new(:background, *color).code)
42
- end
43
-
44
- # Resets terminal to default colors/backgrounds.
45
- #
46
- # It shouldn't be needed to use this method because all methods
47
- # append terminal reset code to end of string.
48
- def reset
49
- wrap_with_code(TERM_EFFECTS[:reset])
50
- end
51
-
52
- # Turns on bright/bold for this text.
53
- def bright
54
- wrap_with_code(TERM_EFFECTS[:bright])
55
- end
56
-
57
- # Turns on italic style for this text (not well supported by terminal
58
- # emulators).
59
- def italic
60
- wrap_with_code(TERM_EFFECTS[:italic])
61
- end
1
+ require 'rainbow/core'
2
+ require 'rainbow/wrapper'
3
+ require 'rainbow/string'
4
+ require 'rainbow/legacy'
62
5
 
63
- # Turns on underline decoration for this text.
64
- def underline
65
- wrap_with_code(TERM_EFFECTS[:underline])
66
- end
67
-
68
- # Turns on blinking attribute for this text (not well supported by terminal
69
- # emulators).
70
- def blink
71
- wrap_with_code(TERM_EFFECTS[:blink])
72
- end
73
-
74
- # Inverses current foreground/background colors.
75
- def inverse
76
- wrap_with_code(TERM_EFFECTS[:inverse])
77
- end
78
-
79
- # Hides this text (set its color to the same as background).
80
- def hide
81
- wrap_with_code(TERM_EFFECTS[:hide])
82
- end
83
-
84
- private
85
-
86
- def wrap_with_code(code) #:nodoc:
87
- return self unless Sickill::Rainbow.enabled
88
-
89
- var = self.dup
90
- matched = var.match(/^(\e\[([\d;]+)m)*/)
91
- var.insert(matched.end(0), "\e[#{code}m")
92
- var.concat("\e[0m") unless var =~ /\e\[0m$/
93
- var
94
- end
6
+ unless STDOUT.tty? && STDERR.tty?
7
+ Rainbow.enabled = false
8
+ end
95
9
 
96
- end
10
+ if ENV['TERM'] == 'dumb'
11
+ Rainbow.enabled = false
97
12
  end
98
13
 
99
- String.send(:include, Sickill::Rainbow)
14
+ if ENV['CLICOLOR_FORCE'] == '1'
15
+ Rainbow.enabled = true
16
+ end
100
17
 
101
18
  # On Windows systems, try to load the local ANSI support library
19
+ require 'rbconfig'
102
20
  if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
103
21
  begin
104
22
  require 'Win32/Console/ANSI'
105
23
  rescue LoadError
106
- Sickill::Rainbow.enabled = false
24
+ Rainbow.enabled = false
107
25
  end
108
26
  end
@@ -0,0 +1,109 @@
1
+ module Rainbow
2
+ class Color
3
+
4
+ attr_reader :ground
5
+
6
+ def self.build(ground, values)
7
+ raise ArgumentError.new(
8
+ "Wrong number of arguments for color definition, should be 1 or 3"
9
+ ) unless [1, 3].include?(values.size)
10
+
11
+ color = values.size == 1 ? values.first : values
12
+
13
+ case color
14
+ when ::Fixnum then Indexed.new(ground, color)
15
+ when ::Symbol then Named.new(ground, color)
16
+ when ::Array then RGB.new(ground, *color)
17
+ when ::String then RGB.new(ground, *parse_hex_color(color))
18
+ end
19
+ end
20
+
21
+ def self.parse_hex_color(hex)
22
+ hex = hex.gsub('#', '')
23
+ r = hex[0..1].to_i(16)
24
+ g = hex[2..3].to_i(16)
25
+ b = hex[4..5].to_i(16)
26
+
27
+ [r, g, b]
28
+ end
29
+
30
+ class Indexed < Color
31
+
32
+ attr_reader :num
33
+
34
+ def initialize(ground, num)
35
+ @ground = ground
36
+ @num = num
37
+ end
38
+
39
+ def codes
40
+ code = num + (ground == :foreground ? 30 : 40)
41
+
42
+ [code]
43
+ end
44
+
45
+ end
46
+
47
+ class Named < Indexed
48
+
49
+ NAMES = {
50
+ :black => 0,
51
+ :red => 1,
52
+ :green => 2,
53
+ :yellow => 3,
54
+ :blue => 4,
55
+ :magenta => 5,
56
+ :cyan => 6,
57
+ :white => 7,
58
+ :default => 9,
59
+ }
60
+
61
+ def initialize(ground, name)
62
+ raise ArgumentError.new(
63
+ "Unknown color name, valid names: #{color_names.join(', ')}"
64
+ ) unless color_names.include?(name)
65
+
66
+ super(ground, NAMES[name])
67
+ end
68
+
69
+ private
70
+
71
+ def color_names
72
+ NAMES.keys
73
+ end
74
+
75
+ end
76
+
77
+ class RGB < Indexed
78
+
79
+ attr_reader :r, :g, :b
80
+
81
+ def self.to_ansi_domain(value)
82
+ (6 * (value / 256.0)).to_i
83
+ end
84
+
85
+ def initialize(ground, *values)
86
+ raise ArgumentError.new(
87
+ "RGB value outside 0-255 range"
88
+ ) if values.min < 0 or values.max > 255
89
+
90
+ super(ground, 8)
91
+ @r, @g, @b = values
92
+ end
93
+
94
+ def codes
95
+ super + [5, code_from_rgb]
96
+ end
97
+
98
+ private
99
+
100
+ def code_from_rgb
101
+ 16 + self.class.to_ansi_domain(r) * 36 +
102
+ self.class.to_ansi_domain(g) * 6 +
103
+ self.class.to_ansi_domain(b)
104
+ end
105
+
106
+ end
107
+
108
+ end
109
+ end