Class: Roebe::Aliases

Inherits:
Base show all
Defined in:
lib/roebe/classes/aliases.rb

Overview

Roebe::Aliases

Constant Summary collapse

DEBUG =
#

Roebe::Aliases::DEBUG

#
false
LINUX_YAML =

else we hardcode it.

'/home/x/DATA/PC/OS/LINUX/yaml/'
RCFILES_YAML =
#

Roebe::Aliases::RCFILES_YAML

#
'/home/x/programming/ruby/src/rcfiles/lib/rcfiles/yaml/'
ENCODING_TO_USE =
#

Roebe::Aliases::ENCODING_TO_USE

Specify which encoding to use next.

#
'UTF-8'
ALIASES =
path+'aliases.yml'
CD_ALIASES =
path+'cd_aliases.yml'
PROGRAMS_ALIASES =
path+'programs_aliases.yml'

Constants inherited from Base

Base::COLOURS, Base::HOME_DIRECTORY_OF_USER_X, Base::N, Base::NAMESPACE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#actions, #append_this_onto_that_file, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #burlywood, #call_next, #chdir, #cheerful_guy, #cliner, #complex_esystem, #convert_global_env, #copy_directory, #copy_file, #cornflowerblue, #create_directory, #crimson, #current_month?, #current_time?, #current_year?, #cyan, #darkcyan, #darkgreen, #darkkhaki, #darkslateblue, #deeppink, #delete_symlink, #do_not_use_the_base_colours, #do_use_the_base_colours, #e, #ecomment, #editor_to_use?, #efancy, #eimp, #emphasis, #ensure_main_encoding, #ensure_utf_encoding, #eparse, #erev, #esteelblue, #etomato, #ewarn, #exit_program, #firebrick, #get_current_day, #get_current_month, #get_files_and_directories, #get_files_from, #get_german_name_for_this_weekday, #gold, #green, #grey, #hh_mm_ss, #hh_mm_ss_day_month_year, #home_dir?, #home_directory_of_user_x?, #infer_the_namespace, #internal_hash?, #is_a_directory?, #is_a_file?, #is_a_jpg_file?, #is_an_image_file?, #is_archive?, #is_audio_file?, #is_in_studium_dir?, #is_multimedia_file?, #is_on_roebe?, #is_on_windows?, #is_studium_available?, #is_symlink?, #is_this_a_ruby_file?, #is_video_file?, #iso_encoding?, #le, #left_colour, #lightblue, #lightgreen, #lightseagreen, #lightsteelblue, #lime, #limegreen, #localhost_to_data, #log_directory?, #main_encoding?, #mediumorchid, #mediumpurple, #mediumseagreen, #mediumslateblue, #mediumspringgreen, #mediumturquoise, #mkdir_p, #mv, #n_days_in_this_month, #n_pages_in_this_pdf_file?, #namespace?, #no_file_exists_at, #ogrey, #olive, #olivedrab, #open_in_browser, #opne, #opnesystem, #opnn, #orange, #orchid, #orev, #palegoldenrod, #palevioletred, #pink, #powderblue, #programs_dir?, #project_base_dir?, #random_html_colour, #rds, #read_file_in_iso_encoding, #read_file_via_the_default_encoding, #read_lines_via_iso_encoding, #readlines_with_main_encoding, #red, #register_sigint, #remove, #remove_directory, #remove_file, #rename_kde_konsole_tab, #replace_localhost_with_data, #report_pwd, #require_rescue, #reset_the_internal_hash, #return_all_directories_from_this_directory, #return_all_files_from_this_directory, #return_current_directory, #return_dd_mm_yyyy, #return_file_or_directory_of, #return_files_from_pwd, #return_last_part_of_the_current_directory, #return_utc, #rev, #right_arrow?, #right_colour, #roebe_log_directory?, #rosybrown, #royalblue, #ruby_base_directory?, #run_in_background, #run_rcfiles_then_run_ata_via_qdbus, #sandybrown, #sdir, #seagreen, #set_be_verbose, #set_xorg_buffer, #sfancy, #sfile, #show_help, #silent_redirection?, #simp, #simple_esystem, #skyblue, #slateblue, #slategray, #springgreen, #steelblue, #string_italic, #swarn, #symlink, #teal, #temp_dir?, #to_camelcase, #to_counted_hash, #tomato, #touch, #try_to_require_the_beautiful_url_gem, #try_to_require_the_html_template, #try_to_require_the_open_gem, #try_to_require_the_program_information_gem, #try_to_require_the_xorg_buffer, #use_colours?, #verbose_truth, #weekday?, #word_wrap, #write_what_into, #yellow

