_bat() {
    local i cur prev opts cmds
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    cmd=""
    opts=""

    for i in ${COMP_WORDS[@]}
    do
        case "${i}" in
            bat)
                cmd="bat"
                ;;
            
            cache)
                cmd+="__cache"
                ;;
            *)
                ;;
        esac
    done

    case "${cmd}" in
        bat)
            opts=" -p -n -u -h -V -l -m  --list-languages --list-themes --plain --number --unbuffered --no-config --config-file --help --version --language --map-syntax --theme --style --line-range --color --decorations --paging --wrap --tabs --terminal-width  <FILE>...  cache"
            if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
                COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
                return 0
            fi
            case "${prev}" in
                
                --language)
                    COMPREPLY=($(compgen -f ${cur}))
                    return 0
                    ;;
                    -l)
                    COMPREPLY=($(compgen -f ${cur}))
                    return 0
                    ;;
                --map-syntax)
                    COMPREPLY=($(compgen -f ${cur}))
                    return 0
                    ;;
                    -m)
                    COMPREPLY=($(compgen -f ${cur}))
                    return 0
                    ;;
                --theme)
                    COMPREPLY=($(compgen -f ${cur}))
                    return 0
                    ;;
                --style)
                    COMPREPLY=($(compgen -W "auto full plain changes header grid numbers" -- ${cur}))
                    return 0
                    ;;
                --line-range)
                    COMPREPLY=($(compgen -f ${cur}))
                    return 0
                    ;;
                --color)
                    COMPREPLY=($(compgen -W "auto never always" -- ${cur}))
                    return 0
                    ;;
                --decorations)
                    COMPREPLY=($(compgen -W "auto never always" -- ${cur}))
                    return 0
                    ;;
                --paging)
                    COMPREPLY=($(compgen -W "auto never always" -- ${cur}))
                    return 0
                    ;;
                --wrap)
                    COMPREPLY=($(compgen -W "auto never character" -- ${cur}))
                    return 0
                    ;;
                --tabs)
                    COMPREPLY=($(compgen -f ${cur}))
                    return 0
                    ;;
                --terminal-width)
                    COMPREPLY=($(compgen -f ${cur}))
                    return 0
                    ;;
                *)
                    COMPREPLY=()
                    ;;
            esac
            COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
            return 0
            ;;
        
        bat__cache)
            opts=" -i -c -d -h -V  --init --clear --config-dir --blank --help --version --source --target  "
            if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
                COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
                return 0
            fi
            case "${prev}" in
                
                --source)
                    COMPREPLY=($(compgen -f ${cur}))
                    return 0
                    ;;
                --target)
                    COMPREPLY=($(compgen -f ${cur}))
                    return 0
                    ;;
                *)
                    COMPREPLY=()
                    ;;
            esac
            COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
            return 0
            ;;
    esac
}

complete -F _bat -o bashdefault -o default bat
