rainbow 1.99.0 → 1.99.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 820cb8349ec300d38fa0b456dfec3d4fe3399314
4
- data.tar.gz: bc326589c9e418bb2dc1d13989d9931b8a7d548e
3
+ metadata.gz: b0bc1801f960efaed6eabd94cd757e0cbe722652
4
+ data.tar.gz: 2dad299611e1e8624548ff9392edbf47e8387261
5
5
  SHA512:
6
- metadata.gz: 7f487f330dd0cc1401f5b1172ac5e2eca94f54371f02ab79f5d12a5c23c2c3ee5938a9036875b53575721ecae9d33257676f3400f042894678d9beffe68c0df9
7
- data.tar.gz: c0b10c5d741321d8743f5b0a35feb3cc9ba3128dd5798462a6b89c273a6d3b725a1062a1fe725a87a2f394b7dd34e78189b9eb0304c8776efd2806ee2ce6413f
6
+ metadata.gz: 0eeb4d8862a768369d6e3e957a9cffad58aa1e5bd259a6e04fdead2387f4ebc649e35181e4ce4fff2670849aa4a995819bd5dcb1d411f19e66bf534c04730863
7
+ data.tar.gz: 6fbcd166b88afa5ff0ade32320a9e8e3d0c381adbd56581452bdb31b5c19be21cc220d5b01a994e710deb6bbb78b07b82f51d07033daa04b1de1f97a0bdaf54f
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ Documentation:
2
+ Enabled: false
3
+
4
+ EmptyLinesAroundBody:
5
+ Enabled: false
6
+
7
+ HashSyntax:
8
+ Enabled: false
9
+
10
+ MethodName:
11
+ Enabled: false
12
+
13
+ StringLiterals:
14
+ Enabled: false
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  bundler_args: --without guard
2
2
  rvm:
3
- - 1.8.7
4
3
  - 1.9.2
5
4
  - 1.9.3
6
5
  - 2.0.0
data/Changelog.md ADDED
@@ -0,0 +1,59 @@
1
+ # Rainbow changelog
2
+
3
+ ## 1.0.0 (2008-7-21)
4
+
5
+ * initial version
6
+
7
+ ## 1.0.1 (2009-3-19)
8
+
9
+ * Windows support
10
+
11
+ ## 1.0.2 (2009-5-15)
12
+
13
+ * improved support for ruby 1.8.6 and 1.9.1
14
+
15
+ ## 1.0.3 (2009-7-26)
16
+
17
+ * rainbow methods don't mutate the original string object anymore
18
+
19
+ ## 1.0.4 (2009-11-27)
20
+
21
+ * support for 256 colors
22
+
23
+ ## 1.1 (2010-6-7)
24
+
25
+ * option for enabling/disabling of escape code wrapping
26
+ * auto-disabling when STDOUT is not a TTY
27
+
28
+ ## 1.1.1 (2011-2-7)
29
+
30
+ * improved Windows support
31
+
32
+ ## 1.1.2 (2011-11-13)
33
+
34
+ * improved compatibility with MRI 1.9.3
35
+
36
+ ## 1.1.3 (2011-12-6)
37
+
38
+ * improved compatibility with MRI 1.8.7
39
+ * fix for regression with regards to original string mutation
40
+
41
+ ## 1.1.4 (2012-4-28)
42
+
43
+ * option for forcing coloring even when STDOUT is not a TTY (CLICOLOR_FORCE env var)
44
+ * fix for frozen strings
45
+
46
+ ## 1.99.0 (2013-12-26)
47
+
48
+ * preparation for dropping String monkey patching
49
+ * `require "rainbow/string"` if you want to use monkey patched String
50
+ * introduction of Rainbow() wrapper
51
+ * support for MRI 1.8.7, 1.9.2, 1.9.3, 2.0 and 2.1, JRuby and Rubinius
52
+ * deprecation of Sickill::Rainbow namespace (use Rainbow.enabled = true instead)
53
+
54
+ ## 1.99.1 (2013-12-28)
55
+
56
+ * drop support for ruby 1.8
57
+ * `require "rainbow/string"` -> `require "rainbow/ext/string"`
58
+ * custom rainbow wrapper instances (with separate enabled/disabled state)
59
+ * shortcut methods for changing text color (`Rainbow("foo").red`)
data/Gemfile CHANGED
@@ -3,8 +3,8 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in rainbow.gemspec
4
4
  gemspec
5
5
 
6
- gem 'coveralls', :require => false
7
- gem 'mime-types', '< 2.0.0', :platforms => [:ruby_18]
6
+ gem 'coveralls', require: false
7
+ gem 'mime-types', '< 2.0.0', platforms: [:ruby_18]
8
8
 
