string_dot_gradient 0.1.7 → 0.3.2
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/lib/string_dot_gradient/gradient.rb +154 -34
- data/lib/string_dot_gradient/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dee7487ec82cd9435b163e3c3659c8147e2e8b6b7cd6b4220888d9ad213a6e5c
|
4
|
+
data.tar.gz: 9006ef1191185c26ebef6801c6c33cd3d5fd7ef1371c8924575e82b6055798e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89ad6e5d4244d86504367ebf61763feace21fd0fa93d381978ecf7ab865e334029a42535cff4b74213daccdb43b0e66c9cd8771f9bb0af4acbd045ae63bd5418
|
7
|
+
data.tar.gz: 84c5f8b2f25a85c2ed0aa94ee902284bafd51e321902ce094043f04e2a7dba64f3bd0eafa428d002ef9e626492d95fa29d79bc8718339e5698a2c553182e76e4
|
@@ -1,6 +1,6 @@
|
|
1
1
|
class String
|
2
2
|
##
|
3
|
-
# = gradient(*arg_colours, bg: false, exclude_spaces: true) # => string or nil
|
3
|
+
# = gradient(*arg_colours, bg: false, exclude_spaces: true, bold: false, blink: false) # => string or nil
|
4
4
|
#
|
5
5
|
# Prettifies your string by adding gradient colours.
|
6
6
|
#
|
@@ -40,75 +40,108 @@ class String
|
|
40
40
|
# This is because \r wipes out the previous characters, and using \u0000 in
|
41
41
|
# a string is uncommon, and developers are requested to delete
|
42
42
|
# \u0000 from string if such situations arise.
|
43
|
-
|
43
|
+
#
|
44
|
+
# The option bold makes texts bold, but it also makes the string bigger.
|
45
|
+
# Set bold to anything truthy or falsey, but better just go with true and false or nil
|
46
|
+
#
|
47
|
+
# The option blink makes the texts blink on supported terminals.
|
48
|
+
# Set blink to anything truthy or falsey, but better just go with true and false or nil
|
49
|
+
def gradient(*arg_colours,
|
50
|
+
exclude_spaces: true,
|
51
|
+
bg: false,
|
52
|
+
bold: false,
|
53
|
+
italic: false,
|
54
|
+
underline: false,
|
55
|
+
blink: false,
|
56
|
+
strikethrough: false,
|
57
|
+
double_underline: false,
|
58
|
+
overline: false
|
59
|
+
)
|
60
|
+
|
44
61
|
temp = ''
|
45
62
|
flatten_colours = arg_colours.flatten
|
46
63
|
|
64
|
+
# Create the styling here rather than creating it in the each_line loop
|
65
|
+
# We also make it a bit different, rather than using \e[1m\e[5m, we will do
|
66
|
+
# \e[1;5m to save the number of characters spit out by this method
|
67
|
+
style = nil
|
68
|
+
|
69
|
+
if bold || italic || underline || blink || strikethrough || double_underline || overline
|
70
|
+
style = "\e["
|
71
|
+
|
72
|
+
style << '1;'.freeze if bold
|
73
|
+
style << '3;'.freeze if italic
|
74
|
+
style << '4;'.freeze if underline
|
75
|
+
style << '5;'.freeze if blink
|
76
|
+
style << '9;'.freeze if strikethrough
|
77
|
+
style << '21;'.freeze if double_underline
|
78
|
+
style << '53;'.freeze if overline
|
79
|
+
|
80
|
+
style.chop!
|
81
|
+
style << ?m.freeze
|
82
|
+
end
|
83
|
+
|
47
84
|
raise ArgumentError, "Wrong numeber of colours (given #{flatten_colours.length}, expected minimum 2)" if flatten_colours.length < 2
|
48
85
|
raise ArgumentError, "Given argument for colour is neither a String nor an Integer" if flatten_colours.any? { |x| !(x.is_a?(String) || x.is_a?(Integer)) }
|
49
86
|
|
50
87
|
all_rgbs = flatten_colours.map!(&method(:hex_to_rgb))
|
51
88
|
block_given = block_given?
|
52
89
|
|
53
|
-
r, g, b
|
54
|
-
r2, g2, b2
|
90
|
+
# r, g, b => starting r, g, b
|
91
|
+
# r2, g2, b2 => stopping r, g, b
|
92
|
+
r, g, b = *all_rgbs[0]
|
93
|
+
r2, g2, b2 = *all_rgbs[1]
|
55
94
|
rotate = all_rgbs.length > 2
|
56
95
|
|
57
96
|
init = bg ? 48 : 38
|
58
97
|
|
59
98
|
each_line do |c|
|
99
|
+
temp << style if style
|
100
|
+
|
60
101
|
_r, _g, _b = r, g, b
|
61
102
|
chomped = !!c.chomp!(''.freeze)
|
62
103
|
|
63
104
|
len = c.length
|
64
105
|
n_variable = exclude_spaces ? c.delete("\t\s".freeze).length : len
|
65
|
-
n_variable
|
66
|
-
|
67
|
-
r_op = r_val = r_max = r_min = nil
|
68
|
-
g_op = g_val = g_max = g_min = nil
|
69
|
-
b_op = b_val = b_max = b_min = nil
|
106
|
+
n_variable -= 1
|
107
|
+
n_variable = 1 if n_variable < 1
|
70
108
|
|
71
|
-
|
72
|
-
|
73
|
-
|
109
|
+
# colour operator, colour value
|
110
|
+
r_op = r_val = nil
|
111
|
+
g_op = g_val = nil
|
112
|
+
b_op = b_val = nil
|
74
113
|
|
75
114
|
if r2 > r
|
76
|
-
r_op, r_val
|
77
|
-
r_comp_op, r_comp_val = :<=, r_max
|
115
|
+
r_op, r_val = :+, r2.-(r).fdiv(n_variable)
|
78
116
|
elsif r2 < r
|
79
|
-
r_op, r_val
|
80
|
-
r_comp_op, r_comp_val = :>=, r_min
|
117
|
+
r_op, r_val = :-, r.-(r2).fdiv(n_variable)
|
81
118
|
end
|
82
119
|
|
83
120
|
if g2 > g
|
84
|
-
g_op, g_val
|
85
|
-
g_comp_op, g_comp_val = :<=, g_max
|
121
|
+
g_op, g_val = :+, g2.-(g).fdiv(n_variable)
|
86
122
|
elsif g2 < g
|
87
|
-
g_op, g_val
|
88
|
-
g_comp_op, g_comp_val = :>=, g_min
|
123
|
+
g_op, g_val = :-, g.-(g2).fdiv(n_variable)
|
89
124
|
end
|
90
125
|
|
91
126
|
if b2 > b
|
92
|
-
b_op, b_val
|
93
|
-
b_comp_op, b_comp_val = :<=, b_max
|
127
|
+
b_op, b_val = :+, b2.-(b).fdiv(n_variable)
|
94
128
|
elsif b2 < b
|
95
|
-
b_op, b_val
|
96
|
-
b_comp_op, b_comp_val = :>=, b_min
|
129
|
+
b_op, b_val = :-, b.-(b2).fdiv(n_variable)
|
97
130
|
end
|
98
131
|
|
99
132
|
# To avoid the value getting adding | subtracted from the initial character
|
100
|
-
_r = _r.send(r_op, r_val * -1) if
|
101
|
-
_g = _g.send(g_op, g_val * -1) if
|
102
|
-
_b = _b.send(b_op, b_val * -1) if
|
133
|
+
_r = _r.send(r_op, r_val * -1) if r_op
|
134
|
+
_g = _g.send(g_op, g_val * -1) if g_op
|
135
|
+
_b = _b.send(b_op, b_val * -1) if b_op
|
103
136
|
|
104
137
|
i = -1
|
105
138
|
while (i += 1) < len
|
106
139
|
_c = c[i]
|
107
140
|
|
108
141
|
if !exclude_spaces || (exclude_spaces && !(_c == ?\s.freeze || _c == ?\t.freeze))
|
109
|
-
_r = _r.send(r_op, r_val) if
|
110
|
-
_g = _g.send(g_op, g_val) if
|
111
|
-
_b = _b.send(b_op, b_val) if
|
142
|
+
_r = _r.send(r_op, r_val) if r_op
|
143
|
+
_g = _g.send(g_op, g_val) if g_op
|
144
|
+
_b = _b.send(b_op, b_val) if b_op
|
112
145
|
end
|
113
146
|
|
114
147
|
r_to_i = _r.to_i
|
@@ -122,7 +155,11 @@ class String
|
|
122
155
|
ret = "\e[#{init};2;#{f_r};#{f_g};#{f_b}m#{_c}"
|
123
156
|
|
124
157
|
if block_given
|
125
|
-
|
158
|
+
if style && i == 0
|
159
|
+
yield style + ret
|
160
|
+
else
|
161
|
+
yield ret
|
162
|
+
end
|
126
163
|
else
|
127
164
|
temp << ret
|
128
165
|
end
|
@@ -135,7 +172,11 @@ class String
|
|
135
172
|
end
|
136
173
|
|
137
174
|
if block_given
|
138
|
-
|
175
|
+
if style
|
176
|
+
yield style + ret
|
177
|
+
else
|
178
|
+
yield ret
|
179
|
+
end
|
139
180
|
else
|
140
181
|
temp << ret
|
141
182
|
end
|
@@ -150,6 +191,86 @@ class String
|
|
150
191
|
block_given ? nil : temp
|
151
192
|
end
|
152
193
|
|
194
|
+
##
|
195
|
+
# = multi_gradient(*n_arg_colours, bg: false, exclude_spaces: true, bold: false, blink: false) # => string or nil
|
196
|
+
#
|
197
|
+
# Accepts n number of colours. Example:
|
198
|
+
# 'Hello world this is multi_gradient()'.multi_gradient('3eb', '55f', 'f55', 'fa0')
|
199
|
+
#
|
200
|
+
# In this example, multi_gradient() paints the string with 4 colours in one line.
|
201
|
+
#
|
202
|
+
# It Splits up a string with the Calls String#gradient() with the given number of colours
|
203
|
+
# So each call to multi_gradient() involves many calls to String#gradient().
|
204
|
+
# Hence it's slower than String#gradient()
|
205
|
+
def multi_gradient(*colours,
|
206
|
+
exclude_spaces: true,
|
207
|
+
bg: false,
|
208
|
+
bold: false,
|
209
|
+
italic: false,
|
210
|
+
underline: false,
|
211
|
+
blink: false,
|
212
|
+
strikethrough: false,
|
213
|
+
double_underline: false,
|
214
|
+
overline: false,
|
215
|
+
&block
|
216
|
+
)
|
217
|
+
|
218
|
+
len = colours.length
|
219
|
+
raise ArgumentError, "Minimum two colours are required, given #{len}" if len < 2
|
220
|
+
|
221
|
+
div = len - 1
|
222
|
+
div_1 = div - 1
|
223
|
+
ret = ''
|
224
|
+
block_given = block_given?
|
225
|
+
|
226
|
+
params = {
|
227
|
+
exclude_spaces: exclude_spaces,
|
228
|
+
bg: bg,
|
229
|
+
bold: bold,
|
230
|
+
italic: italic,
|
231
|
+
underline: underline,
|
232
|
+
blink: blink,
|
233
|
+
strikethrough: strikethrough,
|
234
|
+
double_underline: double_underline,
|
235
|
+
overline: overline,
|
236
|
+
}
|
237
|
+
|
238
|
+
each_line { |l|
|
239
|
+
_len = l.length
|
240
|
+
|
241
|
+
len, c = _len.fdiv(div).round, colours.dup
|
242
|
+
counter, i, j = -1, -1, 0
|
243
|
+
ch = ''
|
244
|
+
|
245
|
+
while x = l[i += 1] do
|
246
|
+
counter += 1
|
247
|
+
|
248
|
+
# colour % len == 0 is very slow approach
|
249
|
+
if counter == len && j < div_1
|
250
|
+
counter, j = 0, j + 1
|
251
|
+
if block_given
|
252
|
+
ch.gradient(c[0], c[1], **params, &block)
|
253
|
+
else
|
254
|
+
ret << ch.gradient(c[0], c[1], **params)
|
255
|
+
end
|
256
|
+
|
257
|
+
c.rotate!
|
258
|
+
ch.clear
|
259
|
+
end
|
260
|
+
|
261
|
+
ch << x
|
262
|
+
end
|
263
|
+
|
264
|
+
if block_given
|
265
|
+
ch.gradient(c[0], c[1], **params, &block)
|
266
|
+
else
|
267
|
+
ret << ch.gradient(c[0], c[1], **params)
|
268
|
+
end
|
269
|
+
}
|
270
|
+
|
271
|
+
block_given ? nil : ret
|
272
|
+
end
|
273
|
+
|
153
274
|
private
|
154
275
|
def hex_to_rgb(hex)
|
155
276
|
# Duplicate colour, even if colour is nil
|
@@ -168,8 +289,7 @@ class String
|
|
168
289
|
oor.include?(x) ? "\e[1;31m#{x}\e[0m" : x
|
169
290
|
}.join
|
170
291
|
|
171
|
-
|
172
|
-
raise ArgumentError
|
292
|
+
raise ArgumentError, "\e[0mHex Colour \e[1m##{invalids} is Out of Range\e[0m"
|
173
293
|
end
|
174
294
|
|
175
295
|
clen = colour.length
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: string_dot_gradient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sourav Goswami
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-01
|
11
|
+
date: 2021-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: An itty-bitty extension that adds gradient method to String class that
|
14
|
-
supports any hex colour, for Linux
|
13
|
+
description: An itty-bitty extension that adds "gradient" method to String class that
|
14
|
+
supports any hex colour, for Linux terminals
|
15
15
|
email:
|
16
16
|
- souravgoswami@protonmail.com
|
17
17
|
executables: []
|
@@ -40,9 +40,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0'
|
42
42
|
requirements: []
|
43
|
-
rubygems_version: 3.
|
43
|
+
rubygems_version: 3.2.13
|
44
44
|
signing_key:
|
45
45
|
specification_version: 4
|
46
|
-
summary: An itty-bitty extension that adds gradient method to String class that
|
47
|
-
any hex colour, for Linux
|
46
|
+
summary: An itty-bitty extension that adds "gradient" method to String class that
|
47
|
+
supports any hex colour, for Linux terminals
|
48
48
|
test_files: []
|