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.37k stars 716 forks source link

Cannot embed vt100 sequences in a prompt which send hidden information to the terminal #1309

Open halloleo opened 3 years ago

halloleo commented 3 years ago

I using the emacs-libvterm terminal emulator and run a prompt-toolkit based REPL inside it.

For interaction with the terminal "host" I need to issue special vt100 sequences at the end of each prompt-toolkit prompt. I try to issue these sequences lkie this:

vtescape = '\033]{}\033\\&'
infostr = '51;A{}@{}:{}'.format(whoami, hostname, pwd)  # whoami, hostname and pwd are strings variables
vtinfo = vtescape.format(infostr)
text = session.prompt('> '+vtinfo)

However prompt_toolkit seems to filter out the escape bits of it, so that the information is not hidden as the terminal requires it.

How can I send exactly these escape sequences at the end of each prompt?

jonathanslenders commented 3 years ago

Hi @halloleo,

This should be possible if you wrap the prompt in an ANSI object: https://python-prompt-toolkit.readthedocs.io/en/master/pages/reference.html#prompt_toolkit.formatted_text.ANSI

Just makes sure to wrap the special characters that prompt_toolkit doesn't recognize as color between \001 and \002.

halloleo commented 3 years ago

Thanks for the suggestion, @jonathanslenders!

Now I tried to built a mini prompt-toolkit repl with the following code:

while True:
    p_str = ANSI('> \x1b]51;A{}\x1b\\'.format('my hidden text'))
    text = session.prompt(p_str)
    print_formatted_text('You entered: ', text)

But this still shows "my hidden text" visibly in the terminal.

Somehow the ANSI sequences are stripped out again. :-(


PS: In a bash REPL with a prompt set via a function like this:

my_print(){
   printf "\e]51;A%s\e\\" "my hidden text"
}
export PS1='> \[$(my_print)\]'

it works perfectly.