#compdef hwatch

autoload -U is-at-least

_hwatch() {
    typeset -A opt_args
    typeset -a _arguments_options
    local ret=1

    if is-at-least 5.2; then
        _arguments_options=(-s -S -C)
    else
        _arguments_options=(-s -C)
    fi

    local context curcontext="$curcontext" state line
    _arguments "${_arguments_options[@]}" : \
'*-A+[Executes the specified command if the output changes. Information about changes is stored in json format in environment variable \${HWATCH_DATA}.]: :_cmdstring' \
'*--aftercommand=[Executes the specified command if the output changes. Information about changes is stored in json format in environment variable \${HWATCH_DATA}.]: :_cmdstring' \
'*-l+[logging file]' \
'*--logfile=[logging file]' \
'*-s+[shell to use at runtime. can  also insert the command to the location specified by {COMMAND}.]: :_cmdstring' \
'*--shell=[shell to use at runtime. can  also insert the command to the location specified by {COMMAND}.]: :_cmdstring' \
'*-n+[seconds to wait between updates]: : ' \
'*--interval=[seconds to wait between updates]: : ' \
'--precise[Attempt to run as close to the interval as possible, regardless of how long the command takes to run]: : ' \
'-L+[Set the number of history records to keep. only work in watch mode. Set \`0\` for unlimited recording. (default\: 5000)]: : ' \
'--limit=[Set the number of history records to keep. only work in watch mode. Set \`0\` for unlimited recording. (default\: 5000)]: : ' \
'*--tab-size=[Specifying tab display size]: : ' \
'*-d+[highlight changes between updates]' \
'*--differences=[highlight changes between updates]' \
'*-o+[Select command output.]' \
'*--output=[Select command output.]' \
'*-K+[Add keymap]: : ' \
'*--keymap=[Add keymap]: : ' \
'--completion=[Output shell completion script]: :(bash fish zsh)' \
'-b[output exection results to stdout]' \
'--batch[output exection results to stdout]' \
'-B[beep if command has a change result]' \
'--beep[beep if command has a change result]' \
'*-g[exit when output changes. With no value, exits after the first change; with N, exits after N changes]:count: ' \
'*--chgexit=[exit when output changes. With no value, exits after the first change; with N, exits after N changes]:count: ' \
'--border[Surround each pane with a border frame]' \
'--with-scrollbar[When the border option is enabled, display scrollbar on the right side of watch pane.]' \
'--mouse[enable mouse wheel support. With this option, copying text with your terminal may be harder. Try holding the Shift key.]' \
'-c[interpret ANSI color and style sequences]' \
'--color[interpret ANSI color and style sequences]' \
'-r[display text upside down.]' \
'--reverse[display text upside down.]' \
'-C[Compress data in memory. Note\: If the output of the command is small, you may not get the desired effect.]' \
'--compress[Compress data in memory. Note\: If the output of the command is small, you may not get the desired effect.]' \
'-t[hide the UI on start. Use \`t\` to toggle it.]' \
'--no-title[hide the UI on start. Use \`t\` to toggle it.]' \
'-N[show line number]' \
'--line-number[show line number]' \
'--no-help-banner[hide the "Display help with h key" message]' \
'-x[Run the command directly, not through the shell. Much like the \`-x\` option of the watch command.]' \
'--exec[Run the command directly, not through the shell. Much like the \`-x\` option of the watch command.]' \
'-O[Display only the lines with differences during \`line\` diff and \`word\` diff.]' \
'--diff-output-only[Display only the lines with differences during \`line\` diff and \`word\` diff.]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'*::command:_hwatch_command' \
&& ret=0
}

(( $+functions[_hwatch_commands] )) ||
_hwatch_commands() {
    local commands; commands=()
    _describe -t commands 'hwatch commands' commands "$@"
}

_hwatch_command() {
    local -a cmd_words
    local cmd_index i

    cmd_words=("${words[@]}")
    cmd_index=0

    for ((i=2; i<=${#cmd_words}; i++)); do
        case "${cmd_words[i]}" in
            --)
                cmd_index=$((i + 1))
                break
                ;;
            -A|--aftercommand|-l|--logfile|-s|--shell|-n|--interval|-L|--limit|--tab-size|-K|--keymap|--completion|-g|--chgexit)
                i=$((i + 1))
                ;;
            -d|--differences)
                if [[ "${cmd_words[i+1]}" == "none" || "${cmd_words[i+1]}" == "watch" || "${cmd_words[i+1]}" == "line" || "${cmd_words[i+1]}" == "word" ]]; then
                    i=$((i + 1))
                fi
                ;;
            -o|--output)
                if [[ "${cmd_words[i+1]}" == "output" || "${cmd_words[i+1]}" == "stdout" || "${cmd_words[i+1]}" == "stderr" ]]; then
                    i=$((i + 1))
                fi
                ;;
            -* )
                ;;
            *)
                cmd_index=$i
                break
                ;;
        esac
    done

    if (( cmd_index > 0 )); then
        words=("${words[@]:$((cmd_index - 1))}")
        CURRENT=$((CURRENT - cmd_index + 1))
        if (( CURRENT < 1 )); then
            CURRENT=1
        fi
        _normal
    else
        _command_names -e
    fi
}

if [ "$funcstack[1]" = "_hwatch" ]; then
    _hwatch "$@"
else
    compdef _hwatch hwatch
fi
