Move zet
script to its own repository
This commit is contained in:
parent
f695a1f905
commit
38d389eb9b
4 changed files with 40 additions and 159 deletions
19
flake.lock
generated
19
flake.lock
generated
|
@ -476,7 +476,8 @@
|
||||||
"standard-vim": "standard-vim",
|
"standard-vim": "standard-vim",
|
||||||
"vim-heritage": "vim-heritage",
|
"vim-heritage": "vim-heritage",
|
||||||
"vim-textobj-xmlattr": "vim-textobj-xmlattr",
|
"vim-textobj-xmlattr": "vim-textobj-xmlattr",
|
||||||
"voidrice": "voidrice"
|
"voidrice": "voidrice",
|
||||||
|
"zet": "zet"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"rwxrob-dot": {
|
"rwxrob-dot": {
|
||||||
|
@ -603,6 +604,22 @@
|
||||||
"repo": "voidrice",
|
"repo": "voidrice",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"zet": {
|
||||||
|
"flake": false,
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1757197595,
|
||||||
|
"narHash": "sha256-4Mo4z7I7W1XOhCJvsDS97zhsNc75iBwUQq4as56rWLs=",
|
||||||
|
"ref": "refs/heads/main",
|
||||||
|
"rev": "d4ade4fe6df2ca4c31e4fa897d989da3313e8166",
|
||||||
|
"revCount": 1,
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://code.oliverdavies.uk/opdavies/zet"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://code.oliverdavies.uk/opdavies/zet"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"root": "root",
|
"root": "root",
|
||||||
|
|
|
@ -87,6 +87,11 @@
|
||||||
flake = false;
|
flake = false;
|
||||||
url = "github:lukesmithxyz/voidrice";
|
url = "github:lukesmithxyz/voidrice";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
zet = {
|
||||||
|
flake = false;
|
||||||
|
url = "git+https://code.oliverdavies.uk/opdavies/zet";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs =
|
outputs =
|
||||||
|
|
|
@ -1,20 +1,26 @@
|
||||||
|
{ inputs, ... }:
|
||||||
|
|
||||||
{
|
{
|
||||||
flake.modules.homeManager.base =
|
flake.modules.homeManager.base =
|
||||||
{ pkgs, ... }:
|
{ pkgs, ... }:
|
||||||
{
|
{
|
||||||
home.packages = [
|
home.packages =
|
||||||
(pkgs.writeShellApplication {
|
let
|
||||||
name = "zet";
|
name = "zet";
|
||||||
|
in
|
||||||
|
[
|
||||||
|
(pkgs.writeShellApplication {
|
||||||
|
inherit name;
|
||||||
|
|
||||||
runtimeInputs = with pkgs; [
|
runtimeInputs = with pkgs; [
|
||||||
bashInteractive
|
bashInteractive
|
||||||
coreutils
|
coreutils
|
||||||
fzf
|
fzf
|
||||||
git
|
git
|
||||||
];
|
];
|
||||||
|
|
||||||
text = builtins.readFile ./script.sh;
|
text = builtins.readFile "${inputs.zet}/${name}";
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,147 +0,0 @@
|
||||||
EDITOR="${EDITOR:-nvim}"
|
|
||||||
ZET_DIR="$HOME/Documents/zet"
|
|
||||||
|
|
||||||
mkdir -p "$ZET_DIR"
|
|
||||||
|
|
||||||
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}"
|
|
||||||
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"
|
|
||||||
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() {
|
|
||||||
filename="${ZET_DIR}/$(date "+%Y%m%d%H%M%S")/index.adoc"
|
|
||||||
mkdir -p "$(dirname "$filename")"
|
|
||||||
|
|
||||||
title="$*"
|
|
||||||
echo "= $title" > "$filename"
|
|
||||||
|
|
||||||
"$EDITOR" "$filename"
|
|
||||||
|
|
||||||
run_git_command add --intent-to-add "$filename"
|
|
||||||
commit_changes "$filename" "Added $filename"
|
|
||||||
}
|
|
||||||
|
|
||||||
run_git_command() {
|
|
||||||
git -C "$ZET_DIR" "$@"
|
|
||||||
}
|
|
||||||
|
|
||||||
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