From 1afa7aa09bf564b0dfe2c06fa9ca24666b76139d Mon Sep 17 00:00:00 2001 From: Benex254 Date: Wed, 7 Aug 2024 16:35:14 +0300 Subject: [PATCH] feat(cli): add cache command to manage your fastanime cache dir --- fastanime/cli/__init__.py | 1 + fastanime/cli/commands/cache.py | 35 +++++++++++++++++++++++++++++++++ fastanime/cli/utils/utils.py | 8 ++++++++ 3 files changed, 44 insertions(+) create mode 100644 fastanime/cli/commands/cache.py diff --git a/fastanime/cli/__init__.py b/fastanime/cli/__init__.py index dd5bade..d02b871 100644 --- a/fastanime/cli/__init__.py +++ b/fastanime/cli/__init__.py @@ -14,6 +14,7 @@ commands = { "anilist": "anilist.anilist", "config": "config.config", "downloads": "downloads.downloads", + "cache": "cache.cache", } diff --git a/fastanime/cli/commands/cache.py b/fastanime/cli/commands/cache.py new file mode 100644 index 0000000..a872c29 --- /dev/null +++ b/fastanime/cli/commands/cache.py @@ -0,0 +1,35 @@ +import click + + +@click.command(help="Helper command to manage cache") +@click.option("--clean", help="Clean the cache dir", is_flag=True) +@click.option("--path", help="The path to the cache dir", is_flag=True) +@click.option("--size", help="The size of the cache dir", is_flag=True) +def cache(clean, path, size): + from ...constants import APP_CACHE_DIR + + if path: + print(APP_CACHE_DIR) + elif clean: + import shutil + + from rich.prompt import Confirm + + if Confirm.ask( + f"Are you sure you want to clean the following path: {APP_CACHE_DIR};(NOTE: !!The action is irreversible and will clean your cache!!)", + default=False, + ): + print("Cleaning...") + shutil.rmtree(APP_CACHE_DIR) + print("Successfully removed: ", APP_CACHE_DIR) + elif size: + import os + + from ..utils.utils import sizeof_fmt + + total_size = 0 + for dirpath, dirnames, filenames in os.walk(APP_CACHE_DIR): + for f in filenames: + fp = os.path.join(dirpath, f) + total_size += os.path.getsize(fp) + print("Total Size: ", sizeof_fmt(total_size)) diff --git a/fastanime/cli/utils/utils.py b/fastanime/cli/utils/utils.py index a35f00f..fba07d3 100644 --- a/fastanime/cli/utils/utils.py +++ b/fastanime/cli/utils/utils.py @@ -22,6 +22,14 @@ BG_GREEN = "\033[48;2;120;233;12;m" GREEN = "\033[38;2;45;24;45;m" +def sizeof_fmt(num, suffix="B"): + for unit in ("", "K", "M", "G", "T", "P", "E", "Z"): + if abs(num) < 1024.0: + return f"{num:3.1f}{unit}{suffix}" + num /= 1024.0 + return f"{num:.1f}Yi{suffix}" + + def get_true_fg(string: str, r: int, g: int, b: int, bold=True) -> str: if bold: return f"{BOLD}\033[38;2;{r};{g};{b};m{string}{RESET}"