Last active
August 12, 2024 18:07
-
-
Save leonid-ed/82e2fecf3b2b882cb6f03f50a90221fa to your computer and use it in GitHub Desktop.
A short Bash script to make quick records with time spent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a script to make short records about activities with time spent in minutes. | |
# Dependencies: | |
# - tac - (install: $ brew install coreutils) | |
# - align - (install: https://github.com/Guitarbum722/align/tree/master/cmd/align, $ go build -o align && go install) | |
# | |
# You can add alias with shortcuts to your bash/zsh config: | |
# alias mr='/Users/leonid/bash-scripts/make_record.sh add' | |
# alias mrs='/Users/leonid/bash-scripts/make_record.sh show' | |
# alias mrsf='/Users/leonid/bash-scripts/make_record.sh show-filter' | |
# alias mre='/Users/leonid/bash-scripts/make_record.sh edit' | |
# | |
# Example of usage: | |
# $ mr 10 programming make_record.sh: adding check on commas in the description | |
# | |
# $ mrsf make_record.sh | |
# 2024-08-09T21:21:14+02:00 | 10 | programming | make_record.sh: adding check on commas in the description | |
# 2024-08-07T20:07:00+02:00 | 75 | programming | make_record.sh: adding formatting and filtering | |
# 2024-08-06T22:35:10+02:00 | 50 | programming | writing a bash script for making quick records with time spent (make_record.sh) | |
# total: 135 minutes or 2.25 hours | |
#!/usr/bin/env bash | |
set -Eeo pipefail | |
JOURNAL="activity.log" # path to the log file | |
COMMAND="" | |
DURATION="" | |
SUBJECT="" | |
DESCRIPTION="" | |
SHOW_LINES="5" | |
EDITOR="nvim" # editor which is used for editing the log file | |
EDITOR_OPT="+" # editor option to go to the end of the file after opening | |
COMMAND="$1" | |
shift | |
case "${COMMAND}" in | |
add) | |
if [[ $# -lt 3 ]]; then | |
echo "Error: too few parameters for command '$COMMAND'!" | |
exit 1 | |
fi | |
DURATION="$1" | |
shift | |
SUBJECT="$1" | |
shift | |
DESCRIPTION="$@" | |
# Validate the duration value | |
if [[ ! "${DURATION}" =~ ^[0-9]+$ ]]; then | |
echo "Error: the duration must contain only digits!" | |
exit 2 | |
fi | |
# Prevent commas in the description as they break CVS format with comma as a separator | |
if [[ "${DESCRIPTION}" == *","* ]]; then | |
echo "Error: the description contains commas (',') which are prohibited!" | |
exit 2 | |
fi | |
echo "$(date -Iseconds),${DURATION},${SUBJECT},${DESCRIPTION}" >> "${JOURNAL}" | |
;; | |
show) | |
if [[ $# -gt 0 ]]; then | |
SHOW_LINES="$1" | |
fi | |
tail -r -n "${SHOW_LINES}" "${JOURNAL}" | align -i 2:right -d \| | |
;; | |
show-filter) | |
if [[ $# -lt 1 ]]; then | |
echo "Error: too few parameters for command '${COMMAND}'!" | |
exit 1 | |
fi | |
PATTERN="$1" | |
RS=$(grep -Ei "${PATTERN}" "${JOURNAL}" | tac) | |
echo "${RS}" | align -i 2:right -d \| | |
echo "${RS}" | awk -F, ' { total += $2 } END { print "total:", total, "minutes or", total/60, "hours" } ' | |
;; | |
edit) | |
"${EDITOR}" "${EDITOR_OPT}" "${JOURNAL}" | |
;; | |
*) echo "Unknown command found '${COMMAND}'" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment