Class: Rcfiles::ExpandCdAliases

Inherits:
Object
  • Object
show all
Defined in:
lib/rcfiles/expand_cd_aliases/run.rb,
lib/rcfiles/expand_cd_aliases/constants.rb,
lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb

Overview

Rcfiles::ExpandCdAliases

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
FILE_CD_ALIASES =
#

Rcfiles::ExpandCdAliases::FILE_CD_ALIASES

This constant, called FILE_CD_ALIASES, will store the path to the file called 'cd_aliases.yml'.

We will query the toplevel module Rcfiles for the path to this file.

#
::Rcfiles.file_cd_aliases?
NAME_OF_FILE =
#

Rcfiles::ExpandCdAliases::NAME_OF_FILE

#
'expand_cd_aliases.rb'
SHALL_WE_SHOW_EXCEPTIONS =
#

Rcfiles::ExpandCdAliases::SHALL_WE_SHOW_EXCEPTIONS

#
false
DEFAULT_RUN_MODE =
#

Rcfiles::ExpandCdAliases::DEFAULT_RUN_MODE

#
false
EMPTY_STRING =
#

EMPTY_STRING

Dummy empty string object.

#
''.dup

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(optional_seek_this_key = nil, run_already = DEFAULT_RUN_MODE) ⇒ ExpandCdAliases

#

initialize

The first argument is optional. If given then we will query for this particular key, aka whether it is a cd-alias or whether it is not.

#


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 137

def initialize(
    optional_seek_this_key = nil,
    run_already            = DEFAULT_RUN_MODE
  )
  reset
  # ======================================================================= #
  # === Handle blocks given to this class next
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :run
    # ===================================================================== #
    when :run
      run_already = true
    # ===================================================================== #
    # === :initialize_main_hash
    # ===================================================================== #
    when :initialize_main_hash
      initialize_main_hash
    end
  end
  case run_already
  when :do_not_report_errors
    @report_errors = false
    run_already = DEFAULT_RUN_MODE
  when :be_silent
    be_silent
    run_already = DEFAULT_RUN_MODE
  end # This block must run before sanitize_cd_aliases() is called.
  if optional_seek_this_key
    set_return_string(optional_seek_this_key)
  end
  run if run_already
end

Class Method Details

.[](i) ⇒ Object

#

Rcfiles::ExpandCdAliases[]

#


577
578
579
580
581
582
583
584
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 577

def self.[](i)
  i = i.to_s
  if self.is_this_alias_included? i
    _ = self.new(i)
    i = _.hash[i]
  end
  return i
end

.cd_aliases?Boolean

#

ExpandCdAliases.cd_aliases?

Main getter over the cd-aliases. This will be our main datastructure.

#

Returns:

  • (Boolean)


70
71
72
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 70

def self.cd_aliases?
  @cd_aliases
end

.clearObject

#

ExpandCdAliases.clear

Reset the main Hash again.

#


79
80
81
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 79

def self.clear
  @cd_aliases.clear
end

.e(i = '') ⇒ Object

#

ExpandCdAliases.e

#


563
564
565
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 563

def self.e(i = '')
  ::Rcfiles.e(i)
end

.is_included?(i) ⇒ Boolean

#

Rcfiles::ExpandCdAliases.is_included?

#

Returns:

  • (Boolean)


498
499
500
501
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 498

def self.is_included?(i)
  _ = new(i)
  return _.is_included?(i)
end

.is_this_alias_included?(i) ⇒ Boolean

#

Rcfiles::ExpandCdAliases.is_this_alias_included?

#

Returns:

  • (Boolean)


570
571
572
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 570

def self.is_this_alias_included?(i)
  self.new(i).is_included?
end

.load_dataset_from_main_file(i = self.yaml_file?) ⇒ Object

#

Load up the dataset next

This determines the main cd-aliases file.

#


517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 517

def self.load_dataset_from_main_file(
    i = self.yaml_file?
  )
  if File.exist? i
    begin
      dataset = YAML.load_file(i) # This may be UTF-8 encoding.
      # =================================================================== #
      # Assign the cd-aliases via a toplevel method next:
      # =================================================================== #
      self.set_cd_aliases(dataset)
    rescue Psych::SyntaxError => error
      e 'A Psych::SyntaxError happened for the file `'+i+'`.'
      pp error
    end
  else # Else use an empty dataset instead.
    if is_on_roebe?
      ::Rcfiles.no_file_exists_at(i)
    end
    self.set_cd_aliases {}
  end
end

.n_entries?Boolean

#

