From 355d4996dc32988aabe000fbfaa1a9bbdd9ec585 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sun, 26 Mar 2023 13:49:36 +0200 Subject: Add initial scripts --- note | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 note (limited to 'note') diff --git a/note b/note new file mode 100755 index 0000000..132ca20 --- /dev/null +++ b/note @@ -0,0 +1,76 @@ +#!/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 -- cgit v1.2.3