m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/note
blob: 132ca205881a7a01afa78497a1c618112ffed72e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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