colorize 0.7.3 → 0.7.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +4 -1
- data/README.md +2 -3
- data/colorize.gemspec +1 -1
- data/lib/colorize.rb +69 -39
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 317b798812e6ac4ce6a4f841d19ace2486046303
|
4
|
+
data.tar.gz: a4428a6bd6095977fbb4105bedf856cae76c6618
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3af5be1a215b84c3e6d3e34f66f198e9f39a7aa3a5c57e4aacbfd03744747a7555aa34204ab92f1bcdf2121dd41e68288acbb2321be81b41bf40aa04ed68400d
|
7
|
+
data.tar.gz: 7da873f65fd383cae435eee5dc52b2ba1676a31fc4e0964f83b65b10faa3316a1d611392f02486851a0046ce5e37ab7467280b7c6a1703c540e12879beccea13
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
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) [![Build Status](https://travis-ci.org/fazibear/colorize.svg?branch=master)](https://travis-ci.org/fazibear/colorize) [![Code Climate](https://codeclimate.com/github/fazibear/colorize/badges/gpa.svg)](https://codeclimate.com/github/fazibear/colorize)
|
2
2
|
========
|
3
3
|
|
4
4
|
Ruby String class extension. Adds methods to set text color, background color and, text effects on ruby console and command line output, using ANSI escape sequences.
|
@@ -30,8 +30,7 @@ Class methods:
|
|
30
30
|
|
31
31
|
String.colors - return array of all possible colors names
|
32
32
|
String.modes - return array of all possible modes
|
33
|
-
String.
|
34
|
-
String.color_matrix("FOO") - display color matrix with "FOO" text
|
33
|
+
String.color_samples - displays color samples in all combinations
|
35
34
|
|
36
35
|
requirements
|
37
36
|
------------
|
data/colorize.gemspec
CHANGED
data/lib/colorize.rb
CHANGED
@@ -42,7 +42,7 @@ class String
|
|
42
42
|
REGEXP_PATTERN = /\033\[([0-9]+);([0-9]+);([0-9]+)m(.+?)\033\[0m|([^\033]+)/m
|
43
43
|
COLOR_OFFSET = 30
|
44
44
|
BACKGROUND_OFFSET = 40
|
45
|
-
|
45
|
+
|
46
46
|
public
|
47
47
|
|
48
48
|
#
|
@@ -62,26 +62,11 @@ class String
|
|
62
62
|
# puts "This is uncolorized".blue.on_red.uncolorize
|
63
63
|
#
|
64
64
|
def colorize(params)
|
65
|
-
|
66
|
-
require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
|
67
|
-
rescue LoadError
|
68
|
-
raise 'You must gem install win32console to use colorize on Windows'
|
69
|
-
end
|
70
|
-
|
71
|
-
self.scan(REGEXP_PATTERN).inject("") do |str, match|
|
72
|
-
match[0] ||= MODES[:default]
|
73
|
-
match[1] ||= COLORS[:default] + COLOR_OFFSET
|
74
|
-
match[2] ||= COLORS[:default] + BACKGROUND_OFFSET
|
75
|
-
match[3] ||= match[4]
|
76
|
-
|
77
|
-
if (params.instance_of?(Hash))
|
78
|
-
match[0] = MODES[params[:mode]] if params[:mode] && MODES[params[:mode]]
|
79
|
-
match[1] = COLORS[params[:color]] + COLOR_OFFSET if params[:color] && COLORS[params[:color]]
|
80
|
-
match[2] = COLORS[params[:background]] + BACKGROUND_OFFSET if params[:background] && COLORS[params[:background]]
|
81
|
-
elsif (params.instance_of?(Symbol))
|
82
|
-
match[1] = COLORS[params] + COLOR_OFFSET if params && COLORS[params]
|
83
|
-
end
|
65
|
+
windows_requires
|
84
66
|
|
67
|
+
scan(REGEXP_PATTERN).inject('') do |str, match|
|
68
|
+
set_defaults(match)
|
69
|
+
set_from_params(match, params)
|
85
70
|
str << "\033[#{match[0]};#{match[1]};#{match[2]}m#{match[3]}\033[0m"
|
86
71
|
end
|
87
72
|
end
|
@@ -90,7 +75,7 @@ class String
|
|
90
75
|
# Return uncolorized string
|
91
76
|
#
|
92
77
|
def uncolorize
|
93
|
-
|
78
|
+
scan(REGEXP_PATTERN).inject('') do |str, match|
|
94
79
|
str << (match[3] || match[4])
|
95
80
|
end
|
96
81
|
end
|
@@ -99,9 +84,7 @@ class String
|
|
99
84
|
# Return true if string is colorized
|
100
85
|
#
|
101
86
|
def colorized?
|
102
|
-
|
103
|
-
match.last
|
104
|
-
end.any?
|
87
|
+
scan(REGEXP_PATTERN).reject(&:last).any?
|
105
88
|
end
|
106
89
|
|
107
90
|
#
|
@@ -111,11 +94,11 @@ class String
|
|
111
94
|
next if key == :default
|
112
95
|
|
113
96
|
define_method key do
|
114
|
-
|
97
|
+
colorize(:color => key)
|
115
98
|
end
|
116
99
|
|
117
100
|
define_method "on_#{key}" do
|
118
|
-
|
101
|
+
colorize(:background => key)
|
119
102
|
end
|
120
103
|
end
|
121
104
|
|
@@ -126,10 +109,59 @@ class String
|
|
126
109
|
next if key == :default
|
127
110
|
|
128
111
|
define_method key do
|
129
|
-
|
112
|
+
colorize(:mode => key)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
|
118
|
+
#
|
119
|
+
# Require windows libs
|
120
|
+
#
|
121
|
+
def windows_requires
|
122
|
+
begin
|
123
|
+
require 'Win32/Console/ANSI' if RUBY_VERSION < "2.0.0" && RUBY_PLATFORM =~ /win32/
|
124
|
+
rescue LoadError
|
125
|
+
raise 'You must gem install win32console to use colorize on Windows'
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
#
|
130
|
+
# Set default colors
|
131
|
+
#
|
132
|
+
def set_defaults(match)
|
133
|
+
match[0] ||= MODES[:default]
|
134
|
+
match[1] ||= COLORS[:default] + COLOR_OFFSET
|
135
|
+
match[2] ||= COLORS[:default] + BACKGROUND_OFFSET
|
136
|
+
match[3] ||= match[4]
|
137
|
+
end
|
138
|
+
|
139
|
+
#
|
140
|
+
# Set color from params
|
141
|
+
#
|
142
|
+
def set_from_params(match, params)
|
143
|
+
case params
|
144
|
+
when Hash then set_from_hash(match, params)
|
145
|
+
when Symbol then set_from_symbol(match, params)
|
130
146
|
end
|
131
147
|
end
|
132
148
|
|
149
|
+
#
|
150
|
+
# Set colors from params hash
|
151
|
+
#
|
152
|
+
def set_from_hash(match, hash)
|
153
|
+
match[0] = MODES[hash[:mode]] if hash[:mode] && MODES[hash[:mode]]
|
154
|
+
match[1] = COLORS[hash[:color]] + COLOR_OFFSET if hash[:color] && COLORS[hash[:color]]
|
155
|
+
match[2] = COLORS[hash[:background]] + BACKGROUND_OFFSET if hash[:background] && COLORS[hash[:background]]
|
156
|
+
end
|
157
|
+
|
158
|
+
#
|
159
|
+
# Set color from params symbol
|
160
|
+
#
|
161
|
+
def set_from_symbol(match, symbol)
|
162
|
+
match[1] = COLORS[symbol] + COLOR_OFFSET if symbol && COLORS[symbol]
|
163
|
+
end
|
164
|
+
|
133
165
|
class << self
|
134
166
|
|
135
167
|
#
|
@@ -147,21 +179,19 @@ class String
|
|
147
179
|
end
|
148
180
|
|
149
181
|
#
|
150
|
-
# Display color
|
182
|
+
# Display color samples
|
151
183
|
#
|
152
|
-
def
|
153
|
-
|
154
|
-
|
155
|
-
String.colors.each do |back|
|
156
|
-
print txt.colorize(:color => color, :background => back)
|
157
|
-
end
|
158
|
-
puts " < #{color}"
|
184
|
+
def color_samples
|
185
|
+
String.colors.permutation(2).each do |background, color|
|
186
|
+
puts "#{color.inspect.rjust(15)} on #{background.inspect.ljust(15)}".colorize(:color => color, :background => background) + "#{color.inspect.rjust(15)} on #{background.inspect.ljust(15)}"
|
159
187
|
end
|
160
|
-
String.colors.reverse.each_with_index do |back, index|
|
161
|
-
puts "#{"|".rjust(txt.length)*(size-index)} < #{back}"
|
162
|
-
end
|
163
|
-
''
|
164
188
|
end
|
165
189
|
|
190
|
+
#
|
191
|
+
# Method removed, raise NoMethodError
|
192
|
+
#
|
193
|
+
def color_matrix(txt = '')
|
194
|
+
fail NoMethodError, '#color_matrix method was removed, try #color_samples instead'
|
195
|
+
end
|
166
196
|
end
|
167
197
|
end
|
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.7.
|
4
|
+
version: 0.7.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fazibear
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Ruby String class extension. Adds methods to set text color, background
|
14
14
|
color and, text effects on ruby console and command line output, using ANSI escape
|