Module: Tabry::Shells::Bash

Defined in:
lib/tabry/shells/bash.rb

Class Method Summary collapse

Class Method Details

.esc(str) ⇒ Object



48
49
50
# File 'lib/tabry/shells/bash.rb', line 48

def self.esc(str)
  Shellwords.escape(str)
end

.generate(cmd_name, tabry_file_path, uniq_fn_id: nil) ⇒ Object

NOTE! This code uses sh/bash/tabry_bash_core.sh and is described in sh/bash/README.md; see that README for more info ruby_path and run_tabry_bash_directly are mutually exclusive – use run_tabry_bash_directly to use the shebang in the tabry-bash file



20
21
22
23
24
25
26
27
28
# File 'lib/tabry/shells/bash.rb', line 20

def self.generate(cmd_name, tabry_file_path, uniq_fn_id: nil)
  generate_internal(
    cmd_name: cmd_name,
    import_path: File.expand_path(tabry_file_path),
    tabry_bash_executable: File.expand_path("#{path_to_tabry}/bin/tabry-bash"),
    tabry_bash_arg: nil,
    uniq_fn_id: uniq_fn_id
  )
end

.generate_internal(cmd_name:, import_path:, tabry_bash_executable:, tabry_bash_arg:, uniq_fn_id: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tabry/shells/bash.rb', line 52

def self.generate_internal(cmd_name:, import_path:, tabry_bash_executable:, tabry_bash_arg:, uniq_fn_id: nil)
  # uniq_fn_id is added to the bash functions to ensure they are unique
  # -- by default this is the capitalized command name
  uniq_fn_id ||= cmd_name
  uniq_fn_id = uniq_fn_id.upcase.gsub(/[^A-Z0-9_]/, "_")
  core = File.read("#{__dir__}/../../../sh/bash/tabry_bash_core.sh")
  core.gsub! "_tabry_completions_internal()", "_tabry_#{uniq_fn_id}_completions_internal()"

  <<~END_BASH_CODE_TEMPLATE + core
    # The following Autocomplete is for a Tabry-powered command. It was
    # generated by the command itself. See the documentation located in
    # #{esc path_to_tabry}/sh/bash/README.md
    _tabry_#{uniq_fn_id}_completions() {
      TABRY_IMPORTS_PATH=#{esc import_path} _tabry_#{uniq_fn_id}_completions_internal #{esc tabry_bash_executable} #{tabry_bash_arg && esc(tabry_bash_arg)}
    }
    complete -F _tabry_#{uniq_fn_id}_completions #{esc cmd_name}
  END_BASH_CODE_TEMPLATE
end

.generate_self(cmd_name: nil) ⇒ Object

Generate bash completion code that will run the currently running command ($0) with “completion” to get completion options. “cmd_name” is used to tell bash which command to make options for (and for naming of the _tabry_CMD_NAME_completions_internal bash function)



34
35
36
37
38
39
40
41
42
# File 'lib/tabry/shells/bash.rb', line 34

def self.generate_self(cmd_name: nil)
  cmd_name ||= File.basename($0)
  generate_internal(
    cmd_name: cmd_name,
    import_path: "",
    tabry_bash_executable: File.expand_path($0),
    tabry_bash_arg: "completion"
  )
end

.path_to_tabryObject



44
45
46
# File 'lib/tabry/shells/bash.rb', line 44

def self.path_to_tabry
  File.expand_path("#{__dir__}/../../../")
end