string_dot_gradient 0.1.1 → 0.1.6

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: 46742b73629d3f3bfec9b12628a2ccc174548d06dce6958faa4a7d849ad13d37
4
- data.tar.gz: 613d584f92df46117cc9bf33115b80fb655363787ba236b3f28bf7f29823fb1b
3
+ metadata.gz: 705f4872e8c680732b71c8d5e1e70f2287418d4b38278f92bbf801bb3d838f5a
4
+ data.tar.gz: bc0c962ecd6615bc40ec1c58f1c90131a752651f1165b6962898e046f38511bf
5
5
  SHA512:
6
- metadata.gz: cc9e51d4ec0b19c6ae6243544d550958e2ed5f31f8f838d4143fceaf82ac3e1c420382d7acfb1a6da458396f6ee3cb03b3c38c0d7020da600d80d3260420586e
7
- data.tar.gz: 25aff2e43cf96421a01b11da8a574c71f894327852befaac4f4a4193985af7bf064e69670d573b5802277680cee556dfedf620ebbb31533dd8e80849c735960b
6
+ metadata.gz: 234f82023c22e4f0acffc3456eee6080b73cfad4516a34cb645a6a1dcc9a742c743e09368f71e15187721805ada57e3d2fbc892413677eb0daa0889bb98c6cbd
7
+ data.tar.gz: '095bc1362d552dae0d1e8e56808fc993a92f47a529ef584fd0fdbbc6705cda1cb40237beb06b64d0ba0f28fe828b3179b76712f652e522658a110104bfbc1d57'
@@ -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,7 +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)
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)
33
44
  colours, line_length = [], -1
34
45
  temp = ''
35
46
  flatten_colours = arg_colours.flatten
@@ -38,7 +49,6 @@ class String
38
49
  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
50
 
40
51
  all_rgbs = flatten_colours.map!(&method(:hex_to_rgb))
41
-
42
52
  block_given = block_given?
43
53
 
44
54
  r, g, b = all_rgbs[0]
@@ -49,11 +59,48 @@ class String
49
59
 
50
60
  each_line do |c|
51
61
  _r, _g, _b = r, g, b
62
+ chomped = !!c.chomp!(''.freeze)
63
+
52
64
  n = c.length
65
+ n_variable = exclude_spaces ? c.delete("\t\s".freeze).length : n
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
81
+ end
53
82
 
54
- r_meth = r == r2 ? :itself : r2 > r ? [:+, r2.fdiv(n)] : [:-, r.fdiv(n)]
55
- g_meth = g == g2 ? :itself : g2 > g ? [:+, g2.fdiv(n)] : [:-, g.fdiv(n)]
56
- b_meth = b == b2 ? :itself : b2 > b ? [:+, b2.fdiv(n)] : [:-, b.fdiv(n)]
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
+ # Note that duplication is fine if we don't get a little bit performance loss
101
+ _r = _r.send(r_op, r_val * -1) if r_comp_op && _r.send(r_comp_op, r_comp_val)
102
+ _g = _g.send(g_op, g_val * -1) if g_comp_op && _g.send(g_comp_op, g_comp_val)
103
+ _b = _b.send(b_op, b_val * -1) if b_comp_op && _b.send(b_comp_op, b_comp_val)
57
104
 
58
105
  if line_length != n || rotate
59
106
  line_length = n
@@ -61,14 +108,33 @@ class String
61
108
 
62
109
  i = -1
63
110
  while (i += 1) < n
64
- _r = _r.send(*r_meth)
65
- _g = _g.send(*g_meth)
66
- _b = _b.send(*b_meth)
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
67
133
 
68
134
  colours << [
69
- (_r < 0 ? 0 : _r > 255 ? 255 : _r.to_i),
70
- (_g < 0 ? 0 : _g > 255 ? 255 : _g.to_i),
71
- _b < 0 ? 0 : _b > 255 ? 255 : _b.to_i
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,
72
138
  ]
73
139
  end
74
140
  end
@@ -85,9 +151,17 @@ class String
85
151
  end
86
152
 
87
153
  if block_given
88
- yield "\e[0m".freeze
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
89
159
  else
90
- temp << "\e[0m".freeze
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
91
165
  end
92
166
 
93
167
  if rotate
@@ -126,7 +200,7 @@ class String
126
200
  elsif clen == 6
127
201
  colour.chars.each_slice(2).map { |x| x.join.to_i(16) }
128
202
  else
129
- sli = clen > 6 ? 'too long' : clen < 3 ? 'too short' : 'invalid'
203
+ sli = clen > 6 ? 'too long'.freeze : clen < 3 ? 'too short'.freeze : 'invalid'.freeze
130
204
  raise ArgumentError, "Invalid Hex Colour ##{colour} (length #{sli})"
131
205
  end
132
206
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StringDotGradient
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.6"
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.1
4
+ version: 0.1.6
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-07 00:00:00.000000000 Z
11
+ date: 2021-01-09 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: []