string_dot_gradient 0.1.4 → 0.1.9
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 +65 -67
- data/lib/string_dot_gradient/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 229c7c52d938962d064671617bd9c8ae622df4a52fa5dd7df0b4485ec14e55a1
|
4
|
+
data.tar.gz: 0d926f9ed11056c3870c3964a7497c345b890582e8816296baa2c38eaa07836d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9baf8ae5b7a5e0285db981cc3d4426493cd44864f40a44cbefecde30b7742602f5424275bfe676a330dd9a8eb5c9117c4c50b055811c76d072c2f9c632ab7e07
|
7
|
+
data.tar.gz: 3186f138e9036fc98801a0810913524c16508a2468126c41a9482d3043b0accaf41bbcbf9aa2c6600365e30d043e6b311aaec8db6b43d83eea6cfe3e75f47565
|
@@ -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,8 +40,13 @@ 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
|
-
|
44
|
-
|
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, bg: false, exclude_spaces: true, bold: false, blink: false)
|
45
50
|
temp = ''
|
46
51
|
flatten_colours = arg_colours.flatten
|
47
52
|
|
@@ -51,100 +56,91 @@ class String
|
|
51
56
|
all_rgbs = flatten_colours.map!(&method(:hex_to_rgb))
|
52
57
|
block_given = block_given?
|
53
58
|
|
54
|
-
r, g, b
|
55
|
-
r2, g2, b2
|
59
|
+
# r, g, b => starting r, g, b
|
60
|
+
# r2, g2, b2 => stopping r, g, b
|
61
|
+
r, g, b = *all_rgbs[0]
|
62
|
+
r2, g2, b2 = *all_rgbs[1]
|
56
63
|
rotate = all_rgbs.length > 2
|
57
64
|
|
58
65
|
init = bg ? 48 : 38
|
59
66
|
|
60
67
|
each_line do |c|
|
68
|
+
temp << "\e[1m".freeze if bold
|
69
|
+
temp << "\e[5m".freeze if blink
|
70
|
+
|
61
71
|
_r, _g, _b = r, g, b
|
62
|
-
|
72
|
+
chomped = !!c.chomp!(''.freeze)
|
63
73
|
|
64
|
-
|
65
|
-
|
66
|
-
|
74
|
+
len = c.length
|
75
|
+
n_variable = exclude_spaces ? c.delete("\t\s".freeze).length : len
|
76
|
+
n_variable -= 1
|
77
|
+
n_variable = 1 if n_variable < 1
|
67
78
|
|
68
|
-
|
69
|
-
|
70
|
-
|
79
|
+
# colour operator, colour value
|
80
|
+
r_op = r_val = nil
|
81
|
+
g_op = g_val = nil
|
82
|
+
b_op = b_val = nil
|
71
83
|
|
72
84
|
if r2 > r
|
73
|
-
r_op, r_val
|
74
|
-
r_comp_op, r_comp_val = :<=, r_max
|
85
|
+
r_op, r_val = :+, r2.-(r).fdiv(n_variable)
|
75
86
|
elsif r2 < r
|
76
|
-
r_op, r_val
|
77
|
-
r_comp_op, r_comp_val = :>=, r_min
|
87
|
+
r_op, r_val = :-, r.-(r2).fdiv(n_variable)
|
78
88
|
end
|
79
89
|
|
80
90
|
if g2 > g
|
81
|
-
g_op, g_val
|
82
|
-
g_comp_op, g_comp_val = :<=, g_max
|
91
|
+
g_op, g_val = :+, g2.-(g).fdiv(n_variable)
|
83
92
|
elsif g2 < g
|
84
|
-
g_op, g_val
|
85
|
-
g_comp_op, g_comp_val = :>=, g_min
|
93
|
+
g_op, g_val = :-, g.-(g2).fdiv(n_variable)
|
86
94
|
end
|
87
95
|
|
88
96
|
if b2 > b
|
89
|
-
b_op, b_val
|
90
|
-
b_comp_op, b_comp_val = :<=, b_max
|
97
|
+
b_op, b_val = :+, b2.-(b).fdiv(n_variable)
|
91
98
|
elsif b2 < b
|
92
|
-
b_op, b_val
|
93
|
-
b_comp_op, b_comp_val = :>=, b_min
|
99
|
+
b_op, b_val = :-, b.-(b2).fdiv(n_variable)
|
94
100
|
end
|
95
101
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
i = -1
|
101
|
-
while (i += 1) < n
|
102
|
-
if exclude_spaces
|
103
|
-
_c = c[i]
|
104
|
-
|
105
|
-
if !(_c == ?\s.freeze || _c == ?\t.freeze)
|
106
|
-
_r = _r.send(r_op, r_val) if r_comp_op && _r.send(r_comp_op, r_comp_val)
|
107
|
-
_g = _g.send(g_op, g_val) if g_comp_op && _g.send(g_comp_op, g_comp_val)
|
108
|
-
_b = _b.send(b_op, b_val) if b_comp_op && _b.send(b_comp_op, b_comp_val)
|
109
|
-
end
|
110
|
-
else
|
111
|
-
# We also have duplication above,
|
112
|
-
# But we are not going to remove this code unless
|
113
|
-
# we find some efficient solution. Using a proc or method
|
114
|
-
# for setting these values and calling that is a way
|
115
|
-
# to make code slower.
|
116
|
-
_r = _r.send(r_op, r_val) if r_comp_op && _r.send(r_comp_op, r_comp_val)
|
117
|
-
_g = _g.send(g_op, g_val) if g_comp_op && _g.send(g_comp_op, g_comp_val)
|
118
|
-
_b = _b.send(b_op, b_val) if b_comp_op && _b.send(b_comp_op, b_comp_val)
|
119
|
-
end
|
120
|
-
|
121
|
-
r_to_i = _r.to_i
|
122
|
-
g_to_i = _g.to_i
|
123
|
-
b_to_i = _b.to_i
|
124
|
-
|
125
|
-
colours << [
|
126
|
-
r_to_i < 0 ? 0 : r_to_i > 255 ? 255 : r_to_i,
|
127
|
-
g_to_i < 0 ? 0 : g_to_i > 255 ? 255 : g_to_i,
|
128
|
-
b_to_i < 0 ? 0 : b_to_i > 255 ? 255 : b_to_i,
|
129
|
-
]
|
130
|
-
end
|
131
|
-
end
|
102
|
+
# To avoid the value getting adding | subtracted from the initial character
|
103
|
+
_r = _r.send(r_op, r_val * -1) if r_op
|
104
|
+
_g = _g.send(g_op, g_val * -1) if g_op
|
105
|
+
_b = _b.send(b_op, b_val * -1) if b_op
|
132
106
|
|
133
107
|
i = -1
|
134
|
-
while (i += 1) <
|
108
|
+
while (i += 1) < len
|
109
|
+
_c = c[i]
|
110
|
+
|
111
|
+
if !exclude_spaces || (exclude_spaces && !(_c == ?\s.freeze || _c == ?\t.freeze))
|
112
|
+
_r = _r.send(r_op, r_val) if r_op
|
113
|
+
_g = _g.send(g_op, g_val) if g_op
|
114
|
+
_b = _b.send(b_op, b_val) if b_op
|
115
|
+
end
|
116
|
+
|
117
|
+
r_to_i = _r.to_i
|
118
|
+
g_to_i = _g.to_i
|
119
|
+
b_to_i = _b.to_i
|
120
|
+
|
121
|
+
f_r = r_to_i < 0 ? 0 : r_to_i > 255 ? 255 : r_to_i
|
122
|
+
f_g = g_to_i < 0 ? 0 : g_to_i > 255 ? 255 : g_to_i
|
123
|
+
f_b = b_to_i < 0 ? 0 : b_to_i > 255 ? 255 : b_to_i
|
124
|
+
|
125
|
+
ret = "\e[#{init};2;#{f_r};#{f_g};#{f_b}m#{_c}"
|
126
|
+
|
135
127
|
if block_given
|
136
|
-
yield
|
128
|
+
yield ret
|
137
129
|
else
|
138
|
-
temp
|
139
|
-
"\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
|
140
|
-
)
|
130
|
+
temp << ret
|
141
131
|
end
|
142
132
|
end
|
143
133
|
|
134
|
+
ret = if !c.empty?
|
135
|
+
chomped ? "\e[0m\n".freeze : "\e[0m".freeze
|
136
|
+
elsif chomped
|
137
|
+
?\n.freeze
|
138
|
+
end
|
139
|
+
|
144
140
|
if block_given
|
145
|
-
yield
|
141
|
+
yield ret
|
146
142
|
else
|
147
|
-
temp <<
|
143
|
+
temp << ret
|
148
144
|
end
|
149
145
|
|
150
146
|
if rotate
|
@@ -160,6 +156,8 @@ class String
|
|
160
156
|
private
|
161
157
|
def hex_to_rgb(hex)
|
162
158
|
# Duplicate colour, even if colour is nil
|
159
|
+
# This workaround is for Ruby 2.0 to Ruby 2.2
|
160
|
+
# Which won't allow duplicate nil.
|
163
161
|
colour = hex && hex.dup.to_s || ''
|
164
162
|
colour.strip!
|
165
163
|
colour.downcase!
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: string_dot_gradient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.9
|
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-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: An itty-bitty extension that adds gradient method to String class that
|
14
14
|
supports any hex colour, for Linux | Unix terminals only
|