2025-09-06 23:26:35 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
EDITOR="${EDITOR:-nvim}"
|
2025-09-07 09:45:03 +01:00
|
|
|
ZET_DIR="${ZET_DIR:-$HOME/Documents/zet}"
|
2025-09-07 00:06:08 +01:00
|
|
|
ZET_TMP="$ZET_DIR/.zets"
|
2025-09-06 23:26:35 +01:00
|
|
|
|
2025-09-07 00:06:08 +01:00
|
|
|
if [[ ! -d "$ZET_DIR" ]]; then
|
|
|
|
mkdir -p "$ZET_DIR"
|
2025-09-07 09:45:27 +01:00
|
|
|
(cd "$ZET_DIR" && git init)
|
2025-09-07 00:06:08 +01:00
|
|
|
touch "$ZET_TMP"
|
|
|
|
echo "/.zets" > "$ZET_DIR/.gitignore"
|
2025-09-07 09:45:27 +01:00
|
|
|
git -C "$ZET_DIR" add .
|
|
|
|
git -C "$ZET_DIR" commit -m 'Initial commit'
|
2025-09-07 00:06:08 +01:00
|
|
|
fi
|
2025-09-06 23:26:35 +01:00
|
|
|
|
|
|
|
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() {
|
|
|
|
id="$1"
|
|
|
|
dir="${ZET_DIR}/${id}"
|
|
|
|
|
|
|
|
if [[ -d "$dir" ]]; then
|
|
|
|
mv -v "$dir" "/tmp/zet-${id}"
|
2025-09-07 00:06:08 +01:00
|
|
|
|
|
|
|
# TODO: Delete from $ZET_TMP.
|
2025-09-06 23:26:35 +01:00
|
|
|
else
|
|
|
|
echo "Note not found: $id" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
edit_zet() {
|
|
|
|
query="$1"
|
|
|
|
|
|
|
|
file="${ZET_DIR}/${query}/index.adoc"
|
|
|
|
|
|
|
|
if [[ -f "$file" ]]; then
|
|
|
|
"$EDITOR" "$file"
|
|
|
|
|
|
|
|
commit_changes "$file" "Edited $file"
|
2025-09-07 00:06:08 +01:00
|
|
|
|
|
|
|
# TODO: Update $ZET_TMP if the title is changed.
|
2025-09-06 23:26:35 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2025-09-07 00:06:08 +01:00
|
|
|
date="$(date "+%Y%m%d%H%M%S")"
|
|
|
|
filename="${ZET_DIR}/$date/index.adoc"
|
2025-09-06 23:26:35 +01:00
|
|
|
mkdir -p "$(dirname "$filename")"
|
|
|
|
|
|
|
|
title="$*"
|
|
|
|
echo "= $title" > "$filename"
|
2025-09-07 00:06:08 +01:00
|
|
|
echo "$date $title" >> "$ZET_TMP"
|
2025-09-06 23:26:35 +01:00
|
|
|
|
|
|
|
"$EDITOR" "$filename"
|
|
|
|
|
|
|
|
run_git_command add --intent-to-add "$filename"
|
|
|
|
commit_changes "$filename" "Added $filename"
|
|
|
|
}
|
|
|
|
|
|
|
|
run_git_command() {
|
|
|
|
git -C "$ZET_DIR" "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
search_zets() {
|
2025-09-07 00:06:08 +01:00
|
|
|
grep --ignore-case "$*" < "$ZET_TMP"
|
2025-09-06 23:26:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
show_titles() {
|
2025-09-07 00:06:08 +01:00
|
|
|
cat "$ZET_TMP"
|
2025-09-06 23:26:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
show_zet() {
|
|
|
|
id="$1"
|
|
|
|
file="${ZET_DIR}/${id}/index.adoc"
|
|
|
|
|
|
|
|
if [[ -f "$file" ]]; then
|
|
|
|
cat "$file" && exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Note not found: $id" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|