colours 0.8.12 → 0.9.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +331 -200
- data/bin/colours +2 -2
- data/bin/show_basic_colour_palette +7 -0
- data/colours.gemspec +1 -2
- data/doc/README.gen +294 -178
- data/doc/how_to_pick_your_own_colours/how_to_pick_your_own_colours.md +33 -0
- data/lib/colours/autoalias_e.rb +4 -1
- data/lib/colours/autogenerated/256_colours_instance_methods.rb +1429 -0
- data/lib/colours/autogenerated/256_colours_methods.rb +1429 -0
- data/lib/colours/autogenerated/html_colours_instance_methods.rb +1191 -0
- data/lib/colours/autogenerated/html_colours_methods.rb +1191 -0
- data/lib/colours/base/base.rb +61 -13
- data/lib/colours/class/colours.rb +8 -6
- data/lib/colours/{toplevel_methods/use_colours.rb → colour/colour.rb} +4 -125
- data/lib/colours/commandline/README.md +2 -1
- data/lib/colours/commandline/commandline.rb +177 -34
- data/lib/colours/constants/constants.rb +752 -6
- data/lib/colours/e/README.md +6 -6
- data/lib/colours/eparse/eparse.rb +2 -1
- data/lib/colours/essentials/README.md +5 -0
- data/lib/colours/{basic_colours/basic_colours.rb → essentials/essentials.rb} +87 -85
- data/lib/colours/html_colours/README.md +2 -1
- data/lib/colours/html_colours/hash_html_colours.rb +168 -0
- data/lib/colours/html_colours/html_colours.rb +226 -244
- data/lib/colours/html_colours/push_the_html_colours_methods_onto_the_toplevel_namespace.rb +30 -0
- data/lib/colours/html_colours/random_html_colour.rb +67 -0
- data/lib/colours/{requires/require_the_basic_colours.rb → html_colours.rb} +4 -2
- data/lib/colours/map_symbol_to_corresponding_colour/map_symbol_to_corresponding_colour.rb +12 -10
- data/lib/colours/module_256_colours/module_256_colours.rb +465 -0
- data/lib/colours/project/project.rb +3 -1
- data/lib/colours/rainbow_colours/check_for_trollop_being_available_or_exit.rb +4 -0
- data/lib/colours/rainbow_colours/constants.rb +3 -3
- data/lib/colours/rainbow_colours/do_parse_via_rainbow_colours.rb +9 -10
- data/lib/colours/rainbow_colours/print_rainbow_line.rb +4 -4
- data/lib/colours/rainbow_colours/println_ani.rb +7 -7
- data/lib/colours/rainbow_colours/println_plain.rb +2 -2
- data/lib/colours/rainbow_colours/rainbow.rb +31 -1
- data/lib/colours/rainbow_colours/report_errors.rb +7 -7
- data/lib/colours/rainbow_colours/returnln_plain.rb +3 -3
- data/lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb +409 -0
- data/lib/colours/requires/require_autogenerated_colour_methods.rb +4 -1
- data/lib/colours/requires/{require_the_colour_table.rb → require_essentials.rb} +2 -2
- data/lib/colours/requires/require_save_file.rb +11 -0
- data/lib/colours/requires/require_the_256_colours_module.rb +1 -3
- data/lib/colours/requires/require_the_colour_methods.rb +1 -22
- data/lib/colours/requires/require_the_project.rb +31 -29
- data/lib/colours/requires/require_the_toplevel_methods.rb +2 -20
- data/lib/colours/rgb/rgb.rb +107 -89
- data/lib/colours/{colour_methods/colour_methods.rb → sfile_sdir_sfancy_swarn_simp_scomments_and_ssymlink/sfile_sdir_sfancy_swarn_simp_scomments_and_ssymlink.rb} +467 -405
- data/lib/colours/testing/testing.rb +1 -3
- data/lib/colours/toplevel_methods/e.rb +9 -13
- data/lib/colours/toplevel_methods/map_this_symbol_to_that_html_colour.rb +57 -0
- data/lib/colours/toplevel_methods/{revert.rb → rev.rb} +5 -48
- data/lib/colours/toplevel_methods/toplevel_methods.rb +1204 -0
- data/lib/colours/version/version.rb +2 -2
- data/lib/colours/yaml/256_colours.yml +776 -257
- data/lib/colours/yaml/html_colours.yml +1 -1
- data/lib/colours/yaml/prefer_this_colour_schemata.yml +1 -1
- data/lib/colours/yaml/use_these_values_for_the_colour_methods.yml +5 -1
- data/lib/colours.rb +1 -1
- data/test/testing_256_colours_support.rb +10 -1
- data/test/testing_replace_number_words_with_the_corresponding_html_colour.rb +15 -9
- data/test/testing_the_colour_methods_such_as_simp_sdir_sfile_swarn_sfancy_sargument_and_ssymlink.rb +53 -0
- metadata +30 -57
- data/doc/HOW_TO_PICK_YOUR_OWN_COLOURS.md +0 -28
- data/lib/colours/256_colours/support_for_256_colours.rb +0 -196
- data/lib/colours/autogenerated/support_for_256_colours.rb +0 -2235
- data/lib/colours/autogenerated/support_for_html_colours.rb +0 -1778
- data/lib/colours/autogenerated/toplevel_basic_colour_methods.rb +0 -7001
- data/lib/colours/basic_colours/README.md +0 -4
- data/lib/colours/colour_methods/README.md +0 -11
- data/lib/colours/colour_table/README.md +0 -2
- data/lib/colours/colour_table/colour_table.rb +0 -282
- data/lib/colours/commandline/menu.rb +0 -122
- data/lib/colours/constants/escape.rb +0 -22
- data/lib/colours/constants/file_constants.rb +0 -73
- data/lib/colours/constants/hash_ansi_colours.rb +0 -39
- data/lib/colours/constants/hash_simple_colours.rb +0 -148
- data/lib/colours/constants/misc.rb +0 -361
- data/lib/colours/constants/newline.rb +0 -14
- data/lib/colours/constants/registered_colour_methods.rb +0 -53
- data/lib/colours/html_colours/add_html_colours_onto_the_toplevel_namespace.rb +0 -22
- data/lib/colours/toplevel_methods/autogenerate.rb +0 -310
- data/lib/colours/toplevel_methods/bold.rb +0 -41
- data/lib/colours/toplevel_methods/bold_and_italic.rb +0 -38
- data/lib/colours/toplevel_methods/cat.rb +0 -39
- data/lib/colours/toplevel_methods/clear_screen.rb +0 -18
- data/lib/colours/toplevel_methods/cliner.rb +0 -17
- data/lib/colours/toplevel_methods/col.rb +0 -54
- data/lib/colours/toplevel_methods/esystem.rb +0 -19
- data/lib/colours/toplevel_methods/html_colour_to_hex_value.rb +0 -41
- data/lib/colours/toplevel_methods/html_colourize.rb +0 -69
- data/lib/colours/toplevel_methods/is_on_roebe.rb +0 -16
- data/lib/colours/toplevel_methods/italic.rb +0 -110
- data/lib/colours/toplevel_methods/make_colour.rb +0 -28
- data/lib/colours/toplevel_methods/methods_related_to_html_colours.rb +0 -336
- data/lib/colours/toplevel_methods/misc.rb +0 -226
- data/lib/colours/toplevel_methods/open_this_file.rb +0 -26
- data/lib/colours/toplevel_methods/prefer_this_colour_schemata.rb +0 -88
- data/lib/colours/toplevel_methods/random_value.rb +0 -37
- data/lib/colours/toplevel_methods/remove_escape_sequence.rb +0 -112
- data/lib/colours/toplevel_methods/set_last_colour_used.rb +0 -32
- data/lib/colours/toplevel_methods/shell_file_containing_the_html_colours.sh +0 -148
- data/lib/colours/toplevel_methods/show_basic_colour_palette.rb +0 -36
- data/lib/colours/toplevel_methods/underline.rb +0 -130
- data/test/testing_the_colour_methods.rb +0 -36
- /data/doc/{COLOUR_CODES_CHART.md → colour_codes_chart/colour_codes_chart.md} +0 -0
@@ -0,0 +1,1204 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# === Colours
|
6
|
+
# =========================================================================== #
|
7
|
+
# require 'colours/toplevel_methods/toplevel_methods.rb'
|
8
|
+
# Colours.return_a_unique_array_containing_all_available_colours
|
9
|
+
# =========================================================================== #
|
10
|
+
module Colours
|
11
|
+
|
12
|
+
require 'colours/requires/require_yaml.rb'
|
13
|
+
require 'colours/constants/constants.rb'
|
14
|
+
require 'colours/toplevel_methods/e.rb'
|
15
|
+
require 'colours/toplevel_methods/rev.rb'
|
16
|
+
require 'colours/eparse/eparse.rb'
|
17
|
+
require 'colours/version/version.rb'
|
18
|
+
|
19
|
+
# ========================================================================= #
|
20
|
+
# === @prefer_this_colour_schemata
|
21
|
+
#
|
22
|
+
# The following Array ranks which colour schemata we will try to use.
|
23
|
+
# More important entries should come on top.
|
24
|
+
#
|
25
|
+
# Konsole will be the default, normally, followed by the 256 colours; and
|
26
|
+
# then the basic colours.
|
27
|
+
#
|
28
|
+
# Of course you can decide on your own via the .yml file, and other
|
29
|
+
# methods that allow us to change this setting, but when it comes
|
30
|
+
# to implementations such as for simp() or sfancy() then we will
|
31
|
+
# honour the setting in the yaml file (if we use any colours at all,
|
32
|
+
# that is). The yaml file is typically called
|
33
|
+
# "prefer_this_colour_schemata.yml".
|
34
|
+
# ========================================================================= #
|
35
|
+
if File.exist? FILE_PREFER_THIS_COLOUR_SCHEMATA
|
36
|
+
@prefer_this_colour_schemata = YAML.load_file(FILE_PREFER_THIS_COLOUR_SCHEMATA)
|
37
|
+
end
|
38
|
+
|
39
|
+
# ========================================================================= #
|
40
|
+
# === Colours.prefer_which_colour_scheme?
|
41
|
+
# ========================================================================= #
|
42
|
+
def self.prefer_which_colour_scheme?
|
43
|
+
@prefer_this_colour_schemata.first.to_sym
|
44
|
+
end; self.instance_eval { alias prefer_which_colour_schemata? prefer_which_colour_scheme? } # === Colours.prefer_which_colour_schemata?
|
45
|
+
|
46
|
+
# ========================================================================= #
|
47
|
+
# === @use_colours
|
48
|
+
#
|
49
|
+
# Whether we will use colours or whether we will not. This can be
|
50
|
+
# used to globally disable colours in every project using the
|
51
|
+
# Colours gem.
|
52
|
+
# ========================================================================= #
|
53
|
+
@use_colours = true
|
54
|
+
|
55
|
+
# ========================================================================= #
|
56
|
+
# === Colours.use_colours?
|
57
|
+
#
|
58
|
+
# Use this method to find out whether we wish to use colours or
|
59
|
+
# whether we do not.
|
60
|
+
# ========================================================================= #
|
61
|
+
def self.use_colours?
|
62
|
+
@use_colours
|
63
|
+
end
|
64
|
+
|
65
|
+
# ========================================================================= #
|
66
|
+
# === use_colours?
|
67
|
+
#
|
68
|
+
# Delegate towards the class-method here. Unsure whether this method
|
69
|
+
# is really needed, but let's keep it for now. After all we can
|
70
|
+
# use "include Colours" to add that method.
|
71
|
+
# ========================================================================= #
|
72
|
+
def use_colours?
|
73
|
+
::Colours.use_colours?
|
74
|
+
end
|
75
|
+
|
76
|
+
# ========================================================================= #
|
77
|
+
# === @last_colour_used
|
78
|
+
#
|
79
|
+
# This instance variable keeps track what the last colour used was.
|
80
|
+
# ========================================================================= #
|
81
|
+
@last_colour_used = nil
|
82
|
+
|
83
|
+
# ========================================================================= #
|
84
|
+
# === last_colour_used?
|
85
|
+
#
|
86
|
+
# Tell us which colour was used last.
|
87
|
+
# ========================================================================= #
|
88
|
+
def last_colour_used?
|
89
|
+
@last_colour_used.to_s # Always required a string.
|
90
|
+
end
|
91
|
+
|
92
|
+
# ========================================================================= #
|
93
|
+
# === Colours.set_last_colour_used
|
94
|
+
# ========================================================================= #
|
95
|
+
def self.set_last_colour_used(i)
|
96
|
+
@last_colour_used = i
|
97
|
+
end
|
98
|
+
|
99
|
+
# ========================================================================= #
|
100
|
+
# === Colours.esystem
|
101
|
+
# ========================================================================= #
|
102
|
+
def self.esystem(i)
|
103
|
+
e i
|
104
|
+
system i
|
105
|
+
end
|
106
|
+
|
107
|
+
# ========================================================================= #
|
108
|
+
# === Colours.read_and_display_this_file
|
109
|
+
#
|
110
|
+
# This method accepts a file path - in other words, the input should be
|
111
|
+
# the location of a specific file on your filesystem - and then simply
|
112
|
+
# reads in the content of said file via File.readlines().
|
113
|
+
#
|
114
|
+
# Content such as <slateblue> found in this file will be replaced
|
115
|
+
# with the respective R,G,B substring value.
|
116
|
+
#
|
117
|
+
# In the test/ subdirectory there is an example file for this - have
|
118
|
+
# a look there for more information.
|
119
|
+
# ========================================================================= #
|
120
|
+
def self.read_and_display_this_file(
|
121
|
+
i = TEST_FILE
|
122
|
+
)
|
123
|
+
i = i.to_s
|
124
|
+
if File.exist? i
|
125
|
+
new_array = []
|
126
|
+
data = File.readlines(i)
|
127
|
+
data.each {|entry|
|
128
|
+
# ======================================================================= #
|
129
|
+
# Check whether the entry has a <> tag:
|
130
|
+
# ======================================================================= #
|
131
|
+
if entry.include?('<') and entry.include?('>') and
|
132
|
+
entry.include?('</>')
|
133
|
+
# ================================================================= #
|
134
|
+
# Ok, we may assume that something like <slateblue> is there.
|
135
|
+
# ================================================================= #
|
136
|
+
entry = sanitize_line(entry)
|
137
|
+
end
|
138
|
+
new_array << entry
|
139
|
+
}
|
140
|
+
e new_array # For now we will simply output that modified Array.
|
141
|
+
else
|
142
|
+
e "Notice: The file at `#{sfile(i)}` does not exist."
|
143
|
+
end
|
144
|
+
end; self.instance_eval { alias read_file read_and_display_this_file } # === Colours.read_file
|
145
|
+
|
146
|
+
# ========================================================================= #
|
147
|
+
# === Colours.return_italic
|
148
|
+
#
|
149
|
+
# To test this method, try:
|
150
|
+
#
|
151
|
+
# Colours.italic('Hello world!')
|
152
|
+
#
|
153
|
+
# ========================================================================= #
|
154
|
+
def self.return_italic(
|
155
|
+
show_this_text = 'This must be italic.',
|
156
|
+
make_newline = false
|
157
|
+
)
|
158
|
+
result = "\x1b".dup
|
159
|
+
result << '['
|
160
|
+
result << ITALIC_CODE
|
161
|
+
if block_given?
|
162
|
+
# ===================================================================== #
|
163
|
+
# Right now we assume that this must be a colour.
|
164
|
+
# ===================================================================== #
|
165
|
+
result << ";38;2;"
|
166
|
+
result << html_colour_to_stringified_rgb_values(yield)
|
167
|
+
end
|
168
|
+
result << 'm'
|
169
|
+
if make_newline
|
170
|
+
show_this_text = "#{show_this_text}\n"
|
171
|
+
end
|
172
|
+
result << show_this_text
|
173
|
+
result << REVERT
|
174
|
+
result
|
175
|
+
end; self.instance_eval { alias string_italic return_italic } # === Colours.string_italic
|
176
|
+
|
177
|
+
# ========================================================================= #
|
178
|
+
# === Colours.make_colour
|
179
|
+
#
|
180
|
+
# This is used primarily for testing.
|
181
|
+
# ========================================================================= #
|
182
|
+
def self.make_colour(
|
183
|
+
what_colour_to_use
|
184
|
+
)
|
185
|
+
i = what_colour_to_use # Copy.
|
186
|
+
constant = i.to_s.upcase
|
187
|
+
name_of_colour = const_get(constant)
|
188
|
+
string = '%-34s' % ("#{name_of_colour} Test with #{constant}.")
|
189
|
+
string << IS_A_TEST+'('+name_of_colour.inspect.delete('"')+')'
|
190
|
+
e(string)
|
191
|
+
end
|
192
|
+
|
193
|
+
# ========================================================================= #
|
194
|
+
# === Colours.disable_colours
|
195
|
+
#
|
196
|
+
# This method can be used to disable the colours on the Colours
|
197
|
+
# namespace.
|
198
|
+
# ========================================================================= #
|
199
|
+
def self.disable_colours(
|
200
|
+
be_verbose = false
|
201
|
+
)
|
202
|
+
puts 'Disabling colours next.' if be_verbose
|
203
|
+
@use_colours = false
|
204
|
+
end; self.instance_eval { alias disable disable_colours } # === Colours.disable
|
205
|
+
|
206
|
+
# ========================================================================= #
|
207
|
+
# === Colours.html_colourize
|
208
|
+
#
|
209
|
+
# This method will simply return the colour-code + text.
|
210
|
+
#
|
211
|
+
# The third argument, called `append_revert`, can be used to determine
|
212
|
+
# whether we will append the revert code to the generated output String
|
213
|
+
# or whether we will not. By default we will, but there are examples
|
214
|
+
# where we may wish to assemble our own colour string, and in these
|
215
|
+
# cases we do not yet know which text is to be shown - hence, this
|
216
|
+
# must become an optional argument.
|
217
|
+
#
|
218
|
+
# A block can be passed to this method. If the block has, as content,
|
219
|
+
# the Symbol :omit_end then the end-part of the ANSI code will not
|
220
|
+
# be used.
|
221
|
+
#
|
222
|
+
# Usage examples:
|
223
|
+
#
|
224
|
+
# x = Colours.colourize('slateblue', 'Hello world!'); pp x
|
225
|
+
# y = Colours.colourize('slateblue', 'Hello world!') { :omit_end }; pp y
|
226
|
+
#
|
227
|
+
# ========================================================================= #
|
228
|
+
def self.html_colourize(
|
229
|
+
colour_to_use = return_random_html_colour,
|
230
|
+
this_text = nil,
|
231
|
+
append_revert = true,
|
232
|
+
&block
|
233
|
+
)
|
234
|
+
require 'colours/html_colours/html_colours.rb'
|
235
|
+
require 'colours/rgb/rgb.rb'
|
236
|
+
if block_given?
|
237
|
+
yielded = yield
|
238
|
+
case yielded
|
239
|
+
# ===================================================================== #
|
240
|
+
# === :omit_end
|
241
|
+
# ===================================================================== #
|
242
|
+
when :omit_end
|
243
|
+
append_revert = false
|
244
|
+
end
|
245
|
+
end
|
246
|
+
if this_text.nil?
|
247
|
+
this_text = colour_to_use.to_s.dup
|
248
|
+
end
|
249
|
+
if append_revert
|
250
|
+
"#{rgb_value_as_escape_code_string(colour_to_use)}#{this_text}#{revert}"
|
251
|
+
else
|
252
|
+
"#{rgb_value_as_escape_code_string(colour_to_use)}#{this_text}"
|
253
|
+
end
|
254
|
+
end; self.instance_eval { alias colourize html_colourize } # === Colours.colourize
|
255
|
+
self.instance_eval { alias kde_colour html_colourize } # === Colours.kde_colour
|
256
|
+
self.instance_eval { alias konsole_colours html_colourize } # === Colours.konsole_colours
|
257
|
+
|
258
|
+
# ========================================================================= #
|
259
|
+
# === Colours.enable_colours
|
260
|
+
#
|
261
|
+
# Use this method to enable colours for the whole Colours namespace.
|
262
|
+
# ========================================================================= #
|
263
|
+
def self.enable_colours(
|
264
|
+
be_verbose = false
|
265
|
+
)
|
266
|
+
puts 'Enabling colours next.' if be_verbose
|
267
|
+
@use_colours = true # Defined below.
|
268
|
+
end; self.instance_eval { alias enable enable_colours } # === Colours.enable
|
269
|
+
|
270
|
+
# ========================================================================= #
|
271
|
+
# === Colours.show_help (help tag)
|
272
|
+
# ========================================================================= #
|
273
|
+
def self.show_help
|
274
|
+
e
|
275
|
+
e 'The commandline-interface for the Colours project supports '\
|
276
|
+
'the following documented instructions:'
|
277
|
+
e
|
278
|
+
eparse ' file? # read in from the default test file'
|
279
|
+
eparse ' html_colours # show all html colours'
|
280
|
+
eparse ' open # open this file here in your editor'
|
281
|
+
eparse ' --version? # report the current version of '\
|
282
|
+
'the colours gem'
|
283
|
+
eparse ' --base-dir? # show the project base directory '\
|
284
|
+
'of the Colours namespace'
|
285
|
+
eparse ' --show-html-colours # show the available HTML colours'
|
286
|
+
e
|
287
|
+
end; self.instance_eval { alias help show_help } # === Colours.help
|
288
|
+
|
289
|
+
# ========================================================================= #
|
290
|
+
# === underline
|
291
|
+
# ========================================================================= #
|
292
|
+
def underline(
|
293
|
+
i = '',
|
294
|
+
make_newline = false
|
295
|
+
)
|
296
|
+
Colours.underline(i, make_newline)
|
297
|
+
end
|
298
|
+
|
299
|
+
# ========================================================================= #
|
300
|
+
# === Colours.return_main_regex_to_use_for_obtaining_the_full_content
|
301
|
+
#
|
302
|
+
# The first regex that is commented out, in the body of this method,
|
303
|
+
# was in use until May 2023. It was then replaced with the new
|
304
|
+
# regex, which also has an associated test-case, in the test/
|
305
|
+
# subdirectory of the colours gem.
|
306
|
+
#
|
307
|
+
# For the current regex see here:
|
308
|
+
#
|
309
|
+
# https://rubular.com/r/1dpSmYqQ9SVvMt
|
310
|
+
#
|
311
|
+
# I was also using a second regex that was slightly incompatible.
|
312
|
+
# For legacy reasons it can be seen here: https://rubular.com/r/sDNQd81MtciMJL
|
313
|
+
# ========================================================================= #
|
314
|
+
def self.return_main_regex_to_use_for_obtaining_the_full_content(this_colour)
|
315
|
+
# /<#{this_colour}>([\-\{\}\[\]\\\(\)\/a-zA-Z0-9’éúÄäÖöÜüÅαβß&%+−=≡πμ°₁₂₃₄₅₆₇₈₉⁰¹²³⁴⁵⁶⁷⁸⁹⁻!#\*~:;∨"',_\|\n\. ]+)<\/#{this_colour}>/
|
316
|
+
# /<#{this_numbered_word}>([-#~≥!\e=><;,→⁻⁺@^„“γαµ_₀₁₂₃₄₅₆₇₈₉²³⁴⁵⁶⁷⁸⁹äöüÄÖÜβß%&:≡°$A-Za-z0-9\n \?\\'\|\(\)\{\}\[\]\"\*\.\+]+)<\/#{this_numbered_word}>/
|
317
|
+
/<#{this_colour}>([\n₀₁₂₃₄₅₆₇₈₉⁰¹²³⁴⁵⁶⁷⁸⁹°→⁻⁺≥$@σπµμγ∨^αβßÅÄäéúÖöÜüa-zA-Z0-9’'"„“~#&%!,;:_+−=≡<>\*\\\/\.\{\}\(\)\|\[\]\?\- ]*?)<\/#{this_colour}>/
|
318
|
+
end
|
319
|
+
|
320
|
+
# ========================================================================= #
|
321
|
+
# === Colours.show_the_version
|
322
|
+
#
|
323
|
+
# Invocation example:
|
324
|
+
#
|
325
|
+
# colours --version?
|
326
|
+
#
|
327
|
+
# ========================================================================= #
|
328
|
+
def self.show_the_version
|
329
|
+
e steelblue(VERSION)
|
330
|
+
end
|
331
|
+
|
332
|
+
# ========================================================================= #
|
333
|
+
# === restore?
|
334
|
+
#
|
335
|
+
# This will restore to the default again.
|
336
|
+
# ========================================================================= #
|
337
|
+
def restore?
|
338
|
+
Colours.restore?
|
339
|
+
end; alias revert restore? # === revert
|
340
|
+
alias rev? restore? # === rev?
|
341
|
+
|
342
|
+
# ========================================================================= #
|
343
|
+
# === Colours.report_whether_colours_will_be_used
|
344
|
+
# ========================================================================= #
|
345
|
+
def self.report_whether_colours_will_be_used
|
346
|
+
if @use_colours
|
347
|
+
puts 'Yes, colours will be used.'
|
348
|
+
else
|
349
|
+
puts 'Colours are currently disabled.'
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
# ========================================================================= #
|
354
|
+
# === Colours.use_colours=
|
355
|
+
#
|
356
|
+
# This setter-method determines whether the colours gem will use
|
357
|
+
# colours or whether it will not.
|
358
|
+
#
|
359
|
+
# The first input argument given to this method should be a Boolean
|
360
|
+
# value, such as true or false.
|
361
|
+
# ========================================================================= #
|
362
|
+
def self.use_colours=(
|
363
|
+
new_value = true
|
364
|
+
)
|
365
|
+
@use_colours = new_value
|
366
|
+
end; self.instance_eval { alias set_use_colours use_colours= } # === Colours.set_use_colours
|
367
|
+
|
368
|
+
# ========================================================================= #
|
369
|
+
# === Colours.replace_html_colours_in_this_string
|
370
|
+
#
|
371
|
+
# This method will replace all HTML colours in a given string, such as
|
372
|
+
# "<slateblue>test</slateblue>", with the corresponding RGB colour
|
373
|
+
# variant for the commandline.
|
374
|
+
#
|
375
|
+
# Typically this refers to a terminal such as the KDE Konsole, and a
|
376
|
+
# shell such as bash (although other shells are fine too, and many
|
377
|
+
# other terminals, such as the gnome-terminal, most likely will work
|
378
|
+
# fine as well - but it is optimized for the KDE Konsole).
|
379
|
+
#
|
380
|
+
# This method should only be called after a prior check was done,
|
381
|
+
# to determine whether the given input String at hand does indeed
|
382
|
+
# include a valid HTML colour; otherwise it would be fairly pointless
|
383
|
+
# to invoke this method, if it is already known that the String at
|
384
|
+
# hand does not contain any HTML colour at all. In order to
|
385
|
+
# determine whether a String may include a valid HTML colour,
|
386
|
+
# the method called line_contains_a_valid_html_colour?() can be
|
387
|
+
# used.
|
388
|
+
#
|
389
|
+
# Usage example:
|
390
|
+
#
|
391
|
+
# Colours.replace_html_colours_in_this_string
|
392
|
+
# Colours.replace_html_colours_in_this_string('- The <one>UID</one> of <royalblue>the</royalblue> user called <two>root</two> is ... ? <one>0</one>.', :lightgreen)
|
393
|
+
#
|
394
|
+
# ========================================================================= #
|
395
|
+
def self.replace_html_colours_in_this_string(
|
396
|
+
i,
|
397
|
+
use_this_colour_for_the_default_colour = :default, # ← This specifies the default colour.
|
398
|
+
shall_we_revert_at_the_end_of_the_line = true
|
399
|
+
)
|
400
|
+
i = i.dup # We want to work on a copy.
|
401
|
+
result = ''.dup # Our result-string.
|
402
|
+
case use_this_colour_for_the_default_colour
|
403
|
+
# ======================================================================= #
|
404
|
+
# === :default
|
405
|
+
# ======================================================================= #
|
406
|
+
when :default,
|
407
|
+
:default_colour
|
408
|
+
use_this_colour_for_the_default_colour = USE_THIS_COLOUR_FOR_THE_DEFAULT_COLOUR
|
409
|
+
end
|
410
|
+
case shall_we_revert_at_the_end_of_the_line
|
411
|
+
# ======================================================================= #
|
412
|
+
# === :revert
|
413
|
+
# ======================================================================= #
|
414
|
+
when :revert
|
415
|
+
shall_we_revert_at_the_end_of_the_line = true
|
416
|
+
end
|
417
|
+
result << ::Colours::HtmlColoursMethods.send(use_this_colour_for_the_default_colour) { :omit_end }
|
418
|
+
# result = result.dup
|
419
|
+
# ======================================================================= #
|
420
|
+
# This method will make use of two different regexes.
|
421
|
+
# ======================================================================= #
|
422
|
+
scanned_results = i.scan(
|
423
|
+
REGEX_FOR_HTML_COLOURS
|
424
|
+
).flatten.uniq
|
425
|
+
add_on_string = i.dup
|
426
|
+
# ======================================================================= #
|
427
|
+
# scanned_results may be an Array such as ['steelblue', 'tomato']
|
428
|
+
# ======================================================================= #
|
429
|
+
scanned_results.each {|this_colour|
|
430
|
+
if is_this_a_valid_html_colour?(this_colour)
|
431
|
+
# =================================================================== #
|
432
|
+
# We must use .gsub!() because the colour-string may occur more
|
433
|
+
# than once. Unfortunately for longer Strings this becomes a bit
|
434
|
+
# fragile.
|
435
|
+
# =================================================================== #
|
436
|
+
part1 = ::Colours::HtmlColoursMethods.send(this_colour.to_sym, "\\1") { :omit_end }
|
437
|
+
part2 = ::Colours::HtmlColoursMethods.send(use_this_colour_for_the_default_colour) { :omit_end }
|
438
|
+
add_on_string.gsub!(
|
439
|
+
::Colours.return_main_regex_to_use_for_obtaining_the_full_content(this_colour),
|
440
|
+
part1+part2
|
441
|
+
).dup
|
442
|
+
end
|
443
|
+
}
|
444
|
+
result << add_on_string
|
445
|
+
if shall_we_revert_at_the_end_of_the_line
|
446
|
+
result << ::Colours.revert
|
447
|
+
end
|
448
|
+
return result
|
449
|
+
end; self.instance_eval { alias replace_html_colours replace_html_colours_in_this_string } # === Colours.replace_html_colours
|
450
|
+
self.instance_eval { alias replace_all_raw_html_colours_in_this_line replace_html_colours_in_this_string } # === Colours.replace_all_raw_html_colours_in_this_line
|
451
|
+
self.instance_eval { alias replace_all_html_colours_in_this_line replace_html_colours_in_this_string } # === Colours.replace_all_html_colours_in_this_line
|
452
|
+
self.instance_eval { alias parse_html_colour replace_html_colours_in_this_string } # === Colours.parse_html_colour
|
453
|
+
|
454
|
+
# ========================================================================= #
|
455
|
+
# === Colours.underline
|
456
|
+
#
|
457
|
+
# This method will "puts" the result of applying underline to a string,
|
458
|
+
# on the console/terminal. If you wish to do the output on your own
|
459
|
+
# then you have to use the method Colours.return_underline or its
|
460
|
+
# alias called Colours.string_underline.
|
461
|
+
#
|
462
|
+
# To test this, try:
|
463
|
+
#
|
464
|
+
# Colours.underline('Hello world!')
|
465
|
+
#
|
466
|
+
# ========================================================================= #
|
467
|
+
def self.underline(
|
468
|
+
i = '',
|
469
|
+
make_newline = false,
|
470
|
+
&block
|
471
|
+
)
|
472
|
+
e return_underline(i, make_newline, &block)
|
473
|
+
end
|
474
|
+
|
475
|
+
# ========================================================================= #
|
476
|
+
# === Colours.colour_method_or_display_the_content_of_the_file_or_use_via_pipe
|
477
|
+
#
|
478
|
+
# This method has an awful name, but its core use case is simple.
|
479
|
+
#
|
480
|
+
# It will handle commandline instructions such as:
|
481
|
+
#
|
482
|
+
# orange Hey there
|
483
|
+
# slateblue how are you doing?
|
484
|
+
#
|
485
|
+
# Usage example from within ruby:
|
486
|
+
#
|
487
|
+
# Colours.testing123('abc', :orange)
|
488
|
+
#
|
489
|
+
# ========================================================================= #
|
490
|
+
def self.colour_method_or_display_the_content_of_the_file_or_use_via_pipe(
|
491
|
+
i = ARGF,
|
492
|
+
name_of_the_colour = $PROGRAM_NAME
|
493
|
+
)
|
494
|
+
require 'colours/autogenerated/html_colours_methods.rb'
|
495
|
+
# ======================================================================= #
|
496
|
+
# === :read
|
497
|
+
# ======================================================================= #
|
498
|
+
if i.respond_to? :read
|
499
|
+
i = i.read
|
500
|
+
elsif i and File.exist?(i)
|
501
|
+
i = File.read(i)
|
502
|
+
end
|
503
|
+
i = Colours.remove_trailing_end_from(i)
|
504
|
+
if i.is_a? Array
|
505
|
+
i = i.join(' ').strip
|
506
|
+
end
|
507
|
+
if name_of_the_colour
|
508
|
+
if name_of_the_colour.respond_to?(:include?) and
|
509
|
+
name_of_the_colour.include?('/')
|
510
|
+
name_of_the_colour = File.basename(name_of_the_colour)
|
511
|
+
end
|
512
|
+
end
|
513
|
+
name_of_the_colour = name_of_the_colour.to_sym
|
514
|
+
return ::Colours::HtmlColoursMethods.send(name_of_the_colour, i)
|
515
|
+
end
|
516
|
+
|
517
|
+
# ========================================================================= #
|
518
|
+
# === Colours.generate_shell_file_containing_the_html_colours
|
519
|
+
#
|
520
|
+
# This method will generate a shell file into the current directory
|
521
|
+
# by default.
|
522
|
+
#
|
523
|
+
# The code in that file will generate a shell script that holds all
|
524
|
+
# the HTML-colours, via UPCASED names.
|
525
|
+
#
|
526
|
+
# These entries will then look like this:
|
527
|
+
#
|
528
|
+
# SADDLEBROWN: "\e[38;2;139;69;19m"
|
529
|
+
# PERU: "\e[38;2;205;133;63m"
|
530
|
+
# CHOCOLATE: "\e[38;2;210;105;30m"
|
531
|
+
#
|
532
|
+
# This can also be generated from the commandline, thanks to the
|
533
|
+
# bin/colours executable, via:
|
534
|
+
#
|
535
|
+
# colours --generate_shell_file_containing_the_html_colours
|
536
|
+
#
|
537
|
+
# ========================================================================= #
|
538
|
+
def self.generate_shell_file_containing_the_html_colours(
|
539
|
+
generate_the_shell_file_into_this_directory = Dir.pwd
|
540
|
+
)
|
541
|
+
require 'colours/html_colours/random_html_colour.rb'
|
542
|
+
require 'save_file/module'
|
543
|
+
unless generate_the_shell_file_into_this_directory.end_with? '/'
|
544
|
+
if generate_the_shell_file_into_this_directory.frozen?
|
545
|
+
generate_the_shell_file_into_this_directory = generate_the_shell_file_into_this_directory.dup
|
546
|
+
end
|
547
|
+
generate_the_shell_file_into_this_directory << '/'
|
548
|
+
end
|
549
|
+
result = ''.dup
|
550
|
+
result << "# This is a shell file that contains the HTML colours\n"
|
551
|
+
available_html_colours?.each {|this_colour|
|
552
|
+
colour_code_to_use = ::Colours.html_colourize(this_colour.to_sym,'',false).inspect
|
553
|
+
result << "export #{this_colour.upcase}=#{colour_code_to_use}\n"
|
554
|
+
}
|
555
|
+
generate_the_shell_file_into_this_directory << 'shell_file_containing_the_html_colours.sh'
|
556
|
+
into = generate_the_shell_file_into_this_directory
|
557
|
+
e "Now creating a shell file at `#{into}`."
|
558
|
+
SaveFile.write_what_into(result, into)
|
559
|
+
# ======================================================================= #
|
560
|
+
# Generate files on my home system as well:
|
561
|
+
# ======================================================================= #
|
562
|
+
if is_on_roebe?
|
563
|
+
into = '/home/x/programming/ruby/src/'\
|
564
|
+
'roebe/lib/roebe/shell_scripts/'+
|
565
|
+
File.basename(generate_the_shell_file_into_this_directory)
|
566
|
+
puts "And also saving into `#{into}`."
|
567
|
+
SaveFile.write_what_into(result, into)
|
568
|
+
end
|
569
|
+
end; self.instance_eval { alias autogenerate_shell_file_containing_the_html_colours generate_shell_file_containing_the_html_colours } # === Colours.autogenerate_shell_file_containing_the_html_colours
|
570
|
+
|
571
|
+
# ========================================================================= #
|
572
|
+
# === Colours.is_on_roebe?
|
573
|
+
# ========================================================================= #
|
574
|
+
def self.is_on_roebe?
|
575
|
+
ENV['IS_ROEBE'].to_s == '1'
|
576
|
+
end
|
577
|
+
|
578
|
+
# ========================================================================= #
|
579
|
+
# === Colours.open_this_file
|
580
|
+
#
|
581
|
+
# This method will make use of the editor called bluefish to open
|
582
|
+
# this file.
|
583
|
+
# ========================================================================= #
|
584
|
+
def open_this_file(
|
585
|
+
shall_we_exit = false
|
586
|
+
)
|
587
|
+
case shall_we_exit
|
588
|
+
# ======================================================================= #
|
589
|
+
# === :then_exit
|
590
|
+
# ======================================================================= #
|
591
|
+
when :then_exit
|
592
|
+
shall_we_exit = true
|
593
|
+
end
|
594
|
+
_ = "bluefish #{__FILE__}"
|
595
|
+
esystem(_)
|
596
|
+
exit if shall_we_exit
|
597
|
+
end
|
598
|
+
|
599
|
+
# ========================================================================= #
|
600
|
+
# === Colours.convert_hex_code_to_RGBA_array
|
601
|
+
#
|
602
|
+
# RGBA stands for "red, green, blue, alpha". Alpha indicates how
|
603
|
+
# opaque each pixel is.
|
604
|
+
#
|
605
|
+
# The usual values for the alpha parameter, aka the last parameter,
|
606
|
+
# is a number between 0.0 (which means "fully transparent") and
|
607
|
+
# the number 1.0 (which means "not transparent at all").
|
608
|
+
#
|
609
|
+
# Note that this method is similar to Colours.convert_hex_to_rgb(hex),
|
610
|
+
# but it has a fourth argument, aka A (for Alpha), on top of
|
611
|
+
# the RGB values.
|
612
|
+
#
|
613
|
+
# Usage example:
|
614
|
+
#
|
615
|
+
# Colours.convert_hex_code_to_RGBA_array('#baf185') # => [186, 241, 133]
|
616
|
+
#
|
617
|
+
# ========================================================================= #
|
618
|
+
def self.convert_hex_code_to_RGBA_array(
|
619
|
+
i,
|
620
|
+
default_alpha_value = 1.0
|
621
|
+
)
|
622
|
+
rgba_array = convert_hex_to_rgb(i)
|
623
|
+
rgba_array << default_alpha_value
|
624
|
+
return rgba_array
|
625
|
+
end
|
626
|
+
|
627
|
+
# ========================================================================= #
|
628
|
+
# === Colours.convert_hex_to_rgb
|
629
|
+
#
|
630
|
+
# This method will convert e. g. #baf185 to [186, 241, 133]. Thus it
|
631
|
+
# will return an Array, denoting the R, G, B values.
|
632
|
+
#
|
633
|
+
# How to do this conversion on your own?
|
634
|
+
#
|
635
|
+
# (1) Get the 2 left digits of the hex color code and convert
|
636
|
+
# to decimal value to get the red color level.
|
637
|
+
# (2) Get the 2 middle digits of the hex color code and
|
638
|
+
# convert to decimal value to get the green color level.
|
639
|
+
# (3) Get the 2 right digits of the hex color code and
|
640
|
+
# convert to decimal value to get the blue color level.
|
641
|
+
#
|
642
|
+
# Usage example:
|
643
|
+
#
|
644
|
+
# Colours.convert_hex_to_rgb('#baf185') # => [186, 241, 133]
|
645
|
+
#
|
646
|
+
# ========================================================================= #
|
647
|
+
def self.convert_hex_to_rgb(hex)
|
648
|
+
if hex.is_a? Array
|
649
|
+
hex = hex.first
|
650
|
+
end
|
651
|
+
hex = hex.to_s.dup
|
652
|
+
hex.delete!('#') if hex.include? '#'
|
653
|
+
array = [] # We will return this Array.
|
654
|
+
r = hex[0,2].to_i(16)
|
655
|
+
g = hex[2,2].to_i(16)
|
656
|
+
b = hex[4,2].to_i(16)
|
657
|
+
array << r << g << b
|
658
|
+
return array
|
659
|
+
end; self.instance_eval { alias hex_to_rgb convert_hex_to_rgb } # === Colours.hex_to_rgb
|
660
|
+
|
661
|
+
# ========================================================================= #
|
662
|
+
# === Colours.sanitize_line
|
663
|
+
#
|
664
|
+
# This method will replace one line with the proper R,G,B valid entries.
|
665
|
+
# ========================================================================= #
|
666
|
+
def self.sanitize_line(entry)
|
667
|
+
all_potential_matches = entry.scan(/<(\w+)>/).flatten
|
668
|
+
all_potential_matches.each {|substring|
|
669
|
+
entry.gsub!(/<\/>/, rev)
|
670
|
+
entry.gsub!(/<#{substring}>/, rgb_format(substring))
|
671
|
+
}
|
672
|
+
begin
|
673
|
+
require 'roebe/modules/remove_html.rb'
|
674
|
+
rescue LoadError; end
|
675
|
+
if Object.const_defined?(:Roebe) and
|
676
|
+
Roebe.respond_to?(:remove_html)
|
677
|
+
entry = Roebe.remove_html[entry]
|
678
|
+
end
|
679
|
+
return entry
|
680
|
+
end
|
681
|
+
|
682
|
+
# ========================================================================= #
|
683
|
+
# === Colours.cliner
|
684
|
+
# ========================================================================= #
|
685
|
+
def self.cliner(
|
686
|
+
i = 78, &block
|
687
|
+
)
|
688
|
+
yield if block_given?
|
689
|
+
e '=' * i
|
690
|
+
end
|
691
|
+
|
692
|
+
# ========================================================================= #
|
693
|
+
# === Colours.return_a_unique_array_containing_all_available_colours
|
694
|
+
# ========================================================================= #
|
695
|
+
def self.return_a_unique_array_containing_all_available_colours
|
696
|
+
array = []
|
697
|
+
array << YAML.load_file(FILE_HTML_COLOURS).keys # (1) First the HTML colours
|
698
|
+
array << YAML.load_file(FILE_256_COLOURS).keys.map(&:downcase) # (2) Then the basic colours
|
699
|
+
array << YAML.load_file(FILE_BASIC_COLOURS) # (3) And finally the basic colours
|
700
|
+
array.flatten!
|
701
|
+
array.uniq!
|
702
|
+
array.map!(&:strip)
|
703
|
+
array.sort
|
704
|
+
end; self.instance_eval { alias all_available_colour_methods? return_a_unique_array_containing_all_available_colours } # === Colours.all_available_colour_methods?
|
705
|
+
|
706
|
+
# ========================================================================= #
|
707
|
+
# === Colours.bold
|
708
|
+
#
|
709
|
+
# ANSI colour escape code for bold is "1".
|
710
|
+
#
|
711
|
+
# The method has to call Colours.rev() at the end, because that way
|
712
|
+
# downstream users can modify the default rev-colour in use.
|
713
|
+
#
|
714
|
+
# Usage example:
|
715
|
+
#
|
716
|
+
# puts ' ok | '+Colours.bold('Hello world!')+' | ok'
|
717
|
+
#
|
718
|
+
# ========================================================================= #
|
719
|
+
def self.bold(
|
720
|
+
show_this_text = 'This must be bold.'
|
721
|
+
)
|
722
|
+
return "\x1b[1m#{show_this_text}#{::Colours.rev}"
|
723
|
+
end; self.instance_eval { alias return_bold bold } # === Colours.return_bold
|
724
|
+
|
725
|
+
# ========================================================================= #
|
726
|
+
# === italic
|
727
|
+
# ========================================================================= #
|
728
|
+
def italic(
|
729
|
+
i = '',
|
730
|
+
make_newline = false
|
731
|
+
)
|
732
|
+
::Colours.italic(i, make_newline)
|
733
|
+
end
|
734
|
+
|
735
|
+
# ========================================================================= #
|
736
|
+
# === Colours.clear_screen
|
737
|
+
#
|
738
|
+
# Simply perform 'clear' here.
|
739
|
+
# ========================================================================= #
|
740
|
+
def self.clear_screen
|
741
|
+
system 'clear'
|
742
|
+
end; self.instance_eval { alias clear clear_screen } # === Colours.clear_screen
|
743
|
+
|
744
|
+
# ========================================================================= #
|
745
|
+
# === Colours.bold_and_italic
|
746
|
+
#
|
747
|
+
# Usage example:
|
748
|
+
#
|
749
|
+
# puts Colours.bold_and_italic 'Hello world!'
|
750
|
+
#
|
751
|
+
# ========================================================================= #
|
752
|
+
def self.bold_and_italic(
|
753
|
+
i = 'This must be bold.'
|
754
|
+
)
|
755
|
+
if i.is_a? Array
|
756
|
+
i = i.join(' ')
|
757
|
+
end
|
758
|
+
italic(bold(i))
|
759
|
+
end
|
760
|
+
|
761
|
+
# ========================================================================= #
|
762
|
+
# === Colours.html_colour_to_hex_value
|
763
|
+
#
|
764
|
+
# This method will return a String, such as "FFFF00".
|
765
|
+
#
|
766
|
+
# Usage example:
|
767
|
+
#
|
768
|
+
# Colours.html_colour_to_hex_value('yellow') # => "#FFFF00"
|
769
|
+
#
|
770
|
+
# ========================================================================= #
|
771
|
+
def self.html_colour_to_hex_value(
|
772
|
+
this_html_colour = nil,
|
773
|
+
return_with_hash_key = true
|
774
|
+
)
|
775
|
+
if this_html_colour.is_a? Array
|
776
|
+
this_html_colour = this_html_colour.first
|
777
|
+
end
|
778
|
+
# ======================================================================= #
|
779
|
+
# Obtain the path to the file that keeps the html-colours. On my home
|
780
|
+
# system this may be a path like this:
|
781
|
+
#
|
782
|
+
# /usr/lib/ruby/site_ruby/3.2.0/colours/yaml/html_colours.yml
|
783
|
+
#
|
784
|
+
# ======================================================================= #
|
785
|
+
_ = file_html_colours_to_rgb?
|
786
|
+
if File.exist? _
|
787
|
+
# ===================================================================== #
|
788
|
+
# Load the yaml-file next:
|
789
|
+
# ===================================================================== #
|
790
|
+
dataset = YAML.load_file(_)
|
791
|
+
if dataset.has_key? this_html_colour
|
792
|
+
_ = dataset[this_html_colour].last
|
793
|
+
if return_with_hash_key
|
794
|
+
_ = _.dup if _.frozen?
|
795
|
+
_ = _.to_s unless _.is_a? String
|
796
|
+
_[0,0] = '#' if _.respond_to? :[]=
|
797
|
+
end
|
798
|
+
return _
|
799
|
+
end
|
800
|
+
end
|
801
|
+
return this_html_colour
|
802
|
+
end
|
803
|
+
|
804
|
+
# ========================================================================= #
|
805
|
+
# === Colours.return_underline
|
806
|
+
#
|
807
|
+
# The point of this method here is to make a given text (String) appear
|
808
|
+
# "underlined", via ANSI escape sequences. For underline the String
|
809
|
+
# that should be used is "\u001b[4m".
|
810
|
+
#
|
811
|
+
# This can be tested on the commandline such as via:
|
812
|
+
#
|
813
|
+
# echoen "\u001b[4m Underline \u001b[0m"
|
814
|
+
#
|
815
|
+
# Usage examples:
|
816
|
+
#
|
817
|
+
# puts ' ok | '+Colours.return_underline('Hello world!')+' | ok'
|
818
|
+
# puts ' ok | '+Colours.return_underline('Hello world!') { :slateblue }+' | ok'
|
819
|
+
#
|
820
|
+
# In January 2022 the second variant was removed, though. Let's keep things
|
821
|
+
# simple for now - perhaps in the future this may be re-enabled.
|
822
|
+
# ========================================================================= #
|
823
|
+
def self.return_underline(
|
824
|
+
show_this_text = 'This must be underline.',
|
825
|
+
make_newline = false,
|
826
|
+
use_this_as_revert_code = REVERT
|
827
|
+
)
|
828
|
+
# ===================================================================== #
|
829
|
+
# Build up our main string that codes for underline.
|
830
|
+
# ===================================================================== #
|
831
|
+
result = "\u001b[#{UNDERLINE_CODE}m".dup
|
832
|
+
# if block_given?
|
833
|
+
# # ===================================================================== #
|
834
|
+
# # Right now we assume that this must be a colour if it is supplied
|
835
|
+
# # via a block. However had, in January 2022 I realised that this
|
836
|
+
# # assumption is not always correct, so this was disabled for now.
|
837
|
+
# # At a later moment in time we may reconsider this.
|
838
|
+
# # ===================================================================== #
|
839
|
+
# result << ";38;2;"
|
840
|
+
# colour_replacement = colour_to_rgb_value(yield)
|
841
|
+
# result << colour_replacement
|
842
|
+
# end
|
843
|
+
# result << 'm'
|
844
|
+
if make_newline # Append a newline in this case.
|
845
|
+
show_this_text = "#{show_this_text}\n"
|
846
|
+
end
|
847
|
+
result << show_this_text
|
848
|
+
result << use_this_as_revert_code
|
849
|
+
return result
|
850
|
+
end; self.instance_eval { alias string_underline return_underline } # === Colours.string_underline
|
851
|
+
|
852
|
+
# ========================================================================= #
|
853
|
+
# === Colours.random_value?
|
854
|
+
#
|
855
|
+
# We will obtain a random value between 0 and 255, hence why we will
|
856
|
+
# use rand(256).
|
857
|
+
# ========================================================================= #
|
858
|
+
def self.random_value?
|
859
|
+
rand(256)
|
860
|
+
end; self.instance_eval { alias random_value random_value? } # === Colours.random_value
|
861
|
+
self.instance_eval { alias rvalue random_value? } # === Colours.rvalue
|
862
|
+
self.instance_eval { alias r? random_value? } # === Colours.r?
|
863
|
+
self.instance_eval { alias g? random_value? } # === Colours.g?
|
864
|
+
self.instance_eval { alias b? random_value? } # === Colours.b?
|
865
|
+
|
866
|
+
# ========================================================================= #
|
867
|
+
# === random_value
|
868
|
+
#
|
869
|
+
# We will obtain a random value between 0 and 255, hence why we will
|
870
|
+
# use rand(256).
|
871
|
+
# ========================================================================= #
|
872
|
+
def random_value?
|
873
|
+
::Colours.random_value?
|
874
|
+
end; alias rvalue random_value? # === rvalue
|
875
|
+
alias random_value random_value? # === random_value
|
876
|
+
alias r? random_value? # === r?
|
877
|
+
alias g? random_value? # === g?
|
878
|
+
alias b? random_value? # === b?
|
879
|
+
|
880
|
+
# ========================================================================= #
|
881
|
+
# === Colours.italic
|
882
|
+
#
|
883
|
+
# This method will "puts" the result of applying italic to a string,
|
884
|
+
# on the console/terminal. If you wish to do the output on your own
|
885
|
+
# then you have to use the method Colours.return_italic or its
|
886
|
+
# alias called Colours.string_italic.
|
887
|
+
#
|
888
|
+
# To test this, try:
|
889
|
+
#
|
890
|
+
# Colours.italic('Hello world!')
|
891
|
+
#
|
892
|
+
# ========================================================================= #
|
893
|
+
def self.italic(
|
894
|
+
i = '',
|
895
|
+
make_newline = false,
|
896
|
+
&block
|
897
|
+
)
|
898
|
+
e return_italic(i, make_newline, &block)
|
899
|
+
end
|
900
|
+
|
901
|
+
# ========================================================================= #
|
902
|
+
# === Colours.remove_escape_sequence
|
903
|
+
#
|
904
|
+
# The method Colours.remove_escape_sequence() will remove all Ansi
|
905
|
+
# Escape sequences from a given string.
|
906
|
+
# ========================================================================= #
|
907
|
+
def self.remove_escape_sequence(i = ARGV)
|
908
|
+
if i.is_a? Array
|
909
|
+
i = i.join("\n")
|
910
|
+
end
|
911
|
+
i = i.to_s.dup
|
912
|
+
# ======================================================================= #
|
913
|
+
# Iterate over the registered ansi-colours next.
|
914
|
+
# ======================================================================= #
|
915
|
+
ARRAY_REGISTERED_ANSI_COLOURS.each {|entry|
|
916
|
+
if i.include? entry
|
917
|
+
entry = Regexp.quote(entry)
|
918
|
+
i.sub!(/#{entry}/, '')
|
919
|
+
end
|
920
|
+
# ===================================================================== #
|
921
|
+
# Next, check for KDE konsole colours. We must use "" there, not ''.
|
922
|
+
# ===================================================================== #
|
923
|
+
if i.include?("\e[") and i =~ /\d+m/ # Such as: "\e[38;2;220;20;60m|"
|
924
|
+
# =================================================================== #
|
925
|
+
# The next regex will (hopefully) remove all escape-characters
|
926
|
+
# from the given String.
|
927
|
+
# =================================================================== #
|
928
|
+
regex_for_html_colours = # Detect entries such as: \e[38;2;106;90;205m
|
929
|
+
/(\e\[\d{2};\d{1};\d{3};\d{1,2};\d{3}m)/ # See: https://rubular.com/r/tG3XeOK5NPsfmI
|
930
|
+
i.gsub!(regex_for_html_colours, '')
|
931
|
+
# =================================================================== #
|
932
|
+
# See: https://rubular.com/r/SdS28fAGSxIELn
|
933
|
+
# =================================================================== #
|
934
|
+
# /(\\e\[\d{1,2};?\d{1,2};?\d{0,3};?\d{0,3};?\d{1,2}m|\\e\[\d{0,2}m|\\e\[K|\\e\[\d{0,2};\d{0,2}m|(\\e\[.+\dm)|\\e\[0;37m)/
|
935
|
+
[
|
936
|
+
/\e\[\d{1,2};?\d{1,2};?\d{0,3};?\d{0,3};?\d{1,2}m/,
|
937
|
+
/\\e\[\d{0,2}m|\\e\[K|\\e\[\d{0,2};\d{0,2}m/,
|
938
|
+
/\\e\[.+\dm/,
|
939
|
+
/\\e\[0;37m/
|
940
|
+
].each {|regex_to_use|
|
941
|
+
i.gsub!(regex_to_use, '')
|
942
|
+
}
|
943
|
+
end
|
944
|
+
}
|
945
|
+
return i
|
946
|
+
end; self.instance_eval { alias remove remove_escape_sequence } # === Colours.remove
|
947
|
+
self.instance_eval { alias remove_escape_sequences remove_escape_sequence } # === Colours.remove_escape_sequences
|
948
|
+
self.instance_eval { alias remove_colours remove_escape_sequence } # === Colours.remove_colours
|
949
|
+
self.instance_eval { alias escape remove_escape_sequence } # === Colours.escape
|
950
|
+
|
951
|
+
# ========================================================================= #
|
952
|
+
# === Colours.does_this_string_include_a_html_number?
|
953
|
+
#
|
954
|
+
# This method will return true if the string includes tags such as
|
955
|
+
# <one> or <two> and so forth.
|
956
|
+
# ========================================================================= #
|
957
|
+
def self.does_this_string_include_a_html_number?(i)
|
958
|
+
i.include?('<one>') or
|
959
|
+
i.include?('<two>') or
|
960
|
+
i.include?('<three>') or
|
961
|
+
i.include?('<four>') or
|
962
|
+
i.include?('<five>')
|
963
|
+
end
|
964
|
+
|
965
|
+
# ========================================================================= #
|
966
|
+
# === Colours.eliminate_html
|
967
|
+
#
|
968
|
+
# This method simply combines two other methods, without any further
|
969
|
+
# checks inside of this method. The method will thus remove entries
|
970
|
+
# such as <one> or <steelblue>.
|
971
|
+
# ========================================================================= #
|
972
|
+
def self.eliminate_html(
|
973
|
+
i,
|
974
|
+
use_this_colour_for_the_default_colour = :default,
|
975
|
+
use_this_as_replacement_hash = :default_hash
|
976
|
+
)
|
977
|
+
i = i.dup
|
978
|
+
i = Colours.replace_number_words_with_the_corresponding_html_colour(
|
979
|
+
i,
|
980
|
+
use_this_colour_for_the_default_colour,
|
981
|
+
use_this_as_replacement_hash
|
982
|
+
)
|
983
|
+
i = i.dup
|
984
|
+
i = Colours.replace_html_colours_in_this_string(
|
985
|
+
i,
|
986
|
+
use_this_colour_for_the_default_colour
|
987
|
+
)
|
988
|
+
return i.dup
|
989
|
+
end; self.instance_eval { alias away_with_html_colours_and_special_numbers eliminate_html } # === Colours.away_with_html_colours_and_special_numbers
|
990
|
+
self.instance_eval { alias remove_html_tags_and_special_words eliminate_html } # === Colours.remove_html_tags_and_special_words
|
991
|
+
self.instance_eval { alias remove_crap eliminate_html } # === Colours.remove_crap
|
992
|
+
|
993
|
+
# ========================================================================= #
|
994
|
+
# === Colours.does_this_string_include_a_html_colour?
|
995
|
+
# ========================================================================= #
|
996
|
+
def self.does_this_string_include_a_html_colour?(i)
|
997
|
+
result = (i =~ REGEX_FOR_HTML_COLOURS)
|
998
|
+
result = false if result.nil?
|
999
|
+
result
|
1000
|
+
end
|
1001
|
+
|
1002
|
+
# ========================================================================= #
|
1003
|
+
# === Colours.show_basic_colour_palette
|
1004
|
+
#
|
1005
|
+
# This will show a colour palette on the commandline.
|
1006
|
+
# ========================================================================= #
|
1007
|
+
def self.show_basic_colour_palette
|
1008
|
+
e
|
1009
|
+
MAIN_COLOURS.reject {|e| e == :black }.each { |entry|
|
1010
|
+
make_colour(entry) # make_colour() is defined in this file here.
|
1011
|
+
}
|
1012
|
+
cliner
|
1013
|
+
SECONDARY_COLOURS.each { |entry|
|
1014
|
+
make_colour(entry)
|
1015
|
+
}
|
1016
|
+
e
|
1017
|
+
end; self.instance_eval { alias show_palette show_basic_colour_palette } # === Colours.show_palette
|
1018
|
+
self.instance_eval { alias show_colour_palette show_basic_colour_palette } # === Colours.show_colour_palette
|
1019
|
+
|
1020
|
+
# ========================================================================= #
|
1021
|
+
# === Colours.reset_the_line
|
1022
|
+
#
|
1023
|
+
# This method will reset the current line. This can then be used in a
|
1024
|
+
# progress indicator application.
|
1025
|
+
# ========================================================================= #
|
1026
|
+
def self.reset_the_line
|
1027
|
+
STDOUT.write("\u001b[1000D") # Move to the left by 1000 characters. Aka reset.
|
1028
|
+
STDOUT.flush # This line will force the output to appear immediately,
|
1029
|
+
end
|
1030
|
+
|
1031
|
+
# ========================================================================= #
|
1032
|
+
# === Colours.remove_trailing_end_from
|
1033
|
+
#
|
1034
|
+
# The second argument to this method can be the escape sequence that you
|
1035
|
+
# wish to remove.
|
1036
|
+
#
|
1037
|
+
# Invocation example:
|
1038
|
+
#
|
1039
|
+
# x = Colours.remove_trailing_end_from("\e[38;2;70;130;180m\e[0;37m") # => "\e[38;2;70;130;180m"
|
1040
|
+
#
|
1041
|
+
# ========================================================================= #
|
1042
|
+
def self.remove_trailing_end_from(
|
1043
|
+
i,
|
1044
|
+
remove_this_escape_sequence = revert?
|
1045
|
+
)
|
1046
|
+
quoted = Regexp.quote(remove_this_escape_sequence)
|
1047
|
+
# ======================================================================= #
|
1048
|
+
# Anchor it at the end via $.
|
1049
|
+
# ======================================================================= #
|
1050
|
+
return i.sub(
|
1051
|
+
/#{quoted}$/, ''
|
1052
|
+
)
|
1053
|
+
end; self.instance_eval { alias remove_trailing_escape_part remove_trailing_end_from } # === Colours.remove_trailing_escape_part
|
1054
|
+
self.instance_eval { alias remove_trailing_ansii_escape_code remove_trailing_end_from } # === Colours.remove_trailing_ansii_escape_code
|
1055
|
+
self.instance_eval { alias remove_trailing_ANSII_escape_code remove_trailing_end_from } # === Colours.remove_trailing_ANSII_escape_code
|
1056
|
+
self.instance_eval { alias remove_trailing_escape_code remove_trailing_end_from } # === Colours.remove_trailing_escape_code
|
1057
|
+
self.instance_eval { alias remove_trailing_code remove_trailing_end_from } # === Colours.remove_trailing_code
|
1058
|
+
|
1059
|
+
end
|
1060
|
+
|
1061
|
+
if __FILE__ == $PROGRAM_NAME
|
1062
|
+
alias e puts
|
1063
|
+
alias ee print
|
1064
|
+
_text = 'Hello world!'
|
1065
|
+
include Colours
|
1066
|
+
e Colours.string_italic('Hello world!')+' - and this is not in italic anymore.'
|
1067
|
+
e
|
1068
|
+
e 'Colours.rev is: '+Colours.rev
|
1069
|
+
e
|
1070
|
+
Colours.show_basic_colour_palette
|
1071
|
+
e
|
1072
|
+
if ARGV.empty?
|
1073
|
+
e Colours.bold_and_italic('Hello world!')+' All is fine.'
|
1074
|
+
else
|
1075
|
+
e Colours.bold_and_italic(ARGV)
|
1076
|
+
end
|
1077
|
+
e
|
1078
|
+
pp Colours.convert_hex_to_rgb('#baf185') # => [186, 241, 133]
|
1079
|
+
e
|
1080
|
+
e Colours.bold('Hello world!')
|
1081
|
+
e
|
1082
|
+
if ARGV.empty?
|
1083
|
+
require 'colours/requires/require_all_colour_methods.rb'
|
1084
|
+
COLOURS = Colours::AllColourMethods
|
1085
|
+
system 'clear'
|
1086
|
+
Colours.italic(txt)
|
1087
|
+
e COLOURS.palegreen('italic in palegreen:')
|
1088
|
+
ee ' '; e Colours.string_italic(txt) { :palegreen }
|
1089
|
+
e COLOURS.slateblue('italic in slateblue:')
|
1090
|
+
ee ' '; e Colours.string_italic(txt) { :slateblue }
|
1091
|
+
e COLOURS.orange('italic in orange:')
|
1092
|
+
ee ' '; e Colours.string_italic(txt) { :orange }
|
1093
|
+
e COLOURS.crimson('italic in crimson:')
|
1094
|
+
ee ' '; e Colours.string_italic(txt) { :crimson }
|
1095
|
+
e 'Italic without colours:'
|
1096
|
+
ee ' '; e 'Die '+Colours.string_italic('R-Gruppe bei')+
|
1097
|
+
' Serin (S) besteht aus ...'
|
1098
|
+
else
|
1099
|
+
ee ' '; e Colours.string_italic(ARGV.join(' ').strip)
|
1100
|
+
ee ' '; e Colours.string_italic(ARGV.join(' ').strip) { :crimson }
|
1101
|
+
end
|
1102
|
+
# ========================================================================= #
|
1103
|
+
# Example code demonstrating the method defined above:
|
1104
|
+
# ========================================================================= #
|
1105
|
+
index = 1
|
1106
|
+
loop {
|
1107
|
+
Colours.reset_the_line
|
1108
|
+
print 'OK! '+
|
1109
|
+
Random.rand(11).to_s
|
1110
|
+
index += 1
|
1111
|
+
break if index == 50
|
1112
|
+
sleep 0.3
|
1113
|
+
}
|
1114
|
+
e
|
1115
|
+
alias e puts
|
1116
|
+
begin
|
1117
|
+
require 'colours/autoinclude'
|
1118
|
+
rescue LoadError; e 'the colours gem is not available.'; end
|
1119
|
+
include Colours
|
1120
|
+
e
|
1121
|
+
e 'Next testing an escape sequence (which will be shown via pp only):'
|
1122
|
+
e
|
1123
|
+
x = "\e[01m\e[Kcc1:\e[m\e[K \e[01;35m\e[Kwarning: \e[m\e[K-Wabi won't warn about anything [\e[01;35m\e[K-Wabi\e[m\e[K]
|
1124
|
+
|
1125
|
+
A\e[38;2;220;20;60m|AGCTT"
|
1126
|
+
pp x
|
1127
|
+
e
|
1128
|
+
x = Colours.remove_escape_sequence(x)
|
1129
|
+
e
|
1130
|
+
pp x
|
1131
|
+
e x
|
1132
|
+
pp Colours.remove_escape_sequences(Colours.slateblue('Hello world!')) # => "\e[38;2;106;90;205mHello world!"
|
1133
|
+
p Colours.slateblue('Hello world!') # => "\e[38;2;106;90;205mHello world!\e[0;37m"
|
1134
|
+
e
|
1135
|
+
if ARGV.empty?
|
1136
|
+
e Colours.underline('Hello world!')
|
1137
|
+
e 'Hello world!'
|
1138
|
+
# ======================================================================= #
|
1139
|
+
# And then run some more tests:
|
1140
|
+
# ======================================================================= #
|
1141
|
+
require 'colours/toplevel_methods/use_colours.rb'
|
1142
|
+
Colours.enable_html_colours
|
1143
|
+
e Colours.palegreen('in palegreen:')
|
1144
|
+
print ' '; print Colours.underline(txt) { :palegreen }
|
1145
|
+
e Colours.slateblue('in slateblue:')
|
1146
|
+
print ' '; print Colours.underline(txt) { :slateblue }
|
1147
|
+
e Colours.orange('in orange:')
|
1148
|
+
print ' '; print Colours.underline(txt) { :orange }
|
1149
|
+
e Colours.crimson('in crimson:')
|
1150
|
+
print ' '; print Colours.underline(txt) { :crimson }
|
1151
|
+
else
|
1152
|
+
e Colours.underline(ARGV.join(' ').strip)
|
1153
|
+
e Colours.underline(ARGV.join(' ').strip) { :crimson }
|
1154
|
+
end
|
1155
|
+
e
|
1156
|
+
input = ARGV
|
1157
|
+
if input.empty?
|
1158
|
+
input << 'steelblue'
|
1159
|
+
end
|
1160
|
+
e
|
1161
|
+
e 'Testing: Colours.html_colour_to_hex_value(ARGV) next'
|
1162
|
+
e
|
1163
|
+
e Colours.html_colour_to_hex_value(input)
|
1164
|
+
e "Testing: Colours.does_this_string_include_a_html_number?('abc <one>def</one>') next"
|
1165
|
+
puts Colours.does_this_string_include_a_html_number?('abc <one>def</one>')
|
1166
|
+
# ========================================================================= #
|
1167
|
+
# Test the behaviour of the method next:
|
1168
|
+
# ========================================================================= #
|
1169
|
+
this_line = "- <lightseagreen>hey</lightseagreen> yo <green>there</green>"
|
1170
|
+
puts Colours.replace_html_colours(
|
1171
|
+
this_line
|
1172
|
+
)
|
1173
|
+
puts Colours.does_this_string_include_a_html_colour?('<steelblue>')
|
1174
|
+
e
|
1175
|
+
e Colours.replace_number_words_with_the_corresponding_html_colour(
|
1176
|
+
"- <teal>Asbestbedingte Erkrankungen</teal> haben eine <two>Latenzzeit</two> von etwa n Jahren? Etwa <steelblue>30 Jahren</steelblue>."
|
1177
|
+
)
|
1178
|
+
x = "<one>Methämoglobin</one> ist <two>ungeeignet</two> für <three>den</three> Sauerstofftransport; die <two>roten Blutkörperchen</two>. Welches Enzymsystem ist dies?"
|
1179
|
+
e Colours.replace_number_words_with_the_corresponding_html_colour(x)
|
1180
|
+
e Colours.replace_html_colours_in_this_string("<one>Methämoglobin</one> ist <steelblue>ungeeignet</steelblue>.")
|
1181
|
+
e
|
1182
|
+
string = "- <teal>Asbestbedingte Erkrankungen</teal> haben eine <two>Latenzzeit</two> von etwa n Jahren? Etwa <steelblue>30 Jahren</steelblue>."
|
1183
|
+
e string
|
1184
|
+
e Colours.replace_html_colours_in_this_string(
|
1185
|
+
string
|
1186
|
+
)
|
1187
|
+
e
|
1188
|
+
result = Colours.replace_all_html_colours_in_this_line(' |<steelblue>E</steelblue>]', :default, :revert)
|
1189
|
+
e result
|
1190
|
+
e
|
1191
|
+
e 'And it looks like this via pp:'
|
1192
|
+
e
|
1193
|
+
pp result
|
1194
|
+
e
|
1195
|
+
# pp Colours.return_a_unique_array_containing_all_available_colours
|
1196
|
+
# e
|
1197
|
+
Colours.autogenerate_the_module_for_the_html_colours
|
1198
|
+
Colours.autogenerate_shell_file_containing_the_html_colours
|
1199
|
+
Colours.autogenerate_the_module_for_the_256_colours
|
1200
|
+
Colours.autogenerate_toplevel_basic_colour_methods
|
1201
|
+
e Colours.html_colourize('slateblue', 'Hello world 1!')
|
1202
|
+
e Colours.html_colourize(:crimson, 'Hello world 2!')
|
1203
|
+
e
|
1204
|
+
end # toplevel_methods.rb
|