summaryrefslogtreecommitdiffstats
path: root/bash-completion
blob: 4e548af71a6ece3dd43d2526b6eaf8d09b5f922e (plain)
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
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
# mkinitcpio bash completion by Seblu <seblu@seblu.net>

_lsinitcpio() {
    local cur opts
    opts=(-a --analyze -c --config -h --help -n --nocolor -v --verbose -x --extract)

    _get_comp_words_by_ref cur

    case $cur in
        -*)
            COMPREPLY=($(compgen -W "${opts[*]}" -- "$cur")) ;;
        *)
            _filedir ;;
    esac
}

_find_kernel_versions() {
    local -a matches
    local dir regex

    # add completions from kernels in /boot
    regex="Linux kernel.*version"
    while IFS=':' read -r file metadata; do
        [[ $metadata =~ $regex ]] || continue
        matches+=("$file")
    done < <(file -e ascii /boot/*)

    # add completions based on kernel versions in /lib/modules
    for dir in /lib/modules/*/kernel; do
        dir=${dir%/kernel}
        matches+=("${dir#/lib/modules/}")
    done

    COMPREPLY=($(compgen -W "${matches[*]}" -- $cur))
}

_files_from_dirs() {
    local files stripsuf d f

    if [[ $1 = -s ]]; then
        stripsuf=$2
        shift 2
    fi

    for d in "$@"; do
        for f in "$d"/*; do
            [[ -f $f ]] && files+=("${f##*/}")
        done
    done

    printf '%s\n' "${files[@]%$stripsuf}"
}

_mkinitcpio() {
    local action cur prev opts
    opts=(-A --addhooks -c --config -g --generate -H --hookhelp -h --help -k --kernel
          -L --listhooks -M --automods -n --nocolor -p --preset -r --moduleroot
          -S --skiphooks -s --save -t --builddir -v --verbose -z --compress)

    _get_comp_words_by_ref cur prev

    case $prev in
        -[cg]|--config|--generate)
            _filedir ;;
        -r|--moduleroot|-t|--builddir)
            _filedir -d ;;
        -k|--kernel)
            _find_kernel_versions ;;
        -p|--preset)
            COMPREPLY=($(compgen -W "$(_files_from_dirs -s .preset /etc/mkinitcpio.d)" -- "$cur")) ;;
        -[AHS]|--add|--hookhelp|--skiphooks)
            COMPREPLY=($(compgen -W "$(_files_from_dirs {/usr,}/lib/initcpio/install)" -- "$cur")) ;;
        *)
            COMPREPLY=($(compgen -W "${opts[*]}" -- "$cur")) ;;
    esac
}

complete -F _mkinitcpio mkinitcpio
complete -F _lsinitcpio lsinitcpio

# vim: set et ts=4 sw=4 ft=sh: