Add zet
script
This commit is contained in:
parent
ccac9e3fd5
commit
ae0a1f82c9
2 changed files with 134 additions and 0 deletions
19
modules/zet/default.nix
Normal file
19
modules/zet/default.nix
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
flake.modules.homeManager.base =
|
||||||
|
{ pkgs, ... }:
|
||||||
|
{
|
||||||
|
home.packages = [
|
||||||
|
(pkgs.writeShellApplication {
|
||||||
|
name = "zet";
|
||||||
|
|
||||||
|
runtimeInputs = with pkgs; [
|
||||||
|
bashInteractive
|
||||||
|
coreutils
|
||||||
|
fzf
|
||||||
|
];
|
||||||
|
|
||||||
|
text = builtins.readFile ./script.sh;
|
||||||
|
})
|
||||||
|
];
|
||||||
|
};
|
||||||
|
}
|
115
modules/zet/script.sh
Normal file
115
modules/zet/script.sh
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
EDITOR="${EDITOR:-nvim}"
|
||||||
|
ZET_DIR="$HOME/Documents/zet"
|
||||||
|
|
||||||
|
mkdir -p "$ZET_DIR"
|
||||||
|
|
||||||
|
delete_zet() {
|
||||||
|
id="$1"
|
||||||
|
dir="${ZET_DIR}/${id}"
|
||||||
|
|
||||||
|
if [[ -d "$dir" ]]; then
|
||||||
|
mv -v "$dir" "/tmp/zet-${id}"
|
||||||
|
else
|
||||||
|
echo "Note not found: $id" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
edit_zet() {
|
||||||
|
file="${ZET_DIR}/${query}/index.adoc"
|
||||||
|
|
||||||
|
if [[ -f "$file" ]]; then
|
||||||
|
"$EDITOR" "$file"
|
||||||
|
else
|
||||||
|
query="$1"
|
||||||
|
|
||||||
|
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" | 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
|
||||||
|
}
|
||||||
|
|
||||||
|
main() {
|
||||||
|
[[ $# -eq 0 ]] && show_titles && exit
|
||||||
|
|
||||||
|
case "${1:-}" in
|
||||||
|
delete) delete_zet "$2" ;;
|
||||||
|
edit) edit_zet "$2" ;;
|
||||||
|
find|search) shift 1; search_zets "$@" ;;
|
||||||
|
links) generate_links "$2" ;;
|
||||||
|
new|create) shift 1; new_zet "$@" ;;
|
||||||
|
source) show_zet "$2" ;;
|
||||||
|
titles) show_titles ;;
|
||||||
|
*) search_zets "$1";
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
new_zet() {
|
||||||
|
filename="${ZET_DIR}/$(date "+%Y%m%d%H%M%S")/index.adoc"
|
||||||
|
mkdir -p "$(dirname "$filename")"
|
||||||
|
|
||||||
|
title="$*"
|
||||||
|
echo "= $title" > "$filename"
|
||||||
|
|
||||||
|
"$EDITOR" "$filename"
|
||||||
|
}
|
||||||
|
|
||||||
|
search_zets() {
|
||||||
|
query="$*"
|
||||||
|
|
||||||
|
grep --files-with-matches --recursive --ignore-case "$query" "$ZET_DIR" | grep '/index\.adoc$' | while read -r filepath; do
|
||||||
|
dirpath=$(dirname "$filepath")
|
||||||
|
relpath="${dirpath#"$ZET_DIR/"}"
|
||||||
|
|
||||||
|
echo -n "$relpath "
|
||||||
|
|
||||||
|
head -n 1 "$filepath" | sed 's/^= //'
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
show_titles() {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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 "$@"
|
Loading…
Add table
Add a link
Reference in a new issue