rcfiles 1.3.54

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rcfiles might be problematic. Click here for more details.

Files changed (98) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +562 -0
  3. data/bin/rcfiles +12 -0
  4. data/bin/rcfiles_run_it +7 -0
  5. data/doc/README.gen +515 -0
  6. data/img/LOGO_FOR_THE_RCFILES_PROJECT.png +0 -0
  7. data/lib/rcfiles/aggregate_all_rcfiles/aggregate_all_rcfiles.rb +137 -0
  8. data/lib/rcfiles/class/colours.rb +54 -0
  9. data/lib/rcfiles/class/initialize.rb +66 -0
  10. data/lib/rcfiles/class/misc.rb +300 -0
  11. data/lib/rcfiles/class/rcfiles.rb +31 -0
  12. data/lib/rcfiles/class/reset.rb +26 -0
  13. data/lib/rcfiles/class/run.rb +17 -0
  14. data/lib/rcfiles/colours/colours.rb +137 -0
  15. data/lib/rcfiles/commandline/commandline.rb +190 -0
  16. data/lib/rcfiles/constants/constants.rb +253 -0
  17. data/lib/rcfiles/directory_aliases/README.md +2 -0
  18. data/lib/rcfiles/directory_aliases/directory_aliases.rb +868 -0
  19. data/lib/rcfiles/files/file_aliases.rb +78 -0
  20. data/lib/rcfiles/files/file_cd_aliases.rb +85 -0
  21. data/lib/rcfiles/files/file_ls_colours.rb +76 -0
  22. data/lib/rcfiles/files/file_path_variable.rb +74 -0
  23. data/lib/rcfiles/files/file_pkgconfig.rb +74 -0
  24. data/lib/rcfiles/files/file_programs_aliases.rb +68 -0
  25. data/lib/rcfiles/files/file_ps1.rb +80 -0
  26. data/lib/rcfiles/files/file_system_settings.rb +75 -0
  27. data/lib/rcfiles/help/help.rb +48 -0
  28. data/lib/rcfiles/no_colours.rb +13 -0
  29. data/lib/rcfiles/project/project.rb +45 -0
  30. data/lib/rcfiles/rcfiles/README.md +5 -0
  31. data/lib/rcfiles/rcfiles/aliases_rc +25077 -0
  32. data/lib/rcfiles/rcfiles/cd_aliases_rc +17138 -0
  33. data/lib/rcfiles/rcfiles/cflags_rc +9 -0
  34. data/lib/rcfiles/rcfiles/compile_rc +11 -0
  35. data/lib/rcfiles/rcfiles/file_information_rc +16 -0
  36. data/lib/rcfiles/rcfiles/gnome_rc +10 -0
  37. data/lib/rcfiles/rcfiles/kde_rc +17 -0
  38. data/lib/rcfiles/rcfiles/kernel_rc +12 -0
  39. data/lib/rcfiles/rcfiles/lang_rc +9 -0
  40. data/lib/rcfiles/rcfiles/ldflags_rc +8 -0
  41. data/lib/rcfiles/rcfiles/ls_colours_rc +9 -0
  42. data/lib/rcfiles/rcfiles/network_rc +37 -0
  43. data/lib/rcfiles/rcfiles/path_variable_rc +9 -0
  44. data/lib/rcfiles/rcfiles/pkgconfig_rc +9 -0
  45. data/lib/rcfiles/rcfiles/programs_aliases_rc +19014 -0
  46. data/lib/rcfiles/rcfiles/ps1_rc +9 -0
  47. data/lib/rcfiles/rcfiles/special_aliases_rc +8 -0
  48. data/lib/rcfiles/rcfiles/system_rc +13 -0
  49. data/lib/rcfiles/rcfiles/system_settings_rc +192 -0
  50. data/lib/rcfiles/rcfiles/term_rc +9 -0
  51. data/lib/rcfiles/rcfiles/video_rc +24 -0
  52. data/lib/rcfiles/requires/require_the_directory_aliases.rb +7 -0
  53. data/lib/rcfiles/requires/require_the_rcfiles_constants.rb +18 -0
  54. data/lib/rcfiles/requires/require_the_rcfiles_project.rb +67 -0
  55. data/lib/rcfiles/requires/try_to_require_syck_and_psych.rb +18 -0
  56. data/lib/rcfiles/toplevel_methods/autogenerate_rc_file_from_this_yaml_file.rb +102 -0
  57. data/lib/rcfiles/toplevel_methods/change_directory.rb +16 -0
  58. data/lib/rcfiles/toplevel_methods/convert.rb +118 -0
  59. data/lib/rcfiles/toplevel_methods/convert_global_variables_in_the_cd_aliases_file_to_a_standalone_yaml_file.rb +50 -0
  60. data/lib/rcfiles/toplevel_methods/create_windows_cd_aliases_bat_file.rb +51 -0
  61. data/lib/rcfiles/toplevel_methods/e.rb +16 -0
  62. data/lib/rcfiles/toplevel_methods/esystem.rb +19 -0
  63. data/lib/rcfiles/toplevel_methods/generate_yaml_files.rb +148 -0
  64. data/lib/rcfiles/toplevel_methods/infer.rb +83 -0
  65. data/lib/rcfiles/toplevel_methods/is_on_roebe.rb +16 -0
  66. data/lib/rcfiles/toplevel_methods/main_directory.rb +108 -0
  67. data/lib/rcfiles/toplevel_methods/misc.rb +343 -0
  68. data/lib/rcfiles/toplevel_methods/n_registered_aliases_in_total.rb +55 -0
  69. data/lib/rcfiles/toplevel_methods/no_file_exists_at.rb +19 -0
  70. data/lib/rcfiles/toplevel_methods/notifications.rb +35 -0
  71. data/lib/rcfiles/toplevel_methods/rds.rb +16 -0
  72. data/lib/rcfiles/toplevel_methods/remove_newlines.rb +29 -0
  73. data/lib/rcfiles/toplevel_methods/report.rb +53 -0
  74. data/lib/rcfiles/toplevel_methods/return_standard_header.rb +36 -0
  75. data/lib/rcfiles/toplevel_methods/run_it.rb +46 -0
  76. data/lib/rcfiles/toplevel_methods/save_file.rb +27 -0
  77. data/lib/rcfiles/toplevel_methods/use_psych_or_syck.rb +50 -0
  78. data/lib/rcfiles/toplevel_methods/use_quotes.rb +68 -0
  79. data/lib/rcfiles/toplevel_methods/verbosity.rb +28 -0
  80. data/lib/rcfiles/version/version.rb +26 -0
  81. data/lib/rcfiles/yaml/aliases.yml +27992 -0
  82. data/lib/rcfiles/yaml/cd_aliases.yml +17939 -0
  83. data/lib/rcfiles/yaml/global_variables/README.md +3 -0
  84. data/lib/rcfiles/yaml/global_variables/global_variables_extracted_from_the_file_cd_aliases.yml +125 -0
  85. data/lib/rcfiles/yaml/ls_colours.yml +128 -0
  86. data/lib/rcfiles/yaml/path_variable.yml +15 -0
  87. data/lib/rcfiles/yaml/pkgconfig.yml +46 -0
  88. data/lib/rcfiles/yaml/programs_aliases.yml +20718 -0
  89. data/lib/rcfiles/yaml/ps1.yml +82 -0
  90. data/lib/rcfiles/yaml/store_into_this_directory.yml +1 -0
  91. data/lib/rcfiles/yaml/system_settings.yml +526 -0
  92. data/lib/rcfiles/yaml/use_psych_or_syck.yml +1 -0
  93. data/lib/rcfiles.rb +1 -0
  94. data/rcfiles.gemspec +57 -0
  95. data/test/testing_directory_aliases.rb +62 -0
  96. data/test/testing_using_another_path_to_directory_aliases.rb +17 -0
  97. data/test/testing_yaml_to_rcfiles.rb +9 -0
  98. metadata +202 -0
