colorize 0.8.1 → 1.0.0.pre.beta.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +107 -0
- data/README.md +51 -4
- data/Rakefile +2 -0
- data/colorize.gemspec +19 -9
- data/lib/colorize/class_methods.rb +117 -34
- data/lib/colorize/errors.rb +9 -0
- data/lib/colorize/instance_methods.rb +24 -12
- data/lib/colorize/version.rb +8 -0
- data/lib/colorize.rb +8 -0
- data/lib/colorized_string.rb +7 -1
- data/test/test_colorize.rb +110 -64
- data/test/test_colorized_string.rb +214 -0
- metadata +89 -17
- data/CHANGELOG +0 -65
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e6cf67e45a9d7d3a06cab45d8588ebc1bcdbcb25f60700bc8b891deaa47aa4ee
|
4
|
+
data.tar.gz: bbee1cd60a0ff68de06a0febcb50cf6655304ed81354dcbde88538f7ea15266f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1cd49dd178b7c9fa10f1b1d1ce0b417ccc26ca19474883900ccf8a454c23f7d8f421799c05d37f6e88909a6dd7fedfe819788c9cf26173464d0a30c0cd051727
|
7
|
+
data.tar.gz: cc199a5456e868009843681eb7cd0c4012a14e654fa2d8cc6701cf2e7ac8e0bc9de96ba12d0a51d610bc903d9399c18ba50b9985930df2de910d6da60b1c4f13
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## [1.0.0-beta.3](https://github.com/fazibear/colorize/compare/v1.0.0-beta.2...v1.0.0-beta.3) (2023-03-23)
|
4
|
+
|
5
|
+
|
6
|
+
### Miscellaneous Chores
|
7
|
+
|
8
|
+
* release 1.0.0-beta.2 ([45c3dd6](https://github.com/fazibear/colorize/commit/45c3dd6f503d5e5588f3f7598019dc7a53adac5e))
|
9
|
+
* release 1.0.0-beta.3 ([2eb8f9d](https://github.com/fazibear/colorize/commit/2eb8f9d8c486722b66acdc525653cdef3cb9b163))
|
10
|
+
|
11
|
+
## [1.0.0-beta.2](https://github.com/fazibear/colorize/compare/v1.0.0-beta.2...v1.0.0-beta.2) (2023-03-23)
|
12
|
+
|
13
|
+
|
14
|
+
### Miscellaneous Chores
|
15
|
+
|
16
|
+
* release 1.0.0-beta.2 ([45c3dd6](https://github.com/fazibear/colorize/commit/45c3dd6f503d5e5588f3f7598019dc7a53adac5e))
|
17
|
+
|
18
|
+
## [1.0.0-beta.2](https://github.com/fazibear/colorize/compare/v1.0.0-beta.1...v1.0.0-beta.2) (2023-03-23)
|
19
|
+
|
20
|
+
|
21
|
+
### Miscellaneous Chores
|
22
|
+
|
23
|
+
* release 1.0.0-beta.2 ([29ed0c5](https://github.com/fazibear/colorize/commit/29ed0c5ae97a9a66e4af7e376e2b2a937eaf024d))
|
24
|
+
|
25
|
+
## [1.0.0-beta.1](https://github.com/fazibear/colorize/compare/v0.8.1...v1.0.0-beta.1) (2023-03-23)
|
26
|
+
|
27
|
+
|
28
|
+
### Miscellaneous Chores
|
29
|
+
|
30
|
+
* release 1.0.0-beta.1 ([6303e25](https://github.com/fazibear/colorize/commit/6303e257690cfa7b3efbf11993a4cd75418b5d5e))
|
31
|
+
* release 1.0.0.beta ([fbb2cb2](https://github.com/fazibear/colorize/commit/fbb2cb25ff17e9f46ca7fc9b8173414b0f8df7db))
|
32
|
+
* release 1.0.0.beta ([f8cb1e4](https://github.com/fazibear/colorize/commit/f8cb1e4033150143918f45663b74c5a574ca372b))
|
33
|
+
|
34
|
+
== 1.0.0 / 2023-03-23
|
35
|
+
* add more modes
|
36
|
+
* add aliases
|
37
|
+
* grey and gray default aliases
|
38
|
+
* update dependencies
|
39
|
+
* add non-visible text delimiters
|
40
|
+
* add prevent_color switch
|
41
|
+
* cleanup and refactor
|
42
|
+
|
43
|
+
== 0.8.1 / 2016-06-29
|
44
|
+
* fix gemspec bug
|
45
|
+
|
46
|
+
== 0.8.0 / 2016-06-27
|
47
|
+
* add ColorizedString class
|
48
|
+
* update README file
|
49
|
+
* add rubocop.yml and follow style
|
50
|
+
* add italic mode
|
51
|
+
* remove interpreter warrnings
|
52
|
+
|
53
|
+
== 0.7.7 / 2015-04-19
|
54
|
+
* update gems
|
55
|
+
|
56
|
+
== 0.7.6 / 2015-04-18
|
57
|
+
* fix bugs
|
58
|
+
|
59
|
+
== 0.7.5 / 2014-12-11
|
60
|
+
* big code refactoring
|
61
|
+
* disable_colorization feature added
|
62
|
+
|
63
|
+
== 0.7.4 / 2014-12-10
|
64
|
+
* code cleanups
|
65
|
+
|
66
|
+
== 0.7.3 / 2014-05-19
|
67
|
+
* fix new line maching
|
68
|
+
|
69
|
+
== 0.7.2 / 2014-04-08
|
70
|
+
* tests cleanups
|
71
|
+
* gem release date fixed
|
72
|
+
|
73
|
+
== 0.7.1 / 2014-04-02
|
74
|
+
* handling wrong color values
|
75
|
+
|
76
|
+
== 0.7.0 / 2014-03-12
|
77
|
+
* refactored to use regexp pattern matching
|
78
|
+
* works with frozen strings
|
79
|
+
* works with concatenated string
|
80
|
+
|
81
|
+
== 0.6.0 / 2013-09-25
|
82
|
+
* code cleanups
|
83
|
+
* bold mode fixed
|
84
|
+
* STDOUT.isatty condition removed
|
85
|
+
* doc updated
|
86
|
+
* jeweler tasks removed
|
87
|
+
* Rakefile updated
|
88
|
+
|
89
|
+
== 0.5.8 / 2009-12-02
|
90
|
+
* code cleanups
|
91
|
+
* removed 'ruby -w' warnings
|
92
|
+
|
93
|
+
== 0.5.7 / 2009-11-24
|
94
|
+
* moved to github/gemcutter
|
95
|
+
* 1.9 compatible
|
96
|
+
|
97
|
+
== 0.5.6 / 2007-08-27
|
98
|
+
* rdocs Allison template
|
99
|
+
* cleanups
|
100
|
+
|
101
|
+
== 0.5.5 / 2007-08-11
|
102
|
+
* added effects methods
|
103
|
+
* README files
|
104
|
+
* new rake file
|
105
|
+
|
106
|
+
== 0.5.0 / 2007-04-22
|
107
|
+
* initial rewritten version
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
colorize [![Gem Version](https://badge.fury.io/rb/colorize.svg)](http://badge.fury.io/rb/colorize) [![
|
1
|
+
colorize [![Gem Version](https://badge.fury.io/rb/colorize.svg)](http://badge.fury.io/rb/colorize) [![Ruby Gem](https://github.com/fazibear/colorize/actions/workflows/gem-push.yml/badge.svg)](https://github.com/fazibear/colorize/actions/workflows/gem-push.yml) [![Code Climate](https://codeclimate.com/github/fazibear/colorize/badges/gpa.svg)](https://codeclimate.com/github/fazibear/colorize) [![Test Coverage](https://codeclimate.com/github/fazibear/colorize/badges/coverage.svg)](https://codeclimate.com/github/fazibear/colorize)
|
2
2
|
========
|
3
3
|
|
4
4
|
Ruby gem for colorizing text using ANSI escape sequences.
|
5
|
-
Extends `String` class or add a `ColorizedString` with methods to set text color, background color and text effects.
|
5
|
+
Extends `String` class or add a `ColorizedString` with methods to set the text color, background color and text effects.
|
6
6
|
|
7
7
|
modes
|
8
8
|
-----
|
@@ -16,8 +16,10 @@ features
|
|
16
16
|
* change string color
|
17
17
|
* change string background
|
18
18
|
* change string effect
|
19
|
+
* chain methods
|
19
20
|
* display color samples
|
20
21
|
* disable colorization
|
22
|
+
* prevent colors
|
21
23
|
|
22
24
|
usage
|
23
25
|
-----
|
@@ -29,20 +31,40 @@ String.colors # return array of all possible colors names
|
|
29
31
|
String.modes # return array of all possible modes
|
30
32
|
String.color_samples # displays color samples in all combinations
|
31
33
|
String.disable_colorization # check if colorization is disabled
|
34
|
+
|
35
|
+
# disable colorization (false by default)
|
36
|
+
|
32
37
|
String.disable_colorization = false # enable colorization
|
33
38
|
String.disable_colorization false # enable colorization
|
34
39
|
String.disable_colorization = true # disable colorization
|
35
40
|
String.disable_colorization true # disable colorization
|
36
41
|
|
42
|
+
# prevent colors (false by default)
|
43
|
+
|
44
|
+
String.prevent_colors = false # override current string colors
|
45
|
+
String.prevent_colors false # override current string colors
|
46
|
+
String.prevent_colors = true # don't colorize colorized strings
|
47
|
+
String.prevent_colors true # don't colorize colorized strings
|
48
|
+
|
49
|
+
# adding aliases (:gray and :grey added by default)
|
50
|
+
|
51
|
+
String.add_color_alias(:niebieski, :blue)
|
52
|
+
String.add_color_alias(:zielony => :green)
|
53
|
+
String.add_color_alias(czarny: :black)
|
54
|
+
String.add_color_alias(czerwony: :red, granatowy: :blue)
|
55
|
+
|
56
|
+
# examaples
|
57
|
+
|
37
58
|
puts "This is blue".colorize(:blue)
|
38
59
|
puts "This is light blue".colorize(:light_blue)
|
39
60
|
puts "This is also blue".colorize(:color => :blue)
|
61
|
+
puts "This is bold green".colorize(:color => :green, :mode => :bold)
|
40
62
|
puts "This is light blue with red background".colorize(:color => :light_blue, :background => :red)
|
41
63
|
puts "This is light blue with red background".colorize(:light_blue ).colorize( :background => :red)
|
42
64
|
puts "This is blue text on red".blue.on_red
|
43
65
|
puts "This is red on blue".colorize(:red).on_blue
|
44
66
|
puts "This is red on blue and underline".colorize(:red).on_blue.underline
|
45
|
-
puts "This is blue text on red".blue.on_red.blink
|
67
|
+
puts "This is blinking blue text on red".blue.on_red.blink
|
46
68
|
puts "This is uncolorized".blue.on_red.uncolorize
|
47
69
|
```
|
48
70
|
|
@@ -53,20 +75,40 @@ ColorizedString.colors # return array of all possible colo
|
|
53
75
|
ColorizedString.modes # return array of all possible modes
|
54
76
|
ColorizedString.color_samples # displays color samples in all combinations
|
55
77
|
ColorizedString.disable_colorization # check if colorization is disabled
|
78
|
+
|
79
|
+
# disable colorization (false by default)
|
80
|
+
|
56
81
|
ColorizedString.disable_colorization = false # enable colorization
|
57
82
|
ColorizedString.disable_colorization false # enable colorization
|
58
83
|
ColorizedString.disable_colorization = true # disable colorization
|
59
84
|
ColorizedString.disable_colorization true # disable colorization
|
60
85
|
|
86
|
+
# prevent colors (false by default)
|
87
|
+
|
88
|
+
ColorizedString.prevent_colors = false # override current string colors
|
89
|
+
ColorizedString.prevent_colors false # override current string colors
|
90
|
+
ColorizedString.prevent_colors = true # don't colorize colorized strings
|
91
|
+
ColorizedString.prevent_colors true # don't colorize colorized strings
|
92
|
+
|
93
|
+
# adding aliases (:gray and :grey added by default)
|
94
|
+
|
95
|
+
ColorizedString.add_color_alias(:niebieski, :blue)
|
96
|
+
ColorizedString.add_color_alias(:zielony => :green)
|
97
|
+
ColorizedString.add_color_alias(czarny: :black)
|
98
|
+
ColorizedString.add_color_alias(czerwony: :red, granatowy: :blue)
|
99
|
+
|
100
|
+
# examples
|
101
|
+
|
61
102
|
puts ColorizedString["This is blue"].colorize(:blue)
|
62
103
|
puts ColorizedString["This is light blue"].colorize(:light_blue)
|
63
104
|
puts ColorizedString["This is also blue"].colorize(:color => :blue)
|
105
|
+
puts ColorizedString["This is bold green"].colorize(:color => :green, :mode => :bold)
|
64
106
|
puts ColorizedString["This is light blue with red background"].colorize(:color => :light_blue, :background => :red)
|
65
107
|
puts ColorizedString["This is light blue with red background"].colorize(:light_blue ).colorize( :background => :red)
|
66
108
|
puts ColorizedString["This is blue text on red"].blue.on_red
|
67
109
|
puts ColorizedString["This is red on blue"].colorize(:red).on_blue
|
68
110
|
puts ColorizedString["This is red on blue and underline"].colorize(:red).on_blue.underline
|
69
|
-
puts ColorizedString["This is blue text on red"].blue.on_red.blink
|
111
|
+
puts ColorizedString["This is blinking blue text on red"].blue.on_red.blink
|
70
112
|
puts ColorizedString["This is uncolorized"].blue.on_red.uncolorize
|
71
113
|
|
72
114
|
puts ColorizedString.new("This is blue").blue
|
@@ -85,6 +127,11 @@ install
|
|
85
127
|
|
86
128
|
*Note:* You may need to use sudo to install gems
|
87
129
|
|
130
|
+
thank you
|
131
|
+
---------
|
132
|
+
|
133
|
+
[![Become Patreon](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/bePatron?u=6912974)
|
134
|
+
|
88
135
|
license
|
89
136
|
-------
|
90
137
|
|
data/Rakefile
CHANGED
data/colorize.gemspec
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require File.expand_path('lib/colorize/version', File.dirname(__FILE__))
|
4
|
+
|
1
5
|
Gem::Specification.new do |s|
|
2
6
|
s.name = 'colorize'
|
3
|
-
s.version =
|
7
|
+
s.version = Colorize::VERSION
|
8
|
+
s.required_ruby_version = '>= 2.6'
|
4
9
|
|
5
10
|
s.authors = ['Michał Kalbarczyk']
|
6
11
|
s.email = 'fazibear@gmail.com'
|
7
12
|
|
8
|
-
s.date = Time.now.strftime('%Y-%m-%d')
|
9
|
-
|
10
13
|
s.homepage = 'http://github.com/fazibear/colorize'
|
11
14
|
s.description = 'Extends String class or add a ColorizedString with methods to set text color, background color and text effects.'
|
12
15
|
s.summary = 'Ruby gem for colorizing text using ANSI escape sequences.'
|
@@ -14,23 +17,30 @@ Gem::Specification.new do |s|
|
|
14
17
|
|
15
18
|
s.require_paths = ['lib']
|
16
19
|
|
17
|
-
s.add_development_dependency '
|
20
|
+
s.add_development_dependency 'json', '~> 2.0'
|
18
21
|
s.add_development_dependency 'minitest', '~> 5.0'
|
19
|
-
s.add_development_dependency '
|
22
|
+
s.add_development_dependency 'rake', '~> 13.0'
|
23
|
+
s.add_development_dependency 'rubocop', '~> 1.0'
|
24
|
+
s.add_development_dependency 'rubocop-github', '~> 0.20'
|
25
|
+
s.add_development_dependency 'rubocop-minitest', '~> 0.29.0'
|
26
|
+
s.add_development_dependency 'rubocop-performance', '~> 1.16'
|
27
|
+
s.add_development_dependency 'rubocop-rake', '~> 0.6'
|
20
28
|
|
21
29
|
s.files = [
|
22
30
|
'LICENSE',
|
23
|
-
'CHANGELOG',
|
31
|
+
'CHANGELOG.md',
|
24
32
|
'README.md',
|
25
33
|
'Rakefile',
|
26
34
|
'colorize.gemspec',
|
27
35
|
'lib/colorize.rb',
|
28
36
|
'lib/colorized_string.rb',
|
37
|
+
'lib/colorize/errors.rb',
|
29
38
|
'lib/colorize/class_methods.rb',
|
30
39
|
'lib/colorize/instance_methods.rb',
|
40
|
+
'lib/colorize/version.rb',
|
31
41
|
'test/test_colorize.rb',
|
42
|
+
'test/test_colorized_string.rb',
|
32
43
|
]
|
33
|
-
|
34
|
-
|
35
|
-
]
|
44
|
+
|
45
|
+
s.metadata['rubygems_mfa_required'] = 'true'
|
36
46
|
end
|
@@ -1,17 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Colorize
|
2
4
|
module ClassMethods
|
5
|
+
@@color_codes ||= {
|
6
|
+
black: 30,
|
7
|
+
red: 31,
|
8
|
+
green: 32,
|
9
|
+
yellow: 33,
|
10
|
+
blue: 34,
|
11
|
+
purple: 35,
|
12
|
+
cyan: 36,
|
13
|
+
white: 37,
|
14
|
+
default: 39,
|
15
|
+
light_black: 90,
|
16
|
+
light_red: 91,
|
17
|
+
light_green: 92,
|
18
|
+
light_yellow: 93,
|
19
|
+
light_blue: 94,
|
20
|
+
light_purple: 95,
|
21
|
+
light_cyan: 96,
|
22
|
+
light_white: 97
|
23
|
+
}
|
24
|
+
|
25
|
+
@@mode_codes ||= {
|
26
|
+
default: 0, # Turn off all attributes
|
27
|
+
bold: 1,
|
28
|
+
dim: 2,
|
29
|
+
italic: 3,
|
30
|
+
underline: 4,
|
31
|
+
blink: 5,
|
32
|
+
blink_slow: 5,
|
33
|
+
blink_fast: 6,
|
34
|
+
invert: 7,
|
35
|
+
hide: 8,
|
36
|
+
strike: 9,
|
37
|
+
double_underline: 20,
|
38
|
+
reveal: 28,
|
39
|
+
overlined: 53
|
40
|
+
}
|
41
|
+
|
3
42
|
#
|
4
|
-
# Property
|
43
|
+
# Property for disable colorization
|
5
44
|
#
|
6
45
|
def disable_colorization(value = nil)
|
7
46
|
if value.nil?
|
8
|
-
if defined?(
|
9
|
-
|
47
|
+
if defined?(@@disable_colorization)
|
48
|
+
@@disable_colorization || false
|
10
49
|
else
|
11
50
|
false
|
12
51
|
end
|
13
52
|
else
|
14
|
-
|
53
|
+
@@disable_colorization = (value || false)
|
15
54
|
end
|
16
55
|
end
|
17
56
|
|
@@ -19,7 +58,29 @@ module Colorize
|
|
19
58
|
# Setter for disable colorization
|
20
59
|
#
|
21
60
|
def disable_colorization=(value)
|
22
|
-
|
61
|
+
@@disable_colorization = (value || false)
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# Property for prevent recolorization
|
66
|
+
#
|
67
|
+
def prevent_colors(value = nil)
|
68
|
+
if value.nil?
|
69
|
+
if defined?(@@prevent_colors)
|
70
|
+
@@prevent_colors || false
|
71
|
+
else
|
72
|
+
false
|
73
|
+
end
|
74
|
+
else
|
75
|
+
@@prevent_colors = (value || false)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
#
|
80
|
+
# Setter for prevent recolorization
|
81
|
+
#
|
82
|
+
def prevent_colors=(value)
|
83
|
+
@@prevent_colors = (value || false)
|
23
84
|
end
|
24
85
|
|
25
86
|
#
|
@@ -47,10 +108,16 @@ module Colorize
|
|
47
108
|
end
|
48
109
|
|
49
110
|
#
|
50
|
-
#
|
111
|
+
# Add color alias
|
51
112
|
#
|
52
|
-
def
|
53
|
-
|
113
|
+
def add_color_alias(*params)
|
114
|
+
parse_color_alias_params(params).each do |_alias_, _color_|
|
115
|
+
check_if_color_available!(_alias_)
|
116
|
+
check_if_color_exist!(_color_)
|
117
|
+
|
118
|
+
add_color_code(_alias_, color_codes[_color_])
|
119
|
+
add_color_method(_alias_)
|
120
|
+
end
|
54
121
|
end
|
55
122
|
|
56
123
|
# private
|
@@ -59,32 +126,18 @@ module Colorize
|
|
59
126
|
# Color codes hash
|
60
127
|
#
|
61
128
|
def color_codes
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
:blue => 4, :light_blue => 64,
|
68
|
-
:magenta => 5, :light_magenta => 65,
|
69
|
-
:cyan => 6, :light_cyan => 66,
|
70
|
-
:white => 7, :light_white => 67,
|
71
|
-
:default => 9
|
72
|
-
}
|
129
|
+
@@color_codes
|
130
|
+
end
|
131
|
+
|
132
|
+
def add_color_code(code, color)
|
133
|
+
@@color_codes[code] = color
|
73
134
|
end
|
74
135
|
|
75
136
|
#
|
76
137
|
# Mode codes hash
|
77
138
|
#
|
78
139
|
def mode_codes
|
79
|
-
|
80
|
-
:default => 0, # Turn off all attributes
|
81
|
-
:bold => 1, # Set bold mode
|
82
|
-
:italic => 3, # Set italic mode
|
83
|
-
:underline => 4, # Set underline mode
|
84
|
-
:blink => 5, # Set blink mode
|
85
|
-
:swap => 7, # Exchange foreground and background colors
|
86
|
-
:hide => 8 # Hide text (foreground color would be the same as background)
|
87
|
-
}
|
140
|
+
@@mode_codes
|
88
141
|
end
|
89
142
|
|
90
143
|
#
|
@@ -94,13 +147,20 @@ module Colorize
|
|
94
147
|
colors.each do |key|
|
95
148
|
next if key == :default
|
96
149
|
|
97
|
-
|
98
|
-
|
99
|
-
|
150
|
+
add_color_method(key)
|
151
|
+
end
|
152
|
+
end
|
100
153
|
|
101
|
-
|
102
|
-
|
103
|
-
|
154
|
+
#
|
155
|
+
# Generate color and on_color method
|
156
|
+
#
|
157
|
+
def add_color_method(key)
|
158
|
+
define_method key do
|
159
|
+
colorize(:color => key)
|
160
|
+
end
|
161
|
+
|
162
|
+
define_method "on_#{key}" do
|
163
|
+
colorize(:background => key)
|
104
164
|
end
|
105
165
|
end
|
106
166
|
|
@@ -116,5 +176,28 @@ module Colorize
|
|
116
176
|
end
|
117
177
|
end
|
118
178
|
end
|
179
|
+
|
180
|
+
def parse_color_alias_params(params)
|
181
|
+
return [params] if params.is_a?(Array) && params.length == 2
|
182
|
+
|
183
|
+
params.flat_map do |param|
|
184
|
+
next param if param.is_a?(Array) && param.length == 2
|
185
|
+
next param.to_a if param.is_a?(Hash)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
#
|
190
|
+
# Check if color exists
|
191
|
+
#
|
192
|
+
def check_if_color_available!(color)
|
193
|
+
color_codes[color] && fail(::Colorize::ColorAlreadyExist, "Colorize: color named :#{color} already exist!")
|
194
|
+
end
|
195
|
+
|
196
|
+
#
|
197
|
+
# Check if color is missing
|
198
|
+
#
|
199
|
+
def check_if_color_exist!(color)
|
200
|
+
color_codes[color] || fail(::Colorize::ColorDontExist, "Colorize: color :#{color} don't exist!")
|
201
|
+
end
|
119
202
|
end
|
120
203
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Colorize
|
2
4
|
module InstanceMethods
|
3
5
|
#
|
@@ -18,11 +20,11 @@ module Colorize
|
|
18
20
|
#
|
19
21
|
def colorize(params)
|
20
22
|
return self if self.class.disable_colorization
|
21
|
-
|
23
|
+
|
22
24
|
scan_for_colors.inject(self.class.new) do |str, match|
|
23
25
|
colors_from_params(match, params)
|
24
26
|
defaults_colors(match)
|
25
|
-
str << "\033[#{match[0]};#{match[1]};#{match[2]}m#{match[3]}\033[0m"
|
27
|
+
str << "\001\033[#{match[0]};#{match[1]};#{match[2]}m\002#{match[3]}\001\033[0m\002"
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
@@ -69,30 +71,40 @@ module Colorize
|
|
69
71
|
# Set colors from params hash
|
70
72
|
#
|
71
73
|
def colors_from_hash(match, hash)
|
72
|
-
|
73
|
-
|
74
|
-
|
74
|
+
if self.class.prevent_colors
|
75
|
+
match[0] ||= mode(hash[:mode]) if mode(hash[:mode])
|
76
|
+
match[1] ||= color(hash[:color]) if color(hash[:color])
|
77
|
+
match[2] ||= background_color(hash[:background]) if background_color(hash[:background])
|
78
|
+
else
|
79
|
+
match[0] = mode(hash[:mode]) if mode(hash[:mode])
|
80
|
+
match[1] = color(hash[:color]) if color(hash[:color])
|
81
|
+
match[2] = background_color(hash[:background]) if background_color(hash[:background])
|
82
|
+
end
|
75
83
|
end
|
76
84
|
|
77
85
|
#
|
78
86
|
# Set color from params symbol
|
79
87
|
#
|
80
88
|
def color_from_symbol(match, symbol)
|
81
|
-
|
89
|
+
if self.class.prevent_colors && color(symbol)
|
90
|
+
match[1] ||= color(symbol)
|
91
|
+
else
|
92
|
+
match[1] = color(symbol)
|
93
|
+
end
|
82
94
|
end
|
83
95
|
|
84
96
|
#
|
85
|
-
# Color for foreground
|
97
|
+
# Color for foreground
|
86
98
|
#
|
87
99
|
def color(color)
|
88
|
-
self.class.color_codes[color]
|
100
|
+
self.class.color_codes[color]
|
89
101
|
end
|
90
102
|
|
91
103
|
#
|
92
|
-
# Color for background (offset
|
104
|
+
# Color for background (offset 10)
|
93
105
|
#
|
94
106
|
def background_color(color)
|
95
|
-
self.class.color_codes[color] +
|
107
|
+
self.class.color_codes[color] + 10 if self.class.color_codes[color]
|
96
108
|
end
|
97
109
|
|
98
110
|
#
|
@@ -106,7 +118,7 @@ module Colorize
|
|
106
118
|
# Scan for colorized string
|
107
119
|
#
|
108
120
|
def scan_for_colors
|
109
|
-
scan(/\033\[([0-9;]+)m(.+?)\033\[0m|([^\033]+)/m).map do |match|
|
121
|
+
scan(/\001\033\[([0-9;]+)m\002(.+?)\001\033\[0m\002|([^\001\033]+)/m).map do |match|
|
110
122
|
split_colors(match)
|
111
123
|
end
|
112
124
|
end
|
@@ -124,7 +136,7 @@ module Colorize
|
|
124
136
|
# Require windows libs
|
125
137
|
#
|
126
138
|
def require_windows_libs
|
127
|
-
require 'Win32/Console/ANSI' if RUBY_VERSION < '2.0.0' && RUBY_PLATFORM
|
139
|
+
require 'Win32/Console/ANSI' if RUBY_VERSION < '2.0.0' && RUBY_PLATFORM.include?('win32')
|
128
140
|
rescue LoadError
|
129
141
|
raise 'You must gem install win32console to use colorize on Windows'
|
130
142
|
end
|
data/lib/colorize.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require File.expand_path('colorize/version', File.dirname(__FILE__))
|
4
|
+
require File.expand_path('colorize/errors', File.dirname(__FILE__))
|
1
5
|
require File.expand_path('colorize/class_methods', File.dirname(__FILE__))
|
2
6
|
require File.expand_path('colorize/instance_methods', File.dirname(__FILE__))
|
7
|
+
|
3
8
|
#
|
4
9
|
# String class extension.
|
5
10
|
#
|
@@ -9,4 +14,7 @@ class String
|
|
9
14
|
|
10
15
|
color_methods
|
11
16
|
modes_methods
|
17
|
+
|
18
|
+
add_color_alias(:grey, :light_black)
|
19
|
+
add_color_alias(:gray, :light_black)
|
12
20
|
end
|
data/lib/colorized_string.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require File.expand_path('colorize/errors', File.dirname(__FILE__))
|
1
4
|
require File.expand_path('colorize/class_methods', File.dirname(__FILE__))
|
2
5
|
require File.expand_path('colorize/instance_methods', File.dirname(__FILE__))
|
3
6
|
#
|
@@ -10,8 +13,11 @@ class ColorizedString < String
|
|
10
13
|
color_methods
|
11
14
|
modes_methods
|
12
15
|
|
16
|
+
add_color_alias(:grey, :light_black)
|
17
|
+
add_color_alias(:gray, :light_black)
|
18
|
+
|
13
19
|
#
|
14
|
-
#
|
20
|
+
# Shortcut to create ColorizedString with ColorizedString['test'].
|
15
21
|
#
|
16
22
|
def self.[](string)
|
17
23
|
ColorizedString.new(string)
|