Skip to content

Instantly share code, notes, and snippets.

@takegue
Forked from oknowton/zbell.zsh
Last active October 16, 2020 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takegue/aaeb57123ac97c649b34dfdc5f278b89 to your computer and use it in GitHub Desktop.
Save takegue/aaeb57123ac97c649b34dfdc5f278b89 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
# This script prints a bell character when a command finishes
# if it has been running for longer than $zbell_duration seconds.
# If there are programs that you know run long that you don't
# want to bell after, then add them to $zbell_ignore.
#
# This script uses only zsh builtins so its fast, there's no needless
# forking, and its only dependency is zsh and its standard modules
#
# Written by Jean-Philippe Ouellet <jpo@vt.edu>
# Made available under the ISC license.
# only do this if we're in an interactive shell
[[ -o interactive ]] || return
# get $EPOCHSECONDS. builtins are faster than date(1)
zmodload zsh/datetime || return
# make sure we can register hooks
autoload -Uz add-zsh-hook || return
autoload -Uz regexp-replace || return
# initialize zbell_duration if not set
(( ${+zbell_duration} )) || zbell_duration=1
(( ${+zbell_duration_email} )) || zbell_duration_email=180
# initialize zbell_ignore if not set
(( ${+zbell_ignore} )) || zbell_ignore=($EDITOR $PAGER fg ls jupyter vim nvim ipython watch htop top ssh iotop dstat vmstat nano emacs vi bwm-ng less more fdisk audacious play aplay sqlite3 wine mtr ping traceroute vlc mplayer smplayer tail tmux screen man sawfish-config powertop)
# initialize it because otherwise we compare a date and an empty string
# the first time we see the prompt. it's fine to have lastcmd empty on the
# initial run because it evaluates to an empty string, and splitting an
# empty string just results in an empty array.
zbell_timestamp=$EPOCHSECONDS
# right before we begin to execute something, store the time it started at
zbell_begin() {
zbell_timestamp=$EPOCHSECONDS
zbell_lastcmd=$1
}
zbell_email() {
local zbell_cmd_duration
zbell_cmd_duration=$(( $EPOCHSECONDS - $zbell_timestamp ))
datetime=$( LC_ALL=C date +'%Y-%m-%d:%H:%M:%S' )
mail -s "Complete Running Command (@${datetime})" $EMAIL <<EOS
Hi! I notify that below long process have been finished.
It is completed with exit status ${zbell_exit_status}
LOG
------------
"${zbell_lastcmd}"
staerted: ${zbell_timestamp}
end: ${zbell_last_timestamp}
Time: $(( $zbell_cmd_duration / 60 )) m $(( $zbell_cmd_duration % 60 )) s
Love,
Zbell
EOS
}
zbell_noise() {
zbell_cmd_duration=$(( $EPOCHSECONDS - $zbell_timestamp ))
msg="${zbell_lastcmd}
TIME: $(( $zbell_cmd_duration / 60 )) m $(( $zbell_cmd_duration % 60 )) s
"
echo $msg | {
case "$OSTYPE" in
darwin*)
if (( $+commands[terminal-notifier])); then
reattach-to-user-namespace terminal-notifier -sound Pop -title "Task Completed (status:${zbell_exit_status})"
fi
echo '\a';
;;
*)
if (( $+commands[notify-send] )); then
notify-send "Finished Job on $HOST:" "$zbell_lastcmd"
elif (( $+commands[wsl-notifier] )); then
wsl-notifier "Finished Job on $HOST: ${zbell_lastcmd}"
else
echo '\a'
fi
esac
}
}
# when it finishes, if it's been running longer than $zbell_duration,
# and we dont have an ignored command in the line, then print a bell.
zbell_end() {
zbell_exit_status=$?
ran_long=$(( $EPOCHSECONDS - $zbell_timestamp >= $zbell_duration ))
local has_ignored_cmd=0
local zbell_lastcmd_tmp
zbell_lastcmd_tmp="$zbell_lastcmd"
regexp-replace zbell_lastcmd_tmp '^sudo ' ''
if [[ $zbell_last_timestamp == $zbell_timestamp ]]; then
return
fi
if [[ $zbell_lastcmd_tmp == "" ]]; then
return;
fi
zbell_last_timestamp=$zbell_timestamp
for cmd in ${(s:;:)zbell_lastcmd_tmp//|/;}; do
words=(${(z)cmd})
util=${words[1]}
if (( ${zbell_ignore[(i)$util]} <= ${#zbell_ignore} )); then
has_ignored_cmd=1
break
fi
done
if (( ! $has_ignored_cmd )) && (( ran_long )); then
local zbell_cmd_duration
zbell_cmd_duration=$(( $EPOCHSECONDS - $zbell_timestamp ))
if [[ $zbell_cmd_duration -gt $zbell_duration_email ]]; then
zbell_email
else
zbell_noise
fi
fi
}
# register the functions as hooks
add-zsh-hook preexec zbell_begin
add-zsh-hook precmd zbell_end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment