big code cleanup and refactoring

This commit is contained in:
James Tomasino 2020-10-11 13:53:53 +00:00
parent c6525212be
commit baf2e281ff
4 changed files with 103 additions and 132 deletions

View File

@ -1,46 +1,32 @@
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
MANDIR ?= $(PREFIX)/share/man
# Install to /usr/local unless otherwise specified, such as `make PREFIX=/app`
PREFIX?=/usr/local
# Attempt to find bash completion dir in order of preference
ifneq ($(wildcard /etc/bash_completion.d/.),)
CPLDIR ?= /etc/bash_completion.d
endif
# What to run to install various files
INSTALL?=install
# Run to install the actual binary
INSTALL_PROGRAM=$(INSTALL) -Dm 755
# Run to install application data, with differing permissions
INSTALL_DATA=$(INSTALL) -Dm 644
HAS_BREW := $(shell command -v brew 2> /dev/null)
ifdef HAS_BREW
CPLDIR ?= $$(brew --prefix)/etc/bash_completion.d
endif
# Directories into which to install the various files
bindir=$(DESTDIR)$(PREFIX)/bin
sharedir=$(DESTDIR)$(PREFIX)/share
HAS_PKGCONFIG := $(shell command -v pkg-config 2> /dev/null)
ifdef HAS_PKGCONFIG
CPLDIR ?= $$(pkg-config --variable=completionsdir bash-completion 2> /dev/null)
endif
help:
@echo "targets:"
@awk -F '#' '/^[a-zA-Z0-9_-\.]+:.*?#/ { print $0 }' $(MAKEFILE_LIST) \
| sed -n 's/^\(.*\): \(.*\)#\(.*\)/ \1|-\3/p' \
| column -t -s '|'
install:
@echo Installing the executable to $(BINDIR)
@mkdir -p $(BINDIR)
@cp -f track $(BINDIR)/track
@chmod 755 $(BINDIR)/track
@echo Installing the manual page to $(MANDIR)/man1
@mkdir -p $(MANDIR)
@cp -f track.1 $(MANDIR)/man1/track.1
@chmod 644 $(MANDIR)/man1/track.1
ifdef CPLDIR
@echo Installing the command completion to $(CPLDIR)
@mkdir -p $(CPLDIR)
@cp -f track.d $(CPLDIR)/track
@chmod 644 $(CPLDIR)/track
endif
install: track track.1 # system install
$(INSTALL_PROGRAM) track $(bindir)/track
$(INSTALL_DATA) track.1 $(sharedir)/man/man1/track.1
uninstall:
@echo Removing the executable from $(BINDIR)
@rm -f $(BINDIR)/track
@echo Removing the manual page from $(MANDIR)/man1
@rm -f $(BINDIR)/man1/track.1
ifdef CPLDIR
@echo Removing the command completion from $(CPLDIR)
@rm -f $(CPLDIR)/track
endif
uninstall: # system uninstall
rm -f $(bindir)/track
rm -f $(sharedir)/man/man1/track.1
.PHONY: install uninstall
README.txt: track.1 # generate readme file
man ./track.1 | col -bx > README.txt
.PHONY: help install uninstall

72
track
View File

