Files
dotfiles/.config/zsh/autoload/git/git-last-changed
Julian Prein 0ae5f17dd0 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.
2023-01-17 16:33:53 +01:00

31 lines
952 B
Bash
Executable File

#!/usr/bin/env zsh
#
# List all files and directories but include the latest commits date and
# subject, similar to the file browser in web-UIs of services like GitHub. Also
# sort the entries by the commits date and time to see the most recent changed
# files/folders at the bottom.
# Execute normal ls when not in git repo
if ! git rev-parse 2>/dev/null; then
ls -1p --color=auto "$@"
return
fi
local color_set
[ -t 1 ] && color_set=always || color_set=never
ls -1p --color="$color_set" "$@" \
| while read -r line; do
sanitized_line="$(sed 's/\x1b[^m]*m//g' <<<"$line")"
git_info="$(git log -1 --format=$'%ci\t%s' "$sanitized_line")"
printf "%s\t%s\n" "$line" "$git_info"
done \
| sort -r -t$'\t' -k2,2 \
| column -s$'\t' -t \
| 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")
}'