Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

xrandr completion (was Re: Caching variables during completion)



Hi,

attached is my slightly improved completion for xrandr:

It completes only connected outputs and only the available modes for each output.

Best
Eike


On 02/14/2013 09:38 AM, Jan Eike von Seggern wrote:
Thanks a lot Bart and Peter!

Using HISTNO should be working.

BTW: I played around with _store_cache/_retrieve_cache but I could not
get it working because I didn't now how to retrieve an associated array
from the cache (not to mention that it's not fitting my need anyways).

Best
Eike


On 02/14/2013 01:39 AM, Bart Schaefer wrote:
On Feb 13,  8:30pm, Peter Stephenson wrote:
}
} Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
} > zstyle ':completion:*' completer _xrcache _oldlist _expand
_complete # etc.
}
} I don't see why you couldn't put that inside the completion function
} that needs the cache

True, you could put it in the completion function, but what if there's
more than one completion function that wants to share the same cache?

Which reminds me that I've never really bothered to understand how the
_store_cache/_retrieve_cache stuff is supposed to work.  I suspect it
would be overkill here.

#compdef xrandr
local context state state_descr line
typeset -A opt_args
local expl
local outputs

# TODO: This creates a global variable to cache the available outputs. 
if [[ $_xr_HISTNO != $HISTNO ]];
then
  _xr_HISTNO=$HISTNO
  typeset -gA _xr_modes 
  _xr_modes=()
  local last_output l
  for l in ${(f)"$(xrandr -q)"}; do
    l=${(z)l}
    if [[ $l[2] == "connected" ]]; then
      last_output=$l[1]
    elif [[ $l[2] == "disconnected" ]]; then
      last_output=""
    elif [[ -n $last_output ]]; then
      _xr_modes[$last_output]="$_xr_modes[$last_output]${_xr_modes[$last_output]+ }$l[1]"
    fi
  done
fi
outputs=${(k)_xr_modes}

_arguments \
  '(-d -display)'{-d,-display}'[X display to use]:X display:_x_display' \
  '-help[display help]' \
  '(-o --orientation)'{-o,--orientation}'[orientation of screen]:rotation:(normal inverted left right 0 1 2 3)' \
  '(-q --query)'{-q,--query}'[display current state]' \
  '(-s --size)'{-s,--size}'[set screen size]:size:' \
  '(-r --rate --refresh)'{*-r,*--rate,*--refresh}'[set refresh rate]:target refresh rate:' \
  '(-v --version)'{-v,--version}'[display version]' \
  '-x[reflect across X axis]' \
  '-y[reflect across Y axis]' \
  '--screen:X screen number' \
  '--verbose[be more verbose]' \
  '--dryrun[make no changes]' \
  "--nograb[don\'t grab screen]" \
  '(--prop --properties)'{--prop,--properties}'[display the contents of properties for each output]' \
  '--fb[configure screen size]:size:' \
  '--fbmm[configure physical screen size]:size:' \
  '--dpi[configure DPI]:dpi:' \
  "*--output[configure output]:output to reconfigure:($outputs)" \
  '*--auto[select preferred mode]' \
  "*--mode[select mode for output]:output mode:->mode" \
  '*--preferred[select preferred mode]' \
  '*--pos[position output]:position:' \
  '*--reflect[reflect across axes]:axes:(normal x y xy)' \
  '*--rotate[rotate output]:rotation:(normal inverted left right)' \
  "*--left-of[position output]:relative position to:($outputs)" \
  "*--right-of[position output]:relative position to:($outputs)" \
  "*--above[position output]:relative position to:($outputs)" \
  "*--below[position output]:relative position to:($outputs)" \
  "*--same-as[position output]:relative position to:($outputs)" \
  '*--set[set property]:property:(Backlight scaling\ mode):value:->value' \
  '*--scale[scale output]:output scaling XxY:' \
  '*--transform[transform output]:transformation matrix:' \
  '*--off[disable the output]' \
  '*--crtc:crtc to use:' \
  '*--panning[enable panning]:panning:' \
  '*--gamma:r\:g\:b:' \
  '*--primary[select output as primary]' \
  '--noprimary' \
  '*--newmode[add new mode line]:name: :clock MHz: :hdisp: :hsync-start: :hsync-end: :htotal: :vdisp: :vsync-start: :vsync-end: :vtotal:' \
  '*--rmmode[remove mode line]:Mode name:' \
  "*--addmode[add new mode to output]:output:($outputs):name:" \
  "*--delmode[remove mode from output]:output:($outputs):name:" \
  && return 0

if [[ $state == value ]]; then
    case $words[CURRENT-1] in
	(scaling* mode)
	    _description value expl "output property 'scaling mode'"
	    compadd "$@" "$expl[@]" None Full Center Full\ aspect && return 0
	    ;;
    esac
elif [[ $state == mode ]]; then
    local i=1 last_output
    while (( CURRENT - i > 2 )); do
        if [[ $words[CURRENT-1-i] == "--output" ]]; then
            last_output=$words[CURRENT-i]
            break
        fi
        (( i += 1 ))
    done
    _description -V mode expl "output mode"
    compadd "$@" "$expl[@]" ${(z)_xr_modes[$last_output]} && return 0
fi


Messages sorted by: Reverse Date, Date, Thread, Author