9
9
  group :guard do
10
10
  gem 'guard'
data/Guardfile CHANGED
@@ -1,7 +1,7 @@
1
1
  # A sample Guardfile
2
2
  # More info at https://github.com/guard/guard#readme
3
3
 
4
- guard :rspec, :cmd => 'rspec --color' do
4
+ guard :rspec, cmd: 'rspec --color' do
5
5
  watch(%r{^spec/.+_spec\.rb$})
6
6
  watch(%r{^lib/(.+)\.rb$}) { "spec" }
7
7
  watch('spec/spec_helper.rb') { "spec" }
data/README.markdown CHANGED
@@ -6,25 +6,33 @@
6
6
 
7
7
  Rainbow is a ruby gem for colorizing printed text on ANSI terminals.
8
8
 
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,
9
+ It provides a string presenter object, which adds several methods to your
10
+ strings for wrapping them in [ANSI escape
11
+ codes](http://en.wikipedia.org/wiki/ANSI_escape_code). These codes when printed
12
+ in a terminal change text attributes like text color, background color,
12
13
  intensity etc.
13
14
 
14
- ## Example
15
+ ## Usage
16
+
17
+ To make your string colored wrap it with `Rainbow()` presenter and call
18
+ `.color(<color name>)` on it.
19
+
20
+ ### Example
15
21
 
16
22
  ```ruby
17
23
  require 'rainbow'
18
24
 
19
- puts Rainbow("this is red").color(:red) + " and " + Rainbow("this on yellow bg").background(:yellow) + " and " + Rainbow("even bright underlined!").underline.bright
25
+ p Rainbow("this is red").red + " and " + Rainbow("this on yellow bg").bg(:yellow) + " and " + Rainbow("even bright underlined!").underline.bright
26
+
27
+ # => "\e[31mthis is red\e[0m and \e[43mthis on yellow bg\e[0m and \e[4m\e[1meven bright underlined!\e[0m"
20
28
  ```
21
29
 
22
- ## API
30
+ ### Rainbow presenter API
23
31
 
24
- Following methods are available on a string wrapper object:
32
+ Rainbow presenter adds the following methods to presented string:
25
33
 
26
- * `foreground(color)` (with `color` and `colour` aliases)
27
- * `background(color)`
34
+ * `color(c)` (with `foreground`, and `fg` aliases)
35
+ * `background(c)` (with `bg` alias)
28
36
  * `bright`
29
37
  * `underline`
30
38
  * `blink`
@@ -32,17 +40,32 @@ Following methods are available on a string wrapper object:
32
40
  * `hide`
33
41
  * `italic` (not well supported by terminal emulators).
34
42
 
35
- All of the methods return wrapped string so you can chain method calls as in
36
- the above example.
43
+ Text color can also be changed by calling a method named by a color:
44
+
45
+ * `black`
46
+ * `red`
47
+ * `green`
48
+ * `yellow`
49
+ * `blue`
50
+ * `magenta`
51
+ * `cyan`
52
+ * `white`
53
+
54
+ All of the methods return `self` (the presenter object) so you can chain method
55
+ calls:
56
+
57
+ ```ruby
58
+ Rainbow("hola!").blue.bright.underline
59
+ ```
37
60
 
38
61
  ### String mixin
39
62
 
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`:
63
+ If you don't like wrapping every string you want to colorize with `Rainbow()`
64
+ you can include all the rainbow presenter methods directly in a String class by
65
+ requiring `rainbow/ext/string`:
43
66
 
44
67
  ```ruby
45
- require 'rainbow/string'
68
+ require 'rainbow/ext/string'
46
69
 
47
70
  puts "this is red".color(:red) + " and " + "this on yellow bg".background(:yellow) + " and " + "even bright underlined!".underline.bright
48
71
  ```
@@ -50,54 +73,87 @@ puts "this is red".color(:red) + " and " + "this on yellow bg".background(:yello
50
73
  This way of using Rainbow is not recommended though as it pollutes String's
51
74
  public interface with methods that are presentation specific.
52
75
 
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.
76
+ NOTE: the mixing doesn't include shortcut methods for changing text color, you
77
+ should use "string".color(:blue) instead of "string".blue
78
+
79
+ NOTE: the mixin is included in String by default in rainbow versions up to (and
80
+ including) 1.99.x to not break backwards compatibility. It won't be included by
81
+ default in rainbow 2.0.
55
82
 
56
83
  ### Color specification
57
84
 
58
- Both `foreground/color/colour` and `background` accept color specified in any
85
+ Both `color` and `background` accept color specified in any
59
86
  of the following ways:
60
87
 
61
- * color number (where 1 is red, 2 is green and so on):
62
- `Rainbow("hello").foreground(1)`
88
+ * color number (where 0 is black, 1 is red, 2 is green and so on):
89
+ `Rainbow("hello").color(1)`
63
90
 
64
- * color name as a symbol (available: :black, :red, :green, :yellow, :blue,
91
+ * color name as a symbol (:black, :red, :green, :yellow, :blue,
65
92
  :magenta, :cyan, :white):
66
- `Rainbow("hello").foreground(:yellow)`
93
+ `Rainbow("hello").color(:yellow)`.
94
+ This can be simplified to `Rainbow("hello").yellow`
67
95
 
68
96
  * RGB triplet as separate values in the range 0-255:
69
- `Rainbow("hello").foreground(115, 23, 98)`
97
+ `Rainbow("hello").color(115, 23, 98)`
70
98
 
71
99
  * RGB triplet as a hex string:
72
- `Rainbow("hello").foreground("FFC482")` or `Rainbow("hello").foreground("#FFC482")`
100
+ `Rainbow("hello").color("FFC482")` or `Rainbow("hello").color("#FFC482")`
73
101
 
74
102
  When you specify a color with a RGB triplet rainbow finds the nearest match
75
103
  from 256 colors palette. Note that it requires a 256-colors capable terminal to
76
104
  display correctly.
77
105
 
78
- ## Windows support
106
+ ### Configuration
79
107
 
80
- For Windows support, you should install the following gems:
108
+ Rainbow can be enabled/disabled globally by setting:
81
109
 
82
110
  ```ruby
83
- gem install windows-pr win32console
111
+ Rainbow.enabled = true/false
84
112
  ```
85
113
 
86
- If the above gems aren't installed then all strings are returned unmodified.
114
+ When disabled all the methods return an unmodified string
115
+ (`Rainbow("hello").red == "hello"`).
116
+
117
+ It's enabled by default, unless STDOUT/STDERR is not a TTY or a terminal is
118
+ dumb.
87
119
 
88
- ## Configuration
120
+ ### Advanced usage
89
121
 
90
- Rainbow can be enabled/disabled globally by setting:
122
+ `Rainbow()` and `Rainbow.enabled` operate on the global Rainbow wrapper
123
+ instance. If you would like to selectively enable/disable coloring in separate
124
+ parts of your application you can get a new Rainbow wrapper instance for each
125
+ of them and control the state of coloring during the runtime.
91
126
 
92
127
  ```ruby
93
- Rainbow.enabled = true/false
128
+ rainbow_one = Rainbow.new
129
+ rainbow_two = Rainbow.new
130
+
131
+ rainbow_one.enabled = false
132
+
133
+ Rainbow("hello").red # => "\e[31mhello\e[0m" ("hello" if not on TTY)
134
+ rainbow_one.wrap("hello").red # => "hello"
135
+ rainbow_two.wrap("hello").red # => "\e[31mhello\e[0m" ("hello" if not on TTY)
94
136
  ```
95
137
 
96
- When disabled all the methods return an unmodified string
97
- (`Rainbow("hello").color(:red) == "hello"`).
138
+ By default each new instance inherits enabled/disabled state from the global
139
+ `Rainbow.enabled`.
98
140
 
99
- It's enabled by default, unless STDOUT/STDERR is not a TTY or a terminal is
100
- dumb.
141
+ This feature comes handy for example when you have multiple output formatters
142
+ in your application and some of them print to a terminal but others write to a
143
+ file. Normally rainbow would detect that STDIN/STDERR is a TTY and would
144
+ colorize all the strings, even the ones that go through file writing
145
+ formatters. You can easily solve that by disabling coloring for the Rainbow
146
+ instances that are used by formatters with file output.
147
+
148
+ ## Windows support
149
+
150
+ For Windows support, you should install the following gems:
151
+
152
+ ```ruby
153
+ gem install windows-pr win32console
154
+ ```
155
+
156
+ If the above gems aren't installed then all strings are returned unmodified.
101
157
 
102
158
  ## Installation
103
159
 
data/lib/rainbow.rb CHANGED
@@ -1,26 +1,25 @@
1
- require 'rainbow/core'
2
- require 'rainbow/wrapper'
3
- require 'rainbow/string'
1
+ require 'rainbow/global'
4
2
  require 'rainbow/legacy'
3
+ require 'rainbow/ext/string'
5
4
 
6
- unless STDOUT.tty? && STDERR.tty?
7
- Rainbow.enabled = false
8
- end
5
+ module Rainbow
9
6
 
10
- if ENV['TERM'] == 'dumb'
11
- Rainbow.enabled = false
12
- end
7
+ def self.new
8
+ Wrapper.new(global.enabled)
9
+ end
13
10
 
14
- if ENV['CLICOLOR_FORCE'] == '1'
15
- Rainbow.enabled = true
16
- end
11
+ self.enabled = false unless STDOUT.tty? && STDERR.tty?
12
+ self.enabled = false if ENV['TERM'] == 'dumb'
13
+ self.enabled = true if ENV['CLICOLOR_FORCE'] == '1'
17
14
 
18
- # On Windows systems, try to load the local ANSI support library
19
- require 'rbconfig'
20
- if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
21
- begin
22
- require 'Win32/Console/ANSI'
23
- rescue LoadError
24
- Rainbow.enabled = false
15
+ # On Windows systems, try to load the local ANSI support library
16
+ require 'rbconfig'
17
+ if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
18
+ begin
19
+ require 'Win32/Console/ANSI'
20
+ rescue LoadError
21
+ self.enabled = false
22
+ end
25
23
  end
24
+
26
25
  end
data/lib/rainbow/color.rb CHANGED
@@ -4,17 +4,22 @@ module Rainbow
4
4
  attr_reader :ground
5
5
 
6
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)
7
+ unless [1, 3].include?(values.size)
8
+ fail ArgumentError,
9
+ "Wrong number of arguments for color definition, should be 1 or 3"
10
+ end
10
11
 
11
12
  color = values.size == 1 ? values.first : values
12
13
 
13
14
  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))
15
+ when ::Fixnum
16
+ Indexed.new(ground, color)
17
+ when ::Symbol
18
+ Named.new(ground, color)
19
+ when ::Array
20
+ RGB.new(ground, *color)
21
+ when ::String
22
+ RGB.new(ground, *parse_hex_color(color))
18
23
  end
19
24
  end
20
25
 
@@ -47,21 +52,22 @@ module Rainbow
47
52
  class Named < Indexed
48
53
 
49
54
  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,
55
+ black: 0,
56
+ red: 1,
57
+ green: 2,
58
+ yellow: 3,
59
+ blue: 4,
60
+ magenta: 5,
61
+ cyan: 6,
62
+ white: 7,
63
+ default: 9,
59
64
  }
60
65
 
61
66
  def initialize(ground, name)
62
- raise ArgumentError.new(
63
- "Unknown color name, valid names: #{color_names.join(', ')}"
64
- ) unless color_names.include?(name)
67
+ unless color_names.include?(name)
68
+ fail ArgumentError,
69
+ "Unknown color name, valid names: #{color_names.join(', ')}"
70
+ end
65
71
 
66
72
  super(ground, NAMES[name])
67
73
  end
@@ -83,9 +89,9 @@ module Rainbow
83
89
  end
84
90
 
85
91
  def initialize(ground, *values)
86
- raise ArgumentError.new(
87
- "RGB value outside 0-255 range"
88
- ) if values.min < 0 or values.max > 255
92
+ if values.min < 0 || values.max > 255
93
+ fail ArgumentError, "RGB value outside 0-255 range"
94
+ end
89
95
 
90
96
  super(ground, 8)
91
97
  @r, @g, @b = values
@@ -0,0 +1,52 @@
1
+ require 'rainbow'
2
+
3
+ module Rainbow
4
+ module Ext
5
+ module String
6
+ module InstanceMethods
7
+
8
+ def foreground(*color)
9
+ Rainbow(self).foreground(*color)
10
+ end
11
+
12
+ alias_method :color, :foreground
13
+ alias_method :colour, :foreground
14
+
15
+ def background(*color)
16
+ Rainbow(self).background(*color)
17
+ end
18
+
19
+ def reset
20
+ Rainbow(self).reset
21
+ end
22
+
23
+ def bright
24
+ Rainbow(self).bright
25
+ end
26
+
27
+ def italic
28
+ Rainbow(self).italic
29
+ end
30
+
31
+ def underline
32
+ Rainbow(self).underline
33
+ end
34
+
35
+ def blink
36
+ Rainbow(self).blink
37
+ end
38
+
39
+ def inverse
40
+ Rainbow(self).inverse
41
+ end
42
+
43
+ def hide
44
+ Rainbow(self).hide
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ ::String.send(:include, Rainbow::Ext::String::InstanceMethods)