SHARE
TWEET

Untitled

a guest Dec 15th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env bash
  2.  
  3. worklog_dir="worklog"
  4. editor="subl"
  5.  
  6. show_usage () {
  7.     cat <<-EOF
  8.     Create a diary text file by week in the $worklog_dir subdirectory
  9.     by the year and month. Automagically adds a heading to the file
  10.     and opens the file with $editor to the current* date.
  11.  
  12.     A date other than now may be specified as the first argument.
  13.  
  14.     E.g.:
  15.     ./log.sh
  16.     ./log.sh "last saturday"
  17.     EOF
  18.    
  19. }
  20.  
  21. if [[ "$*" == *"help"* || "$*" == *"-h"* ]]; then
  22.     show_usage
  23.     exit
  24. fi
  25.  
  26.  
  27. if ! year=$(date --date "${1:-now}" +%Y); then
  28.     # Fail early
  29.     show_usage
  30.     exit
  31. fi
  32.  
  33. month=$(date --date "${1:-now}" +%m)
  34. day=$(date --date "${1:-now}" +%d)
  35. weeknumber=$((($(date --date "${1:-now}" +%-d)-1)/7+1))
  36. filename="$weeknumber.txt"
  37. dayofweek=$(date --date "${1:-now}" +%A)
  38. iso=$(date -Idate --date "${1:-now}")
  39.  
  40. # printf "year '$year' month '$month' day '$day' weeknumber '$weeknumber'\n"
  41.  
  42. mkdir -p "$HOME/$worklog_dir/$year/$month"
  43. pushd "$HOME/$worklog_dir/$year/$month"
  44. touch $filename
  45.  
  46. # Add a header for the date if it doesn't already exist
  47. if ! grep -q "$iso" "$filename"; then
  48.     echo "" >> $filename
  49.     echo "$iso:" >> $filename
  50.     echo "-$dayofweek" >> $filename
  51. fi
  52.  
  53. linenumber=$(($(grep -n $iso $filename | cut -f1 -d:)+2))
  54.  
  55. case "$editor" in
  56.     subl)
  57.         # Hide guake terminal if we are using it
  58.         type guake 2>/dev/null && guake --hide
  59.         subl "$filename:$linenumber"
  60.         ;;
  61.     nano)
  62.         nano "+$linenumber" "$filename"
  63.         ;;
  64.     vim)
  65.         vim "+$linenumber" "$filename"
  66.         ;;
  67. esac
  68.  
  69. popd
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top