36
36
37
37
# types
38
38
Command = commands .Command
39
- if False :
40
- from .types import Callback , SimpleContextManager , KeySpec , CommandName
39
+ from .types import Callback , SimpleContextManager , KeySpec , CommandName
41
40
42
41
43
42
def disp_str (buffer : str ) -> tuple [str , list [int ]]:
@@ -247,6 +246,7 @@ class Reader:
247
246
lxy : tuple [int , int ] = field (init = False )
248
247
scheduled_commands : list [str ] = field (default_factory = list )
249
248
can_colorize : bool = False
249
+ threading_hook : Callback | None = None
250
250
251
251
## cached metadata to speed up screen refreshes
252
252
@dataclass
@@ -722,6 +722,24 @@ def do_cmd(self, cmd: tuple[str, list[str]]) -> None:
722
722
self .console .finish ()
723
723
self .finish ()
724
724
725
+ def run_hooks (self ) -> None :
726
+ threading_hook = self .threading_hook
727
+ if threading_hook is None and 'threading' in sys .modules :
728
+ from ._threading_handler import install_threading_hook
729
+ install_threading_hook (self )
730
+ if threading_hook is not None :
731
+ try :
732
+ threading_hook ()
733
+ except Exception :
734
+ pass
735
+
736
+ input_hook = self .console .input_hook
737
+ if input_hook :
738
+ try :
739
+ input_hook ()
740
+ except Exception :
741
+ pass
742
+
725
743
def handle1 (self , block : bool = True ) -> bool :
726
744
"""Handle a single event. Wait as long as it takes if block
727
745
is true (the default), otherwise return False if no event is
@@ -732,16 +750,13 @@ def handle1(self, block: bool = True) -> bool:
732
750
self .dirty = True
733
751
734
752
while True :
735
- input_hook = self .console .input_hook
736
- if input_hook :
737
- input_hook ()
738
- # We use the same timeout as in readline.c: 100ms
739
- while not self .console .wait (100 ):
740
- input_hook ()
741
- event = self .console .get_event (block = False )
742
- else :
743
- event = self .console .get_event (block )
744
- if not event : # can only happen if we're not blocking
753
+ # We use the same timeout as in readline.c: 100ms
754
+ self .run_hooks ()
755
+ self .console .wait (100 )
756
+ event = self .console .get_event (block = False )
757
+ if not event :
758
+ if block :
759
+ continue
745
760
return False
746
761
747
762
translate = True
@@ -763,8 +778,7 @@ def handle1(self, block: bool = True) -> bool:
763
778
if cmd is None :
764
779
if block :
765
780
continue
766
- else :
767
- return False
781
+ return False
768
782
769
783
self .do_cmd (cmd )
770
784
return True
0 commit comments