ExpandCdAliases.n_entries?

How many entries can be found in class ExpandCdAliases is answered by this toplevel method.

#

Returns:

  • (Boolean)


556
557
558
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 556

def self.n_entries?
  @cd_aliases.keys.size
end

.points_at?(i) ⇒ Boolean

#

Rcfiles::ExpandCdAliases.points_at?

#

Returns:

  • (Boolean)


507
508
509
510
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 507

def self.points_at?(i)
  _ = self.new(i)
  return _._
end

.register_sigintObject

#

Rcfiles::ExpandCdAliases.register_sigint

#


86
87
88
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 86

def self.register_sigint
  Signal.trap('SIGINT') { exit }
end

.save_into_ruby_file(store_into_this_file = '/Depot/j/expand_cd_aliases_hash.rb', name_of_the_main_hash = 'EXPAND_CD_ALIASES_HASH') ⇒ Object

#

Rcfiles::ExpandCdAliases.save_into_ruby_file

This method will store the hash in a CONSTANT, into a file (normally a .rb file).

The first argument to this method denotes where to store this file, so you have the possibility to save into some special location instead.

The name of the hash is provided via the the second argument to this method.

#


598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 598

def self.save_into_ruby_file(
    store_into_this_file  = '/Depot/j/expand_cd_aliases_hash.rb',
    name_of_the_main_hash = 'EXPAND_CD_ALIASES_HASH'
  )
  _ = ::Rcfiles::ExpandCdAliases.new
  _.initialize_main_dataset
  dataset = _.cd_aliases?
  # ======================================================================= #
  # First add the header:
  # ======================================================================= #
  what = '#!/usr/bin/ruby -w
# Encoding: UTF-8
# frozen_string_literal: true
# =========================================================================== #
'.dup
  what << "#{name_of_the_main_hash} = {\n"
  what = ::Rcfiles.ensure_main_encoding(what)
  dataset.each_pair {|key, value|
    key = key.to_s unless key.is_a? String
    value = ConvertGlobalEnv.convert(value)
    key = ::Rcfiles.ensure_main_encoding(key)
    value = ::Rcfiles.ensure_main_encoding(value)
    what << "  \"#{key}\": \"#{value}\",\n"
  }
  what << "}\n"
  into = store_into_this_file
  ::Rcfiles.write_what_into(what, into)
end

.set_cd_aliases(i = {}) ⇒ Object

#

ExpandCdAliases.set_cd_aliases (set_cd_aliases tag)

The @cd_aliases should always be a Hash. This is an explicit setter, though, so the user could always assign a non-Hash. No error will be raised in this case.

#


546
547
548
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 546

def self.set_cd_aliases(i = {})
  @cd_aliases = i
end

.set_main_yaml_file(i = :default) ⇒ Object

#

ExpandCdAliases.set_main_yaml_file

The user can modify which yaml file is to be used, via this method call.

#


108
109
110
111
112
113
114
115
116
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 108

def self.set_main_yaml_file(
    i = :default
  )
  case i
  when :default
    i = FILE_CD_ALIASES
  end
  @main_yaml_file = i
end

.yaml_file?Boolean

#

ExpandCdAliases.yaml_file?

#

Returns:

  • (Boolean)


127
128
129
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 127

def self.yaml_file?
  @main_yaml_file
end

Instance Method Details

#[](i) ⇒ Object

#

[]

#


491
492
493
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 491

def [](i)
  @hash[i]
end

#be_silentObject Also known as: be_quiet

#

be_silent

#


318
319
320
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 318

def be_silent
  @be_verbose = false
end

#be_verboseObject

#

be_verbose

#


311
312
313
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 311

def be_verbose
  @be_verbose = true
end

#be_verbose?Boolean

#

be_verbose?

#

Returns:

  • (Boolean)


339
340
341
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 339

def be_verbose?
  @be_verbose
end

#file?Boolean

#

file?

#

Returns:

  • (Boolean)


224
225
226
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 224

def file?
  ExpandCdAliases.yaml_file?
end

#has_key?Boolean

#

is_included?

Query whether our expanded cd-aliases have the given key (first argument) included or whether they do not.

#

has_key?

Returns:

  • (Boolean)


288
289
290
291
292
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 288

def is_included?(
    i = @seek_this_key
  )
  @hash.has_key?(i)
end

#hash?Boolean Also known as: hash, cd_aliases?

#

hash?

Query the main hash.

#

Returns:

  • (Boolean)


461
462
463
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 461

def hash?
  @hash
end

