66from zipfile import ZipFile , BadZipFile
77from typing import ContextManager , Iterator
88
9- from .console import Console , InputGetter
9+ from blessed import Terminal
10+
11+ from .console import Console
12+ from .input_getter import BaseInputGetter , StackedInputGetter
13+
14+
15+ class FileInputGetter (BaseInputGetter ):
16+ def __init__ (
17+ self ,
18+ console : Console ,
19+ terminal : Terminal ,
20+ input_generator : Iterator [set [Console .Input ]],
21+ ) -> None :
22+ super ().__init__ (console , terminal )
23+ self ._generator = input_generator
24+
25+ def get_pressed (self ) -> set [Console .Input ]:
26+ return next (self ._generator )
1027
1128
1229def get_inputs_ref (console : Console ) -> list [Console .Input ]:
@@ -45,8 +62,8 @@ def open_input_log_file(path: Path) -> Iterator[TextIOWrapper]:
4562
4663@contextmanager
4764def console_input_from_file_context (
48- console : Console , path : Path , skip_first_frames : int = 188
49- ) -> Iterator [InputGetter ]:
65+ console : Console , terminal : Terminal , path : Path , skip_first_frames : int = 188
66+ ) -> Iterator [FileInputGetter ]:
5067 inputs_ref = get_inputs_ref (console )
5168 with open_input_log_file (path ) as f :
5269
@@ -62,19 +79,24 @@ def gen() -> Iterator[set[Console.Input]]:
6279 yield set ()
6380
6481 input_generator = gen ()
65- yield lambda : next (input_generator )
82+ yield FileInputGetter (console , terminal , input_generator )
83+
84+
85+ class WriteInputGetter (StackedInputGetter ):
86+ def __init__ (self , base_getter : BaseInputGetter , file : TextIOWrapper ) -> None :
87+ super ().__init__ (base_getter )
88+ self ._file = file
89+
90+ def get_pressed (self ) -> set [Console .Input ]:
91+ value = super ().get_pressed ()
92+ print (value_to_line (self .console , value ), file = self ._file )
93+ return value
6694
6795
6896@contextmanager
6997def write_input_context (
70- console : Console , context : ContextManager [InputGetter ], path : Path
71- ) -> Iterator [InputGetter ]:
98+ context : ContextManager [BaseInputGetter ], path : Path
99+ ) -> Iterator [WriteInputGetter ]:
72100 with open (path , "w" ) as f :
73- with context as getter :
74-
75- def new_getter () -> set [Console .Input ]:
76- value = getter ()
77- print (value_to_line (console , value ), file = f )
78- return value
79-
80- yield new_getter
101+ with context as base_getter :
102+ yield WriteInputGetter (base_getter , f )
0 commit comments