Methods included from Base::CommandlineArguments

#append_onto_the_commandline_arguments, #clear_commandline_arguments, #commandline_arguments?, #commandline_arguments_as_string?, #commandline_arguments_without_leading_hyphens?, #first_argument?, #first_argument_without_leading_hyphens?, #has_an_argument_been_passed?, #remove_hyphened_arguments_from_the_commandline_arguments, #return_commandline_arguments_with_leading_hyphens, #second_argument?, #set_commandline_arguments

Constructor Details

#initialize(i = ARGV, run_already = true) ⇒ Aliases

#

initialize

Please note that our final aliases will be accessible all merged together in the instance variable @aliases. We prefer to keep the “expanded” version of every pointer. The real value will be kept in the old alias though, in case someone finds that useful to have.

However, since we access the individual entries as well, we must ensure that they are all proper.

#


88
89
90
91
92
93
94
95
# File 'lib/roebe/classes/aliases.rb', line 88

def initialize(
    i           = ARGV,
    run_already = true
  )
  reset
  set_commandline_arguments(i)
  run if run_already
end

Class Method Details

.[](i = '') ⇒ Object

#

Roebe::Aliases[]

#


623
624
625
# File 'lib/roebe/classes/aliases.rb', line 623

def self.[](i = '')
  new(i).aliases? # Simply return all the aliases here.
end

.include?(i) ⇒ Boolean

#

Roebe::Aliases.include?

Whether we include a given alias or whether we don’t.

#

Returns:

  • (Boolean)


632
633
634
635
# File 'lib/roebe/classes/aliases.rb', line 632

def self.include?(i)
  _ = Roebe::Aliases.new
  _.is_included? i
end

Instance Method Details

#alias_was_already_included=(i) ⇒ Object

#

alias_was_already_included=

#


128
129
130
# File 'lib/roebe/classes/aliases.rb', line 128

def alias_was_already_included=(i)
  @alias_was_already_included = i
end

#alias_was_already_included?Boolean Also known as: alias_was_already_included

#

alias_was_already_included?

#

Returns:

  • (Boolean)


121
122
123
# File 'lib/roebe/classes/aliases.rb', line 121

def alias_was_already_included?
  @alias_was_already_included
end

#aliases?Boolean Also known as: aliases

#

aliases?

Alias to return all the aliases of this class.

#

Returns:

  • (Boolean)


517
518
519
# File 'lib/roebe/classes/aliases.rb', line 517

def aliases?
  @aliases
end

#cd_aliases?Boolean Also known as: cd_aliases

#

cd_aliases?

#

Returns:

  • (Boolean)


491
492
493
# File 'lib/roebe/classes/aliases.rb', line 491

def cd_aliases?
  @cd_aliases
end

#clearObject

#

clear

#


135
136
137
138
# File 'lib/roebe/classes/aliases.rb', line 135

def clear
  @old_aliases = {}
  @program_aliases = @cd_aliases = @aliases = nil # Set to nil.
end

#count_how_many_aliases_are_availableObject Also known as: update_size

#

count_how_many_aliases_are_available

#


343
344
345
346
347
348
# File 'lib/roebe/classes/aliases.rb', line 343