#initialize_main_hashObject Also known as: initialize_main_dataset

#

initialize_main_hash

This method will do two things:

1) initialize the main dataset
2) sanitize this dataset (for use within this class)
#


376
377
378
379
380
381
382
383
384
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 376

def initialize_main_hash
  # ======================================================================= #
  # Initialize the main dataset if it has not been loaded up yet.
  # ======================================================================= #
  if ::Rcfiles::ExpandCdAliases.cd_aliases?.empty?
    ::Rcfiles::ExpandCdAliases.load_dataset_from_main_file
  end
  sanitize_cd_aliases
end

#is_included?(i = @seek_this_key) ⇒ Boolean

#

is_included?

Query whether our expanded cd-aliases have the given key (first argument) included or whether they do not.

#

Returns:

  • (Boolean)


284
285
286
287
288
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 284

def is_included?(
    i = @seek_this_key
  )
  @hash.has_key?(i)
end

#is_this_alias_included?Boolean

#

is_included?

Query whether our expanded cd-aliases have the given key (first argument) included or whether they do not.

#

is_this_alias_included?

Returns:

  • (Boolean)


289
290
291
292
293
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 289

def is_included?(
    i = @seek_this_key
  )
  @hash.has_key?(i)
end

#is_this_input_a_cd_alias?Boolean

#

is_included?

Query whether our expanded cd-aliases have the given key (first argument) included or whether they do not.

#

is_this_input_a_cd_alias?

Returns:

  • (Boolean)


290
291
292
293
294
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 290

def is_included?(
    i = @seek_this_key
  )
  @hash.has_key?(i)
end

#opnnObject Also known as: opn

#

opnn

#


203
204
205
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 203

def opnn
  super(NAMESPACE) if Object.const_defined? :Opn
end

#report_errors?Boolean

#

report_errors?

#

Returns:

  • (Boolean)


346
347
348
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 346

def report_errors?
  @report_errors
end

#report_to_userObject

#

report_to_user

This is a “poor man's” debug variant.

#


297
298
299
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 297

def report_to_user
  pp @hash
end

#report_versionObject

#

report_version

#


332
333
334
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 332

def report_version
  e "The version is: #{sfancy(version?)}"
end

#resetObject

#

reset (reset tag)

#


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 177

def reset
  # ======================================================================= #
  # === @hash
  # ======================================================================= #
  @hash = {}
  # ======================================================================= #
  # === @return_string
  # ======================================================================= #
  @return_string = nil # This is the return string. Nil on startup.
  # ======================================================================= #
  # === @seek_this_key
  # ======================================================================= #
  @seek_this_key = '' # The query-key that we are looking for.
  # ======================================================================= #
  # === @report_errors
  #
  # The next variable determines whether we will report errors or
  # whether we will not. By default this class will report errors.
  # ======================================================================= #
  @report_errors = true
  be_quiet # Do not be too noisy by default.
end

#return_string?Boolean Also known as: result?, _

#

return_string?

#

Returns:

  • (Boolean)


249
250
251
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 249

def return_string?
  @return_string # Return the expanded return string.
end

#revObject

#

rev

#


231
232
233
234
235
236
237
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 231

def rev
  if ::Rcfiles.use_colours?
    ::Colours.rev
  else
    EMPTY_STRING
  end
end

#runObject

#

run (run tag)

#


14
15
16
# File 'lib/rcfiles/expand_cd_aliases/run.rb', line 14

def run
  initialize_main_hash
end

#sanitize_cd_aliases(use_this_dataset = ::Rcfiles::ExpandCdAliases.cd_aliases?) ⇒ Object

#

sanitize_cd_aliases

We sanitize the whole dataset. At first, we correct all $ entries.

In order for this method to work, the main dataset has to be set.

#


393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 393

def sanitize_cd_aliases(
    use_this_dataset = ::Rcfiles::ExpandCdAliases.cd_aliases?
  )
  hash = {}
  # ======================================================================= #
  # First, set up our new hash and also replace all $ found.
  # ======================================================================= #
  use_this_dataset.each {|key, value|
    begin
      unless value.to_s.encoding.to_s == MAIN_ENCODING_TO_USE
        value = value.to_s.encode(MAIN_ENCODING_TO_USE) # Work with the main-encoding past this point.
      end
    rescue Encoding::InvalidByteSequenceError => error
      if be_verbose? and show_exceptions? and report_errors?
        opnn; e "#{NAME_OF_FILE}: An exception/error happened for -> #{simp(key.to_s)}"
        opnn; e 'We will now feedback this error:'
        pp error
      end
    end
    # ===================================================================== #
    # Next, work on all value-entries that include a '$' character.
    # ===================================================================== #
    if value.include? '$'
      value = ConvertGlobalEnv.convert(value, report_errors?)
    end
    # ===================================================================== #
    # Next, we must perform a few batch-gsubs, but we must be careful,
    # as this could lead to bugs.
    # ===================================================================== #
    unless value.encoding.to_s == 'UTF-8'
      # =================================================================== #
      # First, we chop off trailing occurences of 'pwd', but as of May
      # 2014, I decided that this is no longer wanted. Certain aliases
      # such as "pwdusb4" would misfunction as a result of 
      # the following .gsub:
      #   value.gsub!(/pwd$/,'') if value.include? 'pwd'
      # =================================================================== #
      value.gsub!(/cd /,'')  if value.include? 'cd '
      value.gsub!(/ ll/,'')  if value.include? ' ll'
      value.gsub!(/ ls/,'')  if value.include? ' ls'
      value.gsub!(/> \/dev\/null/,'') if value.include? '> /dev/null'
      value.gsub!(/2>&1/,'') if value.include? '2>&1'
      # =================================================================== #
      # As of May 2014, we no longer get rid of ; characters.
      # =================================================================== #
      # value.gsub!(/;/,'')    if value.include? ';'
      value = value.strip
    end
    hash[key] = value
  }
  @hash = hash
  # ======================================================================= #
  # Ok, at this point we have our hash data, and we also sanitized
  # it a little bit already, by getting rid of the $ values.
  # So now we call another method to further clean our hash. This
  # is evidently totally hackish, but it also sort of works.
  # ======================================================================= #
  sanitize_hash
  sanitize_hash
  sanitize_hash
  sanitize_hash
end

#sanitize_hashObject

#

sanitize_hash

We make @hash better here, by sanitizing some value entries.

#


355
356
357
358
359
360
361
362
363
364
365
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 355

def sanitize_hash
  _ = {}
  @hash.each {|key, value|
    if value.include? '/' # assume it is ok here
    else # else work on it.
      value = @hash[value] if hash?.has_key? value
    end
    _[key] = value
  }
  @hash = _
end

#seek_this_key?Boolean

#

seek_this_key?

#

Returns:

  • (Boolean)


267
268
269
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 267

def seek_this_key?
  @seek_this_key
end

#set_return_string(i = '') ⇒ Object Also known as: set

#

set_return_string

Before this method is called, sanitize_cd_aliases() should have been called in order to initialize our dataset, and also set up the This method will assign to @seek_this_key and @return_string.

#


475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 475

def set_return_string(i = '')
  if @hash.empty?
    # ===================================================================== #
    # Initialize the main hash if it is empty.
    # ===================================================================== #
    initialize_main_hash
  end
  i = i.first if i.is_a? Array
  i = i.to_s.dup
  set_seek_this_key(i)
  @return_string = @hash[i]
end

#set_seek_this_key(i) ⇒ Object

#

set_seek_this_key

#


274
275
276
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 274

def set_seek_this_key(i)
  @seek_this_key = i
end

#show_exceptions?Boolean

#

show_exceptions?

#

Returns:

  • (Boolean)


304
305
306
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 304

def show_exceptions?
  SHALL_WE_SHOW_EXCEPTIONS
end

#size?Boolean Also known as: size, keys

#

size?

Query how many keys we have.

#

Returns:

  • (Boolean)


259
260
261
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 259

def size?
  @hash.keys.size
end

#test(i = @seek_this_key) ⇒ Object

#

test

#


242
243
244
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 242

def test(i = @seek_this_key)
  verbose_points_to?(i)
end

#verbose_points_to?(i) ⇒ Boolean

#

verbose_points_to?

#

Returns:

  • (Boolean)


210
211
212
213
214
215
216
217
218
219
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 210

def verbose_points_to?(i)
  i = i.to_s
  if is_included? i
    opnn; e "#{rev}Yes, the key `#{::Rcfiles.sfancy(i)}` is included."
    opnn; e "#{rev}It points to the directory: #{sdir(@hash[i])}"
  else
    opnn; e "#{rev}No, the key `#{::Rcfiles.sfancy(i)}` is not "\
            "included in the cd-aliases."
  end
end

#version?Boolean

#

version?

#

Returns:

  • (Boolean)


325
326
327
# File 'lib/rcfiles/expand_cd_aliases/expand_cd_aliases.rb', line 325

def version?
  ::Rcfiles::VERSION
end