mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

throttle (1005B)


      1 #!/bin/bash
      2 
      3 # make time wasters less attractive
      4 # this is like hacker news' noprocrast setting
      5 
      6 NAME="$1"
      7 shift
      8 BLOCK=7200
      9 GRACE=120
     10 WAIT=10
     11 DIR="$HOME/temp/throttle"
     12 FILE="$DIR/$NAME"
     13 
     14 mkdir -p "$DIR"
     15 if [ ! -f "$FILE" ]
     16 then
     17   echo 0 > $FILE
     18 fi
     19 LAST=$(tail -1 $FILE | cut -d' ' -f1)
     20 PRESENT=$(date "+%s")
     21 DIFF=$(($PRESENT - $LAST))
     22 
     23 if [ $DIFF -gt $GRACE -a $DIFF -lt $BLOCK ]
     24 then
     25   # throttle
     26   HOURS=$(($DIFF/3600))
     27   MINS=$((($DIFF%3600)/60))
     28   SECS=$(($DIFF%60))
     29   echo "$USER, the last time you ran $NAME was ${HOURS}h ${MINS}m ${SECS}s ago"
     30   echo "i'm giving you $WAIT seconds to change your mind..."
     31   sleep $WAIT
     32   CODE="$RANDOM"
     33   echo "... OK, if you still want to go, enter '$CODE'"
     34   read text
     35   if [ $text != $CODE ]
     36   then
     37     echo "I knew you didn't really want to! see you again later"
     38     exit 0
     39   fi
     40 fi
     41 
     42 # run command
     43 date "+%s # start" >> $FILE
     44 "$@"
     45 CODE="$?"
     46 # store the end date
     47 # TODO should be a signal because we don't close cleanly
     48 date "+%s # stop" >> $FILE
     49 
     50 
     51 exit "$CODE"
     52