Coding style.

- Main tui function should also set the window title.
This commit is contained in:
Andrew Hamilton 2021-12-08 19:05:20 +10:00
parent 0679cb14b5
commit 5fd06479da
4 changed files with 16 additions and 15 deletions

View file

@ -1014,8 +1014,8 @@ def on_filesystem_event(event, summary, root_path):
fill3.APPEARANCE_CHANGED_EVENT.set() fill3.APPEARANCE_CHANGED_EVENT.set()
async def main(root_path, worker_count=None, editor_command=None, theme=None, compression=None, async def main(title, root_path, worker_count=None, editor_command=None, theme=None,
is_being_tested=False): compression=None, is_being_tested=False):
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
if worker_count is None: if worker_count is None:
worker_count = max(multiprocessing.cpu_count() - 1, 1) worker_count = max(multiprocessing.cpu_count() - 1, 1)
@ -1043,7 +1043,7 @@ async def main(root_path, worker_count=None, editor_command=None, theme=None, co
loop.create_task(worker_.future) loop.create_task(worker_.future)
try: try:
if sys.stdout.isatty() or is_being_tested: if sys.stdout.isatty() or is_being_tested:
await fill3.tui(screen) await fill3.tui(title, screen)
log.log_message("Program stopped.") log.log_message("Program stopped.")
else: else:
shutdown_event = asyncio.Event() shutdown_event = asyncio.Event()
@ -1155,12 +1155,12 @@ def inotify_watches_exceeded():
def entry_point(): def entry_point():
root_path, worker_count, editor_command, theme, compression = check_arguments() root_path, worker_count, editor_command, theme, compression = check_arguments()
manage_cache(root_path) manage_cache(root_path)
with terminal.terminal_title("eris: " + os.path.basename(root_path)): title = "eris: " + os.path.basename(root_path)
with chdir(root_path): # FIX: Don't change directory if possible. with chdir(root_path): # FIX: Don't change directory if possible.
try: try:
asyncio.run(main(root_path, worker_count, editor_command, theme, compression)) asyncio.run(main(title, root_path, worker_count, editor_command, theme, compression))
except pyinotify.WatchManagerError: except pyinotify.WatchManagerError:
inotify_watches_exceeded() inotify_watches_exceeded()
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -202,7 +202,7 @@ class MainTestCase(unittest.TestCase):
with __main__.chdir(root_path): with __main__.chdir(root_path):
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
with contextlib.redirect_stdout(io.StringIO()): with contextlib.redirect_stdout(io.StringIO()):
loop.run_until_complete(__main__.main( loop.run_until_complete(__main__.main("test",
root_path, worker_count=2, is_being_tested=True)) root_path, worker_count=2, is_being_tested=True))
for file_name in ["summary.pickle", "creation_time", for file_name in ["summary.pickle", "creation_time",
"foo-metadata", "foo-contents"]: "foo-metadata", "foo-contents"]:

View file

@ -452,16 +452,17 @@ def signal_handler(loop, signal_, func):
loop.remove_signal_handler(signal_) loop.remove_signal_handler(signal_)
async def tui(screen_widget): async def tui(title, screen_widget):
global APPEARANCE_CHANGED_EVENT global APPEARANCE_CHANGED_EVENT
global SHUTDOWN_EVENT global SHUTDOWN_EVENT
APPEARANCE_CHANGED_EVENT = asyncio.Event() APPEARANCE_CHANGED_EVENT = asyncio.Event()
SHUTDOWN_EVENT = asyncio.Event() SHUTDOWN_EVENT = asyncio.Event()
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
with (signal_handler(loop, signal.SIGWINCH, lambda: draw_screen(screen_widget)), with (terminal.title(title), signal_handler(loop, signal.SIGWINCH, lambda: draw_screen(screen_widget)),
signal_handler(loop, signal.SIGINT, SHUTDOWN_EVENT.set), signal_handler(loop, signal.SIGINT, SHUTDOWN_EVENT.set),
signal_handler(loop, signal.SIGTERM, SHUTDOWN_EVENT.set), signal_handler(loop, signal.SIGTERM, SHUTDOWN_EVENT.set),
terminal.alternate_buffer(), terminal.interactive(), terminal.mouse_tracking()): terminal.alternate_buffer(), terminal.interactive(), terminal.mouse_tracking()
):
update_task = asyncio.create_task(update_screen(screen_widget)) update_task = asyncio.create_task(update_screen(screen_widget))
try: try:
loop.add_reader(sys.stdin, on_terminal_input, screen_widget) loop.add_reader(sys.stdin, on_terminal_input, screen_widget)
@ -498,4 +499,4 @@ class _Screen:
if __name__ == "__main__": if __name__ == "__main__":
asyncio.run(tui(_Screen())) asyncio.run(tui("Hello World", _Screen()))

View file

@ -35,7 +35,7 @@ def move(x, y):
@contextlib.contextmanager @contextlib.contextmanager
def terminal_title(title): def title(title):
sys.stdout.write(ESC + "7") # save sys.stdout.write(ESC + "7") # save
sys.stdout.write(f"\033]0;{title}\007") # set title sys.stdout.write(f"\033]0;{title}\007") # set title
try: try: