#!/usr/bin/env bash set -euo pipefail EDITOR="${EDITOR:-nvim}" ZET_DIR="${ZET_DIR:-$HOME/Documents/zet}" ZET_TMP_FILE="$HOME/.local/state/zets" if [[ ! -d "$ZET_DIR" ]]; then mkdir -p "$ZET_DIR" (cd "$ZET_DIR" && git init) git -C "$ZET_DIR" add . git -C "$ZET_DIR" commit -m 'Initial commit' fi commit_changes() { filename="$1" commit_message="$2" git -C "$ZET_DIR" diff --quiet "$filename" || { read -rp "Commit? " commit_choice if [[ "$commit_choice" =~ ^[Yy]$ ]]; then git -C "$ZET_DIR" add "$filename" commit_message="${commit_message//$ZET_DIR\//}" commit_message="${commit_message//\/index.adoc/}" git -C "$ZET_DIR" commit -m "$commit_message" echo "Changes committed." else echo "Changes not committed." fi } } delete_zet() { query="$1" if [[ "$1" == "last" ]]; then file="$(find "$ZET_DIR" -type f -name "index.adoc" | sort | tail -1)" else file="$(get_filepath_for_zet "$query")" fi if [[ -f "$file" ]]; then run_git_command rm -rf "$file" commit_message="Delete $file" commit_message="${commit_message//$ZET_DIR\//}" commit_message="${commit_message//\/index.adoc/}" git -C "$ZET_DIR" commit -m "$commit_message" update_zet_list else result="$(search_zets "$query")" result_count="$(echo "$result" | grep -c '^')" if [[ "$result_count" -eq 0 ]]; then echo "No results found for query: $query" >&2 exit 1 elif [[ "$result_count" -eq 1 ]]; then id="$(echo "$result" | awk '{print $1}')" else selected="$(echo "$result" | sort | fzf)" || exit 0 id="$(echo "$selected" | awk '{print $1}')" fi delete_zet "$id" fi } edit_zet() { query="$1" if [[ "$1" == "last" ]]; then file="$(get_last_zet)" else file="$(get_filepath_for_zet "$query")" fi if [[ -f "$file" ]]; then "$EDITOR" "$file" commit_changes "$file" "Edit $file" update_zet_list else result="$(search_zets "$query")" result_count="$(echo "$result" | grep -c '^')" if [[ "$result_count" -eq 0 ]]; then echo "No results found for query: $query" >&2 exit 1 elif [[ "$result_count" -eq 1 ]]; then id="$(echo "$result" | awk '{print $1}')" else selected="$(echo "$result" | sort | fzf)" || exit 0 id="$(echo "$selected" | awk '{print $1}')" fi edit_zet "$id" fi } generate_links() { query="$1" related="$(search_zets "$query")" echo "$related" | while IFS= read -r line; do id="${line%% *}" title="${line#* }" echo "* link:../${id}/index.adoc[${title}]" done | sort } get_filepath_for_zet() { echo "${ZET_DIR}/$1/index.adoc" } get_last_zet() { find "$ZET_DIR" -type f -name "index.adoc" | sort | tail -1 } get_zet_id_from_filepath() { basename "$(dirname "$file")" } main() { [[ $# -eq 0 ]] && show_titles && exit case "${1:-}" in delete) delete_zet "$2" ;; edit) edit_zet "$2" ;; find|search) shift 1; search_zets "$@" ;; git) shift 1; run_git_command "$@" ;; links) generate_links "$2" ;; new|create) shift 1; new_zet "$@" ;; source) show_zet "$2" ;; titles) show_titles ;; *) search_zets "$1"; esac } new_zet() { id="$(date "+%Y%m%d%H%M%S")" filename="$(get_filepath_for_zet "$id")" mkdir -p "$(dirname "$filename")" title="$*" echo "= $title" > "$filename" echo "$id $title" >> "$ZET_TMP_FILE" "$EDITOR" "$filename" run_git_command add --intent-to-add "$filename" commit_changes "$filename" "Add $filename" } run_git_command() { git -C "$ZET_DIR" "$@" } search_zets() { grep --recursive --ignore-case --files-with-matches --exclude-dir=.git "$*" "$ZET_DIR" | while read -r file; do id="$(get_zet_id_from_filepath "$file")" title=$(head -1 "$file" | sed 's/^= //' | sed 's/# //') echo "$id $title" done | sort } show_titles() { cat "$ZET_TMP_FILE" } show_zet() { id="$1" file="$(get_filepath_for_zet "$id")" if [[ -f "$file" ]]; then cat "$file" && exit 0 fi echo "Note not found: $id" >&2 exit 1 } update_zet_list() { find "$ZET_DIR" -type f -name 'index.adoc' | while read -r file; do id="$(get_zet_id_from_filepath "$file")" title=$(head -n 1 "$file" | sed 's/^= //' | sed 's/^# //') echo "$id $title" done | sort > "$ZET_TMP_FILE" & } main "$@"