From 122ee3035a31db8b7af9f55575138fd4d4bc15d4 Mon Sep 17 00:00:00 2001 From: cswimr Date: Wed, 27 Nov 2024 13:12:29 -0500 Subject: [PATCH] improve the `vm` alias --- config/xonsh/aliases.py | 20 ++++++++++++------ config/xonsh/colors.py | 47 +++++++++++++++++++++++++++++++++++++++++ nixos/pkg.nix | 7 ++---- nixos/shell.nix | 1 - 4 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 config/xonsh/colors.py mode change 100755 => 100644 nixos/pkg.nix diff --git a/config/xonsh/aliases.py b/config/xonsh/aliases.py index 7465135..c7e71dd 100644 --- a/config/xonsh/aliases.py +++ b/config/xonsh/aliases.py @@ -1,9 +1,14 @@ +from os.path import dirname from pathlib import Path import subprocess +import colors -def run(cmd: list[str]) -> subprocess.CompletedProcess: - print(f"* {' '.join(cmd)}") - return subprocess.run(cmd, check=True) +def run(cmd: list[str], cwd: Path = Path.cwd(), **kwargs) -> subprocess.CompletedProcess: + c = colors.Colors + print(f"{c.GREEN}Running command: {c.RED}'{' '.join(cmd)}'{c.END}") + if cwd != Path.cwd(): + print(f"{c.GREEN} 󱞩 in directory: {c.YELLOW}'{cwd}'{c.END}") + return subprocess.run(cmd, cwd=cwd, check=True, **kwargs) @aliases.register @aliases.return_command @@ -20,11 +25,12 @@ def _vm(args): build_vm_args.extend(["-I", "nixpkgs=/bulk/home/cswimr/Projects/nixpkgs"]) vm_path = Path(f"/etc/nixos/hosts/virtual-machines/{vm_name}") if vm_path.exists(): - print(f"Building virtual machine {vm_name}") + c = colors.Colors + print(f"{c.BLUE}Building virtual machine {c.YELLOW}{vm_name}{c.END}") run(["nixos-rebuild", "build-vm", "-I", "nixos-config=./default.nix", *build_vm_args, "--no-flake"], cwd=vm_path) - print(f"Running virtual vachine {vm_name}") - run([str(vm_path / "result" / "bin" / "run-nixos-vm")], cwd=vm_path) - print(f"Virtual machine {vm_name} has stopped.") + print(f"{c.BLUE}Running virtual vachine {c.YELLOW}{vm_name}{c.END}") + run(["./result/bin/run-nixos-vm"], cwd=vm_path) + print(f"{c.BLUE}Virtual machine {c.YELLOW}{vm_name} {c.BLUE}has {c.RED}stopped.{c.END}") else: raise FileNotFoundError(f"Virtual machine {vm_name} does not exist.") diff --git a/config/xonsh/colors.py b/config/xonsh/colors.py new file mode 100644 index 0000000..bef4a3d --- /dev/null +++ b/config/xonsh/colors.py @@ -0,0 +1,47 @@ +# SGR color constants +# rene-d 2018 +# https://gist.github.com/rene-d/9e584a7dd2935d0f461904b9f2950007 + +class Colors: + """ ANSI color codes """ + BLACK = "\033[0;30m" + RED = "\033[0;31m" + GREEN = "\033[0;32m" + BROWN = "\033[0;33m" + BLUE = "\033[0;34m" + PURPLE = "\033[0;35m" + CYAN = "\033[0;36m" + LIGHT_GRAY = "\033[0;37m" + DARK_GRAY = "\033[1;30m" + LIGHT_RED = "\033[1;31m" + LIGHT_GREEN = "\033[1;32m" + YELLOW = "\033[1;33m" + LIGHT_BLUE = "\033[1;34m" + LIGHT_PURPLE = "\033[1;35m" + LIGHT_CYAN = "\033[1;36m" + LIGHT_WHITE = "\033[1;37m" + BOLD = "\033[1m" + FAINT = "\033[2m" + ITALIC = "\033[3m" + UNDERLINE = "\033[4m" + BLINK = "\033[5m" + NEGATIVE = "\033[7m" + CROSSED = "\033[9m" + END = "\033[0m" + # cancel SGR codes if we don't write to a terminal + if not __import__("sys").stdout.isatty(): + for _ in dir(): + if isinstance(_, str) and _[0] != "_": + locals()[_] = "" + else: + # set Windows console in VT mode + if __import__("platform").system() == "Windows": + kernel32 = __import__("ctypes").windll.kernel32 + kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7) + del kernel32 + + +if __name__ == '__main__': + for i in dir(Colors): + if i[0:1] != "_" and i != "END": + print("{:>16} {}".format(i, getattr(Colors, i) + i + Colors.END)) diff --git a/nixos/pkg.nix b/nixos/pkg.nix old mode 100755 new mode 100644 index b2a5a69..dfc4359 --- a/nixos/pkg.nix +++ b/nixos/pkg.nix @@ -46,11 +46,8 @@ let packwiz xclip starship - - # for xonsh - python311 - python311Packages.pip - python311Packages.python-lsp-server + sops + ssh-to-age ]; flakePackages = with inputs; [ compose2nix.packages.${system}.default ]; in { diff --git a/nixos/shell.nix b/nixos/shell.nix index e7d3b7e..3014296 100644 --- a/nixos/shell.nix +++ b/nixos/shell.nix @@ -73,7 +73,6 @@ enable = true; config = '' $BASH_COMPLETIONS = ('${bashcfg.completion.package}/etc/profile.d/bash_completion.sh') - #execx($(starship init xonsh)) #xontrib load cd xontrib load direnv xontrib load sh