Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pseudo undistract-me implementation in zsh Original undistract-me: http://mumak.net/undistract-me/
# commands to ignore
cmdignore=(htop tmux top vim)
# 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_time=$cmd_end - $cmd_start))
fi
if [ $retval -gt 0 ]; then
cmdstat="with warning"
sndstat="/usr/share/sounds/gnome/default/alerts/sonar.ogg"
else
cmdstat="successfully"
sndstat="/usr/share/sounds/gnome/default/alerts/glass.ogg"
fi
if [ ! -z "$cmd" -a $cmd_time -gt 10 ]; then
if [ ! -z $SSH_TTY ] ; then
notify-send -i utilities-terminal -u low "$cmd_basename on `hostname` completed $cmdstat" "\"$cmd\" took $cmd_time seconds"; play -q $sndstat
else
notify-send -i utilities-terminal -u low "$cmd_basename completed $cmdstat" "\"$cmd\" took $cmd_time seconds"; 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 )
@ketam

This comment has been minimized.

Copy link

@ketam ketam commented Jan 22, 2013

How to implement this on zsh?

@pataluc

This comment has been minimized.

Copy link

@pataluc pataluc commented Jan 28, 2013

i added

unset cmd

on line 9 after "notify-send" to prevent getting notification whenever i press enter after a long command...

@ihashacks

This comment has been minimized.

Copy link
Owner Author

@ihashacks ihashacks commented Feb 13, 2013

To use this, clone it:

git clone https://gist.github.com/4576452.git

Then copy the file somewhere:

cp 4576452/notifyosd.zsh ~/.zsh

Source it in your .zshrc:

[ -e $HOME/.zsh/notifyosd.zsh ] && . $HOME/.zsh/notifyosd.zsh
@vlad-shatskyi

This comment has been minimized.

Copy link

@vlad-shatskyi vlad-shatskyi commented Mar 27, 2013

I've made a small addition, which displays a notification only if the current active windows isn't terminal.
https://gist.github.com/shockone/5255331
Without the line 13 notification is displayed multiple times. Any ideas why?

@DAP-DarkneSS

This comment has been minimized.

Copy link

@DAP-DarkneSS DAP-DarkneSS commented Mar 28, 2013

kdialog --title "$cmd_basename completed" --passivepopup ""$cmd" took $cmd_time seconds"
to use at KDE without notify-tools installed.
PS Thanks a lot!

@ihashacks

This comment has been minimized.

Copy link
Owner Author

@ihashacks ihashacks commented Apr 18, 2013

pataluc: Thank you for pointing that out! The "unset cmd" should actually be on line 10 though. I have fixed it.

@ihashacks

This comment has been minimized.

Copy link
Owner Author

@ihashacks ihashacks commented Apr 19, 2013

Now if you run this thing in an "ssh -X" session you'll have the hostname included in the notification!

@ihashacks

This comment has been minimized.

Copy link
Owner Author

@ihashacks ihashacks commented May 11, 2013

Command completion status now in notification title!

@ihashacks

This comment has been minimized.

Copy link
Owner Author

@ihashacks ihashacks commented Mar 17, 2014

I forgot to mention that if you have sox installed then this will play a sound for $?=0 or $?>0.

@ihashacks

This comment has been minimized.

Copy link
Owner Author

@ihashacks ihashacks commented Apr 8, 2014

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