def count_how_many_aliases_are_available
  @size = 0 # This is the default value.
  if @aliases
    @size = @aliases.keys.size
  end
end

#debug?Boolean

#

debug?

#

Returns:

  • (Boolean)


329
330
331
# File 'lib/roebe/classes/aliases.rb', line 329

def debug?
  @debug
end

#does_include?Boolean

#

include?

Asks whether we have a specific alias. We use only the merged value of @aliases.

#

does_include?

Returns:

  • (Boolean)


584
585
586
# File 'lib/roebe/classes/aliases.rb', line 584

def include?(this_alias)
  @aliases.has_key?(this_alias)
end

#fetch(this_alias) ⇒ Object Also known as: translate, convert_this_alias, []

#

fetch

This will fetch a given alias, else return false. This is the preferred method to retrieve a given key. Please use only this method when you want to obtain an alias.

#


261
262
263
264
265
266
267
# File 'lib/roebe/classes/aliases.rb', line 261

def fetch(this_alias)
  if include?(this_alias)
    return @aliases[this_alias]
  else
    return false 
  end
end

#has_key?Boolean

#

include?

Asks whether we have a specific alias. We use only the merged value of @aliases.

#

has_key?

Returns:

  • (Boolean)


585
586
587
# File 'lib/roebe/classes/aliases.rb', line 585

def include?(this_alias)
  @aliases.has_key?(this_alias)
end

#include?(this_alias) ⇒ Boolean

#

include?

Asks whether we have a specific alias. We use only the merged value of @aliases.

#

Returns:

  • (Boolean)


581
582
583
# File 'lib/roebe/classes/aliases.rb', line 581

def include?(this_alias)
  @aliases.has_key?(this_alias)
end

#is_included?Boolean

#

include?

Asks whether we have a specific alias. We use only the merged value of @aliases.

#

is_included?

Returns:

  • (Boolean)


583
584
585
# File 'lib/roebe/classes/aliases.rb', line 583

def include?(this_alias)
  @aliases.has_key?(this_alias)
end

#is_pointer?(i) ⇒ Boolean Also known as: is_it_a_pointer?

#

is_pointer?

Find out whether a key is a pointer or not.

#

Returns:

  • (Boolean)


565
566
567
568
569
570
571
572
573
# File 'lib/roebe/classes/aliases.rb', line 565

def is_pointer?(i)
  _ = false
    # get the value here
  if include?(i)
    value = @aliases[i] # @old_aliases[i]
    _ = true if @aliases.has_key?(value) # @old_aliases.has_key?(value)
  end
  return _
end

#key?Boolean

#

key?

#

Returns:

  • (Boolean)


243
244
245
# File 'lib/roebe/classes/aliases.rb', line 243

def key?
  @key
end

#load_all_aliasesObject

#

load_all_aliases

This method will load all aliases (after invoking reset() first). load_yaml() is defined in this class here.

#


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/roebe/classes/aliases.rb', line 146

def load_all_aliases
  reset
    # ~7242 entries.
  if File.exist? ALIASES
    @aliases = load_yaml(ALIASES, :all_keys_are_strings) # bl aliases
  end
    # ~4703 entries.
  if File.exist? CD_ALIASES
    @cd_aliases = load_yaml(CD_ALIASES) # bl cdaliases
  end
    # ~2989 entries.
  if File.exist? PROGRAMS_ALIASES
    @program_aliases = load_yaml(PROGRAMS_ALIASES) # bl programsaliases
  end
end

#load_yaml(f, optional_extra_commands = nil) ⇒ Object

#

load_yaml

Internal wrapper for loading a yaml file.

The first argument must be the .yml file at hand.

The second arguments is optional and usually omitted. You can toggle certain extra commands with it, though. For instance, the Symbol :all_keys_are_strings will simply change all keys into Symbols, if used.

#


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/roebe/classes/aliases.rb', line 183

def load_yaml(
    f, optional_extra_commands = nil
  )
  if File.exist? f
    # ===================================================================== #
    # We must put this into a begin-rescue clause, as there may be
    # an error in the yaml-file itself.
    # ===================================================================== #
    begin
      dataset = YAML.load_file(f)
      if optional_extra_commands
        case optional_extra_commands
        # ================================================================= #
        # === :all_keys_are_strings
        #
        # This will effectively turn a key such as 1 into a String such
        # as "1".
        # ================================================================= #
        when :all_keys_are_strings
          new_dataset = {}
          dataset.each_pair {|key, value|
            new_dataset[key.to_s] = value
          }
          dataset = new_dataset
        end
      end
      return dataset
    rescue Psych::SyntaxError => error
      e "#{rev}The yaml file could not be loaded. (Error: #{error})"
    end
  else
    e "#{rev}We try to load the file #{f} but it does not exist."
  end
end

#merge_aliasesObject

#

merge_aliases

Here we merge our aliases, after we finished sanitizing them.

#


232
233
234
# File 'lib/roebe/classes/aliases.rb', line 232

def merge_aliases
  @aliases.merge! @program_aliases if @program_aliases
end

#points_at?(i) ⇒ Boolean Also known as: test_this_alias, find

#

points_at?

This will always try to point at the real key, not an indirect key.

That way it allows us to expand to the real content.

#

Returns:

  • (Boolean)


502
503
504
505
506
507
508
509
# File 'lib/roebe/classes/aliases.rb', line 502

def points_at?(i) # 4 times should be enough.
  i = @old_aliases[i] if is_pointer?(i)
  i = @old_aliases[i] if is_pointer?(i)
  i = @old_aliases[i] if is_pointer?(i)
  i = @old_aliases[i] if is_pointer?(i)
  i = @aliases[i]
  return i
end

#program_aliases?Boolean Also known as: program_aliases

#

program_aliases?

#

Returns:

  • (Boolean)


484
485
486
# File 'lib/roebe/classes/aliases.rb', line 484

def program_aliases?
  @program_aliases
end

#report_real_alias_is_pointing_at(i) ⇒ Object

#

report_real_alias_is_pointing_at

#


336
337
338
# File 'lib/roebe/classes/aliases.rb', line 336

def report_real_alias_is_pointing_at(i)
  e 'The real alias is pointing at '+'`'+sfancy(i.to_s)+'`.'
end

#resetObject

#

reset (reset tag)

#


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/roebe/classes/aliases.rb', line 100

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @debug
  # ======================================================================= #
  @debug = DEBUG
  # ======================================================================= #
  # === @size
  # ======================================================================= #
  @size = nil
  # ======================================================================= #
  # === @alias_was_already_included
  # ======================================================================= #
  @alias_was_already_included = true # This is the default state.
  clear
end

#runObject Also known as: reload

#

run (run tag)

#


616
617
618
# File 'lib/roebe/classes/aliases.rb', line 616

def run
  run_everything
end

#run_everythingObject

#

run_everything

#


590
591
592
593
594
595
596
597
# File 'lib/roebe/classes/aliases.rb', line 590

def run_everything
  load_all_aliases
  save_old_aliases
  # sanitize_aliases  # this will sanitize @aliases. currently not in use.
  sanitize_cd_aliases # first, sanitize my cd aliases. This is MANDATORY.
  merge_aliases       # second, merge our aliases together.
  count_how_many_aliases_are_available # this sets the variable @size
end

#sanitize_aliasesObject

#

sanitize_aliases

#


221
222
223
224
225
# File 'lib/roebe/classes/aliases.rb', line 221

def sanitize_aliases
  @aliases.each { |k, v| # replace pointer with real value
    @aliases[k] = @aliases[v] if @aliases.keys.include? v
  }
end

#save_old_aliasesObject

#

save_old_aliases

#


165
166
167
168
169
# File 'lib/roebe/classes/aliases.rb', line 165

