string_dot_gradient 0.1.6 → 0.1.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 705f4872e8c680732b71c8d5e1e70f2287418d4b38278f92bbf801bb3d838f5a
4
- data.tar.gz: bc0c962ecd6615bc40ec1c58f1c90131a752651f1165b6962898e046f38511bf
3
+ metadata.gz: 73df78b09463b558f80961be83861765cf026a674b0be3ab76213f1b1f5c8aed
4
+ data.tar.gz: 6e561b3415470f06426aab38fc0d97a51289cf82a9ef68bab9b14646d33983fb
5
5
  SHA512:
6
- metadata.gz: 234f82023c22e4f0acffc3456eee6080b73cfad4516a34cb645a6a1dcc9a742c743e09368f71e15187721805ada57e3d2fbc892413677eb0daa0889bb98c6cbd
7
- data.tar.gz: '095bc1362d552dae0d1e8e56808fc993a92f47a529ef584fd0fdbbc6705cda1cb40237beb06b64d0ba0f28fe828b3179b76712f652e522658a110104bfbc1d57'
6
+ metadata.gz: 5772433cb33f8756a01efb8a7b2723624ff8874270a6205e9c51f12ba29113cf95fb3a6a924003e416da3c5e4aa676144297df7b7ab6c1c05b4f455fc991fd83
7
+ data.tar.gz: a35256dc944c405e5652d0cab8792c18f89f2592e0cf1bf918e690f5312b53863ad9048140eeb271d238fd17689f6675510ffc5239737b7d77e09f57f0a239e8
@@ -41,7 +41,6 @@ class String
41
41
  # a string is uncommon, and developers are requested to delete
42
42
  # \u0000 from string if such situations arise.
43
43
  def gradient(*arg_colours, bg: false, exclude_spaces: true)
44
- colours, line_length = [], -1
45
44
  temp = ''
46
45
  flatten_colours = arg_colours.flatten
47
46
 
@@ -61,8 +60,9 @@ class String
61
60
  _r, _g, _b = r, g, b
62
61
  chomped = !!c.chomp!(''.freeze)
63
62
 
64
- n = c.length
65
- n_variable = exclude_spaces ? c.delete("\t\s".freeze).length : n
63
+ len = c.length
64
+ n_variable = exclude_spaces ? c.delete("\t\s".freeze).length : len
65
+ n_variable = 1 if n_variable == 0
66
66
 
67
67
  r_op = r_val = r_max = r_min = nil
68
68
  g_op = g_val = g_max = g_min = nil
@@ -97,71 +97,47 @@ class String
97
97
  end
98
98
 
99
99
  # To avoid the value getting adding | subtracted from the initial character
100
- # Note that duplication is fine if we don't get a little bit performance loss
101
100
  _r = _r.send(r_op, r_val * -1) if r_comp_op && _r.send(r_comp_op, r_comp_val)
102
101
  _g = _g.send(g_op, g_val * -1) if g_comp_op && _g.send(g_comp_op, g_comp_val)
103
102
  _b = _b.send(b_op, b_val * -1) if b_comp_op && _b.send(b_comp_op, b_comp_val)
104
103
 
105
- if line_length != n || rotate
106
- line_length = n
107
- colours.clear
108
-
109
- i = -1
110
- while (i += 1) < n
111
- if exclude_spaces
112
- _c = c[i]
113
-
114
- if !(_c == ?\s.freeze || _c == ?\t.freeze)
115
- _r = _r.send(r_op, r_val) if r_comp_op && _r.send(r_comp_op, r_comp_val)
116
- _g = _g.send(g_op, g_val) if g_comp_op && _g.send(g_comp_op, g_comp_val)
117
- _b = _b.send(b_op, b_val) if b_comp_op && _b.send(b_comp_op, b_comp_val)
118
- end
119
- else
120
- # We also have duplication above,
121
- # But we are not going to remove this code unless
122
- # we find some efficient solution. Using a proc or method
123
- # for setting these values and calling that is a way
124
- # to make code slower.
125
- _r = _r.send(r_op, r_val) if r_comp_op && _r.send(r_comp_op, r_comp_val)
126
- _g = _g.send(g_op, g_val) if g_comp_op && _g.send(g_comp_op, g_comp_val)
127
- _b = _b.send(b_op, b_val) if b_comp_op && _b.send(b_comp_op, b_comp_val)
128
- end
129
-
130
- r_to_i = _r.to_i
131
- g_to_i = _g.to_i
132
- b_to_i = _b.to_i
133
-
134
- colours << [
135
- r_to_i < 0 ? 0 : r_to_i > 255 ? 255 : r_to_i,
136
- g_to_i < 0 ? 0 : g_to_i > 255 ? 255 : g_to_i,
137
- b_to_i < 0 ? 0 : b_to_i > 255 ? 255 : b_to_i,
138
- ]
104
+ i = -1
105
+ while (i += 1) < len
106
+ _c = c[i]
107
+
108
+ if !exclude_spaces || (exclude_spaces && !(_c == ?\s.freeze || _c == ?\t.freeze))
109
+ _r = _r.send(r_op, r_val) if r_comp_op && _r.send(r_comp_op, r_comp_val)
110
+ _g = _g.send(g_op, g_val) if g_comp_op && _g.send(g_comp_op, g_comp_val)
111
+ _b = _b.send(b_op, b_val) if b_comp_op && _b.send(b_comp_op, b_comp_val)
139
112
  end
140
- end
141
113
 
142
- i = -1
143
- while (i += 1) < n
114
+ r_to_i = _r.to_i
115
+ g_to_i = _g.to_i
116
+ b_to_i = _b.to_i
117
+
118
+ f_r = r_to_i < 0 ? 0 : r_to_i > 255 ? 255 : r_to_i
119
+ f_g = g_to_i < 0 ? 0 : g_to_i > 255 ? 255 : g_to_i
120
+ f_b = b_to_i < 0 ? 0 : b_to_i > 255 ? 255 : b_to_i
121
+
122
+ ret = "\e[#{init};2;#{f_r};#{f_g};#{f_b}m#{_c}"
123
+
144
124
  if block_given
145
- yield "\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
125
+ yield ret
146
126
  else
147
- temp.concat(
148
- "\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
149
- )
127
+ temp << ret
150
128
  end
151
129
  end
152
130
 
131
+ ret = if !c.empty?
132
+ chomped ? "\e[0m\n".freeze : "\e[0m".freeze
133
+ elsif chomped
134
+ ?\n.freeze
135
+ end
136
+
153
137
  if block_given
154
- if !c.empty?
155
- yield(chomped ? "\e[0m\n".freeze : "\e[0m".freeze)
156
- elsif chomped && c.empty?
157
- yield(?\n.freeze)
158
- end
138
+ yield ret
159
139
  else
160
- if !c.empty?
161
- temp.concat(chomped ? "\e[0m\n".freeze : "\e[0m".freeze)
162
- elsif chomped && c.empty?
163
- temp.concat(?\n.freeze)
164
- end
140
+ temp << ret
165
141
  end
166
142
 
167
143
  if rotate
@@ -177,6 +153,8 @@ class String
177
153
  private
178
154
  def hex_to_rgb(hex)
179
155
  # Duplicate colour, even if colour is nil
156
+ # This workaround is for Ruby 2.0 to Ruby 2.2
157
+ # Which won't allow duplicate nil.
180
158
  colour = hex && hex.dup.to_s || ''
181
159
  colour.strip!
182
160
  colour.downcase!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StringDotGradient
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
5
5
  end
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.6
4
+ version: 0.1.7
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-09 00:00:00.000000000 Z
11
+ date: 2021-01-10 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