Subject: Re: Feature request: readline's completion-prefix-display-length option
From: Daniel Shahaf <[email-masked]>
To: Hong Xu <[email-masked]>
Cc: Zsh hackers list <[email-masked]>
Date: Thu, 19 May 2016 21:14:27 +0000
List-Id: Zsh Workers List <zsh-workers.zsh.org>

Bart Schaefer wrote on Sun, May 15, 2016 at 04:50:50 -0700:
> On Sat, May 14, 2016 at 11:08 PM, Hong Xu <[email-masked]> wrote:
> >
> > I believe that readline's completion-prefix-display-length is a great
> > option to have for zle. This option enables the common part of long
> > completions to be represented by ellipsis, so the options in the
> > completion list would not be too long in many cases.
> 
> There might be a deviously clever way to do this with the list-colors
> style, similar to the way that show-ambiguity is implemented.

Like this?

[[[
$ zsh -f
% autoload compinit && compinit
% zstyle -e ':completion:*' list-colors $'reply=( "=(#b)(${(b)PREFIX})(?)([^ ]#)*=0=33=${#PREFIX}D…\e[34=33" )'
% rsync --no-<TAB>
…D                        -- turn off --devices and --specials                                                                                                        
…blocking-io              -- turn off --blocking-io                                                                                                                   
…detach                   -- do not detach from the parent                                                                                                            
…devices                  -- turn off --devices                                                                                                                       
…dirs           …d    -- turn off --dirs                                                                                                                          
…group          …g    -- turn off --group                                                                                                                         
…hard-links     …H    -- turn off --hard-links                                                                                                                    
]]]

This is simply a bobby tables solution:

- The xterm control sequence for coloring is «\e[42m».
- The xterm control sequence for "move left" is «\e[42D».
- Setting the part between the two '=' signs to «42Dfoobar\e[43» causes
  the cursor to move left 42 spaces (over the common prefix already
  output), insert "foobar", and switch to color number 43.

(reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html)

This solution breaks alignment¹ since zle doesn't account for the
cursor movement; a workaround for that is to add padding:

[[[
% zstyle -e ':completion:*' list-colors $'reply=( "=(#b)(${(b)PREFIX})(?)([^ ]#)*=0=33=${#PREFIX}D${(r:$#PREFIX-1:: :):-}…\e[34=33" )'
% rsync --delete<TAB>
--delete                               -- delete files that do not exist on the sending side                                                                              
       …-before                        -- receiver deletes before transfer                                                                                                
       …-delay                …-after  -- receiver deletes after transfer                                                                                                 
       …-during                        -- receiver deletes during transfer                                                                                                
       …-excluded                      -- also delete excluded files on the receiving side                                                                                
       …-missing-args                  -- delete missing source args from destination                                                                                     
]]]

However, this workaround doesn't make the listing occupy less columns,
which was the original request.

Cheers,

Daniel

¹ Alignment is broken in two cases: (a) when both ${PREFIX} and
${PREFIX}foo are possible completions; (b) when some completions have the
same description as each other and are shown together.  If ${PREFIX} is
a proper prefix of all possible completions and all descriptions are
unique, the listing will be aligned, but descriptions will not use the
full terminal width.