Coding style.
- Update asyncio code to latest style using asyncio.run().
This commit is contained in:
parent
2bb586c862
commit
0679cb14b5
4 changed files with 46 additions and 65 deletions
|
|
@ -406,7 +406,6 @@ class Fixed:
|
|||
##########################
|
||||
|
||||
|
||||
APPEARANCE_CHANGED_EVENT = asyncio.Event()
|
||||
_LAST_APPEARANCE = []
|
||||
|
||||
|
||||
|
|
@ -429,10 +428,10 @@ def patch_screen(widget):
|
|||
|
||||
async def update_screen(screen_widget):
|
||||
while True:
|
||||
patch_screen(screen_widget)
|
||||
await asyncio.sleep(0.01) # Limit the update rate
|
||||
await APPEARANCE_CHANGED_EVENT.wait()
|
||||
APPEARANCE_CHANGED_EVENT.clear()
|
||||
patch_screen(screen_widget)
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
|
||||
def on_terminal_input(screen_widget):
|
||||
|
|
@ -453,21 +452,21 @@ def signal_handler(loop, signal_, func):
|
|||
loop.remove_signal_handler(signal_)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def context(loop, screen_widget, exit_loop=None):
|
||||
APPEARANCE_CHANGED_EVENT.set()
|
||||
if exit_loop is None:
|
||||
exit_loop = loop.stop
|
||||
async def tui(screen_widget):
|
||||
global APPEARANCE_CHANGED_EVENT
|
||||
global SHUTDOWN_EVENT
|
||||
APPEARANCE_CHANGED_EVENT = asyncio.Event()
|
||||
SHUTDOWN_EVENT = asyncio.Event()
|
||||
loop = asyncio.get_running_loop()
|
||||
with (signal_handler(loop, signal.SIGWINCH, lambda: draw_screen(screen_widget)),
|
||||
signal_handler(loop, signal.SIGINT, exit_loop),
|
||||
signal_handler(loop, signal.SIGTERM, exit_loop), terminal.alternate_buffer(),
|
||||
terminal.interactive(), terminal.mouse_tracking()):
|
||||
update_task = loop.create_task(
|
||||
update_screen(screen_widget))
|
||||
signal_handler(loop, signal.SIGINT, SHUTDOWN_EVENT.set),
|
||||
signal_handler(loop, signal.SIGTERM, SHUTDOWN_EVENT.set),
|
||||
terminal.alternate_buffer(), terminal.interactive(), terminal.mouse_tracking()):
|
||||
update_task = asyncio.create_task(update_screen(screen_widget))
|
||||
try:
|
||||
loop.add_reader(sys.stdin, on_terminal_input, screen_widget)
|
||||
try:
|
||||
yield
|
||||
await SHUTDOWN_EVENT.wait()
|
||||
finally:
|
||||
loop.remove_reader(sys.stdin)
|
||||
finally:
|
||||
|
|
@ -487,7 +486,7 @@ class _Screen:
|
|||
|
||||
def on_keyboard_input(self, term_code):
|
||||
if term_code in ["q", terminal.ESC]:
|
||||
asyncio.get_event_loop().stop()
|
||||
SHUTDOWN_EVENT.set()
|
||||
else:
|
||||
self.content = Filler(Text(repr(term_code)))
|
||||
APPEARANCE_CHANGED_EVENT.set()
|
||||
|
|
@ -498,11 +497,5 @@ class _Screen:
|
|||
APPEARANCE_CHANGED_EVENT.set()
|
||||
|
||||
|
||||
def _main():
|
||||
loop = asyncio.get_event_loop()
|
||||
with context(loop, _Screen()):
|
||||
loop.run_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_main()
|
||||
asyncio.run(tui(_Screen()))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue