Fixed water script and purged old files
parent
1a1ad62a9a
commit
d48eef33ef
@ -1,75 +1,160 @@
|
|||||||
#!/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
|
||||||
# tracking resets everyday at midnight local time
|
usage() {
|
||||||
# to inc/dec water consumed just call water XX where XX is any integer amount of ounces to change your total by
|
cat <<EOF
|
||||||
# it is really only ever exepected to use negatives to fix mistakes
|
usage: $0 [-c|r|s|u|h] [WATER]
|
||||||
# the file writes like a log where the last entry is total consumed and the difference between two consecutive entries is the water consumed in that entry
|
|
||||||
# i.e
|
Options:
|
||||||
# 10:00:01 0 starts off at 0 in a new day
|
-c, --colorize enables tmux color options
|
||||||
# 10:14:21 16 drank 16 ounces of water so it goes up
|
-s, --symbol enables nerdfont compaitable symbol
|
||||||
# 11:10:21 48 drank 32 more ounces of water so new total is 48
|
-r, --reset reset the days water to 0
|
||||||
|
-u, --undo removes the last entry
|
||||||
# setting up vars
|
can be done repeatedly
|
||||||
declare waterfile
|
-h, --help display this message
|
||||||
declare -i water_intake
|
|
||||||
declare output
|
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.
|
||||||
curtime=$(timedatectl | grep Local | cut -d '-' -f 2-) # getting date to MM-DD HH:MM:SS TMZ
|
EOF
|
||||||
date=$(echo $curtime | awk '{print $1}') # for filename
|
|
||||||
time=$(echo $curtime | awk '{print $2}') # for timestamp
|
|
||||||
|
|
||||||
waterfile="$HOME/.dotfiles/bin/water/waterintake/$date" # makes it easy to reset on each new day
|
|
||||||
|
|
||||||
mkdir -p "$HOME/.dotfiles/bin/water/waterintake"
|
|
||||||
|
|
||||||
# update func
|
|
||||||
update() {
|
|
||||||
printf '%s: %s\n' "$time" "$water_intake" >> "$waterfile"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# getting current water level
|
DATE=$(date +%b_%d_%y)
|
||||||
get_water_intake() {
|
TIME=$(date +%H:%M:%S)
|
||||||
if [[ -f $waterfile ]] ; then
|
|
||||||
water_intake=$(cat $waterfile | tail -n 1 | awk '{print $2}')
|
WATER_DIR="$HOME/.dotfiles/bin/water"
|
||||||
|
WATERFILE="${WATER_DIR}/${DATE}.csv" # makes it easy to reset on each new day
|
||||||
|
|
||||||
|
# make dir on fresh installs
|
||||||
|
if [[ ! -d "${WATER_DIR}/archive" ]] ; then
|
||||||
|
mkdir -p "${WATER_DIR}/archive"
|
||||||
|
fi
|
||||||
|
|
||||||
|
get_current_amt() {
|
||||||
|
# gets the current water intake
|
||||||
|
|
||||||
|
if [[ ! -f "$WATERFILE" ]] ; then
|
||||||
|
# no waterfile
|
||||||
|
|
||||||
|
# archiving old file
|
||||||
|
mv *.csv ${WATER_DIR}/archive
|
||||||
|
# creating new file for the day
|
||||||
|
printf 'Time, Change, Running Total\n' > "$WATERFILE"
|
||||||
|
printf '%s, 0, 0\n' "$TIME" >> "$WATERFILE"
|
||||||
else
|
else
|
||||||
water_intake=0
|
# waterfile exists
|
||||||
|
CURRENT_WATER=$(cat $WATERFILE | tail -n 1 | awk '{print $3}')
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
reset_water_intake() {
|
update_water() {
|
||||||
water_intake=0
|
# updates water based on $WATER_CHANGE
|
||||||
update
|
CURRENT_WATER=$(($CURRENT_WATER + $WATER_CHANGE))
|
||||||
|
|
||||||
|
# putting in file
|
||||||
|
printf '%s, %d, %d\n' "$TIME" $WATER_CHANGE $CURRENT_WATER >> "$WATERFILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
colorize_water_intake() {
|
undo_change() {
|
||||||
|
# removes the last entry up to the first
|
||||||
|
|
||||||
|
if [[ $(wc -l < "$WATERFILE") -gt 2 ]] ; then
|
||||||
|
# removing last entry
|
||||||
|
lastentry=$(cat "$WATERFILE" | tail -n 1)
|
||||||
|
ts=$(echo "$lastentry" | awk -F , '{print $1}')
|
||||||
|
amt=$(echo "$lastentry" | awk -F , '{print $2}')
|
||||||
|
prompt=$(printf 'Are you sure you want to remove the %d fl oz added at %s?(y/n)\n' $amt "$ts")
|
||||||
|
read -p "$prompt" -n 1 -r
|
||||||
|
|
||||||
|
if [[ $REPLY =~ ^[Yy]$ ]] ; then
|
||||||
|
# remove
|
||||||
|
sed -i '$d' "$WATERFILE"
|
||||||
|
fi
|
||||||
|
echo ""
|
||||||
|
get_current_amt
|
||||||
|
else
|
||||||
|
echo "No changes to undo!"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
colorize() {
|
||||||
# sends water intake level through conditionals to be colorized
|
# sends water intake level through conditionals to be colorized
|
||||||
dflt='#[default]'
|
|
||||||
if [[ $water_intake -lt 32 ]] ; then
|
if [[ $1 -lt 32 ]] ; then
|
||||||
color='#[fg=color1]' # red
|
color='#[fg=color1]' # red
|
||||||
elif [[ $water_intake -lt 64 ]] ; then
|
elif [[ $1 -lt 64 ]] ; then
|
||||||
color='#[fg=color3]' # bad yellow
|
color='#[fg=color3]' # bad yellow
|
||||||
elif [[ $water_intake -lt 96 ]] ; then
|
elif [[ $1 -lt 96 ]] ; then
|
||||||
color='#[fg=color226]' # pale yellow
|
color='#[fg=color226]' # pale yellow
|
||||||
elif [[ $water_intake -lt 128 ]] ; then
|
elif [[ $1 -lt 128 ]] ; then
|
||||||
color='#[fg=color10]' # light green
|
color='#[fg=color10]' # light green
|
||||||
else
|
else
|
||||||
color='#[fg=color21]' # blue
|
color='#[fg=color21]' # blue
|
||||||
fi
|
fi
|
||||||
output="${color}\uf6aa ${water_intake}${dflt}"
|
|
||||||
|
printf '%s' "$color"
|
||||||
}
|
}
|
||||||
|
|
||||||
# checking for/creating missing files
|
print_water() {
|
||||||
if [[ ! -f $waterfile ]] ; then
|
# prints total
|
||||||
# water intake file doesn't exist
|
if [ "$COLORIZE" = true ] ; then
|
||||||
reset_water_intake
|
dflt="#[default]"
|
||||||
fi
|
printf '%s' "$(colorize $CURRENT_WATER)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$SYMBOL" = true ] ; then
|
||||||
|
printf '%s ' $(echo -e '\uf6aa')
|
||||||
|
fi
|
||||||
|
|
||||||
# evaluating arguements
|
printf '%d%s\n' $CURRENT_WATER "$dflt"
|
||||||
get_water_intake
|
}
|
||||||
if [[ -z $1 ]] ; then
|
|
||||||
colorize_water_intake # optional color codes based on total consumption
|
# handle long forms
|
||||||
echo -e $output # no args is basic echo
|
for arg in "$@"; do
|
||||||
else
|
shift
|
||||||
water_intake="$((water_intake + $1))" # provided value to inc/dec water intake by
|
case "$arg" in
|
||||||
update
|
'--help') set -- "$@" "-h" ;;
|
||||||
|
'--colorize') set -- "$@" "-c" ;;
|
||||||
|
'--reset') set -- "$@" "-r" ;;
|
||||||
|
'--undo') set -- "$@" "-u" ;;
|
||||||
|
'--symbol') set -- "$@" "-s" ;;
|
||||||
|
*) set -- "$@" "$arg" ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
get_current_amt
|
||||||
|
|
||||||
|
# parsing args
|
||||||
|
while getopts "hcrsu" opt ; do
|
||||||
|
case "$opt" in
|
||||||
|
'h' )
|
||||||
|
usage
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
'c' )
|
||||||
|
COLORIZE=true
|
||||||
|
;;
|
||||||
|
's' )
|
||||||
|
SYMBOL=true
|
||||||
|
;;
|
||||||
|
'u' )
|
||||||
|
undo_change
|
||||||
|
;;
|
||||||
|
'r' )
|
||||||
|
reset_water
|
||||||
|
;;
|
||||||
|
'?' )
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
shift $(($OPTIND - 1))
|
||||||
|
|
||||||
|
WATER_CHANGE=$1
|
||||||
|
if [[ ! -z "$WATER_CHANGE" ]] ; then
|
||||||
|
# prevent empty/0 updates
|
||||||
|
update_water
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
print_water
|
||||||
|
@ -1 +0,0 @@
|
|||||||
Subproject commit b699a7e01c253ffb7818b02d62bce24190ec1019
|
|
Loading…
Reference in New Issue