@ -2,7 +2,7 @@
# This file defines track - a minimalist data tracker
# Copyright (C) 2018 tomasino@sdf.org
# Copyright (C) 2020 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,11 +19,11 @@
#-----------------------------------------------------------------------
version="2018.05.29"
version="2020.10.11"
show_help () {
cat > /dev/stdout << END
track [options] [metric] [value]
track [options...] <metric> [<value>]
DIRECTORY:
If the environment variable \$TRACK_DIR is set, track will store
@ -40,18 +40,24 @@ OPTIONS:
-n N Show N values of metric
-x Remove metric file
-c Output CSV
-l List metrics being tracked
-h Show this help
-v Show current version info
-d Debug mode
END
}
# Determine tracking directory and ensure it is a directory
TRACK_DIR=${TRACK_DIR:-.}
if [ -f "$TRACK_DIR" ]; then
TRACK_DIR="$(dirname ${TRACK_DIR})"
fi
arg_metric=""
arg_value=""
arg_listnum=""
arg_remove=""
flag_options="hvdan:x:sc"
flag_options="hvdan:x:lc"
flag_debug=0
flag_help=0
flag_listall=0
@ -61,27 +67,10 @@ flag_version=0
flag_shortlist=0
flag_csv=0
die () {
msg="$1"
code="$2"
# exit code defaults to 1
if printf "%s" "${code}" | grep -q '^[0-9]+$'; then
code=1
fi
# output message to stdout or stderr based on code
if [ ! -z "${msg}" ]; then
if [ "${code}" -eq 0 ]; then
printf "%s\\n" "${msg}"
else
printf "%s\\n" "${msg}" >&2
fi
fi
exit "${code}"
}
parse_input () {
if ! parsed=$(getopt ${flag_options} "$@"); then
die "Invalid input" 2
printf "Invalid input\\n" >&2
exit 2
fi
eval set -- "${parsed}"
@ -116,7 +105,7 @@ parse_input () {
arg_remove="$1"
shift
;;
-s)
-l)
flag_shortlist=1
shift
;;
@ -129,17 +118,19 @@ parse_input () {
break
;;
*)
die "Internal error: $1" 3
printf "Internal error: %s\\n" "$1" >&2
exit 3
;;
esac
done
for arg in "$@"; do
argc=$(printf "%s" "${arg}" | tr '[:upper:]' '[:lower:]')
if [ "${arg_metric}" = "" ]; then
arg_metric="${argc}"
elif [ "${arg_value}" = "" ]; then
arg_value="${argc}"
while [ "$#" -gt 0 ]; do
if [ -z "${arg_metric}" ]; then
arg_metric="$(printf "%s" "${1}" | tr '[:upper:]' '[:lower:]')"
shift
else [ -z "${arg_value}" ]
arg_value="$*"
break
fi
done
}
@ -149,15 +140,18 @@ main () {
if [ ${flag_shortlist} -gt 0 ]; then
out=$(find "${TRACK_DIR}"/*.csv -maxdepth 1 2>/dev/null | sed -e 's/.*\///' | sed -e 's/\..*$//')
die "${out}" 0
printf "%s\\n" "${out}"
exit 0
fi
if [ ${flag_version} -gt 0 ]; then
printf "%s\\n" "${version}"
exit 0
fi
if [ ${flag_help} -gt 0 ]; then
show_help
exit 0
fi
if [ ${flag_debug} -gt 0 ]; then
@ -177,7 +171,7 @@ main () {
fi
if [ "${arg_value}" != "" ]; then
d=$(date '+%Y,%m,%d')
d=$(date '+%Y,%0m,%0d')
l=$(sed -n "/$d/{=;}" "$f")
if [ -z "$l" ]; then
printf "%s,%s\\n" "$d" "${arg_value}" >> "$f"
@ -187,21 +181,21 @@ main () {
else
if [ ${flag_listall} -gt 0 ]; then
if [ ${flag_csv} -gt 0 ]; then
awk -F "," '{printf "%s-%s-%s,%s\n", $1, $2, $3, $4;}' "$f"
awk -F "," '{printf "%02d-%02d-%02d,%s\n", $1, $2, $3, $4;}' "$f"
else
awk -F "," '{printf "%s-%s-%s\t%s\n", $1, $2, $3, $4;}' "$f"
awk -F "," '{printf "%02d-%02d-%02d\t%s\n", $1, $2, $3, $4;}' "$f"
fi
elif [ ${flag_listnum} -gt 0 ]; then
if [ ${flag_csv} -gt 0 ]; then
tail -n "${arg_listnum}" "$f" | awk -F "," '{printf "%s-%s-%s,%s\n", $1, $2, $3, $4;}'
tail -n "${arg_listnum}" "$f" | awk -F "," '{printf "%02d-%02d-%02d,%s\n", $1, $2, $3, $4;}'
else
tail -n "${arg_listnum}" "$f" | awk -F "," '{printf "%s-%s-%s\t%s\n", $1, $2, $3, $4;}'
tail -n "${arg_listnum}" "$f" | awk -F "," '{printf "%02d-%02d-%02d\t%s\n", $1, $2, $3, $4;}'
fi
else
if [ ${flag_csv} -gt 0 ]; then
tail -n 1 "$f" | awk -F "," '{printf "%s-%s-%s,%s\n", $1, $2, $3, $4;}'
tail -n 1 "$f" | awk -F "," '{printf "%02d-%02d-%02d,%s\n", $1, $2, $3, $4;}'
else
tail -n 1 "$f" | awk -F "," '{printf "%s-%s-%s\t%s\n", $1, $2, $3, $4;}'
tail -n 1 "$f" | awk -F "," '{printf "%02d-%02d-%02d\t%s\n", $1, $2, $3, $4;}'
fi
fi
fi

69
track.1
View File

@ -1,13 +1,12 @@
.TH TRACK 1 "29 May 2018" "v2018.05.29"
.TH TRACK 1 "11 Oct 2020" "v2020.10.11"
.SH NAME
track \- a minimalistic data tracker
.SH SYNOPSIS
.B track [options] [metric] [value]
.B track [options] <metric> [<value>]
.P
Setting the
.B $TRACK_DIR
environment variable will change the default location where the data is stored.
If it is not set the current directory is used.
.SH DESRIPTION
Using the command line,
.B track
@ -15,21 +14,13 @@ allows you to enter one and only one data point on any metric per day. It's
great for tracking your weight or how many cups of coffee you drank.
.B track
logs your data for each given metric to a CSV file. If you track the same
metric twice in the same day, the data is updated. There's a few handy options
for printing out the list data in useful formats as well.
.SH USAGE
.TP
.B track weight 150
Log 150lbs for today's weight
.TP
.B track -n 10 calories
Show last 10 days calorie values
.TP
.B track mood
Show last value logged for mood
.SH DEPENDENCIES
None.
metric twice in the same day, the data is updated. Data need not be numeric.
.SH OPTIONS
Setting the
.B $TRACK_DIR
environment variable will change the default location where the data is stored.
If it is not set the current directory is used.
.TP
.B -a
Show all values of a metric
@ -40,15 +31,43 @@ Show N values of a metric
.B -x <metric>
Remove metric file
.TP
.B -c
Output in CSV format
.TP
.B -l
List all metrics being tracked
.TP
.B -h
Show the help.
.TP
.B -v
Display current version information.
.TP
.B -d
Debug mode.
.TP
.B -h
Show the help.
.SH EXAMPLES
.TP
.B track weight 150
Log 150lbs for today's weight
.TP
.B track -n 10 calories
Show last 10 days calorie values
.TP
.B track mood `tired and hungry'
Show last value logged for mood
.TP
.B track mood
Show last value logged for mood
.SH LICENSE
GNU General Public License v3.0 or later at
.TP
.B https://www.gnu.org/licenses/gpl-3.0-standalone.html
.SH BUGS
None known.
Report issues at the git repository at
.B https://github.com/jamestomasino/track
.SH AUTHOR
James Tomasino <tomasino (at) lavabit (dot) com>
James Tomasino <james (at) tomasino (dot) org>

28
track.d
View File

@ -1,28 +0,0 @@
#!/usr/bin/env bash
_track() {
# Get basic autocomplete commands from the function itself
local helplist
helplist=$(track -s)
# Combine all the lists for autocomplete
local cur
cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $( compgen -W "$helplist" -- "$cur" ) )
}
# Detect if current shell is ZSH, and if so, load this file in bash
# compatibility mode.
if [ -n "$ZSH_VERSION" ]; then
autoload bashcompinit
bashcompinit
fi
complete -o default -o nospace -F _track track
# The following are necessary only for Cygwin, and only are needed
# when the user has tab-completed the executable name and consequently
# included the '.exe' suffix.
if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
complete -o default -o nospace -F _track track.exe
fi