Skip to content

Instantly share code, notes, and snippets.

@Austcool-Walker
Last active April 16, 2020 22:22
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 Austcool-Walker/03a8838efaeb8237304f6f032d4bef50 to your computer and use it in GitHub Desktop.
Save Austcool-Walker/03a8838efaeb8237304f6f032d4bef50 to your computer and use it in GitHub Desktop.
undistract me zsh plugin.

Run these commands in TTY shell or a Terminal

sudo download https://gist.github.com/Austcool-Walker/03a8838efaeb8237304f6f032d4bef50/raw/d446ba26cc6ca9f1f4557f907ca5c7aa26c27078/notifyosd.zsh /usr/local/share/zsh/site-functions/notifyosd.zsh

Or

sudo curl -o /usr/local/share/zsh/site-functions/notifyosd.zsh https://gist.github.com/Austcool-Walker/03a8838efaeb8237304f6f032d4bef50/raw/d446ba26cc6ca9f1f4557f907ca5c7aa26c27078/notifyosd.zsh
touch ~/.zprofile
sudo chmod -v +x ~/.zprofile && sudo chmod -v +x /usr/local/share/zsh/site-functions/notifyosd.zsh
echo "
# zprofile and undistractme zsh
source ~/.zprofile
source /usr/local/share/zsh/site-functions/notifyosd.zsh" | >> ~/.zshrc
sudo apt install libnotify-bin sox
# commands to ignore
cmdignore=(htop tmux top vim)
# set gt 0 to enable GNU units for time results
gnuunits=0
# end and compare timer, notify-send if needed
function notifyosd-precmd() {
retval=$?
if [[ ${cmdignore[(r)$cmd_basename]} == $cmd_basename ]]; then
return
else
if [ ! -z "$cmd" ]; then
cmd_end=`date +%s`
((cmd_secs=$cmd_end - $cmd_start))
fi
if [ $retval -gt 0 ]; then
cmdstat="with warning"
sndstat="/usr/share/sounds/gnome/default/alerts/sonar.ogg"
urgency="critical"
else
cmdstat="successfully"
sndstat="/usr/share/sounds/gnome/default/alerts/glass.ogg"
urgency="critical"
fi
if [ ! -z "$cmd" -a $cmd_secs -gt 10 ]; then
if [ $gnuunits -gt 0 ]; then
cmd_time=$(units "$cmd_secs seconds" "centuries;years;months;weeks;days;hours;minutes;seconds" | \
sed -e 's/\ +/\,/g' -e s'/\t//')
else
cmd_time="$cmd_secs seconds"
fi
if [ ! -z $SSH_TTY ] ; then
notify-send --urgency=critical -i utilities-terminal \
-u $urgency "$cmd_basename on `hostname` completed $cmdstat" "\"$cmd\" took $cmd_time"; \
play -q $sndstat
else
notify-send --urgency=critical -i utilities-terminal \
-u $urgency "$cmd_basename completed $cmdstat" "\"$cmd\" took $cmd_time"; \
play -q $sndstat
fi
fi
unset cmd
fi
}
# make sure this plays nicely with any existing precmd
precmd_functions+=( notifyosd-precmd )
# get command name and start the timer
function notifyosd-preexec() {
cmd=$1
cmd_basename=${${cmd:s/sudo //}[(ws: :)1]}
cmd_start=`date +%s`
}
# make sure this plays nicely with any existing preexec
preexec_functions+=( notifyosd-preexec )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment