zsh-users
 help / color / Atom feed
* Grouping and ordering completions with zshcompsys?
@ 2019-10-21  6:57 Chris Nebel
  2019-10-21  7:02 ` Chris Nebel
  0 siblings, 1 reply; 3+ messages in thread
From: Chris Nebel @ 2019-10-21  6:57 UTC (permalink / raw)
  To: zsh-users

zsh-users—

I’ve been fiddling with a zsh completion function for darwinup(1), partly as a learning exercise, partly because I wanted one.  (I’m aware of <https://github.com/tetsuok/darwinup-zsh-completion>, but it’s not as smart as I wanted, and learning exercise.)  For the most part I’ve got it working, but I’m struggling with grouping and ordering.

For background, darwinup(1) manages installed “archives” — it’s basically a simple package manager — and if you ask for a list of what’s installed, you get something like this:

Serial UUID                                  Date          Build    Name
====== ====================================  ============  =======  =================
1      6F5662BD-4D69-470E-B708-3D0F3706D0A6  Sep 20 00:16  18A396   omg.tgz
2      CB732989-ACB4-49B4-B49C-64F40EF2B17B  Sep 22 00:16  17A42    zomg.tgz
4      C2AF0BF1-9FA3-4EE7-A402-32CDBCDA0F8C  Sep 23 00:18  18F205   bbq.tgz
42     52FC4789-769C-4CD8-A39B-D1B6CC0B2009  Oct 01 12:34  18F205   foobar.tgz

darwinup has various sub-commands which take archives as arguments, which can be specified by “serial”, UUID, or name, so I’d like to complete the serial number or name.  Additionally, there are four meta-names: “all”, “newest”, “oldest”, and “superseded”, which should also be completable.

Merely providing all those as completion options is easy — what I haven’t been able to do is get them listed the way I want, which is something like this:

> archives
> 1      omg.tgz   -- installed on 18A396 at Oct 15 00:16
> 2      zomg.tgz  -- installed on 17A42 at Oct 19 00:16  
> 4      bbq.tgz -- installed on 18F205 at Oct 20 00:18
> 42     foobar.tgz -- installed on 18F205 at Oct 20 00:19
> 
> meta-archives
> all -- all archives
> newest -- most recently installed archive
> oldest -- least recently installed archive
> superseded -- all archives replaced by other archives

Notice the two distinct groups with names, and the serial numbers appearing before the names.  For bonus points, I’d like the exclusion behavior that “_arguments” has, so it won’t complete the same archive more than once, and if the user already typed “all” it won’t complete anything more.  The closest I’ve managed to get this this:

omg.tgz     1  -- installed on 18A396 at Oct 15 00:16
zomg.tgz    2  -- installed on 17A42 at Oct 19 00:16  
bbq.tgz      4  -- installed on 18F205 at Oct 20 00:18
foobar.tgz   42 -- installed on 18F205 at Oct 20 00:19
all     newest     oldest     superseded

I can get force the meta-archive names to appear together at the end by not giving them descriptions, but then they don’t have descriptions.  I got the regular archives to sort in numeric order by ordering them myself and using “_describe -V”, but then I lose the exclusions, and everything I’ve tried so far puts the serial numbers *after* the names.  It’s not terrible, but I’m at a loss for how to do better.  An example would help, but I haven’t yet found an existing command that does anything like this.  Any pointers?


—Chris N.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Grouping and ordering completions with zshcompsys?
  2019-10-21  6:57 Grouping and ordering completions with zshcompsys? Chris Nebel
@ 2019-10-21  7:02 ` Chris Nebel
  2019-10-21  8:51   ` dana
  0 siblings, 1 reply; 3+ messages in thread
From: Chris Nebel @ 2019-10-21  7:02 UTC (permalink / raw)
  To: zsh-users

Related question: is there a way to get _files to only complete files that pass a test of some sort?  Specifically, I want files that are in one of four (possibly compressed) archive formats.  I can get a thumbs-up/thumbs-down decision using file(1), but I don’t see how to communicate that to _files.

> On Oct 20, 2019, at 11:57 PM, Chris Nebel <c.nebel@mac.com> wrote:
> 
> zsh-users—
> 
> I’ve been fiddling with a zsh completion function for darwinup(1), partly as a learning exercise, partly because I wanted one.  (I’m aware of <https://github.com/tetsuok/darwinup-zsh-completion>, but it’s not as smart as I wanted, and learning exercise.)  For the most part I’ve got it working, but I’m struggling with grouping and ordering.
> 
> For background, darwinup(1) manages installed “archives” — it’s basically a simple package manager — and if you ask for a list of what’s installed, you get something like this:
> 
> Serial UUID                                  Date          Build    Name
> ====== ====================================  ============  =======  =================
> 1      6F5662BD-4D69-470E-B708-3D0F3706D0A6  Sep 20 00:16  18A396   omg.tgz
> 2      CB732989-ACB4-49B4-B49C-64F40EF2B17B  Sep 22 00:16  17A42    zomg.tgz
> 4      C2AF0BF1-9FA3-4EE7-A402-32CDBCDA0F8C  Sep 23 00:18  18F205   bbq.tgz
> 42     52FC4789-769C-4CD8-A39B-D1B6CC0B2009  Oct 01 12:34  18F205   foobar.tgz
> 
> darwinup has various sub-commands which take archives as arguments, which can be specified by “serial”, UUID, or name, so I’d like to complete the serial number or name.  Additionally, there are four meta-names: “all”, “newest”, “oldest”, and “superseded”, which should also be completable.
> 
> Merely providing all those as completion options is easy — what I haven’t been able to do is get them listed the way I want, which is something like this:
> 
>> archives
>> 1      omg.tgz   -- installed on 18A396 at Oct 15 00:16
>> 2      zomg.tgz  -- installed on 17A42 at Oct 19 00:16  
>> 4      bbq.tgz -- installed on 18F205 at Oct 20 00:18
>> 42     foobar.tgz -- installed on 18F205 at Oct 20 00:19
>> 
>> meta-archives
>> all -- all archives
>> newest -- most recently installed archive
>> oldest -- least recently installed archive
>> superseded -- all archives replaced by other archives
> 
> Notice the two distinct groups with names, and the serial numbers appearing before the names.  For bonus points, I’d like the exclusion behavior that “_arguments” has, so it won’t complete the same archive more than once, and if the user already typed “all” it won’t complete anything more.  The closest I’ve managed to get this this:
> 
> omg.tgz     1  -- installed on 18A396 at Oct 15 00:16
> zomg.tgz    2  -- installed on 17A42 at Oct 19 00:16  
> bbq.tgz      4  -- installed on 18F205 at Oct 20 00:18
> foobar.tgz   42 -- installed on 18F205 at Oct 20 00:19
> all     newest     oldest     superseded
> 
> I can get force the meta-archive names to appear together at the end by not giving them descriptions, but then they don’t have descriptions.  I got the regular archives to sort in numeric order by ordering them myself and using “_describe -V”, but then I lose the exclusions, and everything I’ve tried so far puts the serial numbers *after* the names.  It’s not terrible, but I’m at a loss for how to do better.  An example would help, but I haven’t yet found an existing command that does anything like this.  Any pointers?
> 
> 
> —Chris N.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Grouping and ordering completions with zshcompsys?
  2019-10-21  7:02 ` Chris Nebel
