~gpanders/wk

ref: c0e190f572906dced35606b0d999d4390183c3c6 wk/completions/bash/zet -rw-r--r-- 1.6 KiB
c0e190f5 — Greg Anders Make patterns match on zettel contents 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env bash

_zet () {
    : "${ZETTEL_DIR:=${XDG_DATA_HOME:-$HOME/.local/share}/zet}"

    local cur commands
    cur="${COMP_WORDS[COMP_CWORD]}"
    commands="backlinks show open list search sync new tags help preview"

    if [ "$COMP_CWORD" -eq 1 ]; then
        while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$commands" -- "$cur")
        return 0
    fi

    if [ ! -d "$ZETTEL_DIR" ]; then
        return
    fi

    local zettels
    zettels=$(compgen -f "$ZETTEL_DIR/" | sed -E -e 's/.*\///g' -e '/^[0-9]{14}/!d' -e 's/\..*$//')

    case "${COMP_WORDS[1]}" in
        bl|backlinks|sh|show|o|open|p|pre|prev|preview)
            # compgen only returns prefix-matches, but we want to return
            # matches anywhere in the substring, so use grep to generate the
            # results
            while IFS='' read -r line; do
                COMPREPLY+=("$line")
            done < <(printf '%s\n' "$zettels" | grep -iF "$cur")
            ;;
        t|tag|tags)
            local tags
            tags=$(awk '/^tags:/ {$1="";print}' "$ZETTEL_DIR"/* | tr ',' '\n' | awk '{$1=$1}1' | sort -u)

            # Prioritize prefix matches but if none exist, fallback to matching
            # any substring
            matches=$(compgen -W "$tags" -- "$cur")
            if [ -z "$matches" ]; then
                matches=$(printf '%s\n' "$tags" | grep -iF "$cur")
            fi

            while IFS='' read -r line; do
                COMPREPLY+=("$line")
            done < <(printf '%s\n' "$matches")
            ;;
        *)
            return
            ;;
    esac
} &&
complete -F _zet zet