diff --git a/archmgr.py b/archmgr.py index 680fbfd..e6094e9 100644 --- a/archmgr.py +++ b/archmgr.py @@ -28,7 +28,7 @@ if __name__ == "__main__": try: if args.cmd == "commit": - commit.commit(args.dry_run, args.force, args.no_render) + commit.commit(args.force, args.no_render) elif args.cmd == "config": config.config() elif args.cmd == "init": diff --git a/cli/args.py b/cli/args.py index d9b45e7..98bee98 100644 --- a/cli/args.py +++ b/cli/args.py @@ -21,16 +21,10 @@ def add_cli_args(): commit = sp.add_parser( "commit", help="apply pending changes and commit to git repo" ) - commit.add_argument("-f", "--force", help="force apply", action="store_true") + commit.add_argument("-f", "--force", help="force apply (skips prompts)", action="store_true") commit.add_argument( "--no-render", "-r", help="do not re-render renderables", action="store_true" ) - commit.add_argument( - "--dry-run", - "-d", - help="print out files that would be changed", - action="store_true", - ) sp.add_parser("config", help="prints information about your config") diff --git a/commands/commit.py b/commands/commit.py index fea724b..8136e6a 100644 --- a/commands/commit.py +++ b/commands/commit.py @@ -1,11 +1,31 @@ +from typing import List from commands.util import pacman from commands.util.input_mgr import confirm_overwrite, password +import colorama as cl + +from commands.util.printer import print_list -def commit(dry_run: bool = False, force: bool = False, no_render: bool = False): - if dry_run: - pass +def commit(force: bool = False, no_render: bool = False): print("Commit, force:", force) - password() - print(pacman.list_explicitly_installed()) + # TODO: Make sure we don't uninstall critical system packages by accident (i.e. prompt user) + # Probably do that check in the pacman util lib tho + # pw = password() + print_diff(pacman.list_explicitly_installed(), []) print(confirm_overwrite()) + + +def print_diff(add: List[str], remove: List[str]): + print( + cl.Fore.GREEN + "==>", + cl.Style.RESET_ALL + "Packages that will be", + cl.Fore.GREEN + "installed" + cl.Style.RESET_ALL, + ) + + print_list(add) + + print( + cl.Fore.GREEN + "==>", + cl.Style.RESET_ALL + "Packages that will be", + cl.Fore.RED + "uninstalled" + cl.Style.RESET_ALL, + ) diff --git a/commands/prepare.py b/commands/prepare.py index 6ca83d8..493ca98 100644 --- a/commands/prepare.py +++ b/commands/prepare.py @@ -1,6 +1,7 @@ import subprocess from commands.util import pacman +from commands.util.input_mgr import confirm def setup(): @@ -8,6 +9,20 @@ def setup(): if not pacman.install_package_list(["git"]): print("Git installation failed") return - subprocess.run(["git", "clone", "https://aur.archlinux.org/yay.git"], cwd="/tmp") - subprocess.run(["makepkg", "-si"], cwd="/tmp/yay") + + subprocess.run( + ["git", "clone", "https://aur.archlinux.org/yay.git"], + cwd="/tmp", + capture_output=True, + ) + yay_install = subprocess.run( + ["makepkg", "-si"], cwd="/tmp/yay", capture_output=True + ) + + if yay_install.returncode != 0: + print("==> Installation of yay failed") + if confirm(True, "Do you wish to view the logs?"): + print(yay_install.stdout, "\n", yay_install.stderr) + return + print("==> Installation completed") diff --git a/commands/util/input_mgr.py b/commands/util/input_mgr.py index 0a74b5d..01e5a19 100644 --- a/commands/util/input_mgr.py +++ b/commands/util/input_mgr.py @@ -1,6 +1,7 @@ from typing import Optional import getpass import time +import colorama def choice(default: str, options: str, msg: str) -> str: @@ -42,5 +43,8 @@ def password(msg: str = ""): return pw else: time.sleep(1) - print("Error: Password cannot be empty") + print( + colorama.Fore.RED + "Error:", + colorama.Style.RESET_ALL + "Password cannot be empty", + ) return password(msg) diff --git a/commands/util/pacman.py b/commands/util/pacman.py index 5de2521..b97526d 100644 --- a/commands/util/pacman.py +++ b/commands/util/pacman.py @@ -13,6 +13,15 @@ def list_explicitly_installed() -> List[str]: return run_pkg_manager_cmd(["-Qeq"]).stdout.split() +def remove_orphans() -> bool: + """Removes all orphan packages + + Returns: + True if successful, False otherwise + """ + return sp.run("pacman -Qdtq | pacman -Rnsy -", capture_output=True).returncode == 0 + + def uninstall_package_list(pkgs: List[str]) -> bool: """Uninstall all packages in the list diff --git a/commands/util/printer.py b/commands/util/printer.py new file mode 100644 index 0000000..320a748 --- /dev/null +++ b/commands/util/printer.py @@ -0,0 +1,20 @@ +from typing import Any, List +import colorama as cl + + +def count_digits(number: int): + return len(str(number)) + + +def print_list(list: List[Any]): + digit_count = count_digits(len(list)) + for i, pkg in enumerate(list): + print( + " " + + cl.Fore.BLUE + + cl.Style.DIM + + (" " * (digit_count - count_digits(i))) + + str(i) + + cl.Style.RESET_ALL + " ", + pkg, + )