colorize 0.7.7 → 0.8.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 +4 -4
- data/CHANGELOG +7 -0
- data/README.md +46 -14
- data/Rakefile +6 -1
- data/colorize.gemspec +5 -5
- data/lib/colorize.rb +1 -1
- data/lib/colorize/class_methods.rb +10 -6
- data/lib/colorize/instance_methods.rb +14 -17
- data/test/test_colorize.rb +12 -9
- metadata +7 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0be0a115e11c6219ca857f092e1ff9b066242644
|
4
|
+
data.tar.gz: c223c4263dd10b8446ce5a07940233d754b5c70d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e6b4c310dda218b54beb32fc4117de4d6ff080cf3966bcfa83302af56de3806ab0032df29764838800802adbff81482eb00b60614eb7b8e4720bba329762b9e
|
7
|
+
data.tar.gz: e080a425e29bb91a6763c9fb120391d43b3aca8f8c5887b8a105d2c84145bf86bbfaf3a7c3ec33bd51a0c065a9025d87d984258323250304555b7869f90e301d
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
colorize [](http://badge.fury.io/rb/colorize) [](https://travis-ci.org/fazibear/colorize) [](https://codeclimate.com/github/fazibear/colorize) [](https://codeclimate.com/github/fazibear/colorize)
|
2
2
|
========
|
3
3
|
|
4
|
-
Ruby
|
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.
|
6
|
+
|
7
|
+
modes
|
8
|
+
-----
|
9
|
+
|
10
|
+
* `require 'colorize'` - Extends String class
|
11
|
+
* `require 'colorized_string'` - add ColorizedString class
|
5
12
|
|
6
13
|
features
|
7
14
|
--------
|
@@ -9,13 +16,24 @@ features
|
|
9
16
|
* change string color
|
10
17
|
* change string background
|
11
18
|
* change string effect
|
19
|
+
* display color samples
|
20
|
+
* disable colorization
|
12
21
|
|
13
22
|
usage
|
14
23
|
-----
|
15
24
|
|
16
|
-
Some usage samples:
|
17
|
-
|
18
25
|
```ruby
|
26
|
+
require 'colorize'
|
27
|
+
|
28
|
+
String.colors # return array of all possible colors names
|
29
|
+
String.modes # return array of all possible modes
|
30
|
+
String.color_samples # displays color samples in all combinations
|
31
|
+
String.disable_colorization # check if colorization is disabled
|
32
|
+
String.disable_colorization = false # enable colorization
|
33
|
+
String.disable_colorization false # enable colorization
|
34
|
+
String.disable_colorization = true # disable colorization
|
35
|
+
String.disable_colorization true # disable colorization
|
36
|
+
|
19
37
|
puts "This is blue".colorize(:blue)
|
20
38
|
puts "This is light blue".colorize(:light_blue)
|
21
39
|
puts "This is also blue".colorize(:color => :blue)
|
@@ -28,17 +46,31 @@ puts "This is blue text on red".blue.on_red.blink
|
|
28
46
|
puts "This is uncolorized".blue.on_red.uncolorize
|
29
47
|
```
|
30
48
|
|
31
|
-
Class methods:
|
32
|
-
|
33
49
|
```ruby
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
50
|
+
require 'colorized_string'
|
51
|
+
|
52
|
+
ColorizedString.colors # return array of all possible colors names
|
53
|
+
ColorizedString.modes # return array of all possible modes
|
54
|
+
ColorizedString.color_samples # displays color samples in all combinations
|
55
|
+
ColorizedString.disable_colorization # check if colorization is disabled
|
56
|
+
ColorizedString.disable_colorization = false # enable colorization
|
57
|
+
ColorizedString.disable_colorization false # enable colorization
|
58
|
+
ColorizedString.disable_colorization = true # disable colorization
|
59
|
+
ColorizedString.disable_colorization true # disable colorization
|
60
|
+
|
61
|
+
puts ColorizedString["This is blue"].colorize(:blue)
|
62
|
+
puts ColorizedString["This is light blue"].colorize(:light_blue)
|
63
|
+
puts ColorizedString["This is also blue"].colorize(:color => :blue)
|
64
|
+
puts ColorizedString["This is light blue with red background"].colorize(:color => :light_blue, :background => :red)
|
65
|
+
puts ColorizedString["This is light blue with red background"].colorize(:light_blue ).colorize( :background => :red)
|
66
|
+
puts ColorizedString["This is blue text on red"].blue.on_red
|
67
|
+
puts ColorizedString["This is red on blue"].colorize(:red).on_blue
|
68
|
+
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
|
70
|
+
puts ColorizedString["This is uncolorized"].blue.on_red.uncolorize
|
71
|
+
|
72
|
+
puts ColorizedString.new("This is blue").blue
|
73
|
+
puts ColorizedString.new("This is light blue").colorize(:light_blue)
|
42
74
|
```
|
43
75
|
|
44
76
|
requirements
|
@@ -56,7 +88,7 @@ install
|
|
56
88
|
license
|
57
89
|
-------
|
58
90
|
|
59
|
-
Copyright (C) 2007-
|
91
|
+
Copyright (C) 2007-2016 Michał Kalbarczyk
|
60
92
|
|
61
93
|
This program is free software; you can redistribute it and/or modify
|
62
94
|
it under the terms of the GNU General Public License as published by
|
data/Rakefile
CHANGED
data/colorize.gemspec
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'colorize'
|
3
|
-
s.version = '0.
|
3
|
+
s.version = '0.8.0'
|
4
4
|
|
5
5
|
s.authors = ['Michał Kalbarczyk']
|
6
6
|
s.email = 'fazibear@gmail.com'
|
7
7
|
|
8
|
-
s.date = Time.now.strftime(
|
8
|
+
s.date = Time.now.strftime('%Y-%m-%d')
|
9
9
|
|
10
10
|
s.homepage = 'http://github.com/fazibear/colorize'
|
11
|
-
s.description = '
|
12
|
-
s.summary = '
|
13
|
-
s.license = 'GPL-2'
|
11
|
+
s.description = 'Extends String class or add a ColorizedString with methods to set text color, background color and text effects.'
|
12
|
+
s.summary = 'Ruby gem for colorizing text using ANSI escape sequences.'
|
13
|
+
s.license = 'GPL-2.0'
|
14
14
|
|
15
15
|
s.require_paths = ['lib']
|
16
16
|
|
data/lib/colorize.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require File.expand_path('colorize/class_methods', File.dirname(__FILE__))
|
2
2
|
require File.expand_path('colorize/instance_methods', File.dirname(__FILE__))
|
3
3
|
#
|
4
|
-
#
|
4
|
+
# String class extension.
|
5
5
|
#
|
6
6
|
class String
|
7
7
|
extend Colorize::ClassMethods
|
@@ -1,12 +1,15 @@
|
|
1
1
|
module Colorize
|
2
2
|
module ClassMethods
|
3
|
-
|
4
3
|
#
|
5
4
|
# Property to disable colorization
|
6
5
|
#
|
7
6
|
def disable_colorization(value = nil)
|
8
7
|
if value.nil?
|
9
|
-
@disable_colorization
|
8
|
+
if defined?(@disable_colorization)
|
9
|
+
@disable_colorization || false
|
10
|
+
else
|
11
|
+
false
|
12
|
+
end
|
10
13
|
else
|
11
14
|
@disable_colorization = (value || false)
|
12
15
|
end
|
@@ -39,17 +42,17 @@ module Colorize
|
|
39
42
|
def color_samples
|
40
43
|
colors.permutation(2).each do |background, color|
|
41
44
|
sample_text = "#{color.inspect.rjust(15)} on #{background.inspect.ljust(15)}"
|
42
|
-
puts "#{sample_text.colorize(:color => color, :background => background)} #{sample_text}"
|
45
|
+
puts "#{new(sample_text).colorize(:color => color, :background => background)} #{sample_text}"
|
43
46
|
end
|
44
47
|
end
|
45
48
|
|
46
49
|
#
|
47
50
|
# Method removed, raise NoMethodError
|
48
51
|
#
|
49
|
-
def color_matrix(
|
52
|
+
def color_matrix(_ = '')
|
50
53
|
fail NoMethodError, '#color_matrix method was removed, try #color_samples instead'
|
51
54
|
end
|
52
|
-
|
55
|
+
|
53
56
|
# private
|
54
57
|
|
55
58
|
#
|
@@ -76,13 +79,14 @@ module Colorize
|
|
76
79
|
{
|
77
80
|
:default => 0, # Turn off all attributes
|
78
81
|
:bold => 1, # Set bold mode
|
82
|
+
:italic => 3, # Set italic mode
|
79
83
|
:underline => 4, # Set underline mode
|
80
84
|
:blink => 5, # Set blink mode
|
81
85
|
:swap => 7, # Exchange foreground and background colors
|
82
86
|
:hide => 8 # Hide text (foreground color would be the same as background)
|
83
87
|
}
|
84
88
|
end
|
85
|
-
|
89
|
+
|
86
90
|
#
|
87
91
|
# Generate color and on_color methods
|
88
92
|
#
|
@@ -1,6 +1,5 @@
|
|
1
1
|
module Colorize
|
2
2
|
module InstanceMethods
|
3
|
-
|
4
3
|
#
|
5
4
|
# Change color of string
|
6
5
|
#
|
@@ -20,9 +19,9 @@ module Colorize
|
|
20
19
|
def colorize(params)
|
21
20
|
return self if self.class.disable_colorization
|
22
21
|
require_windows_libs
|
23
|
-
scan_for_colors.inject(
|
24
|
-
defaults_colors(match)
|
22
|
+
scan_for_colors.inject(self.class.new) do |str, match|
|
25
23
|
colors_from_params(match, params)
|
24
|
+
defaults_colors(match)
|
26
25
|
str << "\033[#{match[0]};#{match[1]};#{match[2]}m#{match[3]}\033[0m"
|
27
26
|
end
|
28
27
|
end
|
@@ -31,7 +30,7 @@ module Colorize
|
|
31
30
|
# Return uncolorized string
|
32
31
|
#
|
33
32
|
def uncolorize
|
34
|
-
scan_for_colors.inject(
|
33
|
+
scan_for_colors.inject(self.class.new) do |str, match|
|
35
34
|
str << match[3]
|
36
35
|
end
|
37
36
|
end
|
@@ -70,8 +69,8 @@ module Colorize
|
|
70
69
|
# Set colors from params hash
|
71
70
|
#
|
72
71
|
def colors_from_hash(match, hash)
|
73
|
-
match[0] = mode(hash[:mode])
|
74
|
-
match[1] = color(hash[:color])
|
72
|
+
match[0] = mode(hash[:mode]) if mode(hash[:mode])
|
73
|
+
match[1] = color(hash[:color]) if color(hash[:color])
|
75
74
|
match[2] = background_color(hash[:background]) if background_color(hash[:background])
|
76
75
|
end
|
77
76
|
|
@@ -113,23 +112,21 @@ module Colorize
|
|
113
112
|
end
|
114
113
|
|
115
114
|
def split_colors(match)
|
116
|
-
colors = (match[0] ||
|
117
|
-
Array.new(
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
115
|
+
colors = (match[0] || '').split(';')
|
116
|
+
array = Array.new(3)
|
117
|
+
array[0], array[1], array[2] = colors if colors.length == 3
|
118
|
+
array[1] = colors if colors.length == 1
|
119
|
+
array[3] = match[1] || match[2]
|
120
|
+
array
|
122
121
|
end
|
123
122
|
|
124
123
|
#
|
125
124
|
# Require windows libs
|
126
125
|
#
|
127
126
|
def require_windows_libs
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
raise 'You must gem install win32console to use colorize on Windows'
|
132
|
-
end
|
127
|
+
require 'Win32/Console/ANSI' if RUBY_VERSION < '2.0.0' && RUBY_PLATFORM =~ /win32/
|
128
|
+
rescue LoadError
|
129
|
+
raise 'You must gem install win32console to use colorize on Windows'
|
133
130
|
end
|
134
131
|
end
|
135
132
|
end
|
data/test/test_colorize.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require 'codeclimate-test-reporter'
|
2
2
|
CodeClimate::TestReporter.start
|
3
3
|
|
4
|
-
require
|
4
|
+
require 'minitest/autorun'
|
5
5
|
require File.dirname(__FILE__) + '/../lib/colorize'
|
6
6
|
|
7
7
|
class TestColorize < Minitest::Test
|
@@ -68,25 +68,28 @@ class TestColorize < Minitest::Test
|
|
68
68
|
|
69
69
|
def test_uncolorize
|
70
70
|
assert_equal 'This is uncolorized'.blue.on_red.uncolorize,
|
71
|
-
|
71
|
+
'This is uncolorized'
|
72
72
|
end
|
73
73
|
|
74
74
|
def test_colorized?
|
75
75
|
assert_equal 'Red'.red.colorized?, true
|
76
76
|
assert_equal 'Blue'.colorized?, false
|
77
77
|
assert_equal 'Green'.blue.green.uncolorize.colorized?, false
|
78
|
-
|
79
|
-
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_concatenated__colorize?
|
81
|
+
assert_equal "none #{'red'.red} none #{'blue'.blue} none".colorized?, true
|
82
|
+
assert_equal "none #{'red'.red} none #{'blue'.blue} none".uncolorize.colorized?, false
|
80
83
|
end
|
81
84
|
|
82
85
|
def test_concatenated_strings_on_green
|
83
|
-
assert_equal
|
84
|
-
"\e[0;39;42mnone\e[0m\e[0;31;42mred\e[0m\e[0;39;
|
86
|
+
assert_equal "none #{'red'.red} none #{'blue'.blue} none".on_green,
|
87
|
+
"\e[0;39;42mnone \e[0m\e[0;31;42mred\e[0m\e[0;39;42m none \e[0m\e[0;34;42mblue\e[0m\e[0;39;42m none\e[0m"
|
85
88
|
end
|
86
89
|
|
87
90
|
def test_concatenated_strings_uncolorize
|
88
|
-
assert_equal
|
89
|
-
|
91
|
+
assert_equal "none #{'red'.red} none #{'blue'.blue} none".uncolorize,
|
92
|
+
'none red none blue none'
|
90
93
|
end
|
91
94
|
|
92
95
|
def test_frozen_strings
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: colorize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michał Kalbarczyk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -52,9 +52,8 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0.4'
|
55
|
-
description:
|
56
|
-
color
|
57
|
-
sequences.
|
55
|
+
description: Extends String class or add a ColorizedString with methods to set text
|
56
|
+
color, background color and text effects.
|
58
57
|
email: fazibear@gmail.com
|
59
58
|
executables: []
|
60
59
|
extensions: []
|
@@ -71,7 +70,7 @@ files:
|
|
71
70
|
- test/test_colorize.rb
|
72
71
|
homepage: http://github.com/fazibear/colorize
|
73
72
|
licenses:
|
74
|
-
- GPL-2
|
73
|
+
- GPL-2.0
|
75
74
|
metadata: {}
|
76
75
|
post_install_message:
|
77
76
|
rdoc_options: []
|
@@ -89,9 +88,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
88
|
version: '0'
|
90
89
|
requirements: []
|
91
90
|
rubyforge_project:
|
92
|
-
rubygems_version: 2.
|
91
|
+
rubygems_version: 2.5.1
|
93
92
|
signing_key:
|
94
93
|
specification_version: 4
|
95
|
-
summary:
|
94
|
+
summary: Ruby gem for colorizing text using ANSI escape sequences.
|
96
95
|
test_files:
|
97
96
|
- test/test_colorize.rb
|