Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Add Comment
Please, Sign In to add comment