rainbow 1.99.0 → 2.0.0

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: 372e902e66070b47cdb7c31a2261929f57086e59
4
+ data.tar.gz: 57e6af68cbd87bf8c79298b9d6d77e0a46719dc5
5
5
  SHA512:
6
- metadata.gz: 7f487f330dd0cc1401f5b1172ac5e2eca94f54371f02ab79f5d12a5c23c2c3ee5938a9036875b53575721ecae9d33257676f3400f042894678d9beffe68c0df9
7
- data.tar.gz: c0b10c5d741321d8743f5b0a35feb3cc9ba3128dd5798462a6b89c273a6d3b725a1062a1fe725a87a2f394b7dd34e78189b9eb0304c8776efd2806ee2ce6413f
6
+ metadata.gz: 37394c88313337fb8fc04dc6d1f649eaea0c257d25a4e44e4cd1f54d07a87fb3cfce3673c86a33079468440fd040d21bf4c57d53fd4ef9d8a8a01169465236fc
7
+ data.tar.gz: a0a235ea3d51152d2a107435229730daa19f8d4a1f838803af6af87707a2e17fc2f48fb6195d78fab26acce0673aca33535a499c9ebc4feb5b64d6696edf63f2
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,67 @@
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`)
60
+
61
+ ## 1.99.2 (2014-01-24)
62
+
63
+ * bring back ruby 1.8 support
64
+
65
+ ## 2.0.0 (2014-01-24)
66
+
67
+ * disable string mixin by default
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'
@@ -12,6 +12,6 @@ group :guard do
12
12
  end
13
13
 
14
14
  platform :rbx do
15
+ gem 'json'
15
16
  gem 'rubysl'
16
- gem 'rubysl-json'
17
17
  end
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,88 @@ 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 1.x versions.
80
+ In rainbow 2.x the behavior was changed - if you're upgrading from 1.x to 2.x
81
+ and you used direct String methods then you can either require the string
82
+ extension as shown above or update your code to use the new presenter API.
55
83
 
56
84
  ### Color specification
57
85
 
58
- Both `foreground/color/colour` and `background` accept color specified in any
86
+ Both `color` and `background` accept color specified in any
59
87
  of the following ways:
60
88
 
61
- * color number (where 1 is red, 2 is green and so on):
62
- `Rainbow("hello").foreground(1)`
89
+ * color number (where 0 is black, 1 is red, 2 is green and so on):
90
+ `Rainbow("hello").color(1)`
63
91
 
64
- * color name as a symbol (available: :black, :red, :green, :yellow, :blue,
92
+ * color name as a symbol (:black, :red, :green, :yellow, :blue,
65
93
  :magenta, :cyan, :white):
66
- `Rainbow("hello").foreground(:yellow)`
94
+ `Rainbow("hello").color(:yellow)`.
95
+ This can be simplified to `Rainbow("hello").yellow`
67
96
 
68
97
  * RGB triplet as separate values in the range 0-255:
69
- `Rainbow("hello").foreground(115, 23, 98)`
98
+ `Rainbow("hello").color(115, 23, 98)`
70
99
 
71
100
  * RGB triplet as a hex string:
72
- `Rainbow("hello").foreground("FFC482")` or `Rainbow("hello").foreground("#FFC482")`
101
+ `Rainbow("hello").color("FFC482")` or `Rainbow("hello").color("#FFC482")`
73
102
 
74
103
  When you specify a color with a RGB triplet rainbow finds the nearest match
75
104
  from 256 colors palette. Note that it requires a 256-colors capable terminal to
76
105
  display correctly.
77
106
 
78
- ## Windows support
107
+ ### Configuration
79
108
 
80
- For Windows support, you should install the following gems:
109
+ Rainbow can be enabled/disabled globally by setting:
81
110
 
82
111
  ```ruby
83
- gem install windows-pr win32console
112
+ Rainbow.enabled = true/false
84
113
  ```
85
114
 
86
- If the above gems aren't installed then all strings are returned unmodified.
115
+ When disabled all the methods return an unmodified string
116
+ (`Rainbow("hello").red == "hello"`).
117
+
118
+ It's enabled by default, unless STDOUT/STDERR is not a TTY or a terminal is
119
+ dumb.
87
120
 
88
- ## Configuration
121
+ ### Advanced usage
89
122
 
90
- Rainbow can be enabled/disabled globally by setting:
123
+ `Rainbow()` and `Rainbow.enabled` operate on the global Rainbow wrapper
124
+ instance. If you would like to selectively enable/disable coloring in separate
125
+ parts of your application you can get a new Rainbow wrapper instance for each
126
+ of them and control the state of coloring during the runtime.
91
127
 
92
128
  ```ruby
93
- Rainbow.enabled = true/false
129
+ rainbow_one = Rainbow.new
130
+ rainbow_two = Rainbow.new
131
+
132
+ rainbow_one.enabled = false
133
+
134
+ Rainbow("hello").red # => "\e[31mhello\e[0m" ("hello" if not on TTY)
135
+ rainbow_one.wrap("hello").red # => "hello"
136
+ rainbow_two.wrap("hello").red # => "\e[31mhello\e[0m" ("hello" if not on TTY)
94
137
  ```
95
138
 
96
- When disabled all the methods return an unmodified string
97
- (`Rainbow("hello").color(:red) == "hello"`).
139
+ By default each new instance inherits enabled/disabled state from the global
140
+ `Rainbow.enabled`.
98
141
 
99
- It's enabled by default, unless STDOUT/STDERR is not a TTY or a terminal is
100
- dumb.
142
+ This feature comes handy for example when you have multiple output formatters
143
+ in your application and some of them print to a terminal but others write to a
144
+ file. Normally rainbow would detect that STDIN/STDERR is a TTY and would
145
+ colorize all the strings, even the ones that go through file writing
146
+ formatters. You can easily solve that by disabling coloring for the Rainbow
147
+ instances that are used by formatters with file output.
148
+
149
+ ## Windows support
150
+
151
+ For Windows support, you should install the following gems:
152
+
153
+ ```ruby
154
+ gem install windows-pr win32console
155
+ ```
156
+
157
+ If the above gems aren't installed then all strings are returned unmodified.
101
158
 
102
159
  ## Installation
103
160
 
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)
@@ -0,0 +1,21 @@
1
+ require 'rainbow/wrapper'
2
+
3
+ module Rainbow
4
+
5
+ def self.global
6
+ @global ||= Wrapper.new
7
+ end
8
+
9
+ def self.enabled
10
+ global.enabled
11
+ end
12
+
13
+ def self.enabled=(value)
14
+ global.enabled = value
15
+ end
16
+
17
+ end
18
+
19
+ def Rainbow(string)
20
+ Rainbow.global.wrap(string.to_s)
21
+ end
@@ -2,7 +2,8 @@ module Sickill
2
2
  module Rainbow
3
3
 
4
4
  def self.enabled=(value)
5
- STDERR.puts "Rainbow gem notice: Sickill::Rainbow.enabled= is deprecated, use Rainbow.enabled= instead."
5
+ STDERR.puts("Rainbow gem notice: Sickill::Rainbow.enabled= is " \
6
+ "deprecated, use Rainbow.enabled= instead.")
6
7
  ::Rainbow.enabled = value
7
8
  end
8
9
 
@@ -0,0 +1,30 @@
1
+ module Rainbow
2
+
3
+ class NullPresenter < ::String
4
+
5
+ def color(*values); self; end
6
+ def background(*values); self; end
7
+ def reset; self; end
8
+ def bright; self; end
9
+ def italic; self; end
10
+ def underline; self; end
11
+ def blink; self; end
12
+ def inverse; self; end
13
+ def hide; self; end
14
+
15
+ def black; self; end
16
+ def red; self; end
17
+ def green; self; end
18
+ def yellow; self; end
19
+ def blue; self; end
20
+ def magenta; self; end
21
+ def cyan; self; end
22
+ def white; self; end
23
+
24
+ alias_method :foreground, :color
25
+ alias_method :fg, :color
26
+ alias_method :bg, :background
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,113 @@
1
+ require 'rainbow/string_utils'
2
+ require 'rainbow/color'
3
+
4
+ module Rainbow
5
+
6
+ class Presenter < ::String
7
+
8
+ TERM_EFFECTS = {
9
+ reset: 0,
10
+ bright: 1,
11
+ italic: 3,
12
+ underline: 4,
13
+ blink: 5,
14
+ inverse: 7,
15
+ hide: 8,
16
+ }
17
+
18
+ # Sets color of this text.
19
+ def color(*values)
20
+ wrap_with_sgr(Color.build(:foreground, values).codes)
21
+ end
22
+
23
+ alias_method :foreground, :color
24
+ alias_method :fg, :color
25
+
26
+ # Sets background color of this text.
27
+ def background(*values)
28
+ wrap_with_sgr(Color.build(:background, values).codes)
29
+ end
30
+
31
+ alias_method :bg, :background
32
+
33
+ # Resets terminal to default colors/backgrounds.
34
+ #
35
+ # It shouldn't be needed to use this method because all methods
36
+ # append terminal reset code to end of string.
37
+ def reset
38
+ wrap_with_sgr(TERM_EFFECTS[:reset])
39
+ end
40
+
41
+ # Turns on bright/bold for this text.
42
+ def bright
43
+ wrap_with_sgr(TERM_EFFECTS[:bright])
44
+ end
45
+
46
+ # Turns on italic style for this text (not well supported by terminal
47
+ # emulators).
48
+ def italic
49
+ wrap_with_sgr(TERM_EFFECTS[:italic])
50
+ end
51
+
52
+ # Turns on underline decoration for this text.
53
+ def underline
54
+ wrap_with_sgr(TERM_EFFECTS[:underline])
55
+ end
56
+
57
+ # Turns on blinking attribute for this text (not well supported by terminal
58
+ # emulators).
59
+ def blink
60
+ wrap_with_sgr(TERM_EFFECTS[:blink])
61
+ end
62
+
63
+ # Inverses current foreground/background colors.
64
+ def inverse
65
+ wrap_with_sgr(TERM_EFFECTS[:inverse])
66
+ end
67
+
68
+ # Hides this text (set its color to the same as background).
69
+ def hide
70
+ wrap_with_sgr(TERM_EFFECTS[:hide])
71
+ end
72
+
73
+ def black
74
+ color(:black)
75
+ end
76
+
77
+ def red
78
+ color(:red)
79
+ end
80
+
81
+ def green
82
+ color(:green)
83
+ end
84
+
85
+ def yellow
86
+ color(:yellow)
87
+ end
88
+
89
+ def blue
90
+ color(:blue)
91
+ end
92
+
93
+ def magenta
94
+ color(:magenta)
95
+ end
96
+
97
+ def cyan
98
+ color(:cyan)
99
+ end
100
+
101
+ def white
102
+ color(:white)
103
+ end
104
+
105
+ private
106
+
107
+ def wrap_with_sgr(codes) #:nodoc:
108
+ self.class.new(StringUtils.wrap_with_sgr(self, [*codes]))
109
+ end
110
+
111
+ end
112
+
113
+ end
@@ -1,3 +1,3 @@
1
1
  module Rainbow
2
- VERSION = "1.99.0".freeze
2
+ VERSION = "2.0.0".freeze
3
3
  end