string_dot_gradient 0.1.2 → 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: 2e65f856a5767b2902d8676cc7497d8a759cfcce1caf231929c7671d7d4410dc
4
- data.tar.gz: 68f58d2e0aa05cb117eb502da389315c5beb66d26cb3d8c82897b931a7207dd6
3
+ metadata.gz: 73df78b09463b558f80961be83861765cf026a674b0be3ab76213f1b1f5c8aed
4
+ data.tar.gz: 6e561b3415470f06426aab38fc0d97a51289cf82a9ef68bab9b14646d33983fb
5
5
  SHA512:
6
- metadata.gz: 667ec6cecb555d77bc0a9e31f106d8fa3ce4fd86bd12a6ff37c7c9c03ed9b1d4c6187746cbc8a2e2e3215e90a8a1c748a25b94d37a59970303a3c28c6760aa97
7
- data.tar.gz: fa0f08dd87d24724cb3567a6348bb46e81a7803ef9b069dc15498312404a14ed55e6b19da19da6f24e935f81938dbdb9a18fdd8f87bca0cb75df124c863eebfb
6
+ metadata.gz: 5772433cb33f8756a01efb8a7b2723624ff8874270a6205e9c51f12ba29113cf95fb3a6a924003e416da3c5e4aa676144297df7b7ab6c1c05b4f455fc991fd83
7
+ data.tar.gz: a35256dc944c405e5652d0cab8792c18f89f2592e0cf1bf918e690f5312b53863ad9048140eeb271d238fd17689f6675510ffc5239737b7d77e09f57f0a239e8
@@ -1,8 +1,8 @@
1
1
  class String
2
2
  ##
3
- # = gradient(*arg_colours, bg: false) # => string or nil
3
+ # = gradient(*arg_colours, bg: false, exclude_spaces: true) # => string or nil
4
4
  #
5
- # Prettifies your string by adding gradient colours
5
+ # Prettifies your string by adding gradient colours.
6
6
  #
7
7
  # This method accept a lot of colours. For example:
8
8
  #
@@ -29,8 +29,18 @@ class String
29
29
  #
30
30
  # Adding the option bg will change the background colour, but will keep the foreground colour
31
31
  # defined in the terminal settings.
32
- def gradient(*arg_colours, bg: false)
33
- colours, line_length = [], -1
32
+ #
33
+ # The option exclude_spaces, is expected to set either true or false.
34
+ # By default it's set to true.
35
+ # Enabling this option will not waste colours on white-spaces.
36
+ # White spaces only include: \s, \t
37
+ #
38
+ # Please do note that \u0000 and \r in the middle of the string will not be
39
+ # counted as a white space, but as a character instead.
40
+ # This is because \r wipes out the previous characters, and using \u0000 in
41
+ # a string is uncommon, and developers are requested to delete
42
+ # \u0000 from string if such situations arise.
43
+ def gradient(*arg_colours, bg: false, exclude_spaces: true)
34
44
  temp = ''
35
45
  flatten_colours = arg_colours.flatten
36
46
 
@@ -38,7 +48,6 @@ class String
38
48
  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)) }
39
49
 
40
50
  all_rgbs = flatten_colours.map!(&method(:hex_to_rgb))
41
-
42
51
  block_given = block_given?
43
52
 
44
53
  r, g, b = all_rgbs[0]
@@ -49,53 +58,86 @@ class String
49
58
 
50
59
  each_line do |c|
51
60
  _r, _g, _b = r, g, b
52
- n = c.length
53
-
54
- r_max, r_min = r > r2 ? [r, r2] : [r2, r]
55
- g_max, g_min = g > g2 ? [g, g2] : [g2, g]
56
- b_max, b_min = b > b2 ? [b, b2] : [b2, b]
57
-
58
- r_meth = r == r2 ? :itself : r2 > r ? [:+, r2.fdiv(n)] : [:-, r.fdiv(n)]
59
- g_meth = g == g2 ? :itself : g2 > g ? [:+, g2.fdiv(n)] : [:-, g.fdiv(n)]
60
- b_meth = b == b2 ? :itself : b2 > b ? [:+, b2.fdiv(n)] : [:-, b.fdiv(n)]
61
-
62
- if line_length != n || rotate
63
- line_length = n
64
- colours.clear
65
-
66
- i = -1
67
- while (i += 1) < n
68
- _r = _r.send(*r_meth) unless _r > r_max || _r < r_min
69
- _g = _g.send(*g_meth) unless _g > g_max || _g < g_min
70
- _b = _b.send(*b_meth) unless _b > b_max || _b < b_min
71
-
72
- r_to_i = _r.to_i
73
- g_to_i = _g.to_i
74
- b_to_i = _b.to_i
75
-
76
- colours << [
77
- r_to_i < 0 ? 0 : r_to_i > 255 ? 255 : r_to_i,
78
- g_to_i < 0 ? 0 : g_to_i > 255 ? 255 : g_to_i,
79
- b_to_i < 0 ? 0 : b_to_i > 255 ? 255 : b_to_i,
80
- ]
81
- end
61
+ chomped = !!c.chomp!(''.freeze)
62
+
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
+
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
70
+
71
+ r_comp_op = r_comp_val = nil
72
+ g_comp_op = g_comp_val = nil
73
+ b_comp_op = b_comp_val = nil
74
+
75
+ if r2 > r
76
+ r_op, r_val, r_max, r_min = :+, r2.fdiv(n_variable), r2, r
77
+ r_comp_op, r_comp_val = :<=, r_max
78
+ elsif r2 < r
79
+ r_op, r_val, r_max, r_min = :-, r.fdiv(n_variable), r, r2
80
+ r_comp_op, r_comp_val = :>=, r_min
82
81
  end
