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

Re: How to do completion of toggle flag sequences separated by +/-



On Sun, 01 May 2011 12:16:05 +0200
Haakon Riiser <haakon.riiser@xxxxxxxxxx> wrote:
>  I'm still working on perfecting the FFmpeg completion function, and one 
>  of the most important things missing is good completion of toggle flags.
> 
>  Here's an example of FFmpeg's toggle flag syntax:
> 
>  ffmpeg -flags +gmc-global_header+cgop
> 
>  In the above example, three toggle flags are set:
> 
>  gmc is enabled (+)
>  global_header is disabled (-)
>  cgop is enabled (+)

I don't think there's a prepackaged way of doing this.  I've had some
luck with the following.  One thing it doesn't do is treat the + and -
as separate from the options following, but a bit of patience might sort
that out.

It's actually written to complete options for the fictitious "flags"
command, so you also have to insinuate it into ffmpeg completion
somehow.


#compdef flags

# Attempt to get something that completes
# +flag1+flag2-flag3 etc.

_flag_options() {
  local expl
  _wanted options expl 'option' compadd -S '' -- {-,+}${^flag_options}
}

_more_flag_options() {
  compset -p $1 && _flag_options
}

_new_flag_options() {
  compset -P '*' && _flag_options
}

_flags() {
  local -a flag_options
  flag_options=(foo foobar bar)

  local match mbegin mend
  integer ret=1

  if [[ $PREFIX = (#b)(*)[-+]([^-+]#) ]]; then
    if [[ -n ${flag_options[(R)$match[2]]} ]]; then
      _new_flag_options && ret=0
    fi
    if [[ -n ${flag_options[(R)$match[2]?*]} ]]; then
      _more_flag_options ${#match[1]} && ret=0
    fi
  else
    _flag_options && ret=0
  fi

  return $ret
}

_flags "$@"


-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



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