#!/bin/bash # Helper script for managing a [GitJournal](https://gitjournal.io/) notes # directory on a computer. # USAGE: note # You can select one of the files there, or use the special command `new` which # creates a new file (you will be prompted for a title, which should not include # the .md extension). # Executes a `git pull` before showing file list, commits changes and pushes # after the file is exited. # NOTE: Currently hardcodes notes directory to `~/gitjournal`. # TODO: skip git operations if there is no network connection. set -euo pipefail directory=$HOME/gitjournal/ git -C "$directory" pull || true suffix=.md readarray -t notes < <( \ find "$directory" -iname "*$suffix" -printf "%T@\t%p\n" | \ sort --reverse --numeric-sort | \ awk -F "\t" '// { print $2 }' \ ) notes=("${notes[@]#"$directory"}") notes=("${notes[@]%"$suffix"}") notes+=(new journal) note=$(printf '%s\n' "${notes[@]}" | fzf) if [[ "$note" == "new" ]]; then echo -n "enter new note directory/title: " read new_title file="${directory}${new_title}${suffix}" [[ -f "$file" ]] && echo "Note at $file already exists!" && exit 1 date=`date --iso-8601=seconds` cat << EOF >> "$file" --- created: $date modified: $date --- # ${new_title##*/} EOF commit_message="Created $new_title" elif [[ "$note" == "journal" ]]; then new_title=`date +%Y-%m-%d` long_date=`date +"%B %d, %Y"` file="${directory}Journal/${new_title}${suffix}" [[ -f "$file" ]] && echo "Journal entry for $new_title already exists!" && exit 1 date=`date --iso-8601=seconds` cat << EOF >> "$file" --- created: $date modified: $date --- # ${long_date} EOF commit_message="Created $long_date journal entry" else file="${directory}${note}${suffix}" commit_message="Modified $note" fi [[ -f "$file" ]] && $VISUAL "$file" git -C "$directory" add -N "$file" git -C "$directory" add -p "$file" git -C "$directory" commit -m "$commit_message" git -C "$directory" status echo -n "push changes? [y/N] " read push if [[ "$push" == "y" ]]; then git -C "$directory" push fi