@@ -0,0 +1,868 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === Rcfiles::DirectoryAliases
6
+ #
7
+ # The purpose of this class is to map between e. g. "pwdj" to "/Depot/j".
8
+ #
9
+ # On my home system "pwdj" is an alias to cd into the directory "/Depot/j/",
10
+ # if said directory exists.
11
+ #
12
+ # Thus the class will expand my cd-aliases, which are usually stored
13
+ # in the file "cd_aliases.yml". These are also part of the Rcfiles
14
+ # project, for convenience reasons mostly.
15
+ #
16
+ # The benefit of being able to expand from my local aliases is that we
17
+ # can provide these aliases to other ruby projects easily. In fact, this
18
+ # has been a primary reason for creating this class in the first place -
19
+ # it was to make all cd_aliases available within the Roebe::Shell project,
20
+ # specifically. Since the latter can be used on windows, I can then use
21
+ # vanilla "cmd.exe" and have my shell work with support for all my
22
+ # custom cd ("change directory") operations. So I can type "tosongs",
23
+ # and hit the enter-key, and it will change directory to the directory
24
+ # where I keep all my songs. How convenient! \o/
25
+ #
26
+ # Usage example for this class:
27
+ #
28
+ # Rcfiles::DirectoryAliases.new(ARGV)
29
+ # Rcfiles::DirectoryAliases.is_included?('pwdsongs') # => true
30
+ #
31
+ # =========================================================================== #
32
+ # require 'rcfiles/directory_aliases/directory_aliases.rb'
33
+ # Rcfiles::DirectoryAliases.is_included?('ABC')
34
+ # =========================================================================== #
35
+ module Rcfiles
36
+
37
+ class DirectoryAliases # === Rcfiles::DirectoryAliases
38
+
39
+ alias e puts
40
+
41
+ require 'rcfiles/constants/constants.rb'
42
+ require 'rcfiles/colours/colours.rb'
43
+ require 'rcfiles/version/version.rb'
44
+
45
+ begin
46
+ require 'opn'
47
+ rescue LoadError; end
48
+
49
+ begin
50
+ require 'convert_global_env'
51
+ rescue LoadError; end
52
+
53
+ # ========================================================================= #
54
+ # === NAMESPACE
55
+ # ========================================================================= #
56
+ NAMESPACE = inspect
57
+
58
+ # ========================================================================= #
59
+ # === Rcfiles::DirectoryAliases::NAME_OF_FILE
60
+ # ========================================================================= #
61
+ NAME_OF_FILE = 'directory_aliases.rb'
62
+
63
+ # ========================================================================= #
64
+ # === Rcfiles::DirectoryAliases::SHALL_WE_SHOW_EXCEPTIONS
65
+ # ========================================================================= #
66
+ SHALL_WE_SHOW_EXCEPTIONS = false
67
+
68
+ # ========================================================================= #
69
+ # === Rcfiles::DirectoryAliases::DEFAULT_RUN_MODE
70
+ # ========================================================================= #
71
+ DEFAULT_RUN_MODE = true # or false
72
+
73
+ # ========================================================================= #
74
+ # === initialize
75
+ #
76
+ # The first argument is typically the key that is sought. It is optional,
77
+ # though, as the method .find() or .points_at?() can be used with an
78
+ # argument at any moment in time. That way a cd-alias can be used to
79
+ # query the dataset.
80
+ # ========================================================================= #
81
+ def initialize(
82
+ commandline_arguments = ARGV,
83
+ run_already = DEFAULT_RUN_MODE
84
+ )
85
+ reset
86
+ set_commandline_arguments(
87
+ commandline_arguments
88
+ )
89
+ # ======================================================================= #
90
+ # === Handle blocks given to this class next
91
+ # ======================================================================= #
92
+ if block_given?
93
+ yielded = yield
94
+ case yielded
95
+ # ===================================================================== #
96
+ # === :run
97
+ # ===================================================================== #
98
+ when :run
99
+ run_already = true
100
+ end
101
+ end
102
+ case run_already
103
+ # ======================================================================= #
104
+ # === :be_silent
105
+ # ======================================================================= #
106
+ when :be_silent
107
+ be_silent
108
+ run_already = DEFAULT_RUN_MODE
109
+ # ======================================================================= #
110
+ # === :do_not_report_errors
111
+ # ======================================================================= #
112
+ when :do_not_report_errors
113
+ set_report_errors(false)
114
+ run_already = DEFAULT_RUN_MODE
115
+ end
116
+ run if run_already
117
+ end
118
+
119
+ # ========================================================================= #
120
+ # === reset (reset tag)
121
+ # ========================================================================= #
122
+ def reset
123
+ # ======================================================================= #
124
+ # === @namespace
125
+ # ======================================================================= #
126
+ @namespace = NAMESPACE
127
+ # ======================================================================= #
128
+ # === @internal_hash
129
+ # ======================================================================= #
130
+ @internal_hash = {}
131
+ # ======================================================================= #
132
+ # === :be_verbose
133
+ # ======================================================================= #
134
+ @internal_hash[:be_verbose] = true
135
+ # ======================================================================= #
136
+ # === :report_errors
137
+ #
138
+ # The next variable determines whether we will report errors or
139
+ # whether we will not. By default this class will report errors.
140
+ #
141
+ # An example error may be Encoding::InvalidByteSequenceError being
142
+ # raised due to the .yml file being incorrect. In my own yaml files
143
+ # this error no longer happens, so the class no longer has to handle
144
+ # this - but in the event that we may re-enable this feature one
145
+ # day, for other users, I considered it useful in May 2022 to
146
+ # still retain the :report_errors flag.
147
+ #
148
+ # The old code looked something like this:
149
+ #
150
+ # rescue Encoding::InvalidByteSequenceError => error
151
+ # if be_verbose? and show_exceptions? and report_errors?
152
+ # opnn; e "#{NAME_OF_FILE}: An exception/error happened for → #{simp(key.to_s)}"
153
+ # opnn; e 'We will now feedback this error:'
154
+ # pp error
155
+ # end
156
+ # end
157
+ #
158
+ # ======================================================================= #
159
+ @internal_hash[:report_errors] = true
160
+ be_quiet # Do not be too noisy by default.
161
+ load_the_dataset_from_the_file_cd_aliases
162
+ end
163
+
164
+ # ========================================================================= #
165
+ # === be_verbose
166
+ # ========================================================================= #
167
+ def be_verbose
168
+ @internal_hash[:be_verbose] = true
169
+ end
170
+
171
+ # ========================================================================= #
172
+ # === be_silent
173
+ # ========================================================================= #
174
+ def be_silent
175
+ @internal_hash[:be_verbose] = false
176
+ end; alias be_quiet be_silent # === be_quiet
177
+
178
+ # ========================================================================= #
179
+ # === be_verbose?
180
+ # ========================================================================= #
181
+ def be_verbose?
182
+ @internal_hash[:be_verbose]
183
+ end
184
+
185
+ # ========================================================================= #
186
+ # === dataset?
187
+ #
188
+ # This is the query-method to access the main dataset of this class,
189
+ # which is a Hash.
190
+ # ========================================================================= #
191
+ def dataset?
192
+ @internal_hash[:dataset]
193
+ end; alias hash? dataset? # === hash?
194
+ alias hash dataset? # === hash
195
+ alias cd_aliases? dataset? # === cd_aliases?
196
+
197
+ # ========================================================================= #
198
+ # === set_commandline_arguments
199
+ # ========================================================================= #
200
+ def set_commandline_arguments(i = '')
201
+ i = [i].flatten.compact
202
+ @commandline_arguments = i
203
+ end
204
+
205
+ # ========================================================================= #
206
+ # === commandline_arguments?
207
+ # ========================================================================= #
208
+ def commandline_arguments?
209
+ @commandline_arguments
210
+ end
211
+
212
+ # ========================================================================= #
213
+ # === first_argument?
214
+ # ========================================================================= #
215
+ def first_argument?
216
+ @commandline_arguments.first
217
+ end; alias first? first_argument? # === first?
218
+ alias seek_this_key? first_argument? # === seek_this_key?
219
+ alias seek_which_key? first_argument? # === seek_which_key?
220
+
221
+ # ========================================================================= #
222
+ # === report_version
223
+ # ========================================================================= #
224
+ def report_version
225
+ e "The version is: #{sfancy(version?)}"
226
+ end
227
+
228
+ # ========================================================================= #
229
+ # === opnn
230
+ # ========================================================================= #
231
+ def opnn
232
+ if Object.const_defined? :Opn
233
+ Opn.opn(namespace: NAMESPACE)
234
+ end
235
+ end; alias opn opnn # === opn
236
+
237
+ # ========================================================================= #
238
+ # === report_errors?
239
+ # ========================================================================= #
240
+ def report_errors?
241
+ @internal_hash[:report_errors]
242
+ end
243
+
244
+ # ========================================================================= #
245
+ # === set_report_errors
246
+ # ========================================================================= #
247
+ def set_report_errors(i)
248
+ @internal_hash[:report_errors] = i
249
+ end
250
+
251
+ # ========================================================================= #
252
+ # === translate_key_into_value
253
+ #
254
+ # This method will NOT do any additional sanitizing. It will simply
255
+ # query the main hash, as-is, and return the result.
256
+ # ========================================================================= #
257
+ def translate_key_into_value(key)
258
+ @internal_hash[:dataset][key]
259
+ end; alias translate_this translate_key_into_value # === translate_this
260
+
261
+ # ========================================================================= #
262
+ # === rds
263
+ # ========================================================================= #
264
+ def rds(i)
265
+ i.squeeze('/')
266
+ end
267
+
268
+ # ========================================================================= #
269
+ # === version?
270
+ #
271
+ # This is mostly a convenience method, to quickly query the current
272
+ # version of the rcfiles gem in use.
273
+ # ========================================================================= #
274
+ def version?
275
+ ::Rcfiles::VERSION
276
+ end
277
+
278
+ # ========================================================================= #
279
+ # === keys?
280
+ # ========================================================================= #
281
+ def keys?
282
+ dataset?.keys
283
+ end; alias keys keys? # === keys
284
+
285
+ # ========================================================================= #
286
+ # === size?
287
+ #
288
+ # Query how many keys we have registered.
289
+ # ========================================================================= #
290
+ def size?
291
+ keys?.size
292
+ end; alias size size? # === size
293
+ alias keys size? # === keys
294
+
295
+ # ========================================================================= #
296
+ # === is_this_key_included?
297
+ #
298
+ # This method can be used to query whether our expanded directory-aliases
299
+ # contains the given key (first input argument to this method) or
300
+ # whether it does not.
301
+ # ========================================================================= #
302
+ def is_this_key_included?(
303
+ key = first_argument?
304
+ )
305
+ key = key.to_s unless key.is_a?(String)
306
+ dataset?.has_key?(key)
307
+ end; alias included? is_this_key_included? # === included?
308
+ alias is_included? is_this_key_included? # === is_included?
309
+ alias is_this_input_a_cd_alias? is_this_key_included? # === is_this_input_a_cd_alias?
310
+ alias is_this_key_registered? is_this_key_included? # === is_this_key_registered?
311
+ alias is_this_alias_included? is_this_key_included? # === is_this_alias_included?
312
+ alias has_key? is_this_key_included? # === has_key?
313
+ alias has_this_alias? is_this_key_included? # === has_key?
314
+
315
+ # ========================================================================= #
316
+ # === file?
317
+ #
318
+ # Delegate to the main yaml file in use.
319
+ # ========================================================================= #
320
+ def file?
321
+ ::Rcfiles::DirectoryAliases.yaml_file?
322
+ end
323
+
324
+ # ========================================================================= #
325
+ # === test
326
+ # ========================================================================= #
327
+ def test(
328
+ i = seek_this_key?
329
+ )
330
+ verbose_points_to?(i)
331
+ end
332
+
333
+ # ========================================================================= #
334
+ # === show_exceptions?
335
+ # ========================================================================= #
336
+ def show_exceptions?
337
+ SHALL_WE_SHOW_EXCEPTIONS
338
+ end
339
+
340
+ # ========================================================================= #
341
+ # === do_sanitize_the_loaded_dataset (sanitize tag)
342
+ #
343
+ # This method will sanitize the dataset at hand.
344
+ #
345
+ # All changes will be copied back onto the main hash, as-is - thus
346
+ # we lose the original data.
347
+ # ========================================================================= #
348
+ def do_sanitize_the_loaded_dataset(
349
+ hash = hash?,
350
+ # ===================================================================== #
351
+ # Use a yaml-file containing the correct variables next:
352
+ # ===================================================================== #
353
+ use_this_yaml_file = ::Rcfiles.project_base_directory?+
354
+ 'yaml/global_variables/'\
355
+ 'global_variables_extracted_from_the_file_cd_aliases.yml'
356
+ )
357
+ dataset_from_the_global_variables = YAML.load_file(
358
+ use_this_yaml_file
359
+ )
360
+ _ = {}
361
+ hash.each {|key, value|
362
+ # ===================================================================== #
363
+ # Next, work on all value-entries that include a '$' character. This
364
+ # will work on Linux, but on Windows this may not work, so we may
365
+ # have to use another solution for windows specifically. However
366
+ # had, we actually do not need to use ConvertGlobalEnv here,
367
+ # as the autogenerated .yml file already contains the sanitized
368
+ # dataset. The line has been commented out in May 2022.
369
+ #
370
+ # Examples for a key and value pair may be the following:
371
+ #
372
+ # 'to_valheru' => 'cd $VALHERU'
373
+ # 'pwdshell' => 'cd $LINUX/SHELL/
374
+ #
375
+ # We need to map it towards e. g.:
376
+ #
377
+ # ROEBE_SRC: '/home/x/programming/ruby/src/roebe/lib/roebe'
378
+ # LINUX: '/home/x/programming/ruby/src/roebe/lib/roebe/www/linux'
379
+ # UNI_WIEN: '/home/x/studium/UNI_WIEN'
380
+ #
381
+ # ===================================================================== #
382
+ if value.include? '$'
383
+ value = value.dup # Work on a copy past this point.
384
+ value.gsub!(/cd /,'') if value.include? 'cd '
385
+ value.gsub!(/ ll/,'') if value.include? ' ll'
386
+ value.gsub!(/ ls/,'') if value.include? ' ls'
387
+ value.gsub!(/> \/dev\/null/,'') if value.include? '> /dev/null'
388
+ value.gsub!(/2>&1/,'') if value.include? '2>&1'
389
+ # =================================================================== #
390
+ # The next line has been commented out on 03.05.2022:
391
+ #
392
+ # value = ConvertGlobalEnv.convert(value, report_errors?)
393
+ #
394
+ # =================================================================== #
395
+ shortened_value = value.delete_prefix('cd ').dup # Now it is e. g. '$LINUX/SHELL/'.
396
+ # =================================================================== #
397
+ # In the past, until May 2014, we chopped off trailing occurences
398
+ # of 'pwd', but I decided that this is no longer wanted. Certain
399
+ # aliases such as "pwdusb4" would misfunction as a result of
400
+ # the following .gsub:
401
+ #
402
+ # value.gsub!(/pwd$/,'') if value.include? 'pwd'
403
+ #
404
+ # =================================================================== #
405
+ # =================================================================== #
406
+ # As of May 2014, we no longer get rid of ; characters.
407
+ #
408
+ # value.gsub!(/;/,'') if value.include? ';'
409
+ #
410
+ # =================================================================== #
411
+ if value and value.include?('/') # In this case it is more complicated.
412
+ shortened_value = value.scan(/\$(\w+)\//).flatten.first.to_s # See: https://rubular.com/r/L2rAbtKVnnmBfd
413
+ value.sub!(/\$#{shortened_value}/, dataset_from_the_global_variables[shortened_value].to_s)
414
+ else # else no modification is necessary.
415
+ replace_via = dataset_from_the_global_variables[shortened_value.delete('$')].to_s
416
+ value.gsub!(/#{Regexp.quote(value)}/, replace_via)
417
+ end
418
+ end
419
+ if hash.has_key?(value)
420
+ value = hash[value]
421
+ end
422
+ value = rds(value) if value.include? '//'
423
+ _[key] = value.strip # We don't need trailing empty characters.
424
+ }
425
+ @internal_hash[:dataset] = _
426
+ end; alias sanitize_hash do_sanitize_the_loaded_dataset # === sanitize_hash
427
+ alias sanitize_the_hash do_sanitize_the_loaded_dataset # === sanitize_the_hash
428
+ alias sanitize_cd_aliases do_sanitize_the_loaded_dataset # === sanitize_cd_aliases
429
+
430
+ # ========================================================================= #
431
+ # === report_to_user
432
+ #
433
+ # This is a "poor man's" debug variant.
434
+ # ========================================================================= #
435
+ def report_to_user
436
+ pp hash?
437
+ end
438
+
439
+ # ========================================================================= #
440
+ # === load_the_dataset_from_the_file_cd_aliases
441
+ #
442
+ # An alternative argument to this method would be
443
+ # ::Rcfiles.file_cd_aliases?.
444
+ # ========================================================================= #
445
+ def load_the_dataset_from_the_file_cd_aliases(
446
+ file_cd_aliases =
447
+ ::Rcfiles::DirectoryAliases.yaml_file?
448
+ )
449
+ if File.exist? file_cd_aliases
450
+ @internal_hash[:dataset] =
451
+ YAML.load_file(file_cd_aliases)
452
+ end
453
+ end
454
+
455
+ # ========================================================================= #
456
+ # === replacement_or_original_input?
457
+ # ========================================================================= #
458
+ def replacement_or_original_input?(
459
+ _ = first_argument?
460
+ )
461
+ if is_this_key_included?(_)
462
+ translate_key_into_value(_)
463
+ else
464
+ return _
465
+ end
466
+ end; alias corresponds_to replacement_or_original_input? # === corresponds_to
467
+ alias corresponds_to? replacement_or_original_input? # === corresponds_to?
468
+ alias find_this_cd_alias replacement_or_original_input? # === find_this_cd_alias
469
+ alias find_this_sanitized_cd_alias replacement_or_original_input? # === find_this_sanitized_cd_alias
470
+ alias find replacement_or_original_input? # === find
471
+ alias points_at replacement_or_original_input? # === points_at
472
+ alias points_at? replacement_or_original_input? # === points_at?
473
+ alias result? replacement_or_original_input? # === result?
474
+ alias convert replacement_or_original_input? # === convert
475
+ alias _ replacement_or_original_input? # === _
476
+
477
+ # ========================================================================= #
478
+ # === n_entries?
479
+ # ========================================================================= #
480
+ def n_entries?
481
+ dataset?.size
482
+ end
483
+
484
+ # ========================================================================= #
485
+ # === verbose_points_to?
486
+ # ========================================================================= #
487
+ def verbose_points_to?(i = seek_which_key?)
488
+ i = i.to_s
489
+ if is_included? i
490
+ opnn; e "#{::Rcfiles.rev}Yes, the key `#{::Rcfiles.sfancy(i)}` is included."
491
+ opnn; e "#{::Rcfiles.rev}It points to the directory: #{::Rcfiles.sdir(hash?[i])}"
492
+ else
493
+ opnn; e "#{::Rcfiles.rev}No, the key `#{::Rcfiles.sfancy(i)}` is not "\
494
+ "included in the cd-aliases."
495
+ end
496
+ end
497
+
498
+ # ========================================================================= #
499
+ # === cd_into
500
+ # ========================================================================= #
501
+ def cd_into(i)
502
+ Dir.chdir(translate_key_into_value(i))
503
+ end
504
+
505
+ # ========================================================================= #
506
+ # === run (run tag)
507
+ # ========================================================================= #
508
+ def run
509
+ # ======================================================================= #
510
+ # We will do several clean-ups. This is extremely hackish, but also
511
+ # very simple and it kind of works. We got rid of all the $ values.
512
+ # How often to call it? Well, it seems 4x is more than enough;
513
+ # perhaps we can reduce it even more, but for now this is the way
514
+ # it is.
515
+ # ======================================================================= #
516
+ do_sanitize_the_loaded_dataset
517
+ do_sanitize_the_loaded_dataset
518
+ do_sanitize_the_loaded_dataset
519
+ do_sanitize_the_loaded_dataset
520
+ # do_sanitize_the_loaded_dataset
521
+ end
522
+ require 'rcfiles/files/file_cd_aliases.rb'
523
+
524
+ # ========================================================================= #
525
+ # === @yaml_file_used_by_class_directory_aliases
526
+ #
527
+ # This variable will keep track of the main yaml file, that is, the
528
+ # file that holds the dataset for "cd-aliases".
529
+ #
530
+ # It is nil initially here, but will be changed lateron through a method
531
+ # call.
532
+ # ========================================================================= #
533
+ @yaml_file_used_by_class_directory_aliases = nil
534
+
535
+ # ========================================================================= #
536
+ # === Rcfiles::DirectoryAliases.set_yaml_file_used_by_class_directory_aliases
537
+ #
538
+ # The user can modify which yaml file is to be used, via this method call.
539
+ # ========================================================================= #
540
+ def self.set_yaml_file_used_by_class_directory_aliases(
541
+ i = :default
542
+ )
543
+ case i
544
+ # ======================================================================= #
545
+ # === :default
546
+ # ======================================================================= #
547
+ when :default,
548
+ :default_yaml_file
549
+ i = ::Rcfiles.file_cd_aliases
550
+ end
551
+ @yaml_file_used_by_class_directory_aliases = i
552
+ end
553
+
554
+ # ========================================================================= #
555
+ # Define the main cd-aliases file next. The constant FILE_CD_ALIASES
556
+ # must be defined for this to work.
557
+ # ========================================================================= #
558
+ DirectoryAliases.set_yaml_file_used_by_class_directory_aliases(:default_yaml_file)
559
+
560
+ # ========================================================================= #
561
+ # === Rcfiles::DirectoryAliases.yaml_file?
562
+ #
563
+ # On my home system this defaults to:
564
+ #
565
+ # /usr/lib/ruby/site_ruby/3.1.0/rcfiles/yaml/cd_aliases.yml
566
+ #
567
+ # ========================================================================= #
568
+ def self.yaml_file?
569
+ @yaml_file_used_by_class_directory_aliases
570
+ end
571
+
572
+ # ========================================================================= #
573
+ # === []
574
+ # ========================================================================= #
575
+ def [](i)
576
+ replacement_or_original_input?(i)
577
+ end
578
+
579
+ # ========================================================================= #
580
+ # === Rcfiles::DirectoryAliases.is_included?
581
+ #
582
+ # Note that this instantiates a new DirectoryAliases object, so it
583
+ # is more expensive than instantiating on your own and then simply
584
+ # using .is_included? on that object. The reason why this method
585
+ # still exists, then, is mostly because it is convenient to NOT
586
+ # have to instantiate something specifically, as that normally
587
+ # takes two lines of code.
588
+ # ========================================================================= #
589
+ def self.is_included?(i)
590
+ _ = new(i)
591
+ return _.is_included?(i)
592
+ end; self.instance_eval { alias does_include? is_included? } # === Rcfiles::DirectoryAliases.does_include?
593
+ self.instance_eval { alias is_this_alias_included? is_included? } # === Rcfiles::DirectoryAliases.is_this_alias_included?
594
+ self.instance_eval { alias is_this_input_a_cd_alias? is_included? } # === Rcfiles::DirectoryAliases.is_this_input_a_cd_alias?
595
+ # Rcfiles::DirectoryAliases.include? # ← This here would conflict, hence we can not use it.
596
+
597
+ # ========================================================================= #
598
+ # === Rcfiles::DirectoryAliases.e
599
+ # ========================================================================= #
600
+ def self.e(i = '')
601
+ ::Rcfiles.e(i)
602
+ end
603
+
604
+ # ========================================================================= #
605
+ # === Rcfiles::DirectoryAliases[]
606
+ #
607
+ # This must return a String - either the original input, or the
608
+ # replacement.
609
+ #
610
+ # Usage example:
611
+ #
612
+ # Rcfiles::DirectoryAliases['pwdj'] # => '/Depot/j/'
613
+ #
614
+ # ========================================================================= #
615
+ def self.[](i = ARGV)
616
+ new(i).replacement_or_original_input?
617
+ end
618
+
619
+ require 'yaml'
620
+ require 'rcfiles/toplevel_methods/e.rb'
621
+ require 'rcfiles/toplevel_methods/no_file_exists_at.rb'
622
+ require 'rcfiles/toplevel_methods/save_file.rb'
623
+
624
+ # ========================================================================= #
625
+ # === @toplevel_stored_dataset
626
+ #
627
+ # This variable will hold the whole cd-aliases dataset. It should be
628
+ # kept as a Hash at all times. It also should be empty, initially.
629
+ #
630
+ # In the event that it is filled with content it will be copied
631
+ # onto any new instance of DirectoryAliases. That way we can
632
+ # use a substitute hash, or allow the user to fill it up on
633
+ # his or her own.
634
+ #
635
+ # Note that before May 2022 this was called @cd_aliases, but
636
+ # I think the new name is more logical.
637
+ # ========================================================================= #
638
+ @toplevel_stored_dataset = {}
639
+
640
+ # ========================================================================= #
641
+ # === Rcfiles::DirectoryAliases.toplevel_stored_dataset?
642
+ #
643
+ # Main getter over the cd-aliases. This will be our main datastructure.
644
+ # ========================================================================= #
645
+ def self.toplevel_stored_dataset?
646
+ @toplevel_stored_dataset
647
+ end; self.instance_eval { alias cd_aliases? toplevel_stored_dataset? } # === Rcfiles::DirectoryAliases.cd_aliases?
648
+ self.instance_eval { alias toplevel_dataset? toplevel_stored_dataset? } # === Rcfiles::DirectoryAliases.toplevel_dataset?
649
+ self.instance_eval { alias hash? toplevel_stored_dataset? } # === Rcfiles::DirectoryAliases.hash?
650
+
651
+ # ========================================================================= #
652
+ # === Rcfiles::DirectoryAliases.points_at?
653
+ # ========================================================================= #
654
+ def self.points_at?(i)
655
+ _ = new(i)
656
+ return _.points_at?
657
+ end
658
+
659
+ # ========================================================================= #
660
+ # === Rcfiles::DirectoryAliases.n_entries?
661
+ #
662
+ # How many entries can be found in class DirectoryAliases is answered
663
+ # by this toplevel method.
664
+ # ========================================================================= #
665
+ def self.n_entries?
666
+ toplevel_stored_dataset?.keys.size
667
+ end
668
+
669
+ # ========================================================================= #
670
+ # === Rcfiles::DirectoryAliases.set_toplevel_stored_dataset (set_cd_aliases tag)
671
+ #
672
+ # The @toplevel_stored_dataset should always be a Hash. This is an
673
+ # explicit setter here, though, so the user could always assign a
674
+ # non-Hash. No error will be raised in this case.
675
+ # ========================================================================= #
676
+ def self.set_toplevel_stored_dataset(i = {})
677
+ @toplevel_stored_dataset = i
678
+ end; self.instance_eval { alias set_cd_aliases set_toplevel_stored_dataset } # === Rcfiles::DirectoryAliases.set_cd_aliases=
679
+ self.instance_eval { alias set_cd_aliases= set_toplevel_stored_dataset } # === Rcfiles::DirectoryAliases.set_cd_aliases=
680
+
681
+ # ========================================================================= #
682
+ # === Rcfiles::DirectoryAliases.clear
683
+ #
684
+ # Reset the main Hash again.
685
+ # ========================================================================= #
686
+ def self.clear
687
+ @toplevel_stored_dataset.clear
688
+ end
689
+
690
+ # ========================================================================= #
691
+ # === Rcfiles::DirectoryAliases.load_dataset_from_main_file
692
+ #
693
+ # Load up the dataset next
694
+ #
695
+ # This determines the main cd-aliases file.
696
+ # ========================================================================= #
697
+ def self.load_dataset_from_main_file(
698
+ i = yaml_file?
699
+ )
700
+ if File.exist? i
701
+ dataset = YAML.load_file(i) # This may be UTF-8 encoding.
702
+ # ===================================================================== #
703
+ # Assign the cd-aliases via a toplevel method next:
704
+ # ===================================================================== #
705
+ set_toplevel_stored_dataset(dataset)
706
+ else # Else use an empty dataset instead.
707
+ if is_on_roebe?
708
+ ::Rcfiles.no_file_exists_at(i)
709
+ end
710
+ set_toplevel_stored_dataset({})
711
+ end
712
+ end
713
+
714
+ # ========================================================================= #
715
+ # === Rcfiles::DirectoryAliases.initialize_main_hash
716
+ # ========================================================================= #
717
+ def self.initialize_main_hash
718
+ clear # First empty the old dataset, should it be non-empty.
719
+ if toplevel_dataset?.empty?
720
+ load_dataset_from_main_file
721
+ end
722
+ end; self.instance_eval { alias initialize_main_dataset initialize_main_hash } # === Rcfiles::DirectoryAliases.initialize_main_dataset
723
+ self.instance_eval { alias run initialize_main_hash } # === Rcfiles::DirectoryAliases.run
724
+ # ======================================================================= #
725
+ # Initialize the main dataset if it has not been loaded up yet.
726
+ # ======================================================================= #
727
+ if ::Rcfiles::DirectoryAliases.cd_aliases?.empty?
728
+ ::Rcfiles::DirectoryAliases.load_dataset_from_main_file
729
+ end
730
+
731
+ # ========================================================================= #
732
+ # === Rcfiles::DirectoryAliases.register_sigint
733
+ # ========================================================================= #
734
+ def self.register_sigint
735
+ Signal.trap('SIGINT') { exit }
736
+ end
737
+
738
+ register_sigint # ← And call it at once.
739
+
740
+ # ========================================================================= #
741
+ # === Rcfiles::DirectoryAliases.save_into_ruby_file
742
+ #
743
+ # This method will store the hash in a CONSTANT, into a file (normally
744
+ # a .rb file).
745
+ #
746
+ # The first argument to this method denotes where to store this file,
747
+ # so you have the possibility to save into some special location
748
+ # instead.
749
+ #
750
+ # The name of the hash is provided via the the second argument to
751
+ # this method.
752
+ # ========================================================================= #
753
+ def self.save_into_ruby_file(
754
+ store_into_this_file = '/Depot/j/expand_cd_aliases_hash.rb',
755
+ name_of_the_main_hash = 'EXPAND_CD_ALIASES_HASH'
756
+ )
757
+ _ = ::Rcfiles::DirectoryAliases.new
758
+ dataset = _.cd_aliases?
759
+ # ======================================================================= #
760
+ # First add the header:
761
+ # ======================================================================= #
762
+ what = '#!/usr/bin/ruby -w
763
+ # Encoding: UTF-8
764
+ # frozen_string_literal: true
765
+ # =========================================================================== #
766
+ '.dup
767
+ what << "#{name_of_the_main_hash} = {\n"
768
+ what = ::Rcfiles.ensure_main_encoding(what)
769
+ dataset.each_pair {|key, value|
770
+ key = key.to_s unless key.is_a? String
771
+ value = ConvertGlobalEnv.convert(value)
772
+ key = ::Rcfiles.ensure_main_encoding(key)
773
+ value = ::Rcfiles.ensure_main_encoding(value)
774
+ what << " \"#{key}\": \"#{value}\",\n"
775
+ }
776
+ what << "}\n"
777
+ into = store_into_this_file
778
+ if block_given?
779
+ yielded = yield
780
+ case yielded
781
+ when :be_verbose
782
+ puts 'Storing into the file `'+into+'`.'
783
+ end
784
+ end
785
+ ::Rcfiles.write_what_into(what, into)
786
+ end; self.instance_eval { alias generate_cd_aliases save_into_ruby_file } # === Rcfiles::DirectoryAliases.generate_cd_aliases
787
+
788
+ end
789
+
790
+ # ========================================================================= #
791
+ # === Rcfiles.is_this_alias_included?
792
+ #
793
+ # This method can be used to determine whether a given key is included
794
+ # or not.
795
+ #
796
+ # Usage example:
797
+ #
798
+ # Rcfiles.is_this_alias_included? 'pwdsongs'
799
+ # Rcfiles.is_this_alias_included? 'todoc'
800
+ #
801
+ # ========================================================================= #
802
+ def self.is_this_alias_included?(i)
803
+ ::Rcfiles::DirectoryAliases.is_this_alias_included?(i)
804
+ end; self.instance_eval { alias is_this_input_a_cd_alias? is_this_alias_included? } # === Rcfiles.is_this_input_a_cd_alias?
805
+ self.instance_eval { alias is_this_key_available? is_this_alias_included? } # === Rcfiles.is_this_key_available?
806
+ self.instance_eval { alias is_this_key_registered? is_this_alias_included? } # === Rcfiles.is_this_key_registered?
807
+
808
+ # =========================================================================== #
809
+ # === Rcfiles.directory_aliases
810
+ #
811
+ # This method will instantiate a new DirectoryAliases object.
812
+ # =========================================================================== #
813
+ def self.directory_aliases(i)
814
+ ::Rcfiles::DirectoryAliases.new(i)
815
+ end; self.instance_eval { alias cd_alias directory_aliases } # === Rcfiles.cd_alias
816
+ self.instance_eval { alias expand_cd_aliases directory_aliases } # === Rcfiles.expand_cd_aliases
817
+
818
+ # =========================================================================== #
819
+ # === Rcfiles.find_this_cd_alias
820
+ #
821
+ # Usage example for this method:
822
+ #
823
+ # Rcfiles.find_this_cd_alias('pwdj') # => "cd /Depot/j"
824
+ # Rcfiles.find_this_cd_alias('todoc') # => "/documentation/"
825
+ #
826
+ # =========================================================================== #
827
+ def self.find_this_cd_alias(i)
828
+ ::Rcfiles::DirectoryAliases[i]
829
+ end
830
+
831
+ end
832
+
833
+ if __FILE__ == $PROGRAM_NAME # Testing class Rcfiles::DirectoryAliases here.
834
+ alias e puts
835
+ require 'colours'
836
+ directory_aliases = Rcfiles::DirectoryAliases.new(ARGV)
837
+ print ::Colours.rev
838
+ e '='*80
839
+ e 'The version of the rcfiles gem in use is: '+directory_aliases.version?.to_s
840
+ e directory_aliases.n_entries?.to_s+
841
+ ' keys are registered as being cd-aliases.'
842
+ e 'Is the key `tova` included: '+directory_aliases.is_this_key_included?('tova').to_s
843
+ e 'It corresponds to: '+directory_aliases.corresponds_to('tova')
844
+ e 'Is the key `tovaa` included: '+directory_aliases.is_this_key_included?('tovaa').to_s
845
+ e 'Is the key `pwdsongs` included: '+directory_aliases.is_this_key_included?('pwdsongs').to_s
846
+ e 'It corresponds to: '+directory_aliases.corresponds_to('pwdsongs')
847
+ # e 'The expanded value is: '+
848
+ # directory_aliases.expanded_key?('pwdsongs')
849
+ e 'Is the key `tosongs` included: '+directory_aliases.is_this_key_included?('tosongs').to_s
850
+ e 'It corresponds to: '+directory_aliases.corresponds_to('tosongs')
851
+ # e 'The expanded value is: '+
852
+ # directory_aliases.expanded_key?('tosongs')
853
+ e 'Next testing Rcfiles.is_this_alias_included?(): is
854
+ "pwdsongs" included? '+
855
+ Rcfiles.is_this_alias_included?('pwdsongs').to_s
856
+ e 'Next testing Rcfiles.find_this_cd_alias() - pwdj equals '\
857
+ ' '+Rcfiles.find_this_cd_alias('pwdj') # => "cd /Depot/j" # => "cd /Depot/j"
858
+ e '='*80
859
+ directory_aliases.test('toxx')
860
+ e '='*80
861
+ directory_aliases._.class
862
+ Rcfiles::DirectoryAliases.initialize_main_hash
863
+ pp Rcfiles::DirectoryAliases.n_entries?
864
+ # Rcfiles::DirectoryAliases.save_into_ruby_file
865
+ end # directoryaliases
866
+ # ecdaliases
867
+ # exda pwwdusr; exda pwddwwd
868
+ # exda pwdusb4