reworked water script to add filling bottle and a pop up in tmux

nvim
KeeganForelight 2 years ago
parent 8a61bf7af2
commit 2232aa2108

3
.gitignore vendored

@ -1,8 +1,11 @@
tmux/resurrect tmux/resurrect
tmux/plugins tmux/plugins
!tmux/plugins/tpm !tmux/plugins/tpm
bin/water/archive bin/water/archive
bin/water/*.csv bin/water/*.csv
bin/water/.reminder
bin/weather/.env* bin/weather/.env*
*.swp *.swp
bin/startup/*.json bin/startup/*.json

@ -1,23 +1,45 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# this script enables water tracking in tmux status bar via a simple command # this script enables water tracking in tmux status bar via a simple command
usage() { usage() {
cat <<EOF cat <<EOF
usage: $0 [-c|r|s|u|h] [WATER] usage: $0 [-chr][-s|S][-u|U QTY] [WATER]
Options: Options:
-c, --colorize enables tmux color options -c, --colorize enables tmux color options
-s, --symbol enables nerdfont compaitable symbol -s, --symbol enables static nerdfont compaitable symbol
-r, --reset reset the days water to 0 -S enables dynamic nf symbol
-u, --undo removes the last entry -u, --undo removes the last entry
can be done repeatedly -U removes the last QTY entries.
use all as QTY to remove all entries
-r, --reminder creates a tmux pop up to remind you to hydrate
-h, --help display this message -h, --help display this message
The reminder works based on specified water goals
Defaults to 32 ounces by 10 am, 64 by 1 pm, 96 by 5 pm, and 128 by 8 pm
Goals are defined in the script itself.
If a sub goal is not reached, the reminder will trigger
Reminders have a dismiss option as well as an entry box to enter the water drank
Reminders will pop up every 30 minutes until the goal is reached
The WATER arguement is an integer and will be added to the current total. The WATER arguement is an integer and will be added to the current total.
If a mistake is made, -u can remove the faulty entry. If a mistake is made, -u can remove the faulty entry.
EOF EOF
exit $1
} }
# Hydration thresholds for reminders, all in fl. oz.
# Triggers reminder if you aren't at GOAL#/INTAKE by the assocaited time
# so in this example, if I don't drink 32 ounces by 10 am, I will get an alert
# 64 ounces by 1 pm, etc.
####################################
GOAL_AMT=128
GOALS=( "10:00" "13:00" "16:00" "20:00" )
SNOOZE=$((30 * 60)) # time between reminders (s)
####################################
DATE=$(date +%b_%d_%y) DATE=$(date +%b_%d_%y)
TIME=$(date +%H:%M:%S) TIME=$(date +%H:%M:%S)
@ -29,6 +51,51 @@ if [[ ! -d "${WATER_DIR}/archive" ]] ; then
mkdir -p "${WATER_DIR}/archive" mkdir -p "${WATER_DIR}/archive"
fi fi
check_reminder() {
# checks for reminder
# uses tmux to push reminder
if [[ -z "$TMUX" ]] ; then
echo "TMUX required for reminder!"
exit 1
fi
# this will round up
goal_incr=$((($GOAL_AMT + ${#GOALS[@]}-1) / ${#GOALS[@]}))
# based on water drank, time to check
cur_indx=$(($CURRENT_WATER / $goal_incr))
if [[ $CURRENT_WATER -lt $GOAL_AMT ]] ; then
# goals not finished, check pace
time_unix=$(date -d "$TIME" +%s)
expired=$(( $(date -d "${GOALS[$cur_indx]}" +%s) - $time_unix))
if [[ $expired -lt 0 ]] ; then
# goal missed, check snooze
source "$WATER_DIR/.reminder" 2>/dev/null
# check elapsed
if [[ $time_unix -gt $REMINDER_SNOOZE ]] ; then
# trigger reminder
goal=$(( ($GOAL_AMT * ($cur_indx + 1)) / ${#GOALS[@]}))
fmt_time=$(date -d "${GOALS[$cur_indx]}" "+%l:%M %P")
rem_info=$(printf 'You have only drank %d fl. oz. while your goal is to drink %d fl. oz. by %s!\n\nThis reminder will reappear in %d minutes unless you consume fluids!'\
$CURRENT_WATER $goal "$fmt_time" $(($SNOOZE/60)))
tmux display-popup -E\
dialog\
--title 'DEHYDRATION ALERT'\
--ok-label 'Dismiss'\
--msgbox \
"$rem_info" 0 0
# update last_reminder
printf 'REMINDER_SNOOZE=%d\n' $(($(date +%s) + $SNOOZE)) > "$WATER_DIR/.reminder"
fi
fi
fi
}
get_current_amt() { get_current_amt() {
# gets the current water intake # gets the current water intake
@ -36,7 +103,7 @@ get_current_amt() {
# no waterfile # no waterfile
# archiving old file # archiving old file
mv *.csv ${WATER_DIR}/archive mv ${WATER_DIR}/*.csv ${WATER_DIR}/archive
# creating new file for the day # creating new file for the day
printf 'Time, Change, Running Total\n' > "$WATERFILE" printf 'Time, Change, Running Total\n' > "$WATERFILE"
printf '%s, 0, 0\n' "$TIME" >> "$WATERFILE" printf '%s, 0, 0\n' "$TIME" >> "$WATERFILE"
@ -44,6 +111,8 @@ get_current_amt() {
# waterfile exists # waterfile exists
CURRENT_WATER=$(cat $WATERFILE | tail -n 1 | awk '{print $3}') CURRENT_WATER=$(cat $WATERFILE | tail -n 1 | awk '{print $3}')
fi fi
} }
update_water() { update_water() {
@ -54,26 +123,42 @@ update_water() {
printf '%s, %d, %d\n' "$TIME" $WATER_CHANGE $CURRENT_WATER >> "$WATERFILE" printf '%s, %d, %d\n' "$TIME" $WATER_CHANGE $CURRENT_WATER >> "$WATERFILE"
} }
undo_change() { undo_changes() {
# removes the last entry up to the first # removes specifed entries
if [[ $(wc -l < "$WATERFILE") -gt 2 ]] ; then # gets the lines of entries
# removing last entry WATER_LINES=$(($(wc -l < "$WATERFILE") - 2))
lastentry=$(cat "$WATERFILE" | tail -n 1) if [[ $WATER_LINES -gt 0 ]] ; then
ts=$(echo "$lastentry" | awk -F , '{print $1}') # removing entries
amt=$(echo "$lastentry" | awk -F , '{print $2}') if [[ "$1" == "all" || "$1" == "ALL" ]] ; then
prompt=$(printf 'Are you sure you want to remove the %d fl oz added at %s?(y/n)\n' $amt "$ts") # force a reset
read -p "$prompt" -n 1 -r REMOVAL=$WATER_LINES
elif [[ $1 =~ ^[0-9]+$ ]] ; then
# remove $1 changes
REMOVAL=$1
else
printf 'Arguement %s not recognized\n' "$1"
usage
exit 1
fi
choice=$(bash -c "read -p 'Are you sure you want to remove the changes? (y/n)' -n 1 -r c; echo \$c")
echo ""
if [[ $REPLY =~ ^[Yy]$ ]] ; then if [[ $REPLY =~ ^[Yy]$ ]] ; then
# remove # remove
sed -i '$d' "$WATERFILE" if [[ $WATER_LINES -gt $REMOVAL ]] ; then
# removing specified lines
last_line=$(($WATER_LINES - $1 + 1)) # account for offset
sed -i "$last_line ,$ d" "$WATERFILE"
else
# essentially a reset
printf 'Time, Change, Running Total\n' > "$WATERFILE"
printf '%s, 0, 0\n' "$TIME" >> "$WATERFILE"
fi
fi fi
echo ""
get_current_amt
else
echo "No changes to undo!"
fi fi
echo "Done!\n"
} }
colorize() { colorize() {
@ -96,16 +181,45 @@ colorize() {
print_water() { print_water() {
# prints total # prints total
# dynamic symbol, uses battery because close enough
# colors blue to avoid mistaking
if [ "$DYN_SYMBOL" = true ] ; then
lvl=$((($CURRENT_WATER * 10) / $GOAL_AMT)) # rounds down to nearst 10%
# base = full
SYM='\uf578'
if [[ $lvl -eq 0 ]] ; then
SYM='\uf58d'
elif [[ $lvl -lt 9 ]] ; then
# leverage consecutive symbols
SYM=$(printf 'f5%x' $((120+$lvl)))
SYM=$(echo -e "\u$SYM")
fi
if [ "$COLORIZE" = true ] ; then
clr="#[fg=color21]"
fi
printf '%s%s ' "$clr" "$SYM"
fi
if [ "$COLORIZE" = true ] ; then if [ "$COLORIZE" = true ] ; then
dflt="#[default]" dflt="#[default]"
printf '%s' "$(colorize $CURRENT_WATER)" printf '%s' "$(colorize $CURRENT_WATER)"
fi fi
# static symbol
if [ "$SYMBOL" = true ] ; then if [ "$SYMBOL" = true ] ; then
printf '%s ' $(echo -e '\uf6aa') printf '\uf6aa '
fi fi
printf '%d%s\n' $CURRENT_WATER "$dflt" printf '%d%s\n' $CURRENT_WATER "$dflt"
# checks for a reminder
if [[ "$REMINDER" = true ]] ; then
# reminders enabled
check_reminder
fi
} }
# handle long forms # handle long forms
@ -114,7 +228,7 @@ for arg in "$@"; do
case "$arg" in case "$arg" in
'--help') set -- "$@" "-h" ;; '--help') set -- "$@" "-h" ;;
'--colorize') set -- "$@" "-c" ;; '--colorize') set -- "$@" "-c" ;;
'--reset') set -- "$@" "-r" ;; '--reminder') set -- "$@" "-r" ;;
'--undo') set -- "$@" "-u" ;; '--undo') set -- "$@" "-u" ;;
'--symbol') set -- "$@" "-s" ;; '--symbol') set -- "$@" "-s" ;;
*) set -- "$@" "$arg" ;; *) set -- "$@" "$arg" ;;
@ -124,31 +238,40 @@ done
get_current_amt get_current_amt
# parsing args # parsing args
while getopts "hcrsu" opt ; do while getopts "hcrsSuU:" opt ; do
case "$opt" in case "$opt" in
'h' )
usage
exit 0
;;
'c' ) 'c' )
COLORIZE=true COLORIZE=true
;; ;;
's' ) 's' )
SYMBOL=true [ -n "$DYN_SYMBOL" ] && usage 1 || SYMBOL=true
;;
'S' )
[ -n "$SYMBOL" ] && usage 1 || DYN_SYMBOL=true
;; ;;
'u' ) 'u' )
undo_change [ -n "$UNDO" ] && usage 1 || UNDO=1
;;
'U' )
[ -n "$UNDO" ] && usage 1 || UNDO="$optarg"
;; ;;
'r' ) 'r' )
reset_water REMINDER=true
;; ;;
'?' ) 'h' )
usage usage
exit 1 ;;
'?' )
usage 1 >&2
;; ;;
esac esac
done done
# perform undo
if [[ ! -z "$UNDO" ]] ; then
undo_changes "$UNDO"
fi
shift $(($OPTIND - 1)) shift $(($OPTIND - 1))
WATER_CHANGE=$1 WATER_CHANGE=$1

@ -27,7 +27,7 @@ set -g status-interval 10
set -g status-style "bg=black, fg=brightWhite" set -g status-style "bg=black, fg=brightWhite"
set -g status-right "#(~/.dotfiles/bin/docker.sh)| #(~/.dotfiles/bin/agent/checker.sh) | #(~/.tmux/plugins/tmux-mem-cpu-load/tmux-mem-cpu-load -p -i 1 -a 1) | #(~/.dotfiles/bin/up.sh) | %A, %b %d %l:%M %P " set -g status-right "#(~/.dotfiles/bin/docker.sh)| #(~/.dotfiles/bin/agent/checker.sh) | #(~/.tmux/plugins/tmux-mem-cpu-load/tmux-mem-cpu-load -p -i 1 -a 1) | #(~/.dotfiles/bin/up.sh) | %A, %b %d %l:%M %P "
set -g status-right-length 100 set -g status-right-length 100
set -g status-left "#(~/.dotfiles/bin/weather/weather) | #(~/.dotfiles/bin/bluebikes/bb -c) | #(~/.dotfiles/bin/water/water.sh -sc) |" set -g status-left "#(~/.dotfiles/bin/weather/weather) | #(~/.dotfiles/bin/bluebikes/bb -c) | #(~/.dotfiles/bin/water/water.sh -Scr) |"
set -g status-left-length 100 set -g status-left-length 100
# tmux auto start # tmux auto start

Loading…
Cancel
Save