diff --git a/fzf-vjour b/fzf-vjour index 75621bd..32ae0dc 100755 --- a/fzf-vjour +++ b/fzf-vjour @@ -22,6 +22,62 @@ for label in "${COLLECTION_LABELS[@]}"; do [[ ${#label} -gt $COLLECTION_LABEL_MAX_LEN ]] && COLLECTION_LABEL_MAX_LEN=${#label} done +__vjournalnew() { + python3 -c ' +import sys +from datetime import date, datetime +from icalendar.cal import Calendar, Journal + +if not len(sys.argv) == 2: + print("Pass uid as first argument!", file=sys.stderr) + sys.exit(1) + +UID = sys.argv[1] +ical = Calendar() +j = Journal() + +isnote = True +line = sys.stdin.readline().strip() +if line[:4] == "::: ": + isnote = False + line = sys.stdin.readline().strip() + +if not line[:2] == "# ": + print("Error: Summary line is corrupt!", file=sys.stderr) + sys.exit(1) +summary = line[2:] + +line = sys.stdin.readline().strip() +if not line[:2] == "> " and not line == ">": + print("Error: Categories line is corrupt!", file=sys.stderr) + sys.exit(1) +categories = line[2:].split(",") + +line = sys.stdin.readline().strip() +if not line == "": + print("Error: Missing separating line!", file=sys.stderr) + sys.exit(1) + +description = sys.stdin.read() + +# Create ical +j["SUMMARY"] = summary +j.categories = categories +j["DESCRIPTION"] = description +j.LAST_MODIFIED = datetime.utcnow() +j.DTSTAMP = datetime.utcnow() +j["UID"] = UID +j["SEQUENCE"] = 1 +j["STATUS"] = "FINAL" +if not isnote: + j.DTSTART = date.today() +ical.add_component(j) +ical["VERSION"] = "2.0" +ical["PRODID"] = "vjournew/basic" + +# Print +print(ical.to_ical().decode().replace("\r\n", "\n"))' "$@" +} __vjournalupdate() { python3 -c ' import sys @@ -242,11 +298,45 @@ if [[ "${1:-}" == "--delete" ]]; then rm -v "$vjfile" exit fi +# Generate new entry +if [[ "${1:-}" == "--new" ]]; then + local label_new=$(for label in "${COLLECTION_LABELS[@]}"; do + echo "$label" + done | fzf --prompt="Select collection> ") + local uuid_new=$(uuidgen) + local vjfile_new=$(__filepath_from_selection "$label_new $uuid_new.ics") + if [[ -f "$vjfile_new" ]] + then + echo "Bad luck..." + return 1 + fi + # + local TMPFILE="$(mktemp).md" + local SHAFILE="$TMPFILE.sha" + trap "rm -f $TMPFILE $SHAFILE" EXIT + echo "::: Keep this line if you want to add a **JOURNAL** entry (associated to today), else we will add a **NOTE**" > "$TMPFILE" + echo "# " >> "$TMPFILE" + echo "> " >> "$TMPFILE" + echo >> "$TMPFILE" + sha1sum "$TMPFILE" > "$SHAFILE" + + # Open in editor + $EDITOR "$TMPFILE" + + # Update if changes are detected + if ! sha1sum -c "$SHAFILE" > /dev/null 2>&1 + then + local vjfile_tmp="$TMPFILE.ics" + trap "rm -f $vjfile_tmp" EXIT + __vjournalnew "$uuid_new" < "$TMPFILE" > "$vjfile_tmp" && mv "$vjfile_tmp" "$vjfile_new" + fi + exit +fi selection=$(__lines | fzf --ansi \ --preview="$0 --preview {}" \ - --bind="ctrl-d:execute($0 --delete {})" - #--bind='ctrl-n:execute($HOME/.zsh/journaling/add)' \ + --bind="ctrl-d:execute($0 --delete {})" \ + --bind="ctrl-n:execute($0 --new)" #--bind='ctrl-s:execute(vdirsyncer sync journals)' \ ) if [[ -z "$selection" ]]; then