citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

undistract-me.zsh (2049B)


      1 # commands to ignore
      2 cmdignore=(htop tmux top vim git less bat sacc fz fzf nix-shell rust-dev all-dev npm)
      3 
      4 
      5 function sec_to_human () {
      6     local H=''
      7     local M=''
      8     local S=''
      9 
     10     local h=$(($1 / 3600))
     11     [ $h -gt 0 ] && H="${h} hour " && [ $h -gt 1 ] && H="${H}s"
     12 
     13     local m=$((($1 / 60) % 60))
     14     [ $m -gt 0 ] && M="${m} min " && [ $m -gt 1 ] && M="${M}s"
     15 
     16     local s=$(($1 % 60))
     17     [ $s -gt 0 ] && S="${s} sec" && [ $s -gt 1 ] && S="${S}s"
     18 
     19     echo $H$M$S
     20 }
     21 
     22 # Function taken from undistract-me, get the current window id
     23 function active_window_id () {
     24     if [[ -n $DISPLAY ]] ; then
     25         xprop -root _NET_ACTIVE_WINDOW | awk '{print $5}'
     26         return
     27     fi
     28     echo nowindowid
     29 }
     30 
     31 # end and compare timer, notify-send if needed
     32 function notifyosd-precmd() {
     33     retval=$?
     34     if [[ ${cmdignore[(r)$cmd_basename]} == $cmd_basename ]]; then
     35         return
     36     else
     37         if [ ! -z "$cmd" ]; then
     38             cmd_end=`date +%s`
     39             ((cmd_time=$cmd_end - $cmd_start))
     40         fi
     41         if [ $retval -gt 0 ]; then
     42             icon="cross-mark"
     43         else
     44             icon="check-mark"
     45         fi
     46         if [ ! -z "$cmd" -a $cmd_time -gt 3 ] && [[ "$cmd_active_win" != "$(active_window_id)" ]]; then
     47             longtimeout="$(((cmd_time / 3) * 1000))"
     48             timeout="$(btcs -t 15000 $longtimeout min)"
     49             local human="$(sec_to_human $cmd_time)"
     50             if [ ! -z $SSH_TTY ] ; then
     51                 notify-send -t $timeout -i $icon "Command took $human" "$cmd"
     52             else
     53                 notify-send -t $timeout -i $icon "Command took $human" "$cmd"
     54             fi
     55         fi
     56         unset cmd
     57     fi
     58 }
     59 
     60 # make sure this plays nicely with any existing precmd
     61 precmd_functions+=( notifyosd-precmd )
     62 
     63 # get command name and start the timer
     64 function notifyosd-preexec() {
     65     cmd=$1
     66     cmd_basename=${${cmd:s/sudo //}[(ws: :)1]}
     67     cmd_start=$(date +%s)
     68     cmd_active_win=$(active_window_id)
     69 }
     70 
     71 # make sure this plays nicely with any existing preexec
     72 preexec_functions+=( notifyosd-preexec )