dotfiles/symlinks/bin/t

161 lines
4.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
. ~/.local/opt/script_utils/echo.sh
usage () {
case $1 in
in)
usage_in; exit 0 ;;
out)
usage_out; exit 0 ;;
bal)
usage_bal; exit 0 ;;
esac
if [ ! -z "$1" ]; then
echo -e "Unknown command: $1 \n"
fi
echo "Basic time tracker."
echo "Generates input for ledger-cli time tracking"
echo -e "\nUsage: time COMMAND [options] [arguments] \n"
echo "Commands:"
echo " in - starts a new time entry"
echo " out - completes an old time entry"
echo " bal - outputs current timesheet"
echo ""
echo "Options:"
echo " -h --help - prints this help message (if one of commands is passed, prints help message for that command)"
echo " -v --verbose - enables verbose logging"
if [ ! -z "$1" ]; then
exit 1
fi
}
usage_in () {
echo "Time In"
echo "Starts a new time entry - requires a project"
echo -e "\nUsage: time in [options] <project> [<comment>] \n"
echo "Arguments:"
echo " project - name of project tracking time to"
echo " comment - additional information - optional"
echo ""
echo "Options:"
echo " -h --help - prints this help message"
echo " -v --verbose - enables verbose logging"
echov "Examples:"
echov " > time in project:test"
echov ""
echov " > time in project:test parsing"
}
usage_out () {
echo "Time Out"
echo "Stops an existing time entry - requires a project"
echo -e "\nUsage: time out [options] <project>\n"
echo "Arguments:"
echo " project - name of project tracking time to"
echo ""
echo "Options:"
echo " -h --help - prints this help message"
echo " -v --verbose - enables verbose logging"
echov "Examples:"
echov " > time out project:test"
}
usage_bal () {
echo "Time balance"
echo "Prints out current timesheet - takes in a project optionally"
echo -e "\nUsage: time bal [options] <project> \n"
echo "Arguments:"
echo " project - name of project to get balance of"
echo ""
echo "Options:"
echo " -h --help - prints this help message"
echo " -v --verbose - enables verbose logging"
echov "Examples:"
echov " > time bal project:test"
}
POSITIONAL=()
HELP=0
VERBOSE=0
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h|--help)
HELP=1
shift # past argument
;;
-v|--verbose)
VERBOSE=1
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
COMMAND="${POSITIONAL[0]}"
echov "Detected command: $COMMAND"
if [ $HELP -eq 1 ]; then
usage $COMMAND
exit 0
fi
PROJECT="${POSITIONAL[1]}"
COMMENT="${POSITIONAL[@]:2}"
echov "Project: $PROJECT"
echov "Comment: $COMMENT"
if [ -z "$TIMESHEET_LEDGER_HOME" ]; then
echo "TIMESHEET_LEDGER_HOME variable not defined. It must point to a directory that hosts your main.journal as well as timesheets/ directory with yearly .journal files (e.g. timesheets/2023.journal)"
exit 1
fi
time_in () {
LAST_ENTRY=$(grep "$PROJECT" "$TIMESHEET_LEDGER_HOME/timesheets/$(date +%Y).journal" | tail -1 | cut -f1 -d ' ')
if [ "$LAST_ENTRY" = "i" ]; then
echo "Timer for project $PROJECT was already started. Aborting..."
exit 1
fi
echo "i $(date '+%Y-%m-%d %H:%M:%S') $PROJECT $COMMENT" >> "$TIMESHEET_LEDGER_HOME/timesheets/$(date +%Y).journal"
}
time_out () {
LAST_ENTRY=$(grep "$PROJECT" "$TIMESHEET_LEDGER_HOME/timesheets/$(date +%Y).journal" | tail -1 | cut -f1 -d ' ')
if [ -z "$LAST_ENTRY" ]; then
echo "Timer for project $PROJECT is not started. Aborting..."
exit 1
fi
if [ "$LAST_ENTRY" = "O" ]; then
echo "Timer for project $PROJECT was already stopped. Aborting..."
exit 1
fi
echo "O $(date '+%Y-%m-%d %H:%M:%S') $PROJECT" >> "$TIMESHEET_LEDGER_HOME/timesheets/$(date +%Y).journal"
}
time_bal () {
ledger -f "$TIMESHEET_LEDGER_HOME/main.journal" bal $PROJECT
}
case $COMMAND in
in)
time_in
;;
out)
time_out
;;
bal)
time_bal
;;
*)
usage $COMMAND; exit 1 ;;
esac