~gpanders/dotfiles

ref: 2da1dde5ea566f82ebe6532bc6d3b8670fd77ce8 dotfiles/note/.local/share/bash-completion/completions/note -rw-r--r-- 1.1 KiB
2da1dde5Greg Anders Note 'open' subcommand opens editor in NOTES_DIR when no argument given 1 year, 4 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
#!/usr/bin/env bash

_note () {
    if [ "${#COMP_WORDS[@]}" -gt 3 ]; then
        return
    fi

    if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}"/note/config ]; then
        source "${XDG_CONFIG_HOME:-$HOME/.config}"/note/config
    elif [ -z "$NOTES_DIR" ]; then
        NOTES_DIR=${XDG_DATA_HOME:-$HOME/.local/share}/note
    fi

    if [ -z "$NOTES_DIR" ]; then
        return
    fi

    local cur commands
    cur="${COMP_WORDS[COMP_CWORD]}"
    commands="show open list grep tag help"

    if [ "$COMP_CWORD" -eq 1 ]; then
        COMPREPLY=($(compgen -W "$commands" -- "$cur"))
        return 0
    fi

    local notes=()
    for note in "$NOTES_DIR"/*; do
        notes+=($(basename -- "${note%.*}"))
    done

    case "${COMP_WORDS[1]}" in
        s|sh|sho|show|o|op|ope|open)
            COMPREPLY=($(compgen -W "${notes[*]}" -- "${COMP_WORDS[2]}"))
            ;;
        l|li|lis|list|g|gr|gre|grep|t|ta|tag|h|he|hel|help)
            return
            ;;
        *)
            COMPREPLY=($(compgen -W "$commands" -- "$cur"))
            ;;
    esac
} &&
complete -F _note note