Created
December 21, 2011 15:09
-
-
Save naholyr/1506349 to your computer and use it in GitHub Desktop.
Bash Pomodoro using libnotify
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 | |
# Configuration directory | |
HOOKS_DIR="$HOME/.pomodoro/hooks" | |
# Time for a Pomodoro (see UNIT) | |
POMODORO=25 | |
# Time for a short pause (see UNIT) | |
SHORT_PAUSE=5 | |
# Number of Pomodoro before long pause | |
NB_POMODORO_BEFORE_PAUSE=4 | |
# Time for a long pause (every POMODORO_BEFORE_PAUSE) | |
LONG_PAUSE=15 | |
# Notification icons depending on context | |
ICON_WORK=face-glasses | |
ICON_SHORT_PAUSE=face-smile | |
ICON_LONG_PAUSE=face-cool | |
# Notification time | |
SHORT_NOTIFICATION_TIME=500 | |
LONG_NOTIFICATION_TIME=2000 | |
# Base unit time in second (if you change this, labels will become incoherent, used for debugging purpose) | |
UNIT=60 | |
# Start the Pomodoro magic! | |
# Hooks directories | |
HOOKS_WORK="$HOOKS_DIR/work.d" | |
HOOKS_PAUSE="$HOOKS_DIR/pause.d" | |
HOOKS_PAUSE_SHORT="$HOOKS_DIR/pause-short.d" | |
HOOKS_PAUSE_LONG="$HOOKS_DIR/pause-short.d" | |
HOOKS_EXIT="$HOOKS_DIR/exit.d" | |
if [ ! -d "$HOOKS_DIR" ]; then | |
echo "INIT: Creating hooks directory..." | |
mkdir -p "$HOOKS_DIR" || (echo "Failed to initialize directory '$PREF_DIR'"; exit 1) | |
fi | |
# Hooks execution | |
function execute_hooks () { | |
if [ ! -d "$1" ]; then | |
echo "INIT: Creating dedicated hooks directory..." | |
mkdir -p "$1" || (echo "Failed to initialize directory '$PREF_DIR'") | |
fi | |
find "$1" -maxdepth 1 -mindepth 1 -not -type d -executable | while read script; do | |
echo "HOOK: $script" | |
$script | |
done | |
} | |
# Catch exit as some hooks may need some cleanup | |
function on_exit() { | |
echo "Executing cleanup scripts now." | |
execute_hooks "$HOOKS_EXIT" | |
} | |
trap on_exit EXIT | |
NB_POMODORO=0 | |
while NB_POMODORO=$(($NB_POMODORO+1)); do | |
echo "Work!" | |
execute_hooks "$HOOKS_WORK" | |
notify-send -i "$ICON_WORK" -t $LONG_NOTIFICATION_TIME "Start working!" | |
i=0 | |
while [ $i -lt $POMODORO ]; do | |
notify-send -i "$ICON_WORK" -t $SHORT_NOTIFICATION_TIME "$(($POMODORO-$i)) minute(s) left before the pause" | |
sleep $UNIT | |
i=$((1+$i)) | |
echo "Work: $i/$POMODORO" | |
done | |
echo "Pause!"; | |
execute_hooks "$HOOKS_PAUSE" | |
if [ $(($NB_POMODORO%$NB_POMODORO_BEFORE_PAUSE)) -eq 0 ]; then | |
notify-send -i "$ICON_LONG_PAUSE" -t $LONG_NOTIFICATION_TIME "Long pause :D" | |
pause=$LONG_PAUSE | |
execute_hooks "$HOOKS_PAUSE_LONG" | |
else | |
notify-send -i "$ICON_SHORT_PAUSE" -t $LONG_NOTIFICATION_TIME "Short pause :)" | |
pause=$SHORT_PAUSE | |
execute_hooks "$HOOKS_PAUSE_SHORT" | |
fi | |
i=0 | |
while [ $i -lt $pause ]; do | |
if [ $pause -gt $SHORT_PAUSE -a $(($pause-$i)) -ge $SHORT_PAUSE ]; then | |
icon="$ICON_LONG_PAUSE" | |
else | |
icon="$ICON_SHORT_PAUSE" | |
fi | |
notify-send -i "$icon" -t $SHORT_NOTIFICATION_TIME "$(($pause-$i)) minutes left in this pause" | |
sleep $UNIT | |
i=$(($i+1)) | |
echo "Pause: $i/$pause" | |
done | |
done |
Added support for hooks. 4 directories:
$HOME/.pomodoro/hooks/work.d
→ all executables in this directory will be run when a new pomodoro starts$HOME/.pomodoro/hooks/pause.d
→ all executables in this directory will be run when a pause starts$HOME/.pomodoro/hooks/pause-short.d
→ in addition to the "pause" hook, all executables in this directory will be run when a new short pause starts$HOME/.pomodoro/hooks/pause-long.d
→ in addition to the "pause" hook, all executables in this directory will be run when a new long pause starts
Next step: add a few sample hooks, managine iptables for example ;)
Added sample hooks to block/allow Twitter & Facebook during work/pause.
Added support for exit
hook:
$HOME/.pomodoro/hooks/exit.d
→ all executables in this directory will be run when pomodoro is terminated
This allows to "cleanup" work done by other hooks.
TODO: send some context information to hooks.
Added a note about this fraking stupid Chrome.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed labels ordering