Module: CommandKit::Completion::Install

Includes:
Env::Home, Env::Prefix, Env::Shell, Printing
Defined in:
lib/command_kit/completion/install.rb

Overview

Mixins that adds methods for installing shell completion files.

Environment Variables

  • SHELL - The current shell.
  • PREFIX - The optional root prefix of the file-system.

Since:

  • 0.5.0

Constant Summary

Constants included from Printing

Printing::EOL

Instance Attribute Summary collapse

Attributes included from Env::Prefix

#root

Attributes included from Env

#env

Attributes included from Env::Shell

#shell, #shell_type

Attributes included from Env::Home

#home_dir

Instance Method Summary collapse

Methods included from Env::Home::ModuleMethods

#included

Methods included from Printing

#print_error, #print_exception

Methods included from Stdio

#abort, #gets, #print, #printf, #putc, #puts, #readline, #readlines, #stderr, #stdin, #stdout

Instance Attribute Details

#completions_dirString? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The installation directory for completion files for the current shell.

Returns:

  • (String, nil)
    • Bash
      • Regular users: ~/.local/share/bash-completion/completions
      • Root users:$PREFIX/usr/local/share/bash-completion/completions
    • Zsh: $PREFIX/usr/local/share/zsh/site-functions
    • Fish:
      • Regular users: ~/.config/fish/completions
      • Root users: $PREFIX/usr/local/share/fish/completions

Since:

  • 0.5.0



42
43
44
# File 'lib/command_kit/completion/install.rb', line 42

def completions_dir
  @completions_dir
end

Instance Method Details

#initialize(**kwargs) ⇒ Object

Initialize #completions_dir based on the SHELL environment variable and the UID of the process.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Since:

  • 0.5.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/command_kit/completion/install.rb', line 53

def initialize(**kwargs)
  super(**kwargs)

  @completions_dir = case shell_type
                     when :bash
                       if Process.uid == 0
                         File.join(root,'usr','local','share','bash-completion','completions')
                       else
                         xdg_data_home = env.fetch('XDG_DATA_HOME') do
                           File.join(home_dir,'.local','share')
                         end

                         File.join(xdg_data_home,'bash-completion','completions')
                       end
                     when :zsh
                       File.join(root,'usr','local','share','zsh','site-functions')
                     when :fish
                       if Process.uid == 0
                         File.join(root,'usr','local','share','fish','completions')
                       else
                         xdg_config_home = env.fetch('XDG_CONFIG_HOME') do
                           File.join(home_dir,'.config')
                         end

                         File.join(xdg_config_home,'fish','completions')
                       end
                     end
end

#install_completion_file(path, type: :bash) ⇒ Object

Installs the shell completion file into #completions_dir.

Examples:

Install a Bash completion file:

install_completion_file 'path/to/completions/foo'

Install a Zsh completion file:

install_completion_file 'path/to/completions/foo', type: :zsh

Install a Fish completion file:

install_completion_file 'path/to/completions/foo', type: :fish

Parameters:

  • path (String)

    The path to the shell completion file.

  • type (:bash, :zsh, :fish) (defaults to: :bash)

    The type of the shell completion file.

Since:

  • 0.5.0



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/command_kit/completion/install.rb', line 129

def install_completion_file(path, type: :bash)
  completion_file = normalize_completion_file(path, type: type)
  completion_path = File.join(@completions_dir,completion_file)

  begin
    ::FileUtils.mkdir_p(@completions_dir)
  rescue Errno::EACCES
    print_error "cannot write to #{shell_type} completions directory: #{@completions_dir}"
    exit(-1)
  end

  begin
    File.open(completion_path,'w') do |output|
      write_completion_file(path,output, type: type)
    end
  rescue Errno::EACCES
    print_error "cannot write to #{shell_type} completion file: #{completion_path}"
    exit(-1)
  end
end

Prints the shell completion file to stdout.

Examples:

Prints a Bash completion file:

print_completion_file 'path/to/completions/foo'

Prints a Zsh completion file:

print_completion_file 'path/to/completions/foo', type: :zsh

Prints a Fish completion file:

print_completion_file 'path/to/completions/foo', type: :fish

Parameters:

  • path (String)

    The path to the shell completion file.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :type (:bash, :zsh, :fish) — default: :bash

    The type of the completion file.

Since:

  • 0.5.0



105
106
107
# File 'lib/command_kit/completion/install.rb', line 105

def print_completion_file(path,**kwargs)
  write_completion_file(path,stdout,**kwargs)
end

#uninstall_completion_file_for(command) ⇒ Object

Uninstalls a shell completion file for the specified command.

Examples:

Removes the completion file for the command 'foo':

uninstall_completion_file_for 'foo'

Parameters:

  • command (String)

    The command to uninstall the completions for.

Since:

  • 0.5.0



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/command_kit/completion/install.rb', line 161

def uninstall_completion_file_for(command)
  completion_file = completion_file_for_command(command)
  completion_path = File.join(@completions_dir,completion_file)

  begin
    ::FileUtils.rm_f(completion_path)
  rescue Errno::EACCES
    print_error "cannot remove #{shell_type} completion file: #{completion_path}"
    exit(-1)
  end
end