commit outputs can now be colored with a new script color-commit

This commit is contained in:
2024-11-17 13:03:49 -05:00
parent 76be210ec3
commit beedfd1be2
8 changed files with 66 additions and 14 deletions

View File

@@ -215,3 +215,12 @@ included in my nixpkgs overlay as custom packages, and these packages are declar
- *Usage*: - *Usage*:
- `switchmon` - Does not take arguments. - `switchmon` - Does not take arguments.
- *Defined in*: `overlay/scripts/wm-controls/switchmon.nix` - *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`

View File

@@ -37,6 +37,7 @@ let
myScripts.switchmon myScripts.switchmon
myScripts.git-compose myScripts.git-compose
myScripts.playshellsound myScripts.playshellsound
myScripts.color-commit
]; ];
in { in {
options = { options = {

View File

@@ -6,7 +6,6 @@
shellAliases = { shellAliases = {
grep = "grep --color=auto"; grep = "grep --color=auto";
yazi = "y"; yazi = "y";
vi = "nvim";
mv = "mv -v"; mv = "mv -v";
cp = "cp -vr"; cp = "cp -vr";
gt = "gtrash"; gt = "gtrash";

View File

@@ -2,6 +2,7 @@
let let
shellsound = "${pkgs.myScripts.playshellsound}/bin/playshellsound"; shellsound = "${pkgs.myScripts.playshellsound}/bin/playshellsound";
color-commit = "${pkgs.myScripts.color-commit}/bin/color-commit";
sndpath = "${self}/assets/sound"; sndpath = "${self}/assets/sound";
in in
{ {
@@ -59,11 +60,14 @@ in
fi fi
} }
gitcommit_sfx() { gitcommit_sfx() {
if git commit "$@"; then output=$(git commit "$@")
if [ -n "$output" ]; then
${shellsound} ${sndpath}/gitcommit.wav ${shellsound} ${sndpath}/gitcommit.wav
echo "$output" | ${color-commit}
return 0 return 0
else else
${shellsound} ${sndpath}/error.wav ${shellsound} ${sndpath}/error.wav
echo "$output"
return 1 return 1
fi fi
} }

View File

@@ -20,12 +20,12 @@
}; };
extraConfig = { extraConfig = {
color.diff = { color.diff = {
# meta = "black yellow bold"; # meta = "black yellow bold";
# frag = "white blue bold"; # frag = "white blue bold";
old = "#A9B1D6 #301A1F"; old = "#A9B1D6 #301A1F";
new = "#A9B1D6 #12261E"; new = "#A9B1D6 #12261E";
# plain = "normal"; # plain = "normal";
# whitespace = "reverse red"; # whitespace = "reverse red";
}; };
}; };
}; };

View File

@@ -28,5 +28,6 @@
moveonscreen = super.callPackage ./scripts/wm-controls/moveonscreen.nix {}; moveonscreen = super.callPackage ./scripts/wm-controls/moveonscreen.nix {};
s_check = super.callPackage ./scripts/wm-controls/s_check.nix {}; s_check = super.callPackage ./scripts/wm-controls/s_check.nix {};
switchmon = super.callPackage ./scripts/wm-controls/switchmon.nix {}; switchmon = super.callPackage ./scripts/wm-controls/switchmon.nix {};
color-commit = super.callPackage ./scripts/misc/color-commit.nix {};
}; };
} }

View File

@@ -4,6 +4,7 @@ pkgs.writeShellApplication {
runtimeInputs = with pkgs; [ runtimeInputs = with pkgs; [
git git
gawk gawk
myScripts.color-commit
]; ];
text = '' text = ''
set -e set -e
@@ -64,7 +65,7 @@ pkgs.writeShellApplication {
if [ -z "$line" ]; then if [ -z "$line" ]; then
if [ -n "$msg" ] && [ ''${#lines[@]} -gt 0 ]; then if [ -n "$msg" ] && [ ''${#lines[@]} -gt 0 ]; then
git add "''${lines[@]}" git add "''${lines[@]}"
git commit -m "$msg" git commit -m "$msg" | color-commit
fi fi
collecting=false collecting=false
msg="" msg=""
@@ -80,7 +81,7 @@ pkgs.writeShellApplication {
# Final cleanup # Final cleanup
if [ -n "$msg" ] && [ ''${#lines[@]} -gt 0 ]; then if [ -n "$msg" ] && [ ''${#lines[@]} -gt 0 ]; then
git add "''${lines[@]}" git add "''${lines[@]}"
git commit -m "$msg" git commit -m "$msg" | color-commit
fi fi
) )
''; '';

View File

@@ -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
'';
}