194 lines
4.1 KiB
Bash
Executable file
194 lines
4.1 KiB
Bash
Executable file
#!/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)
|
|
touch "$ZET_TMP_FILE"
|
|
echo "/.zets" > "$ZET_DIR/.gitignore"
|
|
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="${ZET_DIR}/${query}/index.adoc"
|
|
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="$(find "$ZET_DIR" -type f -name "index.adoc" | sort | tail -1)"
|
|
else
|
|
file="${ZET_DIR}/${query}/index.adoc"
|
|
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
|
|
}
|
|
|
|
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"
|
|
mkdir -p "$(dirname "$filename")"
|
|
|
|
title="$*"
|
|
echo "= $title" > "$filename"
|
|
echo "$date $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 "$*" "$ZET_DIR" | while read -r file; do
|
|
date=$(basename "$(dirname "$file")")
|
|
title=$(head -1 "$file" | sed 's/^= //' | sed 's/# //')
|
|
|
|
echo "$date $title"
|
|
done | sort
|
|
}
|
|
|
|
show_titles() {
|
|
cat "$ZET_TMP_FILE"
|
|
}
|
|
|
|
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"
|
|
done | sort > "$ZET_TMP_FILE" &
|
|
}
|
|
|
|
main "$@"
|