@ 2019-10-21  8:51   ` dana
  0 siblings, 0 replies; 3+ messages in thread
From: dana @ 2019-10-21  8:51 UTC (permalink / raw)
  To: Chris Nebel; +Cc: zsh-users

On 21 Oct 2019, at 01:57, Chris Nebel <c.nebel@mac.com> wrote:
> Notice the two distinct groups with names, and the serial numbers appearing
> before the names.  For bonus points, I’d like the exclusion behavior that
> “_arguments” has, so it won’t complete the same archive more than once, and
> if the user already typed “all” it won’t complete anything more.  The
> closest I’ve managed to get this this:

Grouping is usually something you leave up to the user's group-name style. If
they have zsh configured to keep groups separate, they will get that
behaviour. If they don't, they won't. It's possible to force the issue in an
individual completion function, but i think generally you're meant to assume
that it's configured how they want it.

For putting the serial numbers in the names, you can make use of the name2
argument to _describe, like this (just to illustrate the basic concept):

  name1=( {'1  omg','2  zomg','4  bbq','42 foobar'}.tgz )
  name2=( ${(@)name1##[0-9\ ]##} )
  _describe -2Vt archives archive name1 name2

When you add descriptions (to name1), though, it seems to ignore -12JVo and
just put the matches in some random order. I don't understand why, and i can't
look into it right now. Maybe it's expected.

To separate out the meta-archives, i guess just use _alternative:

  _alternative \
    'archives: :_foo_archives' \
    'meta-archives: :_foo_meta_archives'

Where _foo_archives and _foo_meta_archives are your helper functions that
handle completion for those particular matches.

For handling the `all` meta-archive, you can just check to see if `all` is in
$line or $words before deciding how to proceed (again, just to illustrate,
you probably wouldn't do it *exactly* like this):

  if (( $line[(I)all] )); then
    _message 'no more archives'
  else
    _alternative ...
  fi

On 21 Oct 2019, at 02:02, Chris Nebel <c.nebel@mac.com> wrote:
> Related question: is there a way to get _files to only complete files that
> pass a test of some sort?  Specifically, I want files that are in one of
> four (possibly compressed) archive formats.

Usually you give it a pattern that matches file extensions with -g, as in:

  _files -g '*.(tgz|txz|...)(#q-.)'

If you can't get the information you need with globbing, there are ways to be
more clever about it, but that's not a super common thing to do.

dana


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-21  6:57 Grouping and ordering completions with zshcompsys? Chris Nebel
2019-10-21  7:02 ` Chris Nebel
2019-10-21  8:51   ` dana

zsh-users

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-users

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git