pastel 0.5.3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -7
- data/CHANGELOG.md +59 -10
- data/Gemfile +0 -1
- data/README.md +55 -19
- data/benchmarks/nesting_speed.rb +15 -0
- data/benchmarks/speed.rb +26 -2
- data/lib/pastel.rb +6 -2
- data/lib/pastel/alias_importer.rb +5 -4
- data/lib/pastel/ansi.rb +14 -0
- data/lib/pastel/color.rb +45 -73
- data/lib/pastel/color_parser.rb +117 -0
- data/lib/pastel/color_resolver.rb +3 -1
- data/lib/pastel/delegator.rb +4 -1
- data/lib/pastel/version.rb +1 -1
- data/pastel.gemspec +5 -4
- data/spec/unit/alias_color_spec.rb +1 -3
- data/spec/unit/alias_importer_spec.rb +8 -11
- data/spec/unit/color/alias_color_spec.rb +0 -2
- data/spec/unit/color/code_spec.rb +1 -3
- data/spec/unit/color/colored_spec.rb +1 -3
- data/spec/unit/color/decorate_spec.rb +5 -3
- data/spec/unit/color/equal_spec.rb +0 -2
- data/spec/unit/color/lookup_spec.rb +17 -0
- data/spec/unit/color/new_spec.rb +1 -15
- data/spec/unit/color/strip_spec.rb +3 -5
- data/spec/unit/color/styles_spec.rb +1 -3
- data/spec/unit/color/valid_spec.rb +0 -2
- data/spec/unit/color_parser_spec.rb +67 -0
- data/spec/unit/decorate_dsl_spec.rb +85 -0
- data/spec/unit/decorator_chain_spec.rb +0 -2
- data/spec/unit/delegator_spec.rb +0 -2
- data/spec/unit/detach_spec.rb +0 -2
- data/spec/unit/new_spec.rb +18 -80
- data/spec/unit/respond_to_spec.rb +0 -2
- data/spec/unit/undecorate_spec.rb +12 -0
- metadata +38 -10
- data/.ruby-gemset +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 391313f0e40b883c29f307c2484cabe9585f0e5a
|
4
|
+
data.tar.gz: 4ed8c3fce07de31b31b1d1ebe374d47c89f01ab5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 549b782b7c6c4db583298d765ee8d8bc07c42ad317153b5fd2450cc07734a4bfcc0cf4d5460cfa3978b1504c457ed25b218dae1dd73b9d0f2013021cddc0ab69
|
7
|
+
data.tar.gz: 75c0f16901dae62dbec9f3cd3790162a30a29ac0cffedd2dc930a75dd2d0d7bd57212dd669ae0f5b935ff36d3d6ced914fd25dfbbabb995c392ae5466776b2f5
|
data/.travis.yml
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
+
---
|
1
2
|
language: ruby
|
3
|
+
sudo: false
|
4
|
+
cache: bundler
|
2
5
|
bundler_args: --without yard benchmarks
|
3
6
|
script: "bundle exec rake ci"
|
4
7
|
rvm:
|
@@ -6,15 +9,12 @@ rvm:
|
|
6
9
|
- 2.0
|
7
10
|
- 2.1
|
8
11
|
- 2.2
|
12
|
+
- rbx-2
|
13
|
+
- jruby-19mode
|
14
|
+
- jruby
|
15
|
+
- jruby-head
|
9
16
|
- ruby-head
|
10
17
|
matrix:
|
11
|
-
include:
|
12
|
-
- rvm: jruby-19mode
|
13
|
-
- rvm: jruby-20mode
|
14
|
-
- rvm: jruby-21mode
|
15
|
-
- rvm: jruby-9000
|
16
|
-
- rvm: jruby-head
|
17
|
-
- rvm: rbx-2
|
18
18
|
allow_failures:
|
19
19
|
- rvm: ruby-head
|
20
20
|
- rvm: jruby-head
|
data/CHANGELOG.md
CHANGED
@@ -1,44 +1,93 @@
|
|
1
|
-
|
1
|
+
# Change log
|
2
2
|
|
3
|
+
## [v0.6.0] - 2016-01-15
|
4
|
+
|
5
|
+
### Added
|
6
|
+
* Add helper functions #foreground?, #backgroud?, #style to ANSI module
|
7
|
+
* Add ColorParser for parsing color symbols out of text
|
8
|
+
* Add Pastel#undecorate for parsing color names out of strings
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
* Change to use tty-color for color capabilities detection
|
12
|
+
* Change to move enabled option to Pastel#new
|
13
|
+
* Improve performance of Color#lookup
|
14
|
+
* Change Color#decorate performance to be 6x faster!
|
15
|
+
* Change Color DSL styling to be 3x faster!
|
16
|
+
|
17
|
+
### Fixed
|
18
|
+
* Fix #strip to only remove color sequences
|
19
|
+
* Fix #decorate to pass through original text when decorating without colors
|
20
|
+
* Fix #decorate to work correctly with nested background colors
|
21
|
+
|
22
|
+
## [v0.5.3] - 2015-01-05
|
23
|
+
|
24
|
+
### Fixed
|
3
25
|
* Change gemspec to fix dependencies requirement
|
4
26
|
|
5
|
-
|
27
|
+
## [v0.5.2] - 2015-11-27 (Nov 27, 2015)
|
6
28
|
|
7
29
|
* Change Color#decorate to accept non-string values and immediately return
|
8
30
|
|
9
|
-
|
31
|
+
## [v0.5.1] - 2015-09-18
|
10
32
|
|
33
|
+
### Added
|
11
34
|
* Add ability to call detached instance with array access
|
12
35
|
|
13
|
-
|
36
|
+
## [v0.5.0] - 2015-09-13
|
14
37
|
|
38
|
+
### Added
|
15
39
|
* Add external dependency to check for color support
|
16
40
|
* Add #colored? to check if string has color escape codes
|
17
41
|
* Add #eachline option to allow coloring of multiline strings
|
42
|
+
|
43
|
+
### Changed
|
18
44
|
* Further refine #strip method accuracy
|
45
|
+
|
46
|
+
### Fixed
|
19
47
|
* Fix redefining inspect method
|
20
48
|
* Fix string representation for pastel instance
|
21
49
|
|
22
|
-
|
50
|
+
## [v0.4.0] - 2014-11-22
|
23
51
|
|
24
|
-
|
52
|
+
### Added
|
25
53
|
* Add ability to #detach color combination for later reuse
|
26
54
|
* Add ability to nest styles with blocks
|
27
55
|
|
28
|
-
|
56
|
+
### Fixed
|
57
|
+
* Fix Delegator#respond_to method to correctly report existence of methods
|
58
|
+
|
59
|
+
## [v0.3.0] - 2014-11-08
|
29
60
|
|
61
|
+
### Added
|
30
62
|
* Add ability to alias colors through #alias_color method
|
31
63
|
* Add ability to alias colors through the environment variable
|
32
64
|
* Improve performance of Pastel::Color styles and lookup methods
|
65
|
+
|
66
|
+
### Fixed
|
33
67
|
* Fix bug concerned with lack of escaping for nested styles
|
34
68
|
|
35
|
-
|
69
|
+
## [v0.2.1] - 2014-10-13
|
36
70
|
|
71
|
+
### Fixed
|
37
72
|
* Fix issue #1 with unitialize dependency
|
38
73
|
|
39
|
-
|
74
|
+
## [v0.2.0] - 2014-10-12
|
40
75
|
|
41
|
-
|
76
|
+
### Added
|
42
77
|
* Add #supports? to Color to check for terminal color support
|
43
78
|
* Add ability to force color support through :enabled option
|
79
|
+
|
80
|
+
### Changed
|
81
|
+
* Change gemspec to include equatable as dependency
|
44
82
|
* Change Delegator to stop creating instances and improve performance
|
83
|
+
|
84
|
+
[v0.6.0]: https://github.com/peter-murach/pastel/compare/v0.5.3...v0.6.0
|
85
|
+
[v0.5.3]: https://github.com/peter-murach/pastel/compare/v0.5.2...v0.5.3
|
86
|
+
[v0.5.2]: https://github.com/peter-murach/pastel/compare/v0.5.1...v0.5.2
|
87
|
+
[v0.5.1]: https://github.com/peter-murach/pastel/compare/v0.5.0...v0.5.1
|
88
|
+
[v0.5.0]: https://github.com/peter-murach/pastel/compare/v0.4.0...v0.5.0
|
89
|
+
[v0.4.0]: https://github.com/peter-murach/pastel/compare/v0.3.0...v0.4.0
|
90
|
+
[v0.3.0]: https://github.com/peter-murach/pastel/compare/v0.2.1...v0.3.0
|
91
|
+
[v0.2.1]: https://github.com/peter-murach/pastel/compare/v0.2.0...v0.2.1
|
92
|
+
[v0.2.0]: https://github.com/peter-murach/pastel/compare/v0.1.0...v0.2.0
|
93
|
+
[v0.1.0]: https://github.com/peter-murach/pastel/compare/v0.1.0
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
[![Build Status](https://secure.travis-ci.org/peter-murach/pastel.svg?branch=master)][travis]
|
7
7
|
[![Code Climate](https://codeclimate.com/github/peter-murach/pastel/badges/gpa.svg)][codeclimate]
|
8
8
|
[![Coverage Status](https://coveralls.io/repos/peter-murach/pastel/badge.svg)][coverage]
|
9
|
-
[![Inline docs](http://inch-ci.org/github/peter-murach/
|
9
|
+
[![Inline docs](http://inch-ci.org/github/peter-murach/pastel.svg?branch=master)][inchpages]
|
10
10
|
|
11
11
|
[gem]: http://badge.fury.io/rb/pastel
|
12
12
|
[travis]: http://travis-ci.org/peter-murach/pastel
|
@@ -51,16 +51,19 @@ Or install it yourself as:
|
|
51
51
|
* [2. Interface](#2-interface)
|
52
52
|
* [2.1 Color](#21-color)
|
53
53
|
* [2.2 Decorate](#22-decorate)
|
54
|
-
* [2.3
|
55
|
-
* [2.4
|
56
|
-
* [2.5
|
57
|
-
* [2.6
|
58
|
-
* [2.7
|
59
|
-
* [2.8
|
60
|
-
* [2.9
|
61
|
-
* [2.10
|
54
|
+
* [2.3 Undecorate](#23-undecorate)
|
55
|
+
* [2.4 Detach](#24-detach)
|
56
|
+
* [2.5 Strip](#25-strip)
|
57
|
+
* [2.6 Styles](#26-styles)
|
58
|
+
* [2.7 Lookup](#27-lookup)
|
59
|
+
* [2.8 Valid?](#28-valid)
|
60
|
+
* [2.9 Colored?](#29-colored)
|
61
|
+
* [2.10 Enabled?](#30-enabled)
|
62
|
+
* [2.11 Eachline](#211-eachline)
|
63
|
+
* [2.12 Alias Color](#212-alias-color)
|
62
64
|
* [3. Supported Colors](#3-supported-colors)
|
63
65
|
* [4. Environment](#4-environment)
|
66
|
+
* [5. Command line](#5-command-line)
|
64
67
|
|
65
68
|
## 1 Usage
|
66
69
|
|
@@ -130,6 +133,12 @@ puts error.('Error!')
|
|
130
133
|
puts warning.('Warning')
|
131
134
|
```
|
132
135
|
|
136
|
+
**Pastel** has companion library called `pastel-cli` that allows you to style text in terminal via `pastel` executable:
|
137
|
+
|
138
|
+
```bash
|
139
|
+
$ pastel green 'Unicorns & rainbows!'
|
140
|
+
```
|
141
|
+
|
133
142
|
## 2 Interface
|
134
143
|
|
135
144
|
### 2.1 Color
|
@@ -154,7 +163,18 @@ pastel.decorate('Unicorn', :green, :on_blue, :bold)
|
|
154
163
|
|
155
164
|
This method will be useful in situations where colors are provided as a list of parameters that have been generated dynamically.
|
156
165
|
|
157
|
-
### 2.3
|
166
|
+
### 2.3 Undecorate
|
167
|
+
|
168
|
+
It performs the opposite to `decorate` method by turning color escape sequences found in the string into a list of hash objects corresponding with the attribute names set by those sequences. Depending on the parsed string, each hash object may contain `:foreground`, `:background`, `:text` and/or `:style` keys.
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
pastel.undecorate("\e[32mfoo\e[0m \e[31mbar\e[0m")
|
172
|
+
# => [{foreground: :green, text: 'foo'}, {text: ' '}, {foreground: :red, text: 'bar'}]
|
173
|
+
```
|
174
|
+
|
175
|
+
To translate the color name into sequence use [lookup](#27-lookup)
|
176
|
+
|
177
|
+
### 2.4 Detach
|
158
178
|
|
159
179
|
The `detach` method allows to keep all the associated colors with the detached instance for later reference. This method is useful when detached colors are being reused frequently and thus shorthand version is preferred. The detached object can be invoked using `call` method or it's shorthand `.()`, as well as array like access `[]`. For example, the following are equivalent examples of detaching colors:
|
160
180
|
|
@@ -166,15 +186,15 @@ notice.('Unicorns running')
|
|
166
186
|
notice['Unicorns running']
|
167
187
|
```
|
168
188
|
|
169
|
-
### 2.
|
189
|
+
### 2.5 Strip
|
170
190
|
|
171
191
|
Strip only color sequence characters from the provided strings and preserve any movement codes or other escape sequences. The return value will be either array of modified strings or a single string. The arguments are not modified.
|
172
192
|
|
173
193
|
```ruby
|
174
|
-
pastel.strip("\e[1m\e[34mbold blue text\e[0m") # => "
|
194
|
+
pastel.strip("\e[1A\e[1m\e[34mbold blue text\e[0m") # => "\e[1Abold blue text"
|
175
195
|
```
|
176
196
|
|
177
|
-
### 2.
|
197
|
+
### 2.6 Styles
|
178
198
|
|
179
199
|
To get a full list of supported styles with the corresponding color codes do:
|
180
200
|
|
@@ -182,7 +202,15 @@ To get a full list of supported styles with the corresponding color codes do:
|
|
182
202
|
pastel.styles
|
183
203
|
```
|
184
204
|
|
185
|
-
### 2.
|
205
|
+
### 2.7 Lookup
|
206
|
+
|
207
|
+
To perform translation of color name into ansi escape code use `lookup`:
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
color.lookup(:red) # => "\e[31m"
|
211
|
+
```
|
212
|
+
|
213
|
+
### 2.8 Valid?
|
186
214
|
|
187
215
|
Determine whether a color or a list of colors are valid. `valid?` takes one or more attribute strings or symbols and returns true if all attributes are known and false otherwise.
|
188
216
|
|
@@ -191,7 +219,7 @@ pastel.valid?(:red, :blue) # => true
|
|
191
219
|
pastel.valid?(:unicorn) # => false
|
192
220
|
```
|
193
221
|
|
194
|
-
### 2.
|
222
|
+
### 2.9 Colored?
|
195
223
|
|
196
224
|
In order to determine if string has color escape codes use `colored?` like so
|
197
225
|
|
@@ -199,7 +227,7 @@ In order to determine if string has color escape codes use `colored?` like so
|
|
199
227
|
pastel.colored?("\e[31mcolorful\e[0m") # => true
|
200
228
|
```
|
201
229
|
|
202
|
-
### 2.
|
230
|
+
### 2.10 Enabled?
|
203
231
|
|
204
232
|
In order to detect if your terminal supports coloring do:
|
205
233
|
|
@@ -214,7 +242,7 @@ pastel = Pastel.new(enabled: true)
|
|
214
242
|
pastel.enabled? # => true
|
215
243
|
```
|
216
244
|
|
217
|
-
### 2.
|
245
|
+
### 2.11 Eachline
|
218
246
|
|
219
247
|
Normally **Pastel** colors string by putting color codes at the beginning and end of the string, but if you provide `eachline` option set to some string, that string will be considered the line delimiter. Consequently, each line will be separately colored with escape sequence and reset code at the end. This option is desirable if the output string contains newlines and you're using background colors. Since color code that spans more than one line is often interpreted by terminal as providing background for all the lines that follow. This in turn may cause programs such as pagers to spill the colors throughout the text. In most cases you will want to set `eachline` to `\n` character like so:
|
220
248
|
|
@@ -223,7 +251,7 @@ pastel = Pastel.new(eachline: "\n")
|
|
223
251
|
pastel.red("foo\nbar") # => "\e[31mfoo\e[0m\n\e[31mbar\e[0m"
|
224
252
|
```
|
225
253
|
|
226
|
-
### 2.
|
254
|
+
### 2.12 Alias Color
|
227
255
|
|
228
256
|
In order to setup an alias for the standard color do:
|
229
257
|
|
@@ -310,6 +338,14 @@ Only alphanumeric and `_` are allowed in the alias names with the following form
|
|
310
338
|
PASTEL_COLORS_ALIASES='newcolor_1=red,newcolor_2=on_green'
|
311
339
|
```
|
312
340
|
|
341
|
+
## 5. Command line
|
342
|
+
|
343
|
+
You can also install [pastel-cli](https://github.com/peter-murach/pastel-cli) to use `pastel` executable in terminal:
|
344
|
+
|
345
|
+
```bash
|
346
|
+
$ pastel green 'Unicorns & rainbows!'
|
347
|
+
```
|
348
|
+
|
313
349
|
## Contributing
|
314
350
|
|
315
351
|
1. Fork it ( https://github.com/peter-murach/pastel/fork )
|
@@ -320,4 +356,4 @@ PASTEL_COLORS_ALIASES='newcolor_1=red,newcolor_2=on_green'
|
|
320
356
|
|
321
357
|
## Copyright
|
322
358
|
|
323
|
-
Copyright (c) 2014-
|
359
|
+
Copyright (c) 2014-2016 Piotr Murach. See LICENSE for further details.
|
data/benchmarks/nesting_speed.rb
CHANGED
@@ -22,5 +22,20 @@ Benchmark.ips do |bench|
|
|
22
22
|
bench.compare!
|
23
23
|
end
|
24
24
|
|
25
|
+
# version 0.6.0
|
26
|
+
|
27
|
+
# Calculating -------------------------------------
|
28
|
+
# regular nesting 1282 i/100ms
|
29
|
+
# block nesting 1013 i/100ms
|
30
|
+
# -------------------------------------------------
|
31
|
+
# regular nesting 13881.5 (±16.3%) i/s - 67946 in 5.043220s
|
32
|
+
# block nesting 11411.6 (±25.4%) i/s - 53689 in 5.088911s
|
33
|
+
#
|
34
|
+
# Comparison:
|
35
|
+
# regular nesting: 13881.5 i/s
|
36
|
+
# block nesting: 11411.6 i/s - 1.22x slower
|
37
|
+
|
38
|
+
# version 0.5.3
|
39
|
+
|
25
40
|
# regular nesting: 2800/s
|
26
41
|
# block nesting: 2600/s
|
data/benchmarks/speed.rb
CHANGED
@@ -17,5 +17,29 @@ Benchmark.ips do |bench|
|
|
17
17
|
bench.compare!
|
18
18
|
end
|
19
19
|
|
20
|
-
#
|
21
|
-
|
20
|
+
# version 0.6.0
|
21
|
+
|
22
|
+
# Calculating -------------------------------------
|
23
|
+
# color decorate 7346 i/100ms
|
24
|
+
# dsl styling 3436 i/100ms
|
25
|
+
# -------------------------------------------------
|
26
|
+
# color decorate 96062.1 (±7.9%) i/s - 484836 in 5.081126s
|
27
|
+
# dsl styling 38761.1 (±13.9%) i/s - 192416 in 5.065053s
|
28
|
+
#
|
29
|
+
# Comparison:
|
30
|
+
# color decorate: 96062.1 i/s
|
31
|
+
# dsl styling: 38761.1 i/s - 2.48x slower
|
32
|
+
|
33
|
+
# version 0.5.3
|
34
|
+
|
35
|
+
# Calculating -------------------------------------
|
36
|
+
# color decorate 1428 i/100ms
|
37
|
+
# dsl styling 1174 i/100ms
|
38
|
+
# -------------------------------------------------
|
39
|
+
# color decorate 16113.1 (±21.5%) i/s - 77112 in 5.054487s
|
40
|
+
# dsl styling 12622.9 (±20.8%) i/s - 61048 in 5.076738s
|
41
|
+
#
|
42
|
+
# Comparison:
|
43
|
+
# color decorate: 16113.1 i/s
|
44
|
+
# dsl styling: 12622.9 i/s - 1.28x slower
|
45
|
+
#
|
data/lib/pastel.rb
CHANGED
@@ -2,11 +2,12 @@
|
|
2
2
|
|
3
3
|
require 'forwardable'
|
4
4
|
require 'equatable'
|
5
|
-
require 'tty-
|
5
|
+
require 'tty-color'
|
6
6
|
|
7
7
|
require 'pastel/ansi'
|
8
8
|
require 'pastel/alias_importer'
|
9
9
|
require 'pastel/color'
|
10
|
+
require 'pastel/color_parser'
|
10
11
|
require 'pastel/color_resolver'
|
11
12
|
require 'pastel/delegator'
|
12
13
|
require 'pastel/detached'
|
@@ -29,8 +30,11 @@ module Pastel
|
|
29
30
|
#
|
30
31
|
# @api public
|
31
32
|
def new(options = {})
|
33
|
+
unless options.key?(:enabled)
|
34
|
+
options.merge!(enabled: TTY::Color.color?)
|
35
|
+
end
|
32
36
|
color = Color.new(options)
|
33
|
-
importer = AliasImporter.new(color)
|
37
|
+
importer = AliasImporter.new(color, ENV)
|
34
38
|
importer.import
|
35
39
|
resolver = ColorResolver.new(color)
|
36
40
|
Delegator.for(resolver, DecoratorChain.empty)
|
@@ -6,11 +6,12 @@ module Pastel
|
|
6
6
|
# Create alias importer
|
7
7
|
#
|
8
8
|
# @example
|
9
|
-
# importer = Pastel::AliasImporter.new(Pastel::Color.new)
|
9
|
+
# importer = Pastel::AliasImporter.new(Pastel::Color.new, {})
|
10
10
|
#
|
11
11
|
# @api public
|
12
|
-
def initialize(color, output = $stderr)
|
12
|
+
def initialize(color, env, output = $stderr)
|
13
13
|
@color = color
|
14
|
+
@env = env
|
14
15
|
@output = output
|
15
16
|
end
|
16
17
|
|
@@ -24,7 +25,7 @@ module Pastel
|
|
24
25
|
#
|
25
26
|
# @api public
|
26
27
|
def import
|
27
|
-
color_aliases =
|
28
|
+
color_aliases = env['PASTEL_COLORS_ALIASES']
|
28
29
|
return unless color_aliases
|
29
30
|
color_aliases.split(',').each do |color_alias|
|
30
31
|
new_color, old_color = color_alias.split('=').map(&:to_sym)
|
@@ -38,6 +39,6 @@ module Pastel
|
|
38
39
|
|
39
40
|
protected
|
40
41
|
|
41
|
-
attr_reader :color, :output
|
42
|
+
attr_reader :color, :output, :env
|
42
43
|
end # AliasImporter
|
43
44
|
end # Pastel
|
data/lib/pastel/ansi.rb
CHANGED
@@ -52,5 +52,19 @@ module Pastel
|
|
52
52
|
on_bright_cyan: 106,
|
53
53
|
on_bright_white: 107
|
54
54
|
}
|
55
|
+
|
56
|
+
module_function
|
57
|
+
|
58
|
+
def foreground?(code)
|
59
|
+
[*(30..37), *(90..97)].include?(code.to_i)
|
60
|
+
end
|
61
|
+
|
62
|
+
def background?(code)
|
63
|
+
[*(40..47), *(100..107)].include?(code.to_i)
|
64
|
+
end
|
65
|
+
|
66
|
+
def style?(code)
|
67
|
+
(1..9).include?(code.to_i)
|
68
|
+
end
|
55
69
|
end # ANSI
|
56
70
|
end # Pastel
|
data/lib/pastel/color.rb
CHANGED
@@ -6,9 +6,11 @@ module Pastel
|
|
6
6
|
include Equatable
|
7
7
|
include ANSI
|
8
8
|
|
9
|
+
# All color aliases
|
9
10
|
ALIASES = {}
|
10
11
|
|
11
|
-
|
12
|
+
# Match all color escape sequences
|
13
|
+
ANSI_COLOR_REGEXP = /\x1b+(\[|\[\[)[0-9;:?]+m/mo.freeze
|
12
14
|
|
13
15
|
BLANK_REGEX = /\A[[:space:]]*\z/o.freeze
|
14
16
|
|
@@ -21,9 +23,9 @@ module Pastel
|
|
21
23
|
#
|
22
24
|
# @api public
|
23
25
|
def initialize(options = {})
|
24
|
-
@enabled = options
|
26
|
+
@enabled = options[:enabled]
|
25
27
|
@eachline = options.fetch(:eachline) { false }
|
26
|
-
|
28
|
+
@cache = {}
|
27
29
|
end
|
28
30
|
|
29
31
|
# Disable coloring of this terminal session
|
@@ -35,6 +37,8 @@ module Pastel
|
|
35
37
|
|
36
38
|
# Apply ANSI color to the given string.
|
37
39
|
#
|
40
|
+
# Wraps eachline with clear escape.
|
41
|
+
#
|
38
42
|
# @param [String] string
|
39
43
|
# text to add ANSI strings
|
40
44
|
#
|
@@ -49,78 +53,39 @@ module Pastel
|
|
49
53
|
#
|
50
54
|
# @api public
|
51
55
|
def decorate(string, *colors)
|
52
|
-
return string if blank?(string) || !enabled
|
56
|
+
return string if blank?(string) || !enabled || colors.empty?
|
53
57
|
|
54
58
|
ansi_colors = lookup(*colors)
|
55
|
-
ansi_string = wrap_eachline(string, ansi_colors)
|
56
|
-
ansi_string = nest_color(collapse_reset(ansi_string), ansi_colors)
|
57
|
-
ansi_string
|
58
|
-
end
|
59
|
-
|
60
|
-
# Reset sequence
|
61
|
-
#
|
62
|
-
# @api public
|
63
|
-
def clear
|
64
|
-
lookup(:clear)
|
65
|
-
end
|
66
|
-
|
67
|
-
# Wraps eachline with clear character
|
68
|
-
#
|
69
|
-
# @param [String] string
|
70
|
-
# string to wrap with multiline characters
|
71
|
-
#
|
72
|
-
# @param [String] ansi_colors
|
73
|
-
# colors to apply to string
|
74
|
-
#
|
75
|
-
# @return [String]
|
76
|
-
#
|
77
|
-
# @api private
|
78
|
-
def wrap_eachline(string, ansi_colors)
|
79
59
|
if eachline
|
80
|
-
string.split(eachline).map do |line|
|
81
|
-
|
60
|
+
string.dup.split(eachline).map! do |line|
|
61
|
+
apply_codes(line, ansi_colors)
|
82
62
|
end.join(eachline)
|
83
63
|
else
|
84
|
-
|
64
|
+
apply_codes(string.dup, ansi_colors)
|
85
65
|
end
|
86
66
|
end
|
87
67
|
|
88
|
-
#
|
68
|
+
# Apply escape codes to the string
|
89
69
|
#
|
90
70
|
# @param [String] string
|
91
|
-
# the string to
|
71
|
+
# the string to apply escapes to
|
72
|
+
# @param [Strin] ansi_colors
|
73
|
+
# the ansi colors to apply
|
92
74
|
#
|
93
75
|
# @return [String]
|
76
|
+
# return the string surrounded by escape codes
|
94
77
|
#
|
95
78
|
# @api private
|
96
|
-
def
|
97
|
-
|
98
|
-
if ansi_string =~ /(#{Regexp.quote(clear)}){2,}/
|
99
|
-
ansi_string.gsub!(/(#{Regexp.quote(clear)}){2,}/, clear)
|
100
|
-
end
|
101
|
-
ansi_string
|
79
|
+
def apply_codes(string, ansi_colors)
|
80
|
+
"#{ansi_colors}#{string.gsub(/(\e\[0m)([^\e]+)$/, "\\1#{ansi_colors}\\2")}\e[0m"
|
102
81
|
end
|
103
82
|
|
104
|
-
#
|
105
|
-
#
|
106
|
-
# @param [String] string
|
107
|
-
# the string to decorate
|
108
|
-
#
|
109
|
-
# @param [String] ansi_colors
|
110
|
-
# the ansi colors to apply
|
111
|
-
#
|
112
|
-
# @return [String]
|
83
|
+
# Reset sequence
|
113
84
|
#
|
114
|
-
# @api
|
115
|
-
def
|
116
|
-
|
117
|
-
matches = ansi_string.scan(/#{Regexp.quote(clear)}/)
|
118
|
-
if matches.length > 1 && !eachline
|
119
|
-
ansi_string.sub!(/#{Regexp.quote(clear)}/, ansi_colors)
|
120
|
-
end
|
121
|
-
ansi_string
|
85
|
+
# @api public
|
86
|
+
def clear
|
87
|
+
lookup(:clear)
|
122
88
|
end
|
123
|
-
private :collapse_reset, :nest_color
|
124
89
|
|
125
90
|
# Strip ANSI color codes from a string.
|
126
91
|
#
|
@@ -137,7 +102,7 @@ module Pastel
|
|
137
102
|
#
|
138
103
|
# @api public
|
139
104
|
def strip(*strings)
|
140
|
-
modified = strings.map { |string| string.dup.gsub(
|
105
|
+
modified = strings.map { |string| string.dup.gsub(ANSI_COLOR_REGEXP, '') }
|
141
106
|
modified.size == 1 ? modified[0] : modified
|
142
107
|
end
|
143
108
|
|
@@ -151,7 +116,28 @@ module Pastel
|
|
151
116
|
#
|
152
117
|
# @api public
|
153
118
|
def colored?(string)
|
154
|
-
!
|
119
|
+
!ANSI_COLOR_REGEXP.match(string).nil?
|
120
|
+
end
|
121
|
+
|
122
|
+
# Find the escape code for a given set of color attributes
|
123
|
+
#
|
124
|
+
# @example
|
125
|
+
# color.lookup(:red, :on_green) # => "\e[31;42m"
|
126
|
+
#
|
127
|
+
# @param [Array[Symbol]] colors
|
128
|
+
# the list of color name(s) to lookup
|
129
|
+
#
|
130
|
+
# @return [String]
|
131
|
+
# the ANSI code(s)
|
132
|
+
#
|
133
|
+
# @raise [InvalidAttributeNameError]
|
134
|
+
# exception raised for any invalid color name
|
135
|
+
#
|
136
|
+
# @api private
|
137
|
+
def lookup(*colors)
|
138
|
+
@cache.fetch(colors) do
|
139
|
+
@cache[colors] = "\e[#{code(*colors).join(';')}m"
|
140
|
+
end
|
155
141
|
end
|
156
142
|
|
157
143
|
# Return raw color code without embeding it into a string.
|
@@ -173,20 +159,6 @@ module Pastel
|
|
173
159
|
attribute
|
174
160
|
end
|
175
161
|
|
176
|
-
# Find the escape code for color attribute.
|
177
|
-
#
|
178
|
-
# @param [Symbol,String] colors
|
179
|
-
# the color name(s) to lookup
|
180
|
-
#
|
181
|
-
# @return [String]
|
182
|
-
# the ANSI code(s)
|
183
|
-
#
|
184
|
-
# @api private
|
185
|
-
def lookup(*colors)
|
186
|
-
attribute = code(*colors)
|
187
|
-
"\e[#{attribute.join(';')}m"
|
188
|
-
end
|
189
|
-
|
190
162
|
# Expose all ANSI color names and their codes
|
191
163
|
#
|
192
164
|
# @return [Hash[Symbol]]
|