Advertisement
marcoshalano

undistract-me.zsh

Feb 17th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.63 KB | None | 0 0
  1. ###
  2. # Undistract-me is a shell script for Bash which shows a message when a command execution finishses.
  3. # I found this version (I'm not the author) to implement Undistract-me in Zsh.
  4. # I adapted to add a icon  to notification box.
  5. # To use, just add this line to ~/.zshrc:
  6. # source <path>/undistract-me.zsh
  7. ###
  8.  
  9.  
  10.  
  11. # commands to ignore
  12. cmdignore=(htop tmux top vim)
  13.  
  14. # end and compare timer, notify-send if needed
  15. function notifyosd-precmd(){
  16.     retval=$?
  17.     if [[ ${cmdignore[(r)$cmd_basename]} == $cmd_basename ]]; then
  18.         return
  19.     else
  20.         if [ ! -z "$cmd" ]; then
  21.             cmd_end=`date +%s`
  22.             ((cmd_time=$cmd_end - $cmd_start))
  23.         fi
  24.         if [ $retval -gt 0 ]; then
  25.             cmdstat="with warning"
  26.             sndstat="/usr/share/sounds/gnome/default/alerts/sonar.ogg"
  27.             iconstat="error"
  28.         else
  29.             cmdstat="successfully"
  30.             sndstat="/usr/share/sounds/gnome/default/alerts/glass.ogg"
  31.             iconstat="utilities-terminal"
  32.         fi
  33.         if [ ! -z "$cmd" -a $cmd_time -gt 10 ]; then
  34.             if [ ! -z $SSH_TTY ] ; then
  35.                 notify-send -i $iconstat -u low "$cmd_basename on `hostname` completed $cmdstat" "\"$cmd\" took $cmd_time seconds"
  36.             else
  37.                 notify-send -i $iconstat -u low "$cmd_basename completed $cmdstat" "\"$cmd\" took $cmd_time seconds"
  38.             fi
  39.         fi
  40.         unset cmd
  41.     fi
  42. }
  43.  
  44. # make sure this plays nicely with any existing precmd
  45. precmd_functions+=( notifyosd-precmd )
  46.  
  47. # get command name and start the timer
  48. function notifyosd-preexec() {
  49.     cmd=$1
  50.     cmd_basename=${${cmd:s/sudo //}[(ws: :)1]}
  51.     cmd_start=`date +%s`
  52. }
  53.  
  54. # make sure this plays nicely with any existing preexec
  55. preexec_functions+=( notifyosd-preexec )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement