prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.11k stars 717 forks source link

Readline Completions Duplicate bottom_toolbar #1749

Open Arcynic49 opened 1 year ago

Arcynic49 commented 1 year ago

I'm working on a project where I was asked to add a bottom_toolbar. We already have some custom logic using display_completions_like_readline to do different things for tab completion. I ran into an issue where the tool bar was being duplicated above the set of completions if the current text could produce multiple completions (if you put in an expected completion it would fill in the rest of the command but not duplicate the bottom_toolbar, as desired).

Cribbing from an issue that brought this up tangentially, you can see this behavior with the code below:

#!/usr/bin/env python

from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.shortcuts import CompleteStyle, prompt

test_completer = WordCompleter(['completely', 'arbitrary', 'set', 'of', 'words'])

prompt_params = {'completer': test_completer,
                 'complete_style': CompleteStyle.READLINE_LIKE,
                 'bottom_toolbar': "should only appear at the bottom"}

while True:
    prompt('> ', **prompt_params)

If you start typing one of the words, tab will complete it as expected. If you hit tab to bring up the set of completions, you will see something like this (I hit tab on the second empty prompt):

> set
>
should only appear at the bottom                                                                                                                                                                                                        
completely arbitrary  set        of         words      
>
should only appear at the bottom
Arcynic49 commented 1 year ago

If it helps, it seems to work as I expect if I modify the coroutine here:

https://github.com/prompt-toolkit/python-prompt-toolkit/blob/07412a37b38dd70a3d63f2966a21f0a645133264/src/prompt_toolkit/key_binding/bindings/completion.py#L144-L147

And instead set:

render_cli_done=False

Though it should be noted I haven't tested this with the multi-page output this is expected to be able to handle, and may in fact break something else.

jin-eld commented 1 year ago

I ran into exactly the same issue, I can confirm, that the above fix works for me as well, thanks @Arcynic49

With multi-page output the bottom bar disappears when you get asked "Display all 88 possibilities? (y/n)" and also when you paginate, i.e. when it shows --MORE--, the toolbar is not visible.

opt1
opt2
opt3
--MORE--

Imho this is fine, because once you reached the end the bottom bar does reappear again and is shown correctly. At least I'd be happy with a fix like that, having the toolbar duplicated as it is right now is quite disturbing to the user.

@jonathanslenders any chance we could get this into any upcoming release in the near future?