import sys from contextlib import contextmanager from typing import IO, Iterator @contextmanager def redirect_stdio(stdin: IO, stdout: IO, stderr: IO = sys.stderr) -> Iterator[None]: old_stdin, old_stdout, old_stderr = sys.stdin, sys.stdout, sys.stderr sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr try: yield finally: sys.stdin, sys.stdout, sys.stderr = old_stdin, old_stdout, old_stderr def print_header(title: str, reserved_space: int = 0) -> None: padding = 2 field_width = max(len(title) + padding, reserved_space - padding) field_width += field_width % 2 line = '-' * (field_width + padding) print(line) print(f'|{title:^{field_width}}|') print(line)