Repository: lakshaykalbhor/thirsty Branch: master Commit: cb670b9faa09 Files: 4 Total size: 4.2 KB Directory structure: gitextract_pe5fd64b/ ├── LICENSE ├── README.md ├── thirsty.sh └── thirsty_test.sh ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2017 Lakshay Kalbhor Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Thirsty > zsh/bash script to remind you to drink water. If you're like me and you spend a lot of time programming with the command line open, chances are that you forget about everything for hours, which includes drinking water. **At the end of the day I used to realise that I had not consumed enough water.** Hence I created a bash/zsh script to remind me to drink water right 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. 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. Set your time interval in the variable `WATER_TIME` in seconds, eg : `WATER_TIME=1200` sets it to 20 mins. 5. Once notified, you can remove the message by simply typing `not_thirsty` in your command line. ================================================ FILE: thirsty.sh ================================================ #!/bin/sh WATER_TIME=${WATER_TIME:-1200} # Set time interval in seconds DRINK_WATER_CONF="${DRINK_WATER_CONF:-$HOME/.water}" drink_water() { # If the file doesn't exist, create it if [ ! -f $DRINK_WATER_CONF ]; then date +%s > $DRINK_WATER_CONF fi # Tail is used to remain compatible with the pervious file format next_time=$(($(tail -1 $DRINK_WATER_CONF) + $WATER_TIME)) if [ $next_time -lt $(date +%s) ]; then echo -n "💧 You're thirsty" fi } not_thirsty() { date +%s > $DRINK_WATER_CONF echo "Water is essential" } next_drink() { next_time=$(($(cat $DRINK_WATER_CONF) + $WATER_TIME)) next_date="" # Mac's date command uses a different flag case "$(uname)" in 'Darwin') next_date="$(date -r $next_time)" ;; *) next_date="$(date --date="@$next_time")" ;; esac echo "Next drink at $next_date" } ================================================ FILE: thirsty_test.sh ================================================ #!/bin/sh # Override default water time WATER_TIME=5 DRINK_WATER_CONF=$PWD/test.water rm -f "$DRINK_WATER_CONF" ./thirsty.sh echo "Running tests..." # Test that drink_water returns nothing out="$(drink_water)" if [ -n "$out" ]; then echo "drink_water error. Expected '', got '$out'" exit 1 fi sleep "$($WATER_TIME + 1)" # Test that drink_water tells us it's time for a drink out="$(drink_water)" expected="💧 You're thirsty" if [ "$out" != "$expected" ]; then echo "drink_water error. Expected '$expected', got '$out'" exit 1 fi # Test that not_thirsty sets a new time before="$(cat "$DRINK_WATER_CONF")" out="$(not_thirsty)" expected="Water is essential" if [ "$out" != "$expected" ]; then echo "not_thirsty error. Expected '$expected', got '$out'" exit 1 fi if [ "$before" = "$(cat "$DRINK_WATER_CONF")" ]; then echo "not_thirsty error. Last drink time was not updated" exit 1 fi # Test that next_drink outputs correctly out="$(next_drink)" expected="Next drink at $(date --date="@$(($(cat "$DRINK_WATER_CONF") + "$WATER_TIME"))")" if [ "$out" != "$expected" ]; then echo "next_drink error. Expected '$expected', got '$out'" exit 1 fi rm -f "$DRINK_WATER_CONF" echo "All tests passed"