| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
''' |
| 5 |
This module provides functions to retrieve file paths for |
| 6 |
autocompletion scripts for Bash, Fish, and Zsh. |
| 7 |
''' |
| 8 |
|
| 9 |
import subprocess |
| 10 |
|
| 11 |
from . import utils |
| 12 |
|
| 13 |
|
| 14 |
# pylint: disable=broad-exception-caught |
| 15 |
# pylint: disable=broad-exception-raised |
| 16 |
|
| 17 |
|
| 18 |
def _pkg_config(args): |
| 19 |
''' |
| 20 |
Call the `pkg-config` program with `args` and check the return code. |
| 21 |
If everything is fine, return the output as string. |
| 22 |
''' |
| 23 |
|
| 24 |
command = ['pkg-config'] + args |
| 25 |
|
| 26 |
try: |
| 27 |
result = subprocess.run( |
| 28 |
command, |
| 29 |
stderr=subprocess.PIPE, |
| 30 |
stdout=subprocess.PIPE, |
| 31 |
text=True, |
| 32 |
check=False) |
| 33 |
except FileNotFoundError as exc: |
| 34 |
raise Exception('Program `pkg-config` not found') from exc |
| 35 |
|
| 36 |
if result.returncode == 0: |
| 37 |
return result.stdout.strip() |
| 38 |
|
| 39 |
raise Exception('Command `%s` failed: %s' % ( |
| 40 |
' '.join(command), result.stderr.strip())) |
| 41 |
|
| 42 |
|
| 43 |
def get_bash_completion_file(program_name): |
| 44 |
'''Get the path for a Bash completion file.''' |
| 45 |
|
| 46 |
directory = '/usr/share/bash-completion/completions' |
| 47 |
|
| 48 |
try: |
| 49 |
directory = _pkg_config(['--variable=completionsdir', 'bash-completion']) |
| 50 |
except Exception as e: |
| 51 |
utils.warn(e) |
| 52 |
|
| 53 |
return f'{directory}/{program_name}' |
| 54 |
|
| 55 |
|
| 56 |
def get_fish_completion_file(program_name): |
| 57 |
'''Get the path for a Fish completion file.''' |
| 58 |
|
| 59 |
directory = '/usr/share/fish/vendor_completions.d' |
| 60 |
|
| 61 |
try: |
| 62 |
directory = _pkg_config(['--variable=completionsdir', 'fish']) |
| 63 |
except Exception as e: |
| 64 |
utils.warn(e) |
| 65 |
|
| 66 |
return f'{directory}/{program_name}.fish' |
| 67 |
|
| 68 |
|
| 69 |
def get_zsh_completion_file(program_name): |
| 70 |
'''Get the path for a Zsh completion file.''' |
| 71 |
|
| 72 |
# There is '/usr/share/zsh/vendor-completions', but that directory is not |
| 73 |
# in the default $fpath of zsh. |
| 74 |
directory = '/usr/share/zsh/site-functions' |
| 75 |
return f'{directory}/_{program_name}' |