2022-12-17 08:56:44 +13:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import subprocess
|
2022-12-21 07:05:50 +13:00
|
|
|
from os import environ
|
|
|
|
from pathlib import Path
|
2022-12-17 08:56:44 +13:00
|
|
|
|
|
|
|
import appdirs
|
|
|
|
|
|
|
|
|
|
|
|
class Completion:
|
2023-02-19 13:38:17 +13:00
|
|
|
def __init__(self, shell: str) -> None:
|
2022-12-17 08:56:44 +13:00
|
|
|
self.shell = shell
|
2022-12-21 07:05:50 +13:00
|
|
|
self.env = environ.copy()
|
2022-12-17 08:56:44 +13:00
|
|
|
self.share_dir = appdirs.user_data_dir()
|
2022-12-17 17:45:36 +13:00
|
|
|
self.entry_points = ["bdfr", "bdfr-archive", "bdfr-clone", "bdfr-download"]
|
2022-12-17 08:56:44 +13:00
|
|
|
|
2023-02-19 14:16:28 +13:00
|
|
|
def install(self) -> None:
|
2022-12-17 08:56:44 +13:00
|
|
|
if self.shell in ("all", "bash"):
|
|
|
|
comp_dir = self.share_dir + "/bash-completion/completions/"
|
2022-12-21 07:05:50 +13:00
|
|
|
if not Path(comp_dir).exists():
|
2022-12-20 11:54:34 +13:00
|
|
|
print("Creating Bash completion directory.")
|
2022-12-21 07:05:50 +13:00
|
|
|
Path(comp_dir).mkdir(parents=True, exist_ok=True)
|
2022-12-17 08:56:44 +13:00
|
|
|
for point in self.entry_points:
|
|
|
|
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "bash_source"
|
2023-01-26 16:23:59 +13:00
|
|
|
with Path(comp_dir + point).open(mode="w") as file:
|
2023-04-21 07:42:53 +12:00
|
|
|
file.write(
|
|
|
|
subprocess.run([point], env=self.env, capture_output=True, text=True).stdout, # noqa: S603
|
|
|
|
)
|
2022-12-17 08:56:44 +13:00
|
|
|
print(f"Bash completion for {point} written to {comp_dir}{point}")
|
|
|
|
if self.shell in ("all", "fish"):
|
|
|
|
comp_dir = self.share_dir + "/fish/vendor_completions.d/"
|
2022-12-21 07:05:50 +13:00
|
|
|
if not Path(comp_dir).exists():
|
2022-12-20 11:54:34 +13:00
|
|
|
print("Creating Fish completion directory.")
|
2022-12-21 07:05:50 +13:00
|
|
|
Path(comp_dir).mkdir(parents=True, exist_ok=True)
|
2022-12-17 08:56:44 +13:00
|
|
|
for point in self.entry_points:
|
|
|
|
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "fish_source"
|
2023-01-26 16:23:59 +13:00
|
|
|
with Path(comp_dir + point + ".fish").open(mode="w") as file:
|
2023-04-21 07:42:53 +12:00
|
|
|
file.write(
|
|
|
|
subprocess.run([point], env=self.env, capture_output=True, text=True).stdout, # noqa: S603
|
|
|
|
)
|
2022-12-20 11:33:07 +13:00
|
|
|
print(f"Fish completion for {point} written to {comp_dir}{point}.fish")
|
2022-12-17 08:56:44 +13:00
|
|
|
if self.shell in ("all", "zsh"):
|
|
|
|
comp_dir = self.share_dir + "/zsh/site-functions/"
|
2022-12-21 07:05:50 +13:00
|
|
|
if not Path(comp_dir).exists():
|
2022-12-20 11:54:34 +13:00
|
|
|
print("Creating Zsh completion directory.")
|
2022-12-21 07:05:50 +13:00
|
|
|
Path(comp_dir).mkdir(parents=True, exist_ok=True)
|
2022-12-17 08:56:44 +13:00
|
|
|
for point in self.entry_points:
|
|
|
|
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "zsh_source"
|
2023-01-26 16:23:59 +13:00
|
|
|
with Path(comp_dir + "_" + point).open(mode="w") as file:
|
2023-04-21 07:42:53 +12:00
|
|
|
file.write(
|
|
|
|
subprocess.run([point], env=self.env, capture_output=True, text=True).stdout, # noqa: S603
|
|
|
|
)
|
2022-12-20 11:33:07 +13:00
|
|
|
print(f"Zsh completion for {point} written to {comp_dir}_{point}")
|
2022-12-17 08:56:44 +13:00
|
|
|
|
2023-02-19 14:16:28 +13:00
|
|
|
def uninstall(self) -> None:
|
2022-12-17 08:56:44 +13:00
|
|
|
if self.shell in ("all", "bash"):
|
|
|
|
comp_dir = self.share_dir + "/bash-completion/completions/"
|
|
|
|
for point in self.entry_points:
|
2022-12-21 07:05:50 +13:00
|
|
|
if Path(comp_dir + point).exists():
|
|
|
|
Path(comp_dir + point).unlink()
|
2022-12-17 08:56:44 +13:00
|
|
|
print(f"Bash completion for {point} removed from {comp_dir}{point}")
|
|
|
|
if self.shell in ("all", "fish"):
|
|
|
|
comp_dir = self.share_dir + "/fish/vendor_completions.d/"
|
|
|
|
for point in self.entry_points:
|
2022-12-21 07:05:50 +13:00
|
|
|
if Path(comp_dir + point + ".fish").exists():
|
|
|
|
Path(comp_dir + point + ".fish").unlink()
|
2022-12-20 11:33:07 +13:00
|
|
|
print(f"Fish completion for {point} removed from {comp_dir}{point}.fish")
|
2022-12-17 08:56:44 +13:00
|
|
|
if self.shell in ("all", "zsh"):
|
|
|
|
comp_dir = self.share_dir + "/zsh/site-functions/"
|
|
|
|
for point in self.entry_points:
|
2022-12-21 07:05:50 +13:00
|
|
|
if Path(comp_dir + "_" + point).exists():
|
|
|
|
Path(comp_dir + "_" + point).unlink()
|
2022-12-20 11:33:07 +13:00
|
|
|
print(f"Zsh completion for {point} removed from {comp_dir}_{point}")
|