~dricottone/my-utils

ref: 618b5e7801dd9bdc6100b6ed88f04d9dcd75a3ab my-utils/bash-completion/mybashcompletion.bash -rw-r--r-- 1.7 KiB
618b5e78Dominic Ricottone Slight refactor of a usage message 3 years 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
#!/bin/bash

# epub
# Function tries to find a target archive in arguments already specified, and
# then tries to list the filenames stored inside the archive
_epub_completion() {
  # set fallback completion as default (filenames) with 'complete -o default'
  COMPREPLY=()
  # search for a filename in arguments, skipping the first (executable name)
  for arg in "${COMP_WORDS[@]:1}"; do
    if [[ -f "$arg" ]]; then
      # on finding a filename, try to complete with archive entries
      local list=( $(epub --list "$arg" | sort) )
      if [[ "${#list[@]}" -ge 1 ]]; then
        COMPREPLY=( $(compgen -W "${list[*]}" -- "${COMP_WORDS[$COMP_CWORD]}") )
      else
        # if not an archive, or an archive with no entries, halt completion
        compopt +o default
      fi
      break
    fi
  done
}
# Complete with function, and fallback to filenames
complete -o default -F _epub_completion epub

# rmzip, zipls
# Complete with filenames matching pattern '*.@(zip|cbr|epub)', and fallback to
# filenames
complete -o default -f -X '!*.@(zip|cbr|epub)' rmzip

# rmtar, tarcat, tarls, untar
# Complete with filenames matching pattern
# '*.@(tar|tar.@(gz|xz|zst|bz2)|tar.@(gz|xz|zst|bz2).gpg)', and fallback to
# filenames
complete -o default -f -X '!*.@(tar|tar.@(gz|xz|zst|bz2)|tar.@(gz|xz|zst|bz2).gpg)' rmtar
complete -o default -f -X '!*.@(tar|tar.@(gz|xz|zst|bz2)|tar.@(gz|xz|zst|bz2).gpg)' tarcat
complete -o default -f -X '!*.@(tar|tar.@(gz|xz|zst|bz2)|tar.@(gz|xz|zst|bz2).gpg)' tarls
complete -o default -f -X '!*.@(tar|tar.@(gz|xz|zst|bz2)|tar.@(gz|xz|zst|bz2).gpg)' untar

# whichcat, whiched. whichhead, whichvi
# Complete with program names
complete -c whichcat
complete -c whichhead
complete -c whiched
complete -c whichvi