83
82
 
83
+ if g2 > g
84
+ g_op, g_val, g_max, g_min = :+, g2.fdiv(n_variable), g2, g
85
+ g_comp_op, g_comp_val = :<=, g_max
86
+ elsif g2 < g
87
+ g_op, g_val, g_max, g_min = :-, g.fdiv(n_variable), g, g2
88
+ g_comp_op, g_comp_val = :>=, g_min
89
+ end
90
+
91
+ if b2 > b
92
+ b_op, b_val, b_max, b_min = :+, b2.fdiv(n_variable), b2, b
93
+ b_comp_op, b_comp_val = :<=, b_max
94
+ elsif b2 < b
95
+ b_op, b_val, b_max, b_min = :-, b.fdiv(n_variable), b, b2
96
+ b_comp_op, b_comp_val = :>=, b_min
97
+ end
98
+
99
+ # To avoid the value getting adding | subtracted from the initial character
100
+ _r = _r.send(r_op, r_val * -1) if r_comp_op && _r.send(r_comp_op, r_comp_val)
101
+ _g = _g.send(g_op, g_val * -1) if g_comp_op && _g.send(g_comp_op, g_comp_val)
102
+ _b = _b.send(b_op, b_val * -1) if b_comp_op && _b.send(b_comp_op, b_comp_val)
103
+
84
104
  i = -1
85
- while (i += 1) < n
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)
112
+ end
113
+
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
+
86
124
  if block_given
87
- yield "\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
125
+ yield ret
88
126
  else
89
- temp.concat(
90
- "\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
91
- )
127
+ temp << ret
92
128
  end
93
129
  end
94
130
 
131
+ ret = if !c.empty?
132
+ chomped ? "\e[0m\n".freeze : "\e[0m".freeze
133
+ elsif chomped
134
+ ?\n.freeze
135
+ end
136
+
95
137
  if block_given
96
- yield "\e[0m".freeze
138
+ yield ret
97
139
  else
98
- temp << "\e[0m".freeze
140
+ temp << ret
99
141
  end
100
142
 
101
143
  if rotate
@@ -111,6 +153,8 @@ class String
111
153
  private
112
154
  def hex_to_rgb(hex)
113
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.
114
158
  colour = hex && hex.dup.to_s || ''
115
159
  colour.strip!
116
160
  colour.downcase!
@@ -134,7 +178,7 @@ class String
134
178
  elsif clen == 6
135
179
  colour.chars.each_slice(2).map { |x| x.join.to_i(16) }
136
180
  else
137
- sli = clen > 6 ? 'too long' : clen < 3 ? 'too short' : 'invalid'
181
+ sli = clen > 6 ? 'too long'.freeze : clen < 3 ? 'too short'.freeze : 'invalid'.freeze
138
182
  raise ArgumentError, "Invalid Hex Colour ##{colour} (length #{sli})"
139
183
  end
140
184
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StringDotGradient
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.7"
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.2
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-08 00:00:00.000000000 Z
11
+ date: 2021-01-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: An itty-bitty extension that adds gradient method to String class, for
14
- Linux terminals
13
+ description: An itty-bitty extension that adds gradient method to String class that
14
+ supports any hex colour, for Linux | Unix terminals only
15
15
  email:
16
16
  - souravgoswami@protonmail.com
17
17
  executables: []
@@ -33,7 +33,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
33
33
  requirements:
34
34
  - - ">="
35
35
  - !ruby/object:Gem::Version
36
- version: 2.2.0
36
+ version: 2.0.0
37
37
  required_rubygems_version: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - ">="
@@ -43,6 +43,6 @@ requirements: []
43
43
  rubygems_version: 3.1.4
44
44
  signing_key:
45
45
  specification_version: 4
46
- summary: An itty-bitty extension that adds gradient method to String class, for Linux
47
- terminals
46
+ summary: An itty-bitty extension that adds gradient method to String class that supports
47
+ any hex colour, for Linux | Unix terminals only
48
48
  test_files: []