mybin

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

commit cea90f72d7f560a979b9e07588cb750333291f41
parent d666b686561a843e9da98dc3d99a06283d2da9dd
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Thu, 25 Jan 2018 00:15:23 +0100

throttle

Diffstat:
throttle | 44++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+), 0 deletions(-)

diff --git a/throttle b/throttle @@ -0,0 +1,44 @@ +#!/bin/bash + +# make time wasters less attractive +# this is like hacker news' noprocrast setting + +BLOCK=7200 +GRACE=2 +WAIT=1 +COMMAND="$1" +DIR="$HOME/temp/throttle" +FILE="$DIR/$COMMAND" + +mkdir -p "$DIR" +if [ ! -f "$FILE" ] +then + echo 0 > $FILE +fi +LAST=$(cat $FILE) +PRESENT=$(date "+%s") +DIFF=$(($PRESENT - $LAST)) + +if [ $DIFF -gt $GRACE -a $DIFF -lt $BLOCK ] +then + # throttle + HOURS=$(($DIFF/3600)) + MINS=$((($DIFF%3600)/60)) + SECS=$(($DIFF%60)) + echo "$USER, the last time you ran $COMMAND was ${HOURS}h ${MINS}m ${SECS}s ago" + echo "i'm giving you $WAIT seconds to change your mind..." + sleep $WAIT + CODE="$RANDOM" + echo "... OK, if you still want to go, enter '$CODE'" + read text + if [ $text != $CODE ] + then + echo "I knew you didn't really want to! see you again later" + exit 0 + fi +fi + +# run command +date "+%s" > $FILE +exec "$@" +