Skip to content

Instantly share code, notes, and snippets.

@slumos
Created December 7, 2022 18:59
Show Gist options
  • Save slumos/0e985212c9b0204723975c559c4862bd to your computer and use it in GitHub Desktop.
Save slumos/0e985212c9b0204723975c559c4862bd to your computer and use it in GitHub Desktop.
next-output-file-name () {
base="$1"
for ((i=0; i<20; i++)); do
filename="${base}.${i}"
[[ ! -e "$filename" ]] && print "$filename" && return
done
print "ERROR"
}
append-tee-file-base () {
case "$BUFFER" in
*apply*) print 'apply.out' ;;
*check*) print 'check.out' ;;
*plan*) print 'plan.out' ;;
*terraform*init*) print 'init.out' ;;
*test*) print 'test.out' ;;
*)
cmd="${${(z)BUFFER}[1]}"
print "${cmd:-x}.out"
esac
set +x
}
append-tee-command () {
output_base="$(append-tee-file-base "$cmd")"
output_filename="$(next-output-file-name "$output_base")"
print "tee ${output_filename}"
}
append-tee () {
BUFFER="${LBUFFER}|& $(append-tee-command "$cmd")${RBUFFER}"
((CURSOR = $#BUFFER))
}
zle -N append-tee
# option-t on macOS
bindkey † append-tee
# if not on macOS
bindkey "^X^t" append-tee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment