aboutsummaryrefslogtreecommitdiff
path: root/auto_completion/cd-archive
blob: 8c4122c21e6219a40310db3cc7c89a7655f87be5 (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
#set -x
ARCHIVE="${HOME}/_library"

comp() {
    find "${ARCHIVE}" -maxdepth $2 -type d -regextype posix-extended -regex "$1"
}

_cda() {
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Format directory names for completion
    local formatted_dirs
    while IFS= read -r dir; do
        local dir_name=$(basename "$dir")
        local dir_code="${dir_name%%-*}"
        local dir_desc="${dir_name#*-}"
        formatted_dirs+="${dir_code} ${dir_desc}"$'\n'
    done <<< "$directories"

    # Generate completions based on current input
    COMPREPLY=($(compgen -W "$formatted_dirs" -- "$cur"))
}

# Autocomplete function
_cda() {
  local current="${COMP_WORDS[COMP_CWORD]}"
  local target_dir="$HOME/_library"
  local suggestions=()

  # Collect directories and format them
  while IFS= read -r dir; do
    local base_dir=$(basename "$dir")
    # Append directory code and description
    suggestions+=("${base_dir%%-*} ${base_dir#*-}")
  done < <(find "$target_dir" -maxdepth 3 -type d -name "[0-9]*-*")

  # Generate completions matching current input
  COMPREPLY=($(compgen -W "${suggestions[*]}" -- "$current"))
}

#complete -F _cda cda

_cda2() {
    if [ ! -z "$2" ]; then
        local cur
        COMPREPLY=()
        cur="${COMP_WORDS[COMP_CWORD]}"

        if [ "${#2}" = "1" ]; then
            D1=$(comp "${ARCHIVE}/[0-9]-.*" 1)
            echo "$D1"
            target=$(printf "${D1}" | grep "$2" | xargs basename)
        elif [ "${#2}" = "3" ]; then
            D2=$(comp "${ARCHIVE}/[0-9]+-[^/]+/[0-9]{3}-.*" 2)
            target=$(printf "${D2}" | grep "$2" | xargs basename)
        elif [ "${#2}" = "6" ]; then
            D3=$(comp "${ARCHIVE}/[0-9]+-[^/]+/[0-9]{3}-[^/]+/[0-9]{3}\.[0-9]{2}-.*" 3)
            target=$(printf "${D3}" | grep "$2" | xargs basename)
        elif [ "${#2}" = "9" ]; then
            D4=$(comp "${ARCHIVE}/[0-9]+-[^/]+/[0-9]{3}-[^/]+/[0-9]{3}\.[0-9]{2}-[^/]+/[0-9]{3}\.[0-9]{2}\.[a-zA-Z]+-.*" 4)
            target=$(printf "${D4}" | grep "$2" | xargs basename)
        fi
        echo "$target"
        COMPREPLY=($(compgen -W "${target}" $cur))
    fi
}