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.1k stars 717 forks source link

Tab char `' '`(`\t`) displays as `^I` when pasting into input #1868

Open huangyxi opened 2 months ago

huangyxi commented 2 months ago

In some downstream applications, we want to paste code directly into the input. If a tab character is included in the code, the prompt displays it as ^I.

For a specific instance, when coding in an R script in VSCode, the "vscode-R" extension could send a block of code into the REPL of the Python package radian, which utilizes prompt-toolkit as the IO in the REPL. If the block of code contains tabs, the REPL will display abnormally. https://github.com/randy3k/radian/issues/313

Screenshot

Thankfully, reproducing this behavior is not a hard task.

  1. Make a simple prompt:
    >>> from prompt_toolkit import prompt
    >>> prompt("Input: ")
    Input: 
  2. Paste tab char > <, resulting in:
    Input: tab char >^I<

I have tried a solution mentioned in https://github.com/tmbo/questionary/issues/154#issuecomment-905592622, which patches prompt_toolkit.layout.screen .Char.display_mappings['\t'] = '\t', and a solution in Issue #1452, which listens for the tab key to insert spaces. However, it still does not work properly. I also found a workaround mentioned in Issue #556, but it is not suitable in our situation.