Skip to content

Instantly share code, notes, and snippets.

@mataki
Last active February 21, 2025 07:18
Show Gist options
  • Save mataki/c42487e0fa7fba9375f97134c3caf76a to your computer and use it in GitHub Desktop.
Save mataki/c42487e0fa7fba9375f97134c3caf76a to your computer and use it in GitHub Desktop.
Plays a sound to indicate completion or failure if a command takes a long time to execute on a Mac.
function save_command_start() {
COMMAND_START_TIME=$EPOCHSECONDS
}
function notify_command_end() {
local exit_status=$?
local duration=$(( EPOCHSECONDS - COMMAND_START_TIME ))
local last_command=$(fc -ln -1 | sed 's/^[ \t]*//')
if (( duration >= 30 )); then
if [ $exit_status -eq 0 ]; then
(afplay /System/Library/Sounds/Glass.aiff &> /dev/null &)
osascript -e "display notification \"Command: $last_command with $duration sec\" with title \"Last Command Executed\""
else
(afplay /System/Library/Sounds/Basso.aiff &> /dev/null &)
osascript -e "display notification \"Command: $last_command with $duration sec\" with title \"Last Command Failed\""
fi
fi
}
add-zsh-hook preexec save_command_start
add-zsh-hook precmd notify_command_end
@mataki
Copy link
Author

mataki commented Feb 21, 2025

Add .zshrc

zinit snippet https://gist.githubusercontent.com/mataki/c42487e0fa7fba9375f97134c3caf76a/raw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment