Class: GenerateShellCompletion::GenerateShellCompletion

Inherits:
Object
  • Object
show all
Defined in:
lib/generate_shell_completion/generate_shell_completion.rb,
lib/generate_shell_completion/version/version.rb

Overview

GenerateShellCompletion::GenerateShellCompletion

Constant Summary collapse

VERSION =
#

VERSION

#
'0.0.20'
LAST_UPDATE =
#

LAST_UPDATE

#
'25.10.2023'
N =
"\n"
STORE_HERE =
#

STORE_HERE

#
'completion.sh'

Instance Method Summary collapse

Constructor Details

#initialize(i = STORE_HERE, run_already = true) ⇒ GenerateShellCompletion

#

initialize

Usage example:

_ = GenerateShellCompletion.new(, :dont_run_yet)
#


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 48

def initialize(
    i           = STORE_HERE,
    run_already = true
  )
  reset
  set_store_where(i)
  if run_already.to_s.include? 'dont'
    run_already = false
  end
  case run_already
  # ======================================================================= #
  # === :default_value
  # ======================================================================= #
  when :default_value
    run_already = true
  end
  run if run_already
end

Instance Method Details

#add_names_for_how_to_invoke_the_completionsObject

#

add_names_for_how_to_invoke_the_completions

Here come the names for the complete functionality. This will be invoked in run().

#


179
180
181
182
183
184
185
186
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 179

def add_names_for_how_to_invoke_the_completions
  _ = ' # Next, available auto-completion for the commands '+
      '"ry", "wis" and "url".'+N
  @aliases.each {|entry|
    _ << 'complete -F '+name_of_the_shell_function?+' '+entry+N
  } if @aliases
  return _
end

#append_files_from_the_current_directoryObject Also known as: use_files_in_directory_as_well

#

append_files_from_the_current_directory

When this instance variable is set to true, we will always append the files found in the current working directory to the available tab-completable options.

#


162
163
164
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 162

def append_files_from_the_current_directory
  @append_files_from_the_current_directory = true
end

#append_files_from_the_current_directory?Boolean

#

append_files_from_the_current_directory?

#

Returns:

  • (Boolean)


169
170
171
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 169

def append_files_from_the_current_directory?
  @append_files_from_the_current_directory
end

#dataset?Boolean

#

dataset?

#

Returns:

  • (Boolean)


213
214
215
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 213

def dataset?
  @data
end

#inform_the_user_what_we_will_doObject

#

inform_the_user_what_we_will_do

#


203
204
205
206
207
208
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 203

def inform_the_user_what_we_will_do
  opn; e 'Now generating the shell script'
  e
  e '  `'+sfile(store_where?)+'`'
  e
end

#liner(i = :add_newline) ⇒ Object

#

liner

#


142
143
144
145
146
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 142

def liner(i = :add_newline)
  _ = '# '+('=' * 76)+' #'
  _ << N if i == :add_newline
  return _
end

#name_of_the_shell_function?Boolean

#

name_of_the_shell_function?

#

Returns:

  • (Boolean)


151
152
153
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 151

def name_of_the_shell_function?
  @name_of_the_shell_function
end

#prepend_this_string(i) ⇒ Object

#

prepend_this_string

This method can be used to prepend something on top of the file. Specifically, this is used for generation tab-completion for the zsh shell.

#


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

def prepend_this_string(i)
  @prepend_this = i
end

#remove_old_fileObject

#

remove_old_file

#


122
123
124
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 122

def remove_old_file
  File.delete(@store_where) if File.exist? @store_where
end

#resetObject

#

reset (reset tag)

#


70
71
72
73
74
75
76
77
78
79
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 70

def reset
  @append_files_from_the_current_directory = false # Default.
  # ======================================================================= #
  # === @prepend_this
  # ======================================================================= #
  @prepend_this = nil
  set_name_of_the_shell_function 'completion_for_ry' # Default name.
  set_dataset
  set_aliases
end

#return_full_timeObject

#

return_full_time

Will return something like:

", at 20:51:53 o'clock."
#


134
135
136
137
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 134

def return_full_time
  Time.now.strftime('%d.%m.%Y')+', at '+
  Time.now.strftime('%H:%M:%S')+" o'clock."
end

#runObject

#

run (run tag)

#


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 240

def run
  inform_the_user_what_we_will_do
  array = dataset?
  _ = ''.dup
  _ << @prepend_this if @prepend_this
  _ << liner+'# Automatically generated file, generated at '+return_full_time+N+
       liner+'# A bash shell script.'+N+liner
  _ << ''+name_of_the_shell_function?+'()
{
local cur prev all_programs

COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
all_programs="
  '+array.join(N+'   ')+'
"
'
  # ======================================================================= #
  # Whether to append files from the current directory or not:
  # ======================================================================= #
  if append_files_from_the_current_directory?
    _ << '  append_this=`ls`
all_programs+=$append_this
'
  end
  _ << '
if [[ ${cur} == * ]] ; then
  COMPREPLY=( $(compgen -W "${all_programs}" -- ${cur}) )
  return 0
fi
}'
  _ << add_names_for_how_to_invoke_the_completions
  save_dataset(_)
end

#save_dataset(i) ⇒ Object

#

save_dataset

Use this method to save the file.

#


233
234
235
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 233

def save_dataset(i)
  SaveFile.save_what_into(i, store_where?)
end

#set_aliases(i = nil) ⇒ Object Also known as: set_alias

#

set_aliases

#


191
192
193
194
195
196
197
198
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 191

def set_aliases(i = nil)
  if i.is_a? String
    i = i.split(',') if i.include? ','
  end
  i = [i] unless i.is_a? Array
  i = i.flatten if i.is_a? Array
  @aliases = i # Must be an Array or nil.
end

#set_dataset(i = %w( abc def ghi )) ⇒ Object

#

set_dataset

Here comes in your array.

#


86
87
88
89
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 86

def set_dataset(i = %w( abc def ghi ))
  i = i.flatten if i.is_a? Array
  @data = i
end

#set_name_of_the_shell_function(i) ⇒ Object

#

set_name_of_the_shell_function

Set the name of the shell function here.

#


96
97
98
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 96

def set_name_of_the_shell_function(i)
  @name_of_the_shell_function = i
end

#set_store_where(i = '/Depot/Temp/completion.sh') ⇒ Object Also known as: set_name, store_here, store_here=

#

set_store_where

#


103
104
105
106
107
108
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 103

def set_store_where(i = '/Depot/Temp/completion.sh')
  i = i.first if i.is_a? Array
  i = STORE_HERE if i.nil?
  i = i.to_s.dup if i
  @store_where = i
end

#store_where?Boolean Also known as: store_here?

#

store_where

#

Returns:

  • (Boolean)


115
116
117
# File 'lib/generate_shell_completion/generate_shell_completion.rb', line 115

def store_where?
  @store_where
end