
source $SCRIPT_DIR/../modules/completion

## arsh completion
call $BIN_NAME --help | grep '  -' | cut -d ' ' -f 3 | cut -d '[' -f 1 |
for $line in $STDIN {
    var k = $line.startsWith('--') ? "--long" : "--short"
    var o = $line.startsWith('--') ? $line.slice(2) : $line.slice(1)
    ($o == 'e' || $o.startsWith('dump')) && continue
    compdef --cmd arsh $k $o
}
compdef --cmd arsh --long dump-ast= --arg-action file
compdef --cmd arsh --long dump-untyped-ast= --arg-action file
compdef --cmd arsh --long dump-code= --arg-action file
compdef --cmd arsh --short e --arg-action cmd

## arshd
compdef --cmd arshd --long debounce-time
compdef --cmd arshd --long help
compdef --cmd arshd --long language-server
compdef --cmd arshd --long log  --arg-list 'debug info warning error fatal'
compdef --cmd arshd --long test --arg-action file
compdef --cmd arshd --long test-open --arg-action file
compdef --cmd arshd --long wait-time

## dscolorize
compdef --cmd dscolorize --long daemon
compdef --cmd dscolorize --long html-full
compdef --cmd dscolorize --long html-lineno=
compdef --cmd dscolorize --short h
compdef --cmd dscolorize --short l
compdef --cmd dscolorize --short o --arg-action file

if (command -v dscolorize &>> /dev/null) {
    var lines = IFS=$'\n' $(dscolorize -l)
    let size = $lines.size()
    var i = 0
    if $i < $size && $lines[$i] == 'Styles:' {
        var list = ""
        $i++
        for (; $i < $size && $lines[$i] !~ $/^\w/; $i++) {
            var line = $lines[$i]
            $line.startsWith("*") || continue
            $line = $line.replace("* ", "")
            if !$list.empty() { $list += " "; }
            $list += $line
        }
        compdef --cmd dscolorize --short s --arg-list "$list"
    }
    if $i < $size && $lines[$i] == 'Formatters:' {
        var list = ""
        $i++
        for (; $i < $size && $lines[$i] !~ $/^\w/; $i++) {
            var line = $lines[$i]
            $line.startsWith("*") || continue
            $line = $line.replace("* ", "")
            if !$list.empty() { $list += " "; }
            $list += $line
        }
        compdef --cmd dscolorize --short f --arg-list "$list"
    }
}
$PIPESTATUS.clear()

true
