Skip to content

Instantly share code, notes, and snippets.

@naholyr
Created December 21, 2011 15:09
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save naholyr/1506349 to your computer and use it in GitHub Desktop.
Save naholyr/1506349 to your computer and use it in GitHub Desktop.
Bash Pomodoro using libnotify
#!/bin/bash
# Destination: ~/.pomodoro/hooks/work.d/
# Goal: block access to Twitter and Facebook while in a Pomodoro
# Note: won't work if you use Chrome, as it maintains an internal (not flushable from CLI) DNS cache. Fuck it.
# This file must be able to touch "/etc/hosts":
# The best way would be to create a group able to modify the "/etc/hosts" file:
# $ sudo addgroup host-manager
# $ sudo chgrp host-manager /etc/hosts
# $ sudo chmod g+rw /etc/hosts
# Then you add yourself to this group (logout/login again to apply):
# $ sudo usermod --append --groups host-manager $USER
cp /etc/hosts ./hosts.bak
if ! grep -F '# Pomodoro: Block Social network' /etc/hosts &> /dev/null; then
echo 'Block social networks!'
echo '# Pomodoro: Block Social network, keep this comment and the single line below' >> /etc/hosts
echo '0.0.0.0 twitter.com www.twitter.com facebook.com www.facebook.com s-static.ak.facebook.com fbcdn.net s-external.ak.fbcdn.net' >> /etc/hosts
else
echo 'Social networks already blocked.'
notify-send -i warning "Error in 'Block social networks'" "Social networks already blocked\nYou should check /etc/hosts manually."
fi
#!/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
#!/bin/bash
# Destination: ~/.pomodoro/hooks/pause.d/ and ~/.pomodoro/hooks/exit.d/ (symlink)
# Goal: restore access to Twitter and Facebook while in a pause (reverts modifications done by "block-social-networks.sh")
# Note: won't work if you use Chrome, as it maintains an internal (not flushable from CLI) DNS cache. Fuck it.
# This file must be able to touch "/etc/hosts":
# The best way would be to create a group able to modify the "/etc/hosts" file:
# $ sudo addgroup host-manager
# $ sudo chgrp host-manager /etc/hosts
# $ sudo chmod g+rw /etc/hosts
# Then you add yourself to this group (logout/login again to apply):
# $ sudo usermod --append --groups host-manager $USER
grep -n '^# Pomodoro: Block Social network' /etc/hosts 2> /dev/null | while read search; do
cp /etc/hosts ./hosts.bak
line_n=$(echo "$search" | sed 's/\([0-9]\+\):.*$/\1/')
next_line=$(head -n +$(($line_n)) | tail -n 1 /etc/hosts)
if grep '^0\.0\.0\.0' /etc/hosts &> /dev/null; then
echo 'Allow social networks!'
f=$(tempfile)
head -n $(($line_n - 1)) /etc/hosts > $f
tail -n +$(($line_n + 2)) /etc/hosts >> $f
cp $f /etc/hosts
rm -f $f
else
echo "Error: unexpected content at line $(($line_n + 1))"
echo "You should check your /etc/hosts manually"
notify-send -i error -u critical "Error in 'Allow social networks'" "Unexpected content in /etc/hosts at line $(($line_n + 1))\nYou should check this file manually"
fi
done
@naholyr
Copy link
Author

naholyr commented Dec 21, 2011

Fixed labels ordering

@naholyr
Copy link
Author

naholyr commented Dec 21, 2011

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 ;)

@naholyr
Copy link
Author

naholyr commented Dec 23, 2011

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.

@naholyr
Copy link
Author

naholyr commented Dec 23, 2011

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