rainbow 3.0.0 → 3.1.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.
@@ -1,135 +0,0 @@
1
- require_relative 'string_utils'
2
- require_relative 'x11_color_names'
3
- require_relative 'color'
4
-
5
- module Rainbow
6
- class Presenter < ::String
7
- TERM_EFFECTS = {
8
- reset: 0,
9
- bright: 1,
10
- faint: 2,
11
- italic: 3,
12
- underline: 4,
13
- blink: 5,
14
- inverse: 7,
15
- hide: 8
16
- }.freeze
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 foreground color
24
- alias 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 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
- alias bold bright
47
-
48
- # Turns on faint/dark for this text (not well supported by terminal
49
- # emulators).
50
- def faint
51
- wrap_with_sgr(TERM_EFFECTS[:faint])
52
- end
53
-
54
- alias dark faint
55
-
56
- # Turns on italic style for this text (not well supported by terminal
57
- # emulators).
58
- def italic
59
- wrap_with_sgr(TERM_EFFECTS[:italic])
60
- end
61
-
62
- # Turns on underline decoration for this text.
63
- def underline
64
- wrap_with_sgr(TERM_EFFECTS[:underline])
65
- end
66
-
67
- # Turns on blinking attribute for this text (not well supported by terminal
68
- # emulators).
69
- def blink
70
- wrap_with_sgr(TERM_EFFECTS[:blink])
71
- end
72
-
73
- # Inverses current foreground/background colors.
74
- def inverse
75
- wrap_with_sgr(TERM_EFFECTS[:inverse])
76
- end
77
-
78
- # Hides this text (set its color to the same as background).
79
- def hide
80
- wrap_with_sgr(TERM_EFFECTS[:hide])
81
- end
82
-
83
- def black
84
- color(:black)
85
- end
86
-
87
- def red
88
- color(:red)
89
- end
90
-
91
- def green
92
- color(:green)
93
- end
94
-
95
- def yellow
96
- color(:yellow)
97
- end
98
-
99
- def blue
100
- color(:blue)
101
- end
102
-
103
- def magenta
104
- color(:magenta)
105
- end
106
-
107
- def cyan
108
- color(:cyan)
109
- end
110
-
111
- def white
112
- color(:white)
113
- end
114
-
115
- # We take care of X11 color method call here.
116
- # Such as #aqua, #ghostwhite.
117
- def method_missing(method_name, *args)
118
- if Color::X11Named.color_names.include?(method_name) && args.empty?
119
- color(method_name)
120
- else
121
- super
122
- end
123
- end
124
-
125
- def respond_to_missing?(method_name, *args)
126
- Color::X11Named.color_names.include?(method_name) && args.empty? || super
127
- end
128
-
129
- private
130
-
131
- def wrap_with_sgr(codes) #:nodoc:
132
- self.class.new(StringUtils.wrap_with_sgr(self, [*codes]))
133
- end
134
- end
135
- end
@@ -1,12 +0,0 @@
1
- require_relative 'presenter'
2
- require_relative 'global'
3
-
4
- module Rainbow
5
- refine String do
6
- Presenter.instance_methods(false).each do |method_name|
7
- define_method(method_name) do |*args|
8
- ::Rainbow.global.wrap(self).send(method_name, *args)
9
- end
10
- end
11
- end
12
- end
@@ -1,21 +0,0 @@
1
- module Rainbow
2
- class StringUtils
3
- def self.wrap_with_sgr(string, codes)
4
- return string if codes.empty?
5
-
6
- seq = "\e[" + codes.join(";") + "m"
7
- match = string.match(/^(\e\[([\d;]+)m)*/)
8
- seq_pos = match.end(0)
9
- string = string[0...seq_pos] + seq + string[seq_pos..-1]
10
-
11
- string += "\e[0m" unless string =~ /\e\[0m$/
12
-
13
- string
14
- end
15
-
16
- def self.uncolor(string)
17
- # See http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
18
- string.gsub(/\e\[[0-9;]*m/, '')
19
- end
20
- end
21
- end
@@ -1,3 +0,0 @@
1
- module Rainbow
2
- VERSION = "3.0.0".freeze
3
- end
@@ -1,20 +0,0 @@
1
- require_relative 'presenter'
2
- require_relative 'null_presenter'
3
-
4
- module Rainbow
5
- class Wrapper
6
- attr_accessor :enabled
7
-
8
- def initialize(enabled = true)
9
- @enabled = enabled
10
- end
11
-
12
- def wrap(string)
13
- if enabled
14
- Presenter.new(string.to_s)
15
- else
16
- NullPresenter.new(string.to_s)
17
- end
18
- end
19
- end
20
- end
@@ -1,151 +0,0 @@
1
- module Rainbow
2
- module X11ColorNames
3
- NAMES = {
4
- aqua: [0, 255, 255],
5
- aquamarine: [127, 255, 212],
6
- mediumaquamarine: [102, 205, 170],
7
- azure: [240, 255, 255],
8
- beige: [245, 245, 220],
9
- bisque: [255, 228, 196],
10
- black: [0, 0, 0],
11
- blanchedalmond: [255, 235, 205],
12
- blue: [0, 0, 255],
13
- darkblue: [0, 0, 139],
14
- lightblue: [173, 216, 230],
15
- mediumblue: [0, 0, 205],
16
- aliceblue: [240, 248, 255],
17
- cadetblue: [95, 158, 160],
18
- dodgerblue: [30, 144, 255],
19
- midnightblue: [25, 25, 112],
20
- navyblue: [0, 0, 128],
21
- powderblue: [176, 224, 230],
22
- royalblue: [65, 105, 225],
23
- skyblue: [135, 206, 235],
24
- deepskyblue: [0, 191, 255],
25
- lightskyblue: [135, 206, 250],
26
- slateblue: [106, 90, 205],
27
- darkslateblue: [72, 61, 139],
28
- mediumslateblue: [123, 104, 238],
29
- steelblue: [70, 130, 180],
30
- lightsteelblue: [176, 196, 222],
31
- brown: [165, 42, 42],
32
- rosybrown: [188, 143, 143],
33
- saddlebrown: [139, 69, 19],
34
- sandybrown: [244, 164, 96],
35
- burlywood: [222, 184, 135],
36
- chartreuse: [127, 255, 0],
37
- chocolate: [210, 105, 30],
38
- coral: [255, 127, 80],
39
- lightcoral: [240, 128, 128],
40
- cornflower: [100, 149, 237],
41
- cornsilk: [255, 248, 220],
42
- crimson: [220, 20, 60],
43
- cyan: [0, 255, 255],
44
- darkcyan: [0, 139, 139],
45
- lightcyan: [224, 255, 255],
46
- firebrick: [178, 34, 34],
47
- fuchsia: [255, 0, 255],
48
- gainsboro: [220, 220, 220],
49
- gold: [255, 215, 0],
50
- goldenrod: [218, 165, 32],
51
- darkgoldenrod: [184, 134, 11],
52
- lightgoldenrod: [250, 250, 210],
53
- palegoldenrod: [238, 232, 170],
54
- gray: [190, 190, 190],
55
- darkgray: [169, 169, 169],
56
- dimgray: [105, 105, 105],
57
- lightgray: [211, 211, 211],
58
- slategray: [112, 128, 144],
59
- lightslategray: [119, 136, 153],
60
- webgray: [128, 128, 128],
61
- green: [0, 255, 0],
62
- darkgreen: [0, 100, 0],
63
- lightgreen: [144, 238, 144],
64
- palegreen: [152, 251, 152],
65
- darkolivegreen: [85, 107, 47],
66
- yellowgreen: [154, 205, 50],
67
- forestgreen: [34, 139, 34],
68
- lawngreen: [124, 252, 0],
69
- limegreen: [50, 205, 50],
70
- seagreen: [46, 139, 87],
71
- darkseagreen: [143, 188, 143],
72
- lightseagreen: [32, 178, 170],
73
- mediumseagreen: [60, 179, 113],
74
- springgreen: [0, 255, 127],
75
- mediumspringgreen: [0, 250, 154],
76
- webgreen: [0, 128, 0],
77
- honeydew: [240, 255, 240],
78
- indianred: [205, 92, 92],
79
- indigo: [75, 0, 130],
80
- ivory: [255, 255, 240],
81
- khaki: [240, 230, 140],
82
- darkkhaki: [189, 183, 107],
83
- lavender: [230, 230, 250],
84
- lavenderblush: [255, 240, 245],
85
- lemonchiffon: [255, 250, 205],
86
- lime: [0, 255, 0],
87
- linen: [250, 240, 230],
88
- magenta: [255, 0, 255],
89
- darkmagenta: [139, 0, 139],
90
- maroon: [176, 48, 96],
91
- webmaroon: [127, 0, 0],
92
- mintcream: [245, 255, 250],
93
- mistyrose: [255, 228, 225],
94
- moccasin: [255, 228, 181],
95
- oldlace: [253, 245, 230],
96
- olive: [128, 128, 0],
97
- olivedrab: [107, 142, 35],
98
- orange: [255, 165, 0],
99
- darkorange: [255, 140, 0],
100
- orchid: [218, 112, 214],
101
- darkorchid: [153, 50, 204],
102
- mediumorchid: [186, 85, 211],
103
- papayawhip: [255, 239, 213],
104
- peachpuff: [255, 218, 185],
105
- peru: [205, 133, 63],
106
- pink: [255, 192, 203],
107
- deeppink: [255, 20, 147],
108
- lightpink: [255, 182, 193],
109
- hotpink: [255, 105, 180],
110
- plum: [221, 160, 221],
111
- purple: [160, 32, 240],
112
- mediumpurple: [147, 112, 219],
113
- rebeccapurple: [102, 51, 153],
114
- webpurple: [127, 0, 127],
115
- red: [255, 0, 0],
116
- darkred: [139, 0, 0],
117
- orangered: [255, 69, 0],
118
- mediumvioletred: [199, 21, 133],
119
- palevioletred: [219, 112, 147],
120
- salmon: [250, 128, 114],
121
- darksalmon: [233, 150, 122],
122
- lightsalmon: [255, 160, 122],
123
- seashell: [255, 245, 238],
124
- sienna: [160, 82, 45],
125
- silver: [192, 192, 192],
126
- darkslategray: [47, 79, 79],
127
- snow: [255, 250, 250],
128
- tan: [210, 180, 140],
129
- teal: [0, 128, 128],
130
- thistle: [216, 191, 216],
131
- tomato: [255, 99, 71],
132
- turquoise: [64, 224, 208],
133
- darkturquoise: [0, 206, 209],
134
- mediumturquoise: [72, 209, 204],
135
- paleturquoise: [175, 238, 238],
136
- violet: [238, 130, 238],
137
- darkviolet: [148, 0, 211],
138
- blueviolet: [138, 43, 226],
139
- wheat: [245, 222, 179],
140
- white: [255, 255, 255],
141
- antiquewhite: [250, 235, 215],
142
- floralwhite: [255, 250, 240],
143
- ghostwhite: [248, 248, 255],
144
- navajowhite: [255, 222, 173],
145
- whitesmoke: [245, 245, 245],
146
- yellow: [255, 255, 0],
147
- lightyellow: [255, 255, 224],
148
- greenyellow: [173, 255, 47]
149
- }.freeze
150
- end
151
- end
data/lib/rainbow.rb DELETED
@@ -1,11 +0,0 @@
1
- require_relative 'rainbow/global'
2
-
3
- module Rainbow
4
- def self.new
5
- Wrapper.new(global.enabled)
6
- end
7
-
8
- self.enabled = false unless STDOUT.tty? && STDERR.tty?
9
- self.enabled = false if ENV['TERM'] == 'dumb'
10
- self.enabled = true if ENV['CLICOLOR_FORCE'] == '1'
11
- end
data/rainbow.gemspec DELETED
@@ -1,22 +0,0 @@
1
- lib = File.expand_path('../lib', __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require 'rainbow/version'
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "rainbow"
7
- spec.version = Rainbow::VERSION
8
- spec.authors = ["Marcin Kulik", "Olle Jonsson"]
9
- spec.email = ["m@ku1ik.com"]
10
- spec.description = 'Colorize printed text on ANSI terminals'
11
- spec.summary = 'Colorize printed text on ANSI terminals'
12
- spec.homepage = "https://github.com/sickill/rainbow"
13
- spec.license = "MIT"
14
- spec.required_ruby_version = '>= 2.1.0'
15
-
16
- spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
-
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- end
@@ -1,33 +0,0 @@
1
- require 'spec_helper'
2
- require 'rainbow'
3
-
4
- RSpec.describe 'Custom Rainbow instance' do
5
- it 'inherits enabled state from the global instance' do
6
- Rainbow.enabled = :yep
7
- expect(Rainbow.new.enabled).to eq(:yep)
8
- end
9
-
10
- it 'tracks its own state separately from the global instance' do
11
- Rainbow.enabled = :yep
12
- rainbow = Rainbow.new
13
- expect(Rainbow.new.enabled).to eq(:yep)
14
-
15
- rainbow.enabled = :nope
16
- expect(Rainbow.enabled).to eq(:yep)
17
- expect(rainbow.enabled).to eq(:nope)
18
- end
19
-
20
- it 'wraps string with escape codes when enabled' do
21
- rainbow = Rainbow.new
22
- rainbow.enabled = true
23
-
24
- expect(rainbow.wrap('hello').green).to eq("\e[32mhello\e[0m")
25
- end
26
-
27
- it "doesn't wrap string with any escape code when disabled" do
28
- rainbow = Rainbow.new
29
- rainbow.enabled = false
30
-
31
- expect(rainbow.wrap('hello').green).to eq('hello')
32
- end
33
- end
@@ -1,157 +0,0 @@
1
- require 'spec_helper'
2
- require 'rainbow'
3
-
4
- RSpec.describe 'Rainbow() wrapper' do
5
- before do
6
- Rainbow.enabled = true
7
- end
8
-
9
- it 'allows foreground coloring by color number' do
10
- result = Rainbow('hello').foreground(5)
11
- expect(result).to eq("\e[35mhello\e[0m")
12
- end
13
-
14
- it 'allows foreground coloring by color name' do
15
- result = Rainbow('hello').foreground(:red)
16
- expect(result).to eq("\e[31mhello\e[0m")
17
- end
18
-
19
- it 'allows foreground coloring directly by ANSI method name' do
20
- result = Rainbow('hello').red
21
- expect(result).to eq("\e[31mhello\e[0m")
22
- end
23
-
24
- it 'allows foreground coloring directly by X11 method name' do
25
- result = Rainbow('hello').midnightblue
26
- expect(result).to eq("\e[38;5;18mhello\e[0m")
27
- end
28
-
29
- it 'allows foreground coloring by ANSI color name (color alias)' do
30
- result = Rainbow('hello').color(:red)
31
- expect(result).to eq("\e[31mhello\e[0m")
32
- end
33
-
34
- it 'allows foreground coloring by color name (colour alias)' do
35
- result = Rainbow('hello').colour(:red)
36
- expect(result).to eq("\e[31mhello\e[0m")
37
- end
38
-
39
- it 'allows foreground coloring by X11 color name' do
40
- result = Rainbow('hello').foreground(:midnightblue)
41
- expect(result).to eq("\e[38;5;18mhello\e[0m")
42
- end
43
-
44
- it 'allows foreground coloring by color rgb' do
45
- result = Rainbow('hello').foreground(255, 128, 64)
46
- expect(result).to eq("\e[38;5;215mhello\e[0m")
47
- end
48
-
49
- it 'allows foreground coloring by color rgb (hex string)' do
50
- result = Rainbow('hello').foreground('ff8040')
51
- expect(result).to eq("\e[38;5;215mhello\e[0m")
52
- end
53
-
54
- it 'allows background coloring by color number' do
55
- result = Rainbow('hello').background(3)
56
- expect(result).to eq("\e[43mhello\e[0m")
57
- end
58
-
59
- it 'allows background coloring by ANSI color name' do
60
- result = Rainbow('hello').background(:green)
61
- expect(result).to eq("\e[42mhello\e[0m")
62
- end
63
-
64
- it 'allows background coloring by X11 color name' do
65
- result = Rainbow('hello').background(:midnightblue)
66
- expect(result).to eq("\e[48;5;18mhello\e[0m")
67
- end
68
-
69
- it 'allows background coloring by color rgb' do
70
- result = Rainbow('hello').background(255, 128, 64)
71
- expect(result).to eq("\e[48;5;215mhello\e[0m")
72
- end
73
-
74
- it 'allows making text bright' do
75
- result = Rainbow('hello').bright
76
- expect(result).to eq("\e[1mhello\e[0m")
77
- end
78
-
79
- it 'allows making text faint' do
80
- result = Rainbow('hello').faint
81
- expect(result).to eq("\e[2mhello\e[0m")
82
- end
83
-
84
- it 'allows making text italic' do
85
- result = Rainbow('hello').italic
86
- expect(result).to eq("\e[3mhello\e[0m")
87
- end
88
-
89
- it 'allows making text underlined' do
90
- result = Rainbow('hello').underline
91
- expect(result).to eq("\e[4mhello\e[0m")
92
- end
93
-
94
- it 'allows making text blinking' do
95
- result = Rainbow('hello').blink
96
- expect(result).to eq("\e[5mhello\e[0m")
97
- end
98
-
99
- it 'allows making text inversed' do
100
- result = Rainbow('hello').inverse
101
- expect(result).to eq("\e[7mhello\e[0m")
102
- end
103
-
104
- it 'allows making text hidden' do
105
- result = Rainbow('hello').hide
106
- expect(result).to eq("\e[8mhello\e[0m")
107
- end
108
-
109
- it 'allows resetting all the attributes' do
110
- result = Rainbow('hello').reset
111
- expect(result).to eq("\e[0mhello\e[0m")
112
- end
113
-
114
- it 'allows chaining' do
115
- result = Rainbow('hello')
116
- .foreground(:red)
117
- .bright
118
- .bold
119
- .faint
120
- .dark
121
- .italic
122
- .background('#ff8040')
123
- .underline
124
- .color(:blue)
125
- .blink
126
- .inverse
127
- .hide
128
-
129
- expect(result).to eq(
130
- "\e[31m\e[1m\e[1m\e[2m\e[2m\e[3m\e[48;5;215m\e[4m\e[34m\e[5m\e[7m\e[8mhello\e[0m"
131
- )
132
- end
133
-
134
- context "when Rainbow is disabled" do
135
- before do
136
- Rainbow.enabled = false
137
- end
138
-
139
- it "allows chaining but doesn't wrap with escape codes" do
140
- result = Rainbow('hello')
141
- .foreground(:red)
142
- .bright
143
- .bold
144
- .faint
145
- .dark
146
- .italic
147
- .background('#ff8040')
148
- .underline
149
- .color(:blue)
150
- .blink
151
- .inverse
152
- .hide
153
-
154
- expect(result).to eq('hello')
155
- end
156
- end
157
- end
@@ -1,36 +0,0 @@
1
- require 'spec_helper'
2
- require 'rainbow'
3
-
4
- RSpec.describe 'Rainbow refinement' do
5
- before do
6
- require 'rainbow/refinement'
7
-
8
- class ScopeNotIncluded
9
- def self.red_hello
10
- 'hello'.red
11
- end
12
- end
13
-
14
- class ScopeIncluded
15
- using Rainbow
16
- def self.red_hello
17
- 'hello'.red
18
- end
19
- end
20
- end
21
-
22
- it 'is not active by default' do
23
- expect do
24
- ScopeNotIncluded.red_hello
25
- end.to raise_error(NoMethodError)
26
- end
27
-
28
- it 'is available when used' do
29
- expect(ScopeIncluded.red_hello).to eq(Rainbow('hello').red)
30
- end
31
-
32
- it 'respects disabled state' do
33
- Rainbow.enabled = false
34
- expect(ScopeIncluded.red_hello).to eq('hello')
35
- end
36
- end
@@ -1,81 +0,0 @@
1
- require 'spec_helper'
2
- require 'rainbow/ext/string'
3
-
4
- RSpec.describe 'String mixin' do
5
- before do
6
- Rainbow.enabled = true
7
- end
8
-
9
- it 'proxies foreground to Rainbow().foreground' do
10
- expect('hello'.foreground(:red)).to eq(Rainbow('hello').foreground(:red))
11
- end
12
-
13
- it 'proxies color to Rainbow().color' do
14
- expect('hello'.color(:red)).to eq(Rainbow('hello').color(:red))
15
- end
16
-
17
- it 'proxies x11 color to Rainbow().color' do
18
- expect('hello'.color(:midnightblue)).to eq(Rainbow('hello').color(:midnightblue))
19
- end
20
-
21
- it 'proxies colour to Rainbow().colour' do
22
- expect('hello'.colour(:red)).to eq(Rainbow('hello').colour(:red))
23
- end
24
-
25
- it 'proxies background to Rainbow().background' do
26
- expect('hello'.background(:red)).to eq(Rainbow('hello').background(:red))
27
- end
28
-
29
- it 'proxies bright to Rainbow().bright' do
30
- expect('hello'.bright).to eq(Rainbow('hello').bright)
31
- end
32
-
33
- it 'proxies faint to Rainbow().faint' do
34
- expect('hello'.faint).to eq(Rainbow('hello').faint)
35
- end
36
-
37
- it 'proxies italic to Rainbow().italic' do
38
- expect('hello'.italic).to eq(Rainbow('hello').italic)
39
- end
40
-
41
- it 'proxies underline to Rainbow().underline' do
42
- expect('hello'.underline).to eq(Rainbow('hello').underline)
43
- end
44
-
45
- it 'proxies blink to Rainbow().blink' do
46
- expect('hello'.blink).to eq(Rainbow('hello').blink)
47
- end
48
-
49
- it 'proxies inverse to Rainbow().inverse' do
50
- expect('hello'.inverse).to eq(Rainbow('hello').inverse)
51
- end
52
-
53
- it 'proxies hide to Rainbow().hide' do
54
- expect('hello'.hide).to eq(Rainbow('hello').hide)
55
- end
56
-
57
- it 'proxies reset to Rainbow().reset' do
58
- expect('hello'.reset).to eq(Rainbow('hello').reset)
59
- end
60
-
61
- context "when Rainbow is disabled" do
62
- before do
63
- Rainbow.enabled = false
64
- end
65
-
66
- it "allows chaining but doesn't wrap with escape codes" do
67
- result = 'hello'
68
- .foreground(:red)
69
- .bright
70
- .italic
71
- .background('#ff8040')
72
- .underline
73
- .color(:blue)
74
- .blink
75
- .inverse
76
- .hide
77
-
78
- expect(result).to eq('hello')
79
- end
80
- end
81
- end