Created
May 8, 2018 15:44
-
-
Save danilomo/ff1f082d8c258238144c3d175e4a841a to your computer and use it in GitHub Desktop.
pomodoro timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function check-command { | |
type $1 > /dev/null 2>/dev/null | |
if [ $? -ne 0 ] | |
then | |
(>&2 echo "Command $1 expected, but not found.") | |
(>&2 echo "Please install dependencies: sudo apt-get install sqlite3 jq") | |
exit 1 | |
fi | |
} | |
function see-config { | |
cat ~/.pomodoro_manager/config.json | |
} | |
function save-config { | |
l1=" \"pomodoro_duration\": \"$pomodoro_duration\",\n" | |
l2=" \"break_duration\": \"$break_duration\",\n" | |
l3=" \"long_break_duration\": \"$long_break_duration\"\n" | |
printf "{\n$l1$l2$l3}\n" | |
} | |
function check-working-directory { | |
if [ ! -d ~/.pomodoro_manager ] | |
then | |
mkdir ~/.pomodoro_manager | |
echo '{ | |
"pomodoro_duration": "25m", | |
"break_duration": "5m", | |
"long_break_duration": "15m" | |
}' > ~/.pomodoro_manager/config.json | |
mkdir ~/.pomodoro_manager/sounds | |
wget wget -q https://upload.wikimedia.org/wikipedia/commons/e/e1/Activated_fire_alarm_%28sound%29.ogg -O ~/.pomodoro_manager/sounds/alarm.ogg | |
sqlite3 ~/.pomodoro_manager/database.db "CREATE TABLE pomodoros( pomDate TEXT, pomTime TEXT )" | |
fi | |
} | |
function load-variables { | |
config="/home/$USER/.pomodoro_manager/config.json" | |
pomodoro_duration=$(jq '.pomodoro_duration' "$config" | sed -e 's/^"//' -e 's/"$//' ) | |
break_duration=$(jq '.break_duration' "$config" | sed -e 's/^"//' -e 's/"$//' ) | |
long_break_duration=$(jq '.long_break_duration' "$config" | sed -e 's/^"//' -e 's/"$//' ) | |
} | |
function record-pomodoro { | |
dt=$(date +%Y-%m-%d) | |
t=$(date +%H:%M) | |
sqlite3 ~/.pomodoro_manager/database.db "INSERT INTO pomodoros VALUES( '$dt', '$t' )" | |
pomodoro b | |
} | |
function cleanup { | |
rm -f ~/.pomodoro_manager/*.txt | |
} | |
function alarm { | |
if [ -z "$2" ] | |
then | |
comm=":" | |
else | |
comm=$2 | |
fi | |
d=$(echo $1 | sed 's/s/sec/g' | sed 's/m/min/g') | |
d=$(date -d "+$d" +%s) | |
echo $d > ~/.pomodoro_manager/stopat.txt | |
sleep $1 && cleanup && eval "$comm" && mplayer ~/.pomodoro_manager/sounds/alarm.ogg > /dev/null 2>&1 & | |
p=$! | |
echo $p > ~/.pomodoro_manager/pid.txt | |
} | |
function cancel-alarm { | |
if [ -f ~/.pomodoro_manager/pid.txt ] | |
then | |
psleep=$(pgrep -P $(cat ~/.pomodoro_manager/pid.txt)) | |
kill $(cat ~/.pomodoro_manager/pid.txt) 2>/dev/null | |
kill $psleep 2>/dev/null | |
cleanup | |
fi | |
} | |
function pomodoros-today { | |
dt=$(date '+%Y-%m-%d') | |
sqlite3 ~/.pomodoro_manager/database.db "SELECT pomTime FROM pomodoros WHERE pomDate = '$dt'" | awk -F ":" ' | |
function printPomodoroCount(num){ | |
count = 0; | |
str = ""; | |
while(count < num){ | |
if(count % 4 == 0 && count != 0){ | |
str = str ", X"; | |
}else{ | |
str = str " X"; | |
} | |
count = count+1; | |
} | |
return str; | |
} | |
BEGIN{ | |
morning=0; | |
afternoon=0; | |
evening=0; | |
} | |
{ | |
if( $1 <= 12 ){ | |
morning++; | |
}else if( $1 > 12 && $1 <= 17 ){ | |
afternoon++; | |
}else{ | |
evening++; | |
} | |
} | |
END{ | |
print "Morning: " printPomodoroCount(morning); | |
print "Afternoon: " printPomodoroCount(afternoon); | |
print "Evening: " printPomodoroCount(evening); | |
print "Pomodoros today: " (morning + afternoon + evening); | |
} | |
' | |
} | |
function remaining { | |
if [ -f ~/.pomodoro_manager/stopat.txt ] | |
then | |
d1=$(date +%s) | |
d2=$(cat ~/.pomodoro_manager/stopat.txt) | |
remaining=$(($d2 - $d1)) | |
if [ $remaining -gt 60 ] | |
then | |
remaining=$(($remaining / 60)) | |
if [ $remaining -eq 1 ] | |
then | |
echo "1 minute left." | |
else | |
echo "$remaining minutes left." | |
fi | |
else | |
echo "$remaining seconds left." | |
fi | |
fi | |
} | |
function remaining-notification { | |
if [ -f ~/.pomodoro_manager/stopat.txt ] | |
then | |
d1=$(date +%s) | |
d2=$(cat ~/.pomodoro_manager/stopat.txt) | |
remaining=$(($d2 - $d1)) | |
if [ $remaining -gt 60 ] | |
then | |
remaining=$(($remaining / 60)) | |
if [ $remaining -eq 1 ] | |
then | |
notify-send Pomodoro "1 minute left." | |
else | |
notify-send Pomodoro "$remaining minutes left." | |
fi | |
else | |
notify-send Pomodoro "$remaining seconds left." | |
fi | |
fi | |
} | |
check-command sqlite3 | |
check-command jq | |
check-command mplayer | |
check-working-directory | |
load-variables | |
case "$1" in | |
start|s) | |
cancel-alarm | |
if ! [ -z $2 ] | |
then | |
pomodoro_duration=$2 | |
fi | |
echo "Starting pomodoro." | |
background-noise $pomodoro_duration | |
alarm $pomodoro_duration record-pomodoro | |
;; | |
cancel|c) | |
echo "Current pomodoro/break canceled." | |
background-noise stop | |
cancel-alarm | |
;; | |
break|b) | |
cancel-alarm | |
background-noise stop | |
echo "Starting short break." | |
alarm $break_duration | |
;; | |
long-break|lb) | |
background-noise stop | |
cancel-alarm | |
echo "Starting long break." | |
alarm $long_break_duration | |
;; | |
time-left|l) | |
remaining | |
;; | |
time-left-n|ln) | |
remaining-notification | |
;; | |
today|t) | |
pomodoros-today | |
;; | |
set-variable|set) | |
declare "${2}"="${3}" | |
save-config | |
;; | |
see-config) | |
see-config | |
echo $pomodoro_duration | |
echo $long_break_duration | |
echo $break_duration | |
;; | |
*) | |
echo "Usage: pomodoro start|cancel|break|long-break|time-left|today, or" | |
echo "Usage: pomodoro s|c|b|lb|l|t." | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment