Skip to content

Instantly share code, notes, and snippets.

@duckythescientist
Created December 15, 2018 00:15
Show Gist options
  • Select an option

  • Save duckythescientist/a544ac01e6d0747501093673130fd81b to your computer and use it in GitHub Desktop.

Select an option

Save duckythescientist/a544ac01e6d0747501093673130fd81b to your computer and use it in GitHub Desktop.
Quick notes by date
#!/usr/bin/env bash
worklog_dir="worklog"
editor="subl"
show_usage () {
cat <<-EOF
Create a diary text file by week in the $worklog_dir subdirectory
by the year and month. Automagically adds a heading to the file
and opens the file with $editor to the current* date.
A date other than now may be specified as the first argument.
E.g.:
./log.sh
./log.sh "last saturday"
EOF
}
if [[ "$*" == *"help"* || "$*" == *"-h"* ]]; then
show_usage
exit
fi
if ! year=$(date --date "${1:-now}" +%Y); then
# Fail early
show_usage
exit
fi
month=$(date --date "${1:-now}" +%m)
day=$(date --date "${1:-now}" +%d)
weeknumber=$((($(date --date "${1:-now}" +%-d)-1)/7+1))
filename="$weeknumber.txt"
dayofweek=$(date --date "${1:-now}" +%A)
iso=$(date -Idate --date "${1:-now}")
# printf "year '$year' month '$month' day '$day' weeknumber '$weeknumber'\n"
mkdir -p "$HOME/$worklog_dir/$year/$month"
pushd "$HOME/$worklog_dir/$year/$month"
touch $filename
# Add a header for the date if it doesn't already exist
if ! grep -q "$iso" "$filename"; then
echo "" >> $filename
echo "$iso:" >> $filename
echo "-$dayofweek" >> $filename
fi
linenumber=$(($(grep -n $iso $filename | cut -f1 -d:)+2))
case "$editor" in
subl)
# Hide guake terminal if we are using it
type guake 2>/dev/null && guake --hide
subl "$filename:$linenumber"
;;
nano)
nano "+$linenumber" "$filename"
;;
vim)
vim "+$linenumber" "$filename"
;;
esac
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment