Coding style.
- Since there is only one display, appearance_changed_event can be a module level global in fill3. - Remove all the appearance_changed_event plumbing.
This commit is contained in:
parent
71b9da128b
commit
4150a9a250
5 changed files with 55 additions and 76 deletions
|
|
@ -406,30 +406,31 @@ class Fixed:
|
|||
##########################
|
||||
|
||||
|
||||
_last_appearance = []
|
||||
APPEARANCE_CHANGED_EVENT = asyncio.Event()
|
||||
_LAST_APPEARANCE = []
|
||||
|
||||
|
||||
def draw_screen(widget):
|
||||
global _last_appearance
|
||||
global _LAST_APPEARANCE
|
||||
appearance = widget.appearance(os.get_terminal_size())
|
||||
print(terminal.move(0, 0), *appearance, sep="", end="", flush=True)
|
||||
_last_appearance = appearance
|
||||
_LAST_APPEARANCE = appearance
|
||||
|
||||
|
||||
def patch_screen(widget):
|
||||
global _last_appearance
|
||||
global _LAST_APPEARANCE
|
||||
appearance = widget.appearance(os.get_terminal_size())
|
||||
zip_func = (itertools.zip_longest if len(appearance) > len(_last_appearance) else zip)
|
||||
zip_func = (itertools.zip_longest if len(appearance) > len(_LAST_APPEARANCE) else zip)
|
||||
changed_lines = (str(terminal.move(0, row_index)) + line for row_index, (line, old_line)
|
||||
in enumerate(zip_func(appearance, _last_appearance)) if line != old_line)
|
||||
in enumerate(zip_func(appearance, _LAST_APPEARANCE)) if line != old_line)
|
||||
print(*changed_lines, sep="", end="", flush=True)
|
||||
_last_appearance = appearance
|
||||
_LAST_APPEARANCE = appearance
|
||||
|
||||
|
||||
async def update_screen(screen_widget, appearance_changed_event):
|
||||
async def update_screen(screen_widget):
|
||||
while True:
|
||||
await appearance_changed_event.wait()
|
||||
appearance_changed_event.clear()
|
||||
await APPEARANCE_CHANGED_EVENT.wait()
|
||||
APPEARANCE_CHANGED_EVENT.clear()
|
||||
patch_screen(screen_widget)
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
|
|
@ -453,8 +454,8 @@ def signal_handler(loop, signal_, func):
|
|||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def context(loop, appearance_changed_event, screen_widget, exit_loop=None):
|
||||
appearance_changed_event.set()
|
||||
def context(loop, screen_widget, exit_loop=None):
|
||||
APPEARANCE_CHANGED_EVENT.set()
|
||||
if exit_loop is None:
|
||||
exit_loop = loop.stop
|
||||
with (signal_handler(loop, signal.SIGWINCH, lambda: draw_screen(screen_widget)),
|
||||
|
|
@ -462,7 +463,7 @@ def context(loop, appearance_changed_event, screen_widget, exit_loop=None):
|
|||
signal_handler(loop, signal.SIGTERM, exit_loop), terminal.alternate_buffer(),
|
||||
terminal.interactive(), terminal.mouse_tracking()):
|
||||
update_task = loop.create_task(
|
||||
update_screen(screen_widget, appearance_changed_event))
|
||||
update_screen(screen_widget))
|
||||
try:
|
||||
loop.add_reader(sys.stdin, on_terminal_input, screen_widget)
|
||||
try:
|
||||
|
|
@ -478,8 +479,7 @@ def context(loop, appearance_changed_event, screen_widget, exit_loop=None):
|
|||
|
||||
class _Screen:
|
||||
|
||||
def __init__(self, appearance_changed_event):
|
||||
self._appearance_changed_event = appearance_changed_event
|
||||
def __init__(self):
|
||||
self.content = Filler(Text("Hello World"))
|
||||
|
||||
def appearance(self, dimensions):
|
||||
|
|
@ -490,19 +490,17 @@ class _Screen:
|
|||
asyncio.get_event_loop().stop()
|
||||
else:
|
||||
self.content = Filler(Text(repr(term_code)))
|
||||
self._appearance_changed_event.set()
|
||||
APPEARANCE_CHANGED_EVENT.set()
|
||||
|
||||
def on_mouse_input(self, term_code):
|
||||
mouse_code = terminal.decode_mouse_input(term_code)
|
||||
self.content = Filler(Text(repr(term_code) + " " + repr(mouse_code)))
|
||||
self._appearance_changed_event.set()
|
||||
APPEARANCE_CHANGED_EVENT.set()
|
||||
|
||||
|
||||
def _main():
|
||||
loop = asyncio.get_event_loop()
|
||||
appearance_changed_event = asyncio.Event()
|
||||
screen = _Screen(appearance_changed_event)
|
||||
with context(loop, appearance_changed_event, screen):
|
||||
with context(loop, _Screen()):
|
||||
loop.run_forever()
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue