diff --git a/flake.lock b/flake.lock index c312763d..483c77d6 100644 --- a/flake.lock +++ b/flake.lock @@ -476,7 +476,8 @@ "standard-vim": "standard-vim", "vim-heritage": "vim-heritage", "vim-textobj-xmlattr": "vim-textobj-xmlattr", - "voidrice": "voidrice" + "voidrice": "voidrice", + "zet": "zet" } }, "rwxrob-dot": { @@ -603,6 +604,22 @@ "repo": "voidrice", "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", diff --git a/flake.nix b/flake.nix index 0a81b14e..25b1bf65 100644 --- a/flake.nix +++ b/flake.nix @@ -87,6 +87,11 @@ flake = false; url = "github:lukesmithxyz/voidrice"; }; + + zet = { + flake = false; + url = "git+https://code.oliverdavies.uk/opdavies/zet"; + }; }; outputs = diff --git a/modules/zet/default.nix b/modules/zet/default.nix index 7b2543f4..32b0e75e 100644 --- a/modules/zet/default.nix +++ b/modules/zet/default.nix @@ -1,20 +1,26 @@ +{ inputs, ... }: + { flake.modules.homeManager.base = { pkgs, ... }: { - home.packages = [ - (pkgs.writeShellApplication { + home.packages = + let name = "zet"; + in + [ + (pkgs.writeShellApplication { + inherit name; - runtimeInputs = with pkgs; [ - bashInteractive - coreutils - fzf - git - ]; + runtimeInputs = with pkgs; [ + bashInteractive + coreutils + fzf + git + ]; - text = builtins.readFile ./script.sh; - }) - ]; + text = builtins.readFile "${inputs.zet}/${name}"; + }) + ]; }; } diff --git a/modules/zet/script.sh b/modules/zet/script.sh deleted file mode 100644 index 48721e43..00000000 --- a/modules/zet/script.sh +++ /dev/null @@ -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 "$@"