aboutsummaryrefslogtreecommitdiff
path: root/transcode.sh.bash-completion
blob: 558cf094f0d75025b521af09d38c87389b0223b7 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# SPDX-FileCopyrightText: 2025 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

# bash completion for transcode.sh
# Install to:
#   /etc/bash_completion.d/transcode.sh
# or source manually:
#   source transcode.sh.bash-completion
#
# Each option that takes an argument is supported in all three forms:
#   short          -f list.txt
#   long space     --encode-file list.txt
#   long equals    --encode-file=list.txt

_transcode_sh() {
    local cur prev words cword
    _init_completion || return

    local -r preset_dir="${XDG_CONFIG_HOME:-$HOME/.config}/transcode.sh/presets"

    # ---------------------------------------------------------------------------
    # Helper: collect preset names from the preset directory
    # ---------------------------------------------------------------------------
    _transcode_presets() {
        local presets=()
        if [[ -d "$preset_dir" ]]; then
            local f
            while IFS= read -r f; do
                f="${f##*/}"   # basename
                f="${f%.sh}"   # strip .sh extension
                presets+=("$f")
            done < <(find "$preset_dir" -maxdepth 1 -name '*.sh' -type f 2>/dev/null)
        fi
        printf '%s\n' "${presets[@]}"
    }

    local codecs='h264 hevc av1 vp8 vp9 mpeg2video mpeg4 mjpeg theora'
    local nice_vals='0 5 10 15 19'

    # ---------------------------------------------------------------------------
    # Handle --option=VALUE: cur contains the entire "--opt=val" token.
    # Strip the "opt=" prefix and delegate to the appropriate completer.
    # This must be checked before the $prev dispatch below.
    # ---------------------------------------------------------------------------
    case "$cur" in
        --config-file=*)
            cur="${cur#*=}"
            _filedir
            return
            ;;
        --encode-file=*)
            cur="${cur#*=}"
            _filedir
            return
            ;;
        --saving-file=*)
            cur="${cur#*=}"
            _filedir
            return
            ;;
        --nice=*)
            cur="${cur#*=}"
            COMPREPLY=( $(compgen -W "$nice_vals" -- "$cur") )
            return
            ;;
        --skip-codec=*)
            cur="${cur#*=}"
            COMPREPLY=( $(compgen -W "$codecs" -- "$cur") )
            return
            ;;
        --preset=*)
            cur="${cur#*=}"
            COMPREPLY=( $(compgen -W "$(_transcode_presets)" -- "$cur") )
            return
            ;;
    esac

    # ---------------------------------------------------------------------------
    # Handle VALUE after short form (-f, -N, -S, -p) and
    # long space form (--encode-file, --nice, --saving-file, --skip-codec, --preset).
    # $prev is the option word, $cur is the value being typed.
    # ---------------------------------------------------------------------------
    case "$prev" in
        --config-file)
            _filedir
            return
            ;;
        -f|--encode-file)
            _filedir
            return
            ;;
        -N|--nice)
            COMPREPLY=( $(compgen -W "$nice_vals" -- "$cur") )
            return
            ;;
        --saving-file)
            _filedir
            return
            ;;
        -S|--skip-codec)
            COMPREPLY=( $(compgen -W "$codecs" -- "$cur") )
            return
            ;;
        -p|--preset)
            COMPREPLY=( $(compgen -W "$(_transcode_presets)" -- "$cur") )
            return
            ;;
    esac

    # ---------------------------------------------------------------------------
    # Complete option names themselves.
    # For options that accept an argument we list both the bare long form
    # (--nice) and the equals form (--nice=) so the user can choose their
    # preferred style.  compopt -o nospace is applied when the sole match
    # ends with '=' to avoid an unwanted trailing space in that case.
    # ---------------------------------------------------------------------------
    case "$cur" in
        --*)
            local longopts='
                --continue
                --no-continue
                --config-file
                --config-file=
                --encode-file
                --encode-file=
                --dry-run
                --nice
                --nice=
                --saving
                --no-saving
                --saving-file
                --saving-file=
                --skip-codec
                --skip-codec=
                --only-if-smaller
                --no-only-if-smaller
                --preset
                --preset=
                --help
                --quiet
                --verbose
                --version
                --color
                --no-color
            '
            COMPREPLY=( $(compgen -W "$longopts" -- "$cur") )
            # Suppress the trailing space when the only completion ends with '='
            [[ ${#COMPREPLY[@]} -eq 1 && "${COMPREPLY[0]}" == *= ]] \
                && compopt -o nospace
            return
            ;;
        -*)
            local shortopts='-c -f -n -N -s -S -l -p -h -? -q -v --'
            COMPREPLY=( $(compgen -W "$shortopts" -- "$cur") )
            return
            ;;
    esac

    # ---------------------------------------------------------------------------
    # Default: complete positional FILE arguments with common media extensions
    # ---------------------------------------------------------------------------
    _filedir '@(mkv|mp4|mov|avi|webm|flv|wmv|m4v|ts|mts|m2ts|mpg|mpeg|ogv|3gp|rm|rmvb)'
}

complete -F _transcode_sh transcode.sh
complete -F _transcode_sh transcode