File tree Expand file tree Collapse file tree 4 files changed +53
-6
lines changed
docs/pages/advanced_topics Expand file tree Collapse file tree 4 files changed +53
-6
lines changed Original file line number Diff line number Diff line change @@ -282,6 +282,33 @@ called, even if there's an active `ab` binding.
282
282
283
283
This is mainly useful in order to conditionally override another binding.
284
284
285
+ Asyncio coroutines
286
+ ------------------
287
+
288
+ Key binding handlers can be asyncio coroutines.
289
+
290
+ .. code :: python
291
+
292
+ from prompt_toolkit.application import in_terminal
293
+
294
+ @bindings.add (' x' )
295
+ async def print_hello (event ):
296
+ """
297
+ Pressing 'x' will print 5 times "hello" in the background above the
298
+ prompt.
299
+ """
300
+ for i in range (5 ):
301
+ # Print hello above the current prompt.
302
+ async with in_terminal():
303
+ print (' hello' )
304
+
305
+ # Sleep, but allow further input editing in the meantime.
306
+ await asyncio.sleep(1 )
307
+
308
+ If the user accepts the input on the prompt, while this coroutine is not yet
309
+ finished , an `asyncio.CancelledError ` exception will be thrown in this
310
+ coroutine.
311
+
285
312
286
313
Timeouts
287
314
--------
Original file line number Diff line number Diff line change 2
2
"""
3
3
Example of adding a custom key binding to a prompt.
4
4
"""
5
+ import asyncio
6
+
5
7
from prompt_toolkit import prompt
6
- from prompt_toolkit .application import run_in_terminal
8
+ from prompt_toolkit .application import in_terminal , run_in_terminal
7
9
from prompt_toolkit .key_binding import KeyBindings
8
10
9
11
@@ -52,6 +54,19 @@ def print_hello():
52
54
53
55
run_in_terminal (print_hello )
54
56
57
+ @bindings .add ("c-k" )
58
+ async def _ (event ):
59
+ """
60
+ Example of asyncio coroutine as a key binding.
61
+ """
62
+ try :
63
+ for i in range (5 ):
64
+ async with in_terminal ():
65
+ print ("hello" )
66
+ await asyncio .sleep (1 )
67
+ except asyncio .CancelledError :
68
+ print ("Prompt terminated before we completed." )
69
+
55
70
# Read input.
56
71
print ('Press F4 to insert "hello world", type "xy" to insert "z":' )
57
72
text = prompt ("> " , key_bindings = bindings )
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ def my_key_binding(event):
37
37
from abc import ABCMeta , abstractmethod , abstractproperty
38
38
from typing import (
39
39
TYPE_CHECKING ,
40
+ Awaitable ,
40
41
Callable ,
41
42
Hashable ,
42
43
List ,
@@ -67,7 +68,7 @@ def my_key_binding(event):
67
68
"GlobalOnlyKeyBindings" ,
68
69
]
69
70
70
- KeyHandlerCallable = Callable [["KeyPressEvent" ], None ]
71
+ KeyHandlerCallable = Callable [["KeyPressEvent" ], Union [ None , Awaitable [ None ]] ]
71
72
72
73
73
74
class Binding :
@@ -98,7 +99,11 @@ def __init__(
98
99
self .record_in_macro = to_filter (record_in_macro )
99
100
100
101
def call (self , event : "KeyPressEvent" ) -> None :
101
- self .handler (event )
102
+ result = self .handler (event )
103
+
104
+ # If the handler is a coroutine, create an asyncio task.
105
+ if result is not None :
106
+ event .app .create_background_task (result )
102
107
103
108
def __repr__ (self ) -> str :
104
109
return "%s(keys=%r, handler=%r)" % (
Original file line number Diff line number Diff line change @@ -127,9 +127,9 @@ def _cancel(event: E) -> None:
127
127
event .app .layout .focus_last ()
128
128
129
129
@handle ("enter" , filter = focused )
130
- def _accept (event : E ) -> None :
130
+ async def _accept (event : E ) -> None :
131
131
" Run system command. "
132
- event .app .run_system_command (
132
+ await event .app .run_system_command (
133
133
self .system_buffer .text ,
134
134
display_before_text = self ._get_display_before_text (),
135
135
)
@@ -149,7 +149,7 @@ def _cancel_vi(event: E) -> None:
149
149
event .app .layout .focus_last ()
150
150
151
151
@handle ("enter" , filter = focused )
152
- def _accept_vi (event : E ) -> None :
152
+ async def _accept_vi (event : E ) -> None :
153
153
" Run system command. "
154
154
event .app .vi_state .input_mode = InputMode .NAVIGATION
155
155
event .app .run_system_command (
You can’t perform that action at this time.
0 commit comments