Coding style.
Simplified main() a little by moving general display code into fill3.py.
This commit is contained in:
parent
967b16a6bf
commit
8c11b73714
2 changed files with 61 additions and 49 deletions
60
fill3.py
60
fill3.py
|
|
@ -5,8 +5,15 @@
|
||||||
# Licensed under the Artistic License 2.0.
|
# Licensed under the Artistic License 2.0.
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
|
import contextlib
|
||||||
import itertools
|
import itertools
|
||||||
import os
|
import os
|
||||||
|
import signal
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
|
||||||
|
import urwid
|
||||||
|
import urwid.raw_display
|
||||||
|
|
||||||
import terminal
|
import terminal
|
||||||
import termstr
|
import termstr
|
||||||
|
|
@ -409,3 +416,56 @@ def patch_screen(widget):
|
||||||
if line != old_line)
|
if line != old_line)
|
||||||
print(*changed_lines, sep="", end="", flush=True)
|
print(*changed_lines, sep="", end="", flush=True)
|
||||||
_last_appearance = appearance
|
_last_appearance = appearance
|
||||||
|
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def _urwid_screen():
|
||||||
|
screen = urwid.raw_display.Screen()
|
||||||
|
screen.set_mouse_tracking(True)
|
||||||
|
screen.start()
|
||||||
|
try:
|
||||||
|
yield screen
|
||||||
|
finally:
|
||||||
|
screen.stop()
|
||||||
|
|
||||||
|
|
||||||
|
_UPDATE_THREAD_STOPPED = threading.Event()
|
||||||
|
|
||||||
|
|
||||||
|
def _update_screen(main_widget, appearance_changed_event):
|
||||||
|
while True:
|
||||||
|
appearance_changed_event.wait()
|
||||||
|
appearance_changed_event.clear()
|
||||||
|
if _UPDATE_THREAD_STOPPED.is_set():
|
||||||
|
break
|
||||||
|
patch_screen(main_widget)
|
||||||
|
|
||||||
|
|
||||||
|
def main(loop, appearance_changed_event, screen, exit_loop=None):
|
||||||
|
appearance_changed_event.set()
|
||||||
|
update_display_thread = threading.Thread(
|
||||||
|
target=_update_screen, args=(screen, appearance_changed_event),
|
||||||
|
daemon=True)
|
||||||
|
|
||||||
|
def exit_loop_():
|
||||||
|
loop.stop()
|
||||||
|
if exit_loop is None:
|
||||||
|
exit_loop = exit_loop_
|
||||||
|
loop.add_signal_handler(signal.SIGWINCH, appearance_changed_event.set)
|
||||||
|
loop.add_signal_handler(signal.SIGINT, exit_loop)
|
||||||
|
loop.add_signal_handler(signal.SIGTERM, exit_loop)
|
||||||
|
with terminal.hidden_cursor():
|
||||||
|
with _urwid_screen() as urwid_screen:
|
||||||
|
|
||||||
|
def on_input(urwid_screen):
|
||||||
|
for event in urwid_screen.get_input():
|
||||||
|
screen.on_input_event(event)
|
||||||
|
loop.add_reader(sys.stdin, on_input, urwid_screen)
|
||||||
|
update_display_thread.start()
|
||||||
|
try:
|
||||||
|
loop.run_forever()
|
||||||
|
finally:
|
||||||
|
_UPDATE_THREAD_STOPPED.set()
|
||||||
|
appearance_changed_event.set()
|
||||||
|
update_display_thread.join()
|
||||||
|
# loop.close()
|
||||||
|
|
|
||||||
50
vigil
50
vigil
|
|
@ -39,8 +39,6 @@ import traceback
|
||||||
|
|
||||||
import docopt
|
import docopt
|
||||||
import pyinotify
|
import pyinotify
|
||||||
import urwid
|
|
||||||
import urwid.raw_display
|
|
||||||
|
|
||||||
import fill3
|
import fill3
|
||||||
import sandbox_fs
|
import sandbox_fs
|
||||||
|
|
@ -838,29 +836,6 @@ def _add_watch_manager_to_mainloop(root_path, mainloop, on_filesystem_change,
|
||||||
return watch_manager_fd
|
return watch_manager_fd
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
|
||||||
def _urwid_screen():
|
|
||||||
screen = urwid.raw_display.Screen()
|
|
||||||
screen.set_mouse_tracking(True)
|
|
||||||
screen.start()
|
|
||||||
try:
|
|
||||||
yield screen
|
|
||||||
finally:
|
|
||||||
screen.stop()
|
|
||||||
|
|
||||||
|
|
||||||
_UPDATE_THREAD_STOPPED = False
|
|
||||||
|
|
||||||
|
|
||||||
def _update_screen(main_widget, appearance_changed_event):
|
|
||||||
while True:
|
|
||||||
appearance_changed_event.wait()
|
|
||||||
appearance_changed_event.clear()
|
|
||||||
if _UPDATE_THREAD_STOPPED:
|
|
||||||
break
|
|
||||||
fill3.patch_screen(main_widget)
|
|
||||||
|
|
||||||
|
|
||||||
def make_sandbox(mount_point):
|
def make_sandbox(mount_point):
|
||||||
sandbox = sandbox_fs.SandboxFs(mount_point)
|
sandbox = sandbox_fs.SandboxFs(mount_point)
|
||||||
sandbox.mount()
|
sandbox.mount()
|
||||||
|
|
@ -927,13 +902,6 @@ def main(root_path, loop, worker_count=None, is_sandboxed=True,
|
||||||
summary, log, jobs_added_event, appearance_changed_event)
|
summary, log, jobs_added_event, appearance_changed_event)
|
||||||
worker_.future = asyncio.ensure_future(future, loop=loop)
|
worker_.future = asyncio.ensure_future(future, loop=loop)
|
||||||
|
|
||||||
def on_window_resize():
|
|
||||||
appearance_changed_event.set()
|
|
||||||
appearance_changed_event.set()
|
|
||||||
update_display_thread = threading.Thread(
|
|
||||||
target=_update_screen, args=(screen, appearance_changed_event),
|
|
||||||
daemon=True)
|
|
||||||
|
|
||||||
def exit_loop():
|
def exit_loop():
|
||||||
log.log_command("Exiting...")
|
log.log_command("Exiting...")
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
|
|
@ -943,23 +911,7 @@ def main(root_path, loop, worker_count=None, is_sandboxed=True,
|
||||||
if worker_.result is not None:
|
if worker_.result is not None:
|
||||||
worker_.result.reset()
|
worker_.result.reset()
|
||||||
loop.stop()
|
loop.stop()
|
||||||
loop.add_signal_handler(signal.SIGWINCH, on_window_resize)
|
fill3.main(loop, appearance_changed_event, screen, exit_loop=exit_loop)
|
||||||
loop.add_signal_handler(signal.SIGINT, exit_loop)
|
|
||||||
loop.add_signal_handler(signal.SIGTERM, exit_loop)
|
|
||||||
with terminal.hidden_cursor():
|
|
||||||
with _urwid_screen() as urwid_screen:
|
|
||||||
|
|
||||||
def on_input(urwid_screen):
|
|
||||||
for event in urwid_screen.get_input():
|
|
||||||
screen.on_input_event(event)
|
|
||||||
loop.add_reader(sys.stdin, on_input, urwid_screen)
|
|
||||||
update_display_thread.start()
|
|
||||||
try:
|
|
||||||
loop.run_forever()
|
|
||||||
finally:
|
|
||||||
_UPDATE_THREAD_STOPPED = True
|
|
||||||
appearance_changed_event.set()
|
|
||||||
update_display_thread.join()
|
|
||||||
log.log_message("Program stopped.")
|
log.log_message("Program stopped.")
|
||||||
# Cannot pickle generators, locks, sockets or events.
|
# Cannot pickle generators, locks, sockets or events.
|
||||||
(summary.closest_placeholder_generator, summary._lock,
|
(summary.closest_placeholder_generator, summary._lock,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue