0

I have a custom ZSH tab-completion function for one of my tools. It works well, however sometimes it takes a long time for the tool to answer; is there a way to display some sort of indication that something is happening while the tool is running (and before it completes running)?

For example, is it possible to make it show a message below the current line, like:

prompt$ pypath /providers/conf<TAB>
Completing...

The challenge here is that the cursor must return to its previous position (where I hit 'TAB') once completion candidates are available. I know ZSH can do this, but can it display a message BEFORE the tool finishes running?

Here's my current completion script:

#compdef pypath

# This does not work; it is only added when the whole thing ends
# _message -r "Completing..."

IFS=$'\n' path_candidates=($(pypath "${PREFIX}*" | sed 's|.*/||' | sort -u))
compset -P '*/'

if [ -z "$path_candidates" ]; then
    compadd -x "No matches found."
else
    compadd -q -S '/' $path_candidates
fi
Whyte
  • 3
  • 1
  • Have you tried to just use `print 'Completing...'` instead of `_message`? – Adaephon Feb 24 '17 at 11:45
  • Yes, but it is not deleted afterwards which makes the completer add completions at the end of "Completing...". – Whyte Feb 24 '17 at 17:07
  • In that case you can try `print -n 'Completing...\r'`. `-n` prevents printing a newline and `\r` moves the cursor to the beginning of the line. Once the list of completions is printed it should overwrite the text. – Adaephon Feb 24 '17 at 17:31
  • The above completion code is executed at the moment when the Tab key is pressed, and the cursor is still on the line being edited. Printing \r would make the completer add the rest of the word being completed at the start of the line, overwriting my prompt, and leaving the cursor in a strange place. – Whyte Feb 24 '17 at 17:59

1 Answers1

1

You could try to use zle -R "Completing...".

zle -R [ -c ] [ display-string ] [ string ... ]
...
-R [ -c ] [ display-string ] [ string ... ]

Redisplay the command line; this is to be called from within a user-defined widget to allow changes to become visible. If a display-string is given and not empty, this is shown in the status line (immediately below the line being edited).

If the optional strings are given they are listed below the prompt in the same way as completion lists are printed. If no strings are given but the -c option is used such a list is cleared.

Note that this option is only useful for widgets that do not exit immediately after using it because the strings displayed will be erased immediately after return from the widget.

This command can safely be called outside user defined widgets; if zle is active, the display will be refreshed, while if zle is not active, the command has no effect. In this case there will usually be no other arguments.

-- zshzle(1): Zsh Line Editor, Zle Bulitins, zle -R
(I couldn't find a good anchor, so please find/search in the page with zle -R)

hchbaw
  • 4,894
  • 18
  • 16
  • That works! It works for all my cases: when no completions are found, when one completion is found and auto-completed, and when multiple completions are found and a menu is shown. In all cases the 'Completing...' is shown and then properly removed when done. Thanks! – Whyte Feb 26 '17 at 12:13