string_dot_gradient 0.1.5 → 0.2.0

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: 107ebffe10579b889cfe5b6bd0ebbc774375674fd7e9db1998f3fc4561d86b12
4
- data.tar.gz: 26588857b88ff2f8a732a840a3807d7d327448b55cc02eb385294e21c718d71b
3
+ metadata.gz: 888e756d71707ea2545d43cf8a3fdc38ac9a390570fe7b9e52271c89e7af3d7d
4
+ data.tar.gz: fc1bf3010fca21e26c7ee07504ad2e76dd66affe2eff5acd6b316ebf2805a243
5
5
  SHA512:
6
- metadata.gz: 5c40057440f5a6d8c3d1855c3defa40e878a1aeadaefb3e7fda49fd95521135d0c3c418b10dfe3ffabb8b743e25bce3962cafebfb1b4725392619287c67f1938
7
- data.tar.gz: 668f2212665a28aeabcc58cfd823d5a0995617f785a93d10ab6c301a714328dc057f9d3dade654a3021296a596de8fadec3dd30104681aaa3215426d89cf0493
6
+ metadata.gz: c9fce9010dba35978ef9b93e5336911f534b6d37c8635817240414a86411317f32626a6a50c0a52ef8bab7f27f12a7912fbaec572fa200a7b2011c858d8c5c0d
7
+ data.tar.gz: 9cbf19d2ef2361f1d2bd6cd2ec39ea3e96264617bf5ba0a216c542bc4c401da58a72927095b360a204c0040c6e84d2688328002b0601e628b4eca5e6d9cd2157
@@ -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,112 +40,137 @@ 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)
44
- colours, line_length = [], -1
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
+
45
61
  temp = ''
46
62
  flatten_colours = arg_colours.flatten
47
63
 
64
+ # Create the styling here rather than creating it in the each_line loop
65
+ # We also make it a bit different, rather than using \e[1m\e[5m, we will do
66
+ # \e[1;5m to save the number of characters spit out by this method
67
+ style = nil
68
+
69
+ if bold || italic || underline || blink || strikethrough || double_underline || overline
70
+ style = "\e["
71
+
72
+ style << '1;'.freeze if bold
73
+ style << '3;'.freeze if italic
74
+ style << '4;'.freeze if underline
75
+ style << '5;'.freeze if blink
76
+ style << '9;'.freeze if strikethrough
77
+ style << '21;'.freeze if double_underline
78
+ style << '53;'.freeze if overline
79
+
80
+ style.chop!
81
+ style << ?m.freeze
82
+ end
83
+
48
84
  raise ArgumentError, "Wrong numeber of colours (given #{flatten_colours.length}, expected minimum 2)" if flatten_colours.length < 2
49
85
  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)) }
50
86
 
51
87
  all_rgbs = flatten_colours.map!(&method(:hex_to_rgb))
52
88
  block_given = block_given?
53
89
 
54
- r, g, b = all_rgbs[0]
55
- r2, g2, b2 = all_rgbs[1]
90
+ # r, g, b => starting r, g, b
91
+ # r2, g2, b2 => stopping r, g, b
92
+ r, g, b = *all_rgbs[0]
93
+ r2, g2, b2 = *all_rgbs[1]
56
94
  rotate = all_rgbs.length > 2
57
95
 
58
96
  init = bg ? 48 : 38
59
97
 
60
98
  each_line do |c|
99
+ temp << style if style
100
+
61
101
  _r, _g, _b = r, g, b
62
- n = c.length
63
- n_variable = exclude_spaces ? c.dup.delete("\t\s".freeze).length : n
102
+ chomped = !!c.chomp!(''.freeze)
64
103
 
65
- r_op = r_val = r_max = r_min = nil
66
- g_op = g_val = g_max = g_min = nil
67
- b_op = b_val = b_max = b_min = nil
104
+ len = c.length
105
+ n_variable = exclude_spaces ? c.delete("\t\s".freeze).length : len
106
+ n_variable -= 1
107
+ n_variable = 1 if n_variable < 1
68
108
 
69
- r_comp_op = r_comp_val = nil
70
- g_comp_op = g_comp_val = nil
71
- b_comp_op = b_comp_val = nil
109
+ # colour operator, colour value
110
+ r_op = r_val = nil
111
+ g_op = g_val = nil
112
+ b_op = b_val = nil
72
113
 
73
114
  if r2 > r
74
- r_op, r_val, r_max, r_min = :+, r2.fdiv(n_variable), r2, r
75
- r_comp_op, r_comp_val = :<=, r_max
115
+ r_op, r_val = :+, r2.-(r).fdiv(n_variable)
76
116
  elsif r2 < r
77
- r_op, r_val, r_max, r_min = :-, r.fdiv(n_variable), r, r2
78
- r_comp_op, r_comp_val = :>=, r_min
117
+ r_op, r_val = :-, r.-(r2).fdiv(n_variable)
79
118
  end
80
119
 
81
120
  if g2 > g
82
- g_op, g_val, g_max, g_min = :+, g2.fdiv(n_variable), g2, g
83
- g_comp_op, g_comp_val = :<=, g_max
121
+ g_op, g_val = :+, g2.-(g).fdiv(n_variable)
84
122
  elsif g2 < g
