Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting, compatibility, and simplification #4

Merged
merged 2 commits into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ If you're like me and you spend a lot of your time programming with the command
**At the end of the day I would find out that I haven't had enough water.**
Hence I created a bash script that reminds me to drink water on my command line.


## How to use
1. Copy the [thirsty.sh](thirsty.sh) file and paste it in your zshrc or bashrc or theme, etc.
1. Copy the [thirsty.sh](thirsty.sh) file and paste it in your zshrc or bashrc or theme, etc.
2. In your zshrc/theme/bashrc locate `PROMPT='$(some_function)'` or `PROMPT_COMMAND='$(some_function)'` for bash and add `drink_water` inside that function. *(This will be the case if you're using a theme)*
3. If you can't locate `PROMPT`, then just add `PROMPT='$(drink_water)` or `PROMPT_COMMAND='$(drink_water)` for bash in the file.
4. Create two files `touch $HOME/.water $HOME/.water_last_time`
5. Set your time interval in the variable `water_time` in seconds, eg : `water_time=72000` sets it to 20 mins.
6. Once notified, you can remove the message by simply typing `not_thirsty` in your command line.
4. Set your time interval in the variable `WATER_TIME` in seconds, eg : `WATER_TIME=72000` sets it to 20 mins.
5. Once notified, you can remove the message by simply typing `not_thirsty` in your command line.

## Contribute
## Contribute
Since my knowledge on bash/zsh is limited, I'd be more than willing to accept improvements and additional features in this script.
34 changes: 21 additions & 13 deletions thirsty.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
water_time=10800 # Set time interval in seconds
#!/bin/sh

WATER_TIME=${WATER_TIME:-10800} # Set time interval in seconds
DRINK_WATER_CONF="$HOME/.water"

drink_water() {
thirsty=`cat $HOME/.water`
last_time=`cat $HOME/.water_last_time`
# If the file doesn't exist, create it
if [ ! -f $DRINK_WATER_CONF ]; then
not_thirsty
fi

if [[ $thirsty == false ]]; then
echo "$[$(date +%s) + $water_time]" > $HOME/.water_last_time
echo "true" > $HOME/.water
echo -n " Water is essential "
thirsty=$(head -1 $DRINK_WATER_CONF)
last_time=$(tail -1 $DRINK_WATER_CONF)

elif [[ $[last_time] -lt $(date +%s) ]]; then
if [ $thirsty = 'false' ]; then
echo "true" > $DRINK_WATER_CONF
echo "$(($(date +%s) + $WATER_TIME))" >> $DRINK_WATER_CONF
echo -n "Water is essential "
elif [ $last_time -lt $(date +%s) ]; then
echo -n "💧 You're thirsty"
fi
}

not_thirsty() {
echo "false" > $HOME/.water
echo "0" > $HOME/.water_last_time
echo "false" > $DRINK_WATER_CONF
echo "0" >> $DRINK_WATER_CONF
}

# Username.
# If user is root, then pain it in red. Otherwise, just print in yellow.
# If user is root, then print it in red. Otherwise, just print in yellow.
spaceship_user() {
if [[ $USER == 'root' ]]; then
if [ $USER = 'root' ]; then
echo -n "%{$fg_bold[red]%}"
else
echo -n "%{$fg_bold[yellow]%}"
fi
echo -n "%n"
echo -n "%{$reset_color%}"
}
}
17 changes: 17 additions & 0 deletions thirsty_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

# Override default water time
WATER_TIME=5

. ./thirsty.sh

echo "Water time: $WATER_TIME"

drink_water
echo
not_thirsty
drink_water
echo
sleep $(($WATER_TIME + 1))
drink_water
echo