Module: Completion::Shell
- Defined in:
- lib/completion/shell.rb,
lib/completion/shell/zsh.rb,
lib/completion/shell/bash.rb,
lib/completion/shell/fish.rb
Overview
Shell adapter generation helpers.
Defined Under Namespace
Constant Summary collapse
- MARKER_PREFIX =
"# Auto-generated completion: "
Class Method Summary collapse
-
.adapter_directory(shell, executable = nil, directory: nil) ⇒ Object
Get the adapter directory for a shell and executable.
-
.annotate(script, kind:, shell:, executable: nil) ⇒ Object
Add a managed-file metadata marker to a generated script.
-
.annotate_after_first_line(script, kind:, shell:, executable: nil) ⇒ Object
Add a managed-file metadata marker after the first line of a generated script.
-
.command_name(executable) ⇒ Object
Get the command name from an executable path.
-
.default_configuration_directory ⇒ Object
Get the default Fish configuration directory.
-
.default_directory(shell) ⇒ Object
Get the default completion directory for a shell.
-
.default_function_directory ⇒ Object
Get the default Fish function directory.
-
.default_shell ⇒ Object
Detect the current shell from the environment.
-
.file_name(shell, executable = nil) ⇒ Object
Get the installed adapter file name for a shell.
-
.managed?(path) ⇒ Boolean
Check whether a script is managed by completion.
-
.marker(kind:, shell:, executable: nil) ⇒ Object
Generate a managed-file metadata marker.
-
.metadata(path) ⇒ Object
Extract managed-file metadata from a generated script.
-
.script(shell:, executable: nil) ⇒ Object
Generate a shell adapter script.
-
.shared_file_name(shell) ⇒ Object
Get the installed shared helper file name for a shell.
-
.shared_script(shell:) ⇒ Object
Generate a shared shell helper script.
-
.shell_name(path) ⇒ Object
Extract a shell name from a path.
Class Method Details
.adapter_directory(shell, executable = nil, directory: nil) ⇒ Object
Get the adapter directory for a shell and executable.
142 143 144 145 146 147 148 149 150 |
# File 'lib/completion/shell.rb', line 142 def self.adapter_directory(shell, executable = nil, directory: nil) return directory if directory if shell == "fish" && !executable return default_configuration_directory end return default_directory(shell) end |
.annotate(script, kind:, shell:, executable: nil) ⇒ Object
Add a managed-file metadata marker to a generated script.
40 41 42 |
# File 'lib/completion/shell.rb', line 40 def self.annotate(script, kind:, shell:, executable: nil) return "#{marker(kind: kind, shell: shell, executable: executable)}\n#{script}" end |
.annotate_after_first_line(script, kind:, shell:, executable: nil) ⇒ Object
Add a managed-file metadata marker after the first line of a generated script.
51 52 53 54 55 |
# File 'lib/completion/shell.rb', line 51 def self.annotate_after_first_line(script, kind:, shell:, executable: nil) first, rest = script.split("\n", 2) return "#{first}\n#{marker(kind: kind, shell: shell, executable: executable)}\n#{rest}" end |
.command_name(executable) ⇒ Object
Get the command name from an executable path.
224 225 226 |
# File 'lib/completion/shell.rb', line 224 def self.command_name(executable) File.basename(executable || "completion") end |
.default_configuration_directory ⇒ Object
Get the default Fish configuration directory.
132 133 134 |
# File 'lib/completion/shell.rb', line 132 def self.default_configuration_directory File.("~/.config/fish/conf.d") end |
.default_directory(shell) ⇒ Object
Get the default completion directory for a shell.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/completion/shell.rb', line 109 def self.default_directory(shell) case shell when "bash" File.("~/.local/share/bash-completion/completions") when "fish" File.("~/.config/fish/completions") when "zsh" File.("~/.zsh/completions") else raise ArgumentError, "Unsupported shell: #{shell.inspect}" end end |
.default_function_directory ⇒ Object
Get the default Fish function directory.
125 126 127 |
# File 'lib/completion/shell.rb', line 125 def self.default_function_directory File.("~/.config/fish/functions") end |
.default_shell ⇒ Object
Detect the current shell from the environment.
101 102 103 |
# File 'lib/completion/shell.rb', line 101 def self.default_shell shell_name(ENV["SHELL"]) end |
.file_name(shell, executable = nil) ⇒ Object
Get the installed adapter file name for a shell.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/completion/shell.rb', line 157 def self.file_name(shell, executable = nil) command = executable || "completion" case shell when "bash" command when "fish" "#{command}.fish" when "zsh" "_#{command}" else raise ArgumentError, "Unsupported shell: #{shell.inspect}" end end |
.managed?(path) ⇒ Boolean
Check whether a script is managed by completion.
84 85 86 87 88 |
# File 'lib/completion/shell.rb', line 84 def self.managed?(path) = self.(path) return && ["managed"] == true || false end |
.marker(kind:, shell:, executable: nil) ⇒ Object
Generate a managed-file metadata marker.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/completion/shell.rb', line 19 def self.marker(kind:, shell:, executable: nil) = { managed: true, kind: kind, shell: shell.to_s, } if executable [:command] = command_name(executable) end return "#{MARKER_PREFIX}#{JSON.generate()}" end |
.metadata(path) ⇒ Object
Extract managed-file metadata from a generated script.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/completion/shell.rb', line 61 def self.(path) return nil unless File.file?(path) File.open(path) do |file| 2.times do line = file.gets break unless line if line.start_with?(MARKER_PREFIX) return JSON.parse(line.delete_prefix(MARKER_PREFIX)) end end end return nil rescue JSON::ParserError return nil end |
.script(shell:, executable: nil) ⇒ Object
Generate a shell adapter script.
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/completion/shell.rb', line 192 def self.script(shell:, executable: nil) case shell.to_sym when :bash Bash.script(executable) when :fish Fish.script(executable) when :zsh Zsh.script(executable) else raise ArgumentError, "Unsupported shell: #{shell.inspect}" end end |
.shared_file_name(shell) ⇒ Object
Get the installed shared helper file name for a shell.
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/completion/shell.rb', line 176 def self.shared_file_name(shell) case shell when "bash" "completion.bash" when "zsh" "completion.zsh" else raise ArgumentError, "Unsupported shell: #{shell.inspect}" end end |
.shared_script(shell:) ⇒ Object
Generate a shared shell helper script.
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/completion/shell.rb', line 209 def self.shared_script(shell:) case shell.to_sym when :bash Bash.shared_script when :zsh Zsh.shared_script else raise ArgumentError, "Unsupported shell: #{shell.inspect}" end end |
.shell_name(path) ⇒ Object
Extract a shell name from a path.
94 95 96 |
# File 'lib/completion/shell.rb', line 94 def self.shell_name(path) File.basename(path.to_s) end |