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 718 forks source link

Unable to redo after undo #1703

Open bew opened 1 year ago

bew commented 1 year ago

Hello!

While tweaking my ipython REPL keybindings, I discovered that buf.redo() never works.

Here is a little script that shows the issue:

#!/usr/bin/env python

from prompt_toolkit import PromptSession
from prompt_toolkit.key_binding import KeyBindings

def dyn_prompt(buf):
    def inner():
        return f"undo: {len(buf._undo_stack)} | redo: {len(buf._redo_stack)}"
    return inner

def main():
    kb = KeyBindings()

    @kb.add("escape", "u")
    def _(event):
        event.current_buffer.undo()

    @kb.add("escape", "r")
    def _(event):
        event.current_buffer.redo()

    print("Try to type some text, then try: Alt-u to undo | Alt-r to redo")
    s = PromptSession("> ", key_bindings=kb)
    s.rprompt = dyn_prompt(s.app.current_buffer)
    try:
        s.prompt()
    except (EOFError, KeyboardInterrupt):
        pass

if __name__ == "__main__":
    main()
  1. Input abcdef
  2. RPrompt is now undo: 6 | redo: 0
  3. Type Alt-u to undo (remove f)
  4. RPrompt is now undo: 5 | redo: 1
  5. Type Alt-u to undo again (remove e)
  6. RPrompt is now undo: 4 | redo: 1 (redo didn't change!)
  7. Type Alt-r to try to redo (add e)
  8. :warning: e is not added, redo didn't work!
  9. RPrompt is now undo: 5 | redo: 0

Is that the right way to try to use undo/redo ? Is that a bug on your side maybe?

Snooz82 commented 1 year ago

I can confirm that behaviour. :-/