rich: [BUG] Backspacing deletes the prompt in `console.input` using `readline`
Describe the bug
Very similar to a previous issue https://github.com/Textualize/rich/issues/299 . Not exactly a regression, since that one depended on having a custom theme. But the steps to reproduce are the same:
- Enter some text at the prompt
- Delete it with backspace
- The prompt gets deleted too
It also happens by using Ctrl-u to delete the whole line.
It doesn’t happen if readline isn’t used.
I’m using version 12.4.4, but I’ve tried some older versions and I think it started happening between v11.2.0 and v12.0.0.
import readline
from rich.console import Console
def main():
console = Console()
command = console.input('> ')
print('C1', command)
if __name__ == '__main__':
main()
Platform
Click to expand
> python -m rich.diagnose
╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface. │
│ │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=191 ColorSystem.TRUECOLOR> │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│ │
│ color_system = 'truecolor' │
│ encoding = 'utf-8' │
│ file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│ height = 53 │
│ is_alt_screen = False │
│ is_dumb_terminal = False │
│ is_interactive = True │
│ is_jupyter = False │
│ is_terminal = True │
│ legacy_windows = False │
│ no_color = False │
│ options = ConsoleOptions( │
│ size=ConsoleDimensions(width=191, height=53), │
│ legacy_windows=False, │
│ min_width=1, │
│ max_width=191, │
│ is_terminal=True, │
│ encoding='utf-8', │
│ max_height=53, │
│ justify=None, │
│ overflow=None, │
│ no_wrap=False, │
│ highlight=None, │
│ markup=None, │
│ height=None │
│ ) │
│ quiet = False │
│ record = False │
│ safe_box = True │
│ size = ConsoleDimensions(width=191, height=53) │
│ soft_wrap = False │
│ stderr = False │
│ style = None │
│ tab_size = 8 │
│ width = 191 │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available. │
│ │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│ │
│ truecolor = False │
│ vt = False │
╰───────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────────────────────────────────────── Environment Variables ───────────────────────────────────────────────────────────────────────────────────╮
│ { │
│ 'TERM': 'xterm', │
│ 'COLORTERM': 'truecolor', │
│ 'CLICOLOR': None, │
│ 'NO_COLOR': None, │
│ 'TERM_PROGRAM': None, │
│ 'COLUMNS': None, │
│ 'LINES': None, │
│ 'JPY_PARENT_PID': None, │
│ 'VSCODE_VERBOSE_LOGGING': None │
│ } │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
platform="Linux"
> pip freeze | grep rich
rich==12.4.4
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 7
- Comments: 15 (2 by maintainers)
Commits related to this issue
- Work around rich issue 2293. Solution adapted from: https://github.com/Textualize/rich/issues/2293#issuecomment-1365567714 — committed to dronefly-garden/dronefly-cli by synrg 4 years ago
- Workaround for rich bug Textualize/rich#2293 — committed to evilpan/gptcli by evilpan 4 years ago
I did some digging and reverting to code prior to 568b951 seems to correct the issue. I’m guessing that the
readlinelibrary needs to know how long the prompt is in order to properly maintain this, and to do that, you need to pass in the prompt toinput(), and that’s why the old code was rendering the prompt within acapture()context. Reverting to that old approach seems like the solution. (Although I’m not sure why it was removed --and this fix would possibly make #1757 worse…)The same issue in Click: https://github.com/pallets/click/issues/665 was resolved with a bit dirty hack: https://github.com/pallets/click/pull/1836
which is definitely not perfect, because it implies other issues like https://github.com/pallets/click/issues/2018.