85
- g_op, g_val, g_max, g_min = :-, g.fdiv(n_variable), g, g2
86
- g_comp_op, g_comp_val = :>=, g_min
123
+ g_op, g_val = :-, g.-(g2).fdiv(n_variable)
87
124
  end
88
125
 
89
126
  if b2 > b
90
- b_op, b_val, b_max, b_min = :+, b2.fdiv(n_variable), b2, b
91
- b_comp_op, b_comp_val = :<=, b_max
127
+ b_op, b_val = :+, b2.-(b).fdiv(n_variable)
92
128
  elsif b2 < b
93
- b_op, b_val, b_max, b_min = :-, b.fdiv(n_variable), b, b2
94
- b_comp_op, b_comp_val = :>=, b_min
129
+ b_op, b_val = :-, b.-(b2).fdiv(n_variable)
95
130
  end
96
131
 
97
- if line_length != n || rotate
98
- line_length = n
99
- colours.clear
100
-
101
- i = -1
102
- while (i += 1) < n
103
- if exclude_spaces
104
- _c = c[i]
105
-
106
- if !(_c == ?\s.freeze || _c == ?\t.freeze)
107
- _r = _r.send(r_op, r_val) if r_comp_op && _r.send(r_comp_op, r_comp_val)
108
- _g = _g.send(g_op, g_val) if g_comp_op && _g.send(g_comp_op, g_comp_val)
109
- _b = _b.send(b_op, b_val) if b_comp_op && _b.send(b_comp_op, b_comp_val)
110
- end
111
- else
112
- # We also have duplication above,
113
- # But we are not going to remove this code unless
114
- # we find some efficient solution. Using a proc or method
115
- # for setting these values and calling that is a way
116
- # to make code slower.
117
- _r = _r.send(r_op, r_val) if r_comp_op && _r.send(r_comp_op, r_comp_val)
118
- _g = _g.send(g_op, g_val) if g_comp_op && _g.send(g_comp_op, g_comp_val)
119
- _b = _b.send(b_op, b_val) if b_comp_op && _b.send(b_comp_op, b_comp_val)
120
- end
121
-
122
- r_to_i = _r.to_i
123
- g_to_i = _g.to_i
124
- b_to_i = _b.to_i
125
-
126
- colours << [
127
- r_to_i < 0 ? 0 : r_to_i > 255 ? 255 : r_to_i,
128
- g_to_i < 0 ? 0 : g_to_i > 255 ? 255 : g_to_i,
129
- b_to_i < 0 ? 0 : b_to_i > 255 ? 255 : b_to_i,
130
- ]
131
- end
132
- end
132
+ # To avoid the value getting adding | subtracted from the initial character
133
+ _r = _r.send(r_op, r_val * -1) if r_op
134
+ _g = _g.send(g_op, g_val * -1) if g_op
135
+ _b = _b.send(b_op, b_val * -1) if b_op
133
136
 
134
137
  i = -1
135
- while (i += 1) < n
138
+ while (i += 1) < len
139
+ _c = c[i]
140
+
141
+ if !exclude_spaces || (exclude_spaces && !(_c == ?\s.freeze || _c == ?\t.freeze))
142
+ _r = _r.send(r_op, r_val) if r_op
143
+ _g = _g.send(g_op, g_val) if g_op
144
+ _b = _b.send(b_op, b_val) if b_op
145
+ end
146
+
147
+ r_to_i = _r.to_i
148
+ g_to_i = _g.to_i
149
+ b_to_i = _b.to_i
150
+
151
+ f_r = r_to_i < 0 ? 0 : r_to_i > 255 ? 255 : r_to_i
152
+ f_g = g_to_i < 0 ? 0 : g_to_i > 255 ? 255 : g_to_i
153
+ f_b = b_to_i < 0 ? 0 : b_to_i > 255 ? 255 : b_to_i
154
+
155
+ ret = "\e[#{init};2;#{f_r};#{f_g};#{f_b}m#{_c}"
156
+
136
157
  if block_given
137
- yield "\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
158
+ yield ret
138
159
  else
139
- temp.concat(
140
- "\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
141
- )
160
+ temp << ret
142
161
  end
143
162
  end
144
163
 
164
+ ret = if !c.empty?
165
+ chomped ? "\e[0m\n".freeze : "\e[0m".freeze
166
+ elsif chomped
167
+ ?\n.freeze
168
+ end
169
+
145
170
  if block_given
146
- yield "\e[0m".freeze
171
+ yield ret
147
172
  else
148
- temp << "\e[0m".freeze
173
+ temp << ret
149
174
  end
150
175
 
151
176
  if rotate
@@ -161,6 +186,8 @@ class String
161
186
  private
162
187
  def hex_to_rgb(hex)
163
188
  # Duplicate colour, even if colour is nil
189
+ # This workaround is for Ruby 2.0 to Ruby 2.2
190
+ # Which won't allow duplicate nil.
164
191
  colour = hex && hex.dup.to_s || ''
165
192
  colour.strip!
166
193
  colour.downcase!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StringDotGradient
4
- VERSION = "0.1.5"
4
+ VERSION = "0.2.0"
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.5
4
+ version: 0.2.0
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-15 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: []
@@ -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 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: []