def save_old_aliases #simply sets @old_aliases.
  @old_aliases.merge! @cd_aliases if @cd_aliases
  @old_aliases.merge! @aliases if @aliases
  @old_aliases.merge! @program_aliases if @program_aliases
end

#set_alias(i, force_change = true) ⇒ Object

#

set_alias

Use this method to set an alias.

We will also allow for input that includes the ‘=’ character.

#


541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/roebe/classes/aliases.rb', line 541

def set_alias(i, force_change = true)
  i = i.to_s
  if i.include?('=')
    splitted = i.split('=')
    @key, @value = splitted # Decompose the entries.
  end
  if force_change
    @alias_was_already_included = false unless @aliases.has_key? @key
    @aliases[@key] = @value
  else
    unless @aliases.has_key? @key
      @aliases[@key] = @value
      @alias_was_already_included = false
    else
      @alias_was_already_included = true
    end
  end
end

#show_alias(i) ⇒ Object

#

show_alias

This method will display a given alias and should be used whenever we wish to present a specific alias setting to our users. Please note - an alias should not be allowed to be set to nil BEFORE we may use this method here.

Since December 2013 we also support numbers as input.

To get it, type i.e. the following:

alias pwduloc
#


284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/roebe/classes/aliases.rb', line 284

def show_alias(i)
  if i.to_s =~ /^\d+$/ # If only numbers (but only if we don't have a key like that)
    i = @aliases.keys[i.to_i - 1] unless @aliases.has_key?(i)
  end
  if @aliases.include? i # Ok, the alias is included.
    translated = translate(i) # This is equivalent to .fetch()
    e 'alias '+sfancy(i)+'='+"'"+simp(translated)+"'"
    # ===================================================================== #
    # Now find out whether it is a pointer or not.
    # ===================================================================== #
    if is_it_a_pointer? i
      e 'It is a pointer-alias, pointing to '\
        'another alias.'
      # =================================================================== #
      # Since Feb 2012 we also get the pointed alias here.
      # =================================================================== #
      begin # Must rescue this as the key may no longer exist.
        pointing_at = @aliases.fetch(translated)
        report_real_alias_is_pointing_at(pointing_at)
      rescue KeyError => error
        if translated.include? 'change_directory'
          report_real_alias_is_pointing_at(
            translated.gsub(/change_directory/,'cd')
          )
        else # Else report that error.
          pp error
        end
      end
    else
      e 'It is a real alias.'
    end
    return true
  else # Ok we did not find an alias with that name, so we report to the user.
    output = ::Colours.rev+
             sfancy(File.basename(__FILE__))+': '+
             swarn('The alias `')+
             simp(i)+swarn('` was not found.')
    e output
    return false
  end
end

#show_aliasesObject Also known as: to_s

#

show_aliases

Show all of our aliases with this convenient method.

#


604
605
606
607
608
609
610
611
# File 'lib/roebe/classes/aliases.rb', line 604

def show_aliases
  counter = 0
  @aliases.each_pair { |a, b|
    counter += 1
    e "(#{counter}) #{simp('alias:')} "\
      "#{steelblue(a)} #{tomato('')} #{mediumpurple(b)}"
  }
end

#size?Boolean Also known as: size, n_aliases?

#

size?

Return how many aliases this class has.

#

Returns:

  • (Boolean)


355
356
357
# File 'lib/roebe/classes/aliases.rb', line 355

def size?
  @size
end

#unset(i) ⇒ Object Also known as: unalias

#

unset (unset tag)

Use this method to unset an alias again.

#


526
527
528
529
530
531
532
# File 'lib/roebe/classes/aliases.rb', line 526

def unset(i)
  result = fetch(i)
  if result
    @aliases.delete(i)
    update_size # Must also update the @size variable.
  end
end

#value?Boolean

#

value?

#

Returns:

  • (Boolean)


250
251
252
# File 'lib/roebe/classes/aliases.rb', line 250

def value?
  @value
end