Fixed water script and purged old files

nvim
KeeganForelight 2 years ago
parent 1a1ad62a9a
commit d48eef33ef

3
.gitignore vendored

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

@ -3,18 +3,17 @@
#set -e #set -e
usage() { usage() {
# basic usage statement
cat <<EOF cat <<EOF
Usage: bb [-c|f|s|h|r [option]] usage: $0 [-c|f|s|h|r [option]]
Queries Bluebikes API to get recent bike status Queries Bluebikes API to get recent bike status
Options: Options:
-c, --colorize adds tmux color options -c, --colorize enables tmux color options
-f, --force force refreshes bb data -f, --force force refreshes bb data
-s, --search pulls up a searchable fzf menu to update -s, --search pulls up a searchable fzf menu to update
displayed station displayed station
-r, --rename[=NAME] rename the current station -r, --rename [NAME] rename the current station
stored in data/alias.json stored in data/alias.json
if called without an arguement resets to default if called without an arguement resets to default
-h, --help display this message -h, --help display this message
@ -116,8 +115,12 @@ set_station_alias() {
OUT=$(cat "$ALIAS_FILE" | gojq ".$FMT_STATIONID |= $FMT_ALIAS") OUT=$(cat "$ALIAS_FILE" | gojq ".$FMT_STATIONID |= $FMT_ALIAS")
echo "$OUT" > "$ALIAS_FILE" echo "$OUT" > "$ALIAS_FILE"
printf 'Updated alias to %s\n' "$ALIAS" # printing change
if [[ -z "$ALIAS" ]] ; then
printf 'Removing alias\n'
else
printf 'Updated alias to %s\n' "$ALIAS"
fi
} }
@ -181,46 +184,55 @@ print_status() {
BIKES=$(colorize $BIKES) BIKES=$(colorize $BIKES)
DOCKS=$(colorize $DOCKS) DOCKS=$(colorize $DOCKS)
fi fi
printf '%s  %s  %s%s' $BIKES $DOCKS "$DEFAULT" "$STATION_NAME" printf '%s  %s  %s%s\n' $BIKES $DOCKS "$DEFAULT" "$STATION_NAME"
} }
# always check/clean status # always check/clean status
check_update check_update
if [[ $# -eq 0 ]] ; then # change long form
# no args passed, default to print for arg in "$@"; do
print_status shift
fi case "$arg" in
'--help') set -- "$@" "-h" ;;
'--search') set -- "$@" "-s" ;;
'--force') set -- "$@" "-f" ;;
'--rename') set -- "$@" "-r" ;;
'--colorize') set -- "$@" "-c" ;;
*) set -- "$@" "$arg" ;;
esac
done
while [[ $# -gt 0 ]] ; do # parsing args
# loop over args if present while getopts "hsfcr" opt ; do
case $1 in case "$opt" in
-h | --help) 'h' )
usage usage
exit 0 exit 0
;; ;;
-s | --search) 's' )
update_station update_station
;; ;;
-f | --force) 'f' )
UPDATE=true UPDATE=true
output_status
;; ;;
-r | --rename) 'c' )
shift
ALIAS="$1"
set_station_alias
;;
-c | --colorize)
COLORIZE=true COLORIZE=true
print_status
;; ;;
*) 'r' )
echo "Error: bb" >&2 nextopt=${!OPTIND}
if [[ -n "$nextopt" && "$nextopt" != -* ]] ; then
ALIAS="$nextopt"
fi
set_station_alias
;;
'?' )
usage usage
exit 1 exit 1
;; ;;
esac esac
shift # shift over the args before loop
done done
# finish by printing
print_status

@ -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

@ -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) |" set -g status-left "#(~/.dotfiles/bin/weather/weather) | #(~/.dotfiles/bin/bluebikes/bb -c) | #(~/.dotfiles/bin/water/water.sh -sc) |"
set -g status-left-length 100 set -g status-left-length 100
# tmux auto start # tmux auto start

@ -1 +0,0 @@
Subproject commit b699a7e01c253ffb7818b02d62bce24190ec1019
Loading…
Cancel
Save