#compdef autorandr

__autorandr_profile () {
    declare -a virtual
    virtual=("off":"disable all outputs"
             "common":"clone at the largest common resolution"
             "clone-largest":"clone with the largest resolution"
             "horizontal":"stack all connected outputs horizontally"
             "vertical":"stack all connected outputs vertically")
    _describe -t virtual-profiles "virtual profiles" virtual
    __autorandr_saved_profile
}
__autorandr_saved_profile () {
    declare -a saved
    saved=(${${(f)${:-"$(autorandr)"}}/ /:})
    _describe -t profiles "saved profiles" saved
}

_autorandr () {
    local curcontext="$curcontext" state line exclude="-s --save -l --load -r --remove -c --change"

    _arguments -C \
       "(: -)"{-h,--help}"[get help]" \
       "($exclude)"{-c,--change}"[automatically load the first detected profile]" \
       "($exclude)"{-d,--default}"[set default profile]:profile:__autorandr_profile" \
       "($exclude)"{-l,--load}"[load profile]:profile:__autorandr_profile" \
       "($exclude)"{-s,--save}"[save current setup to a profile]:profile: " \
       "($exclude)"{-r,--remove}"[remove profile]:profile:__autorandr_saved_profile" \
       --batch"[run autorandr for all users]" \
       --current"[list current active configurations]" \
       --cycle"[cycle through all detected profiles]" \
       --config"[dump current xrandr setup]" \
       --debug"[enable verbose output]" \
       --dry-run"[don't change anything]" \
       --fingerprint"[fingerprint current hardware]" \
       --match-edid"[match displays using edid]" \
       --force"[force loading of a profile]" \
       --list"[list all profiles]" \
       --skip-options"[skip xrandr options]:xrandr options:_values -s , options gamma brightness panning transform primary mode pos rate" \
       --version"[show version]"
}

_autorandr "$@"
