From beedfd1be2866f2e2e5bd8a814af5ddf9ee9f890 Mon Sep 17 00:00:00 2001 From: pagedmov Date: Sun, 17 Nov 2024 13:03:49 -0500 Subject: [PATCH] commit outputs can now be colored with a new script `color-commit` --- docs/scripts.md | 9 +++++ modules/home/environment/userpkgs.nix | 1 + modules/home/environment/zsh/aliases.nix | 1 - modules/home/environment/zsh/extraconfig.nix | 6 +++- modules/home/programs/git.nix | 20 +++++------ overlay/overlay.nix | 1 + overlay/scripts/commands/git-compose.nix | 5 +-- overlay/scripts/misc/color-commit.nix | 37 ++++++++++++++++++++ 8 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 overlay/scripts/misc/color-commit.nix diff --git a/docs/scripts.md b/docs/scripts.md index c13fe4a..05e9b2c 100644 --- a/docs/scripts.md +++ b/docs/scripts.md @@ -215,3 +215,12 @@ included in my nixpkgs overlay as custom packages, and these packages are declar - *Usage*: - `switchmon` - Does not take arguments. - *Defined in*: `overlay/scripts/wm-controls/switchmon.nix` + +--- + +- **color-commit** + - *Description*: + - Colorizes the output of `git commit` if piped into it via stdin + - *Usage*: + - `git commit -m "message" | color-commit` + - *Defined in*: `overlay/scripts/misc/color-commit.nix` diff --git a/modules/home/environment/userpkgs.nix b/modules/home/environment/userpkgs.nix index c8a50b7..56112ca 100755 --- a/modules/home/environment/userpkgs.nix +++ b/modules/home/environment/userpkgs.nix @@ -37,6 +37,7 @@ let myScripts.switchmon myScripts.git-compose myScripts.playshellsound + myScripts.color-commit ]; in { options = { diff --git a/modules/home/environment/zsh/aliases.nix b/modules/home/environment/zsh/aliases.nix index 47fc599..7d2d439 100644 --- a/modules/home/environment/zsh/aliases.nix +++ b/modules/home/environment/zsh/aliases.nix @@ -6,7 +6,6 @@ shellAliases = { grep = "grep --color=auto"; yazi = "y"; - vi = "nvim"; mv = "mv -v"; cp = "cp -vr"; gt = "gtrash"; diff --git a/modules/home/environment/zsh/extraconfig.nix b/modules/home/environment/zsh/extraconfig.nix index 5608316..0a1dafa 100644 --- a/modules/home/environment/zsh/extraconfig.nix +++ b/modules/home/environment/zsh/extraconfig.nix @@ -2,6 +2,7 @@ let shellsound = "${pkgs.myScripts.playshellsound}/bin/playshellsound"; + color-commit = "${pkgs.myScripts.color-commit}/bin/color-commit"; sndpath = "${self}/assets/sound"; in { @@ -59,11 +60,14 @@ in fi } gitcommit_sfx() { - if git commit "$@"; then + output=$(git commit "$@") + if [ -n "$output" ]; then ${shellsound} ${sndpath}/gitcommit.wav + echo "$output" | ${color-commit} return 0 else ${shellsound} ${sndpath}/error.wav + echo "$output" return 1 fi } diff --git a/modules/home/programs/git.nix b/modules/home/programs/git.nix index adb65c0..d654b42 100755 --- a/modules/home/programs/git.nix +++ b/modules/home/programs/git.nix @@ -18,16 +18,16 @@ markEmptyLines = false; stripLeadingSymbols = false; }; - extraConfig = { - color.diff = { -# meta = "black yellow bold"; -# frag = "white blue bold"; - old = "#A9B1D6 #301A1F"; - new = "#A9B1D6 #12261E"; -# plain = "normal"; -# whitespace = "reverse red"; - }; - }; + extraConfig = { + color.diff = { + # meta = "black yellow bold"; + # frag = "white blue bold"; + old = "#A9B1D6 #301A1F"; + new = "#A9B1D6 #12261E"; + # plain = "normal"; + # whitespace = "reverse red"; + }; + }; }; }; } diff --git a/overlay/overlay.nix b/overlay/overlay.nix index d2d7faf..dc2136e 100644 --- a/overlay/overlay.nix +++ b/overlay/overlay.nix @@ -28,5 +28,6 @@ moveonscreen = super.callPackage ./scripts/wm-controls/moveonscreen.nix {}; s_check = super.callPackage ./scripts/wm-controls/s_check.nix {}; switchmon = super.callPackage ./scripts/wm-controls/switchmon.nix {}; + color-commit = super.callPackage ./scripts/misc/color-commit.nix {}; }; } diff --git a/overlay/scripts/commands/git-compose.nix b/overlay/scripts/commands/git-compose.nix index ed7e8b2..ae8d70e 100644 --- a/overlay/scripts/commands/git-compose.nix +++ b/overlay/scripts/commands/git-compose.nix @@ -4,6 +4,7 @@ pkgs.writeShellApplication { runtimeInputs = with pkgs; [ git gawk + myScripts.color-commit ]; text = '' set -e @@ -64,7 +65,7 @@ pkgs.writeShellApplication { if [ -z "$line" ]; then if [ -n "$msg" ] && [ ''${#lines[@]} -gt 0 ]; then git add "''${lines[@]}" - git commit -m "$msg" + git commit -m "$msg" | color-commit fi collecting=false msg="" @@ -80,7 +81,7 @@ pkgs.writeShellApplication { # Final cleanup if [ -n "$msg" ] && [ ''${#lines[@]} -gt 0 ]; then git add "''${lines[@]}" - git commit -m "$msg" + git commit -m "$msg" | color-commit fi ) ''; diff --git a/overlay/scripts/misc/color-commit.nix b/overlay/scripts/misc/color-commit.nix new file mode 100644 index 0000000..67702a2 --- /dev/null +++ b/overlay/scripts/misc/color-commit.nix @@ -0,0 +1,37 @@ +{ pkgs }: + +pkgs.writeShellApplication { + name = "color-commit"; + text = '' + stdin=$(cat) + [ -z "$stdin" ] && echo "Requires input via stdin" && exit 1 + + teal="\\\033[38;2;180;249;248m" + pink="\\\033[38;2;187;154;247m" + reset="\\\033[0m" + green_bg="\\\033[48;2;16;55;39m" + red_bg="\\\033[48;2;62;21;31m" + blue_bg="\\\033[48;2;33;73;129m" + + branch=$(git branch | grep "\*" | cut -d' ' -f2) + + output=$(echo "$stdin" | grep -A 1 -E "\[''${branch} ") + [ -z "$output" ] && echo "This doesn't look like commit output: " && echo "$stdin" && exit 1 + + echo "$output" | while IFS= read -r line; do + [ "$line" = "--" ] && continue + + if [[ "$line" =~ ^\[$branch ]]; then + line=$(echo "$line" | sed -E "s/\[([a-zA-Z0-9_-]+) ([a-zA-Z0-9]{7})\] (.*)/$(printf '%s' "$teal")\[\\1 \\2\]$(printf '%s' "$pink") \"\3\"$(printf '%s' "$reset")/") + echo -e "$line" + else + line=$(echo "$line" | sed -E \ + -e "s/([0-9]+ file(s)? changed,?)/''${blue_bg}\1''${reset}/g" \ + -e "s/([0-9]+ insertion(s)?\(\+\),?)/''${green_bg}\1''${reset}/g" \ + -e "s/([0-9]+ deletion(s)?\(-\),?)/''${red_bg}\1''${reset}/g") + echo -e "$line" + echo + fi + done + ''; +}