updates track to support additive logs

This commit is contained in:
James Tomasino 2024-04-15 15:40:32 +00:00
parent d740d8144e
commit d2212ef6f4
2 changed files with 37 additions and 10 deletions

45
track
View File

@ -2,7 +2,7 @@
# This file defines track - a minimalist data tracker
# Copyright (C) 2020 James Tomasino
# Copyright (C) 2024 James Tomasino
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -19,7 +19,7 @@
#-----------------------------------------------------------------------
version="2020.10.11"
version="2024.04.15"
show_help () {
cat > /dev/stdout << END
@ -32,6 +32,7 @@ DIRECTORY:
USAGE:
track weight 150 Log 150lbs for today's weight
track steps +500 Increase the numeric log of today's steps by 500
track -n 10 calories Show last 10 days calorie values
track mood Show last value logged for mood
@ -50,7 +51,7 @@ END
# Determine tracking directory and ensure it is a directory
TRACK_DIR=${TRACK_DIR:-.}
if [ -f "$TRACK_DIR" ]; then
TRACK_DIR="$(dirname ${TRACK_DIR})"
TRACK_DIR="$(dirname "${TRACK_DIR}")"
fi
arg_metric=""
@ -170,15 +171,41 @@ main () {
printf "year,month,day,%s\\n" "${arg_metric}" > "$f"
fi
if [ "${arg_value}" != "" ]; then
# Get arg parts
first_letter=$(printf %.1s "${arg_value}")
if [ "${arg_value}" != "" ]; then # add/update
d=$(date '+%Y,%0m,%0d')
l=$(sed -n "/$d/{=;}" "$f")
if [ -z "$l" ]; then
printf "%s,%s\\n" "$d" "${arg_value}" >> "$f"
else
sed -i"" "$l c $d,${arg_value}" "$f"
if [ "${first_letter}" = "+" ]; then # Add to existing value
rest="${arg_value#+}"
if [ -z "$l" ]; then
printf "%s,%s\\n" "$d" "${rest}" >> "$f"
else
val="$(sed "${l}q;d" "$f" | awk -F "," '{print $4}')" # get current value
# check if existing value is numeric before attempting to sum
if printf "%s" "$val" | grep -vEq '^[0-9]+([.][0-9]+)?$'; then
printf "Current value is not numeric. Abort.\\n"-
exit 1
fi
# check if new value is numeric before attempting to sum
if printf "%s" "$rest" | grep -vEq '^[0-9]+([.][0-9]+)?$'; then
printf "New value is not numeric. Abort.\\n"-
exit 1
fi
# sum new and previous values
newval=$((val+rest)) || exit 1
sed -i"" "$l c $d,${newval}" "$f"
fi
else # Replace existing value
if [ -z "$l" ]; then
printf "%s,%s\\n" "$d" "${arg_value}" >> "$f"
else
sed -i"" "$l c $d,${arg_value}" "$f"
fi
fi
else
else # display value
if [ ${flag_listall} -gt 0 ]; then
if [ ${flag_csv} -gt 0 ]; then
awk -F "," '{printf "%02d-%02d-%02d,%s\n", $1, $2, $3, $4;}' "$f"

View File

@ -1,4 +1,4 @@
.TH TRACK 1 "11 Oct 2020" "v2020.10.11"
.TH TRACK 1 "15 Apr 2024" "v2024.04.15"
.SH NAME
track \- a minimalistic data tracker