cmd-zet/zet

190 lines
4 KiB
Text
Raw Normal View History

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-08 12:00:00 +01:00
ZET_TMP_FILE="$HOME/.local/state/zets"
2025-09-06 23:26:35 +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 20:51:24 +01:00
touch "$ZET_TMP_FILE"
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'
fi
2025-09-06 23:26:35 +01:00
commit_changes() {
filename="$1"
commit_message="$2"
git -C "$ZET_DIR" diff --quiet "$filename" || {
2025-09-07 19:41:19 +01:00
read -rp "Commit? " commit_choice
2025-09-06 23:26:35 +01:00
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() {
2025-09-07 18:54:55 +01:00
query="$1"
2025-09-07 10:13:24 +01:00
if [[ "$1" == "last" ]]; then
file="$(find "$ZET_DIR" -type f -name "index.adoc" | sort | tail -1)"
else
file="${ZET_DIR}/${query}/index.adoc"
fi
2025-09-06 23:26:35 +01:00
2025-09-07 18:54:55 +01:00
if [[ -f "$file" ]]; then
run_git_command rm -rf "$file"
2025-09-07 18:54:55 +01:00
commit_message="Delete $file"
commit_message="${commit_message//$ZET_DIR\//}"
commit_message="${commit_message//\/index.adoc/}"
2025-09-07 10:08:08 +01:00
2025-09-07 18:54:55 +01:00
git -C "$ZET_DIR" commit -m "$commit_message"
2025-09-07 10:08:08 +01:00
update_zet_list
2025-09-06 23:26:35 +01:00
else
2025-09-07 18:54:55 +01:00
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"
2025-09-06 23:26:35 +01:00
fi
2025-09-07 18:54:55 +01:00
2025-09-06 23:26:35 +01:00
}
edit_zet() {
query="$1"
if [[ "$1" == "last" ]]; then
file="$(find "$ZET_DIR" -type f -name "index.adoc" | sort | tail -1)"
else
file="${ZET_DIR}/${query}/index.adoc"
fi
2025-09-06 23:26:35 +01:00
if [[ -f "$file" ]]; then
"$EDITOR" "$file"
2025-09-07 09:46:42 +01:00
commit_changes "$file" "Edit $file"
update_zet_list
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() {
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 20:51:24 +01:00
echo "$date $title" >> "$ZET_TMP_FILE"
2025-09-06 23:26:35 +01:00
"$EDITOR" "$filename"
run_git_command add --intent-to-add "$filename"
2025-09-07 09:46:42 +01:00
commit_changes "$filename" "Add $filename"
2025-09-06 23:26:35 +01:00
}
run_git_command() {
git -C "$ZET_DIR" "$@"
}
search_zets() {
2025-09-07 20:51:24 +01:00
grep --ignore-case "$*" < "$ZET_TMP_FILE"
2025-09-06 23:26:35 +01:00
}
show_titles() {
2025-09-07 20:51:24 +01:00
cat "$ZET_TMP_FILE"
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
}
update_zet_list() {
find "$ZET_DIR" -type f -name 'index.adoc' | while read -r filename; do
id=$(basename "$(dirname "$filename")")
title=$(head -n 1 "$filename" | sed 's/^= //' | sed 's/^# //')
echo "$id $title"
2025-09-07 20:51:24 +01:00
done | sort > "$ZET_TMP_FILE" &
}
2025-09-06 23:26:35 +01:00
main "$@"