string_dot_gradient 0.1.8 → 0.3.3

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: 7f734ad48d80c57969e3fb4598f5a89fafdb35b30c2b5dddbc1bbbf1a835a829
4
- data.tar.gz: 80c268d0c7754047bf1040d1ade34a3e9652180441ca3cbf23a0d67f8873d531
3
+ metadata.gz: 06603caa967d156eea0acf41986eddbd09f651a4f21d64da62edbaa029b878b9
4
+ data.tar.gz: f5d3c0fbc4b9bfc9caf61ea56b2f4c0518a457ce90a35c9c1425376c5a707963
5
5
  SHA512:
6
- metadata.gz: 0102ffe6acc5355cc7d29dea013439eb245fc5ec11415eea6cc91f1025a6128f639120b74d29d0dec44ebf085f73d41411d6027480b4270dc832d17e5e353cf7
7
- data.tar.gz: d0d2c157c03af2a8688fcba1ade8d5177e0d8656512b9293a26672faf9dddb492a530e409df9e6ffa5f32f75f05d0c6c041ac13f4cc0a75c4067b4fc59bf9144
6
+ metadata.gz: a03febddbf80cb165467b627e5f91709626d4f1dd0e56d84e1c023f41b450971d6043bddc408ce7a5bba774a3f0f3151d52112598dcefbcb58d1568d6f5836b4
7
+ data.tar.gz: 9908f51246f7fbf885fa94e4499f0684241cb4159110fb472adfdf8e545251dae8bc8b9628e770a9478ad9217255c71b81f61163d98616ef49ad9494fd14ea5e
@@ -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,15 +40,54 @@ 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
- def gradient(*arg_colours, bg: false, exclude_spaces: true)
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
+
61
+ block_given = block_given?
44
62
  temp = ''
45
63
  flatten_colours = arg_colours.flatten
46
64
 
65
+ # Create the styling here rather than creating it in the each_line loop
66
+ # We also make it a bit different, rather than using \e[1m\e[5m, we will do
67
+ # \e[1;5m to save the number of characters spit out by this method
68
+ style = nil
69
+
70
+ if bold || italic || underline || blink || strikethrough || double_underline || overline
71
+ style = "\e["
72
+
73
+ style << '1;'.freeze if bold
74
+ style << '3;'.freeze if italic
75
+ style << '4;'.freeze if underline
76
+ style << '5;'.freeze if blink
77
+ style << '9;'.freeze if strikethrough
78
+ style << '21;'.freeze if double_underline
79
+ style << '53;'.freeze if overline
80
+
81
+ style.chop!
82
+ style << ?m.freeze
83
+ end
84
+
47
85
  raise ArgumentError, "Wrong numeber of colours (given #{flatten_colours.length}, expected minimum 2)" if flatten_colours.length < 2
48
86
  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
87
 
50
88
  all_rgbs = flatten_colours.map!(&method(:hex_to_rgb))
51
- block_given = block_given?
89
+
90
+ yield style if block_given
52
91
 
53
92
  # r, g, b => starting r, g, b
54
93
  # r2, g2, b2 => stopping r, g, b
@@ -59,6 +98,8 @@ class String
59
98
  init = bg ? 48 : 38
60
99
 
61
100
  each_line do |c|
101
+ temp << style if style
102
+
62
103
  _r, _g, _b = r, g, b
63
104
  chomped = !!c.chomp!(''.freeze)
64
105
 
@@ -144,6 +185,86 @@ class String
144
185
  block_given ? nil : temp
145
186
  end
146
187
 
188
+ ##
189
+ # = multi_gradient(*n_arg_colours, bg: false, exclude_spaces: true, bold: false, blink: false) # => string or nil
190
+ #
191
+ # Accepts n number of colours. Example:
192
+ # 'Hello world this is multi_gradient()'.multi_gradient('3eb', '55f', 'f55', 'fa0')
193
+ #
194
+ # In this example, multi_gradient() paints the string with 4 colours in one line.
195
+ #
196
+ # It Splits up a string with the Calls String#gradient() with the given number of colours
197
+ # So each call to multi_gradient() involves many calls to String#gradient().
198
+ # Hence it's slower than String#gradient()
199
+ def multi_gradient(*colours,
200
+ exclude_spaces: true,
201
+ bg: false,
202
+ bold: false,
203
+ italic: false,
204
+ underline: false,
205
+ blink: false,
206
+ strikethrough: false,
207
+ double_underline: false,
208
+ overline: false,
209
+ &block
210
+ )
211
+
212
+ len = colours.length
213
+ raise ArgumentError, "Minimum two colours are required, given #{len}" if len < 2
214
+
215
+ div = len - 1
216
+ div_1 = div - 1
217
+ ret = ''
218
+ block_given = block_given?
219
+
220
+ params = {
221
+ exclude_spaces: exclude_spaces,
222
+ bg: bg,
223
+ bold: bold,
224
+ italic: italic,
225
+ underline: underline,
226
+ blink: blink,
227
+ strikethrough: strikethrough,
228
+ double_underline: double_underline,
229
+ overline: overline,
230
+ }
231
+
232
+ each_line { |l|
233
+ _len = l.length
234
+
235
+ len, c = _len.fdiv(div).round, colours.dup
236
+ counter, i, j = -1, -1, 0
237
+ ch = ''
238
+
239
+ while x = l[i += 1] do
240
+ counter += 1
241
+
242
+ # colour % len == 0 is very slow approach
243
+ if counter == len && j < div_1
244
+ counter, j = 0, j + 1
245
+ if block_given
246
+ ch.gradient(c[0], c[1], **params, &block)
247
+ else
248
+ ret << ch.gradient(c[0], c[1], **params)
249
+ end
250
+
251
+ c.rotate!
252
+ ch.clear
253
+ end
254
+
255
+ ch << x
256
+ end
257
+
258
+ if block_given
259
+ ch.gradient(c[0], c[1], **params, &block)
260
+ else
261
+ ret << ch.gradient(c[0], c[1], **params)
262
+ end
263
+ }
264
+
265
+ block_given ? nil : ret
266
+ end
267
+
147
268
  private
148
269
  def hex_to_rgb(hex)
149
270
  # Duplicate colour, even if colour is nil
@@ -162,8 +283,7 @@ class String
162
283
  oor.include?(x) ? "\e[1;31m#{x}\e[0m" : x
163
284
  }.join
164
285
 
165
- puts "Hex Colour ##{invalids} is Out of Range"
166
- raise ArgumentError
286
+ raise ArgumentError, "\e[0mHex Colour \e[1m##{invalids} is Out of Range\e[0m"
167
287
  end
168
288
 
169
289
  clen = colour.length
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StringDotGradient
4
- VERSION = "0.1.8"
4
+ VERSION = "0.3.3"
5
5
  end
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.1.8
4
+ version: 0.3.3
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-12 00:00:00.000000000 Z
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 | Unix terminals only
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.1.4
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 supports
47
- any hex colour, for Linux | Unix terminals only
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: []