From 0ae5f17dd0b3fa5a23851e264f1d3cdde5f48979 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Tue, 17 Jan 2023 16:33:53 +0100 Subject: [PATCH] git:last-changed: Fix truncation of colored lines `cut` counts bytes instead of printable character, making it truncate colored lines too early. Fix this by using awk and adding the length difference between a colored and uncolored version to the allowed length. COLUMNS is a shell variable and thus needs exporting for `ENVIRON` to see it in awk. --- .config/zsh/autoload/git/git-last-changed | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.config/zsh/autoload/git/git-last-changed b/.config/zsh/autoload/git/git-last-changed index 9e09434..0fe40f5 100755 --- a/.config/zsh/autoload/git/git-last-changed +++ b/.config/zsh/autoload/git/git-last-changed @@ -22,4 +22,9 @@ ls -1p --color="$color_set" "$@" \ done \ | sort -r -t$'\t' -k2,2 \ | column -s$'\t' -t \ -| cut -c -"$COLUMNS" +| env COLUMNS="$COLUMNS" awk \ + '{ + sanit = gensub(/\033\[[0-9;]*m/, "", "g", $0); + trunc_len = ENVIRON["COLUMNS"] + length($0) - length(sanit) - 3; + print gensub("^(.{" trunc_len "}).{4,}$", "\\1...", "g") + }'