Open olejorgenb opened 11 months ago
The default prompt-session includes a key-binding for Ctrl-c which exits the current application with a KeyboardInterrupt
exception, like this:
import prompt_toolkit
from prompt_toolkit.key_binding import KeyBindings
def main():
bindings = KeyBindings()
@bindings.add("c-c")
def _keyboard_interrupt(event):
"""Abort when Control-C has been pressed."""
event.app.exit(exception=KeyboardInterrupt, style="class:aborting")
session = prompt_toolkit.PromptSession(key_bindings=bindings)
while True:
try:
line = session.prompt("> ")
except KeyboardInterrupt:
break
if __name__ == "__main__":
main()
Using Application.exit
instead of sys.exit
will allow the application renderer to restore the terminal state when it exits.
Ah, I thought the reason I bound ctrl-c was that it wasn't bound by the default bindings to exit, but maybe I mixed up. (Maybe there downsides to this, but it is possible to handle sys.exit as well by catching the SystemExit exception)
Feel free to close this issue then.
After pressing ctrl-c, the terminal is no longer processing ctr-c (a literal
^C
is inserted).Version: 3.0.36 Terminals tested: Tilix, gnome-terminal