|
1 | 1 | from rich.console import Console |
| 2 | +from rich.traceback import install |
| 3 | + |
2 | 4 | from my_xxt.my_tools import select_menu, show_start |
3 | 5 | from my_xxt.api import NewXxt |
4 | 6 | from my_xxt.login import login |
| 7 | +from config import __LOG_FILE__, __SCREEN_WIDTH__ |
| 8 | + |
| 9 | + |
| 10 | +class MyConsole(Console): |
| 11 | + |
| 12 | + def __init__(self, *args, **kwargs): |
| 13 | + super().__init__(*args, **kwargs) |
| 14 | + self.log_file = open(__LOG_FILE__, "w", encoding="utf-8") |
| 15 | + |
| 16 | + def log(self, *args, **kwargs): |
| 17 | + super().log(*args, **kwargs) |
| 18 | + if not self.log_file.closed: |
| 19 | + self.log_file.write(" ".join(map(str, args)) + "\n") |
| 20 | + |
| 21 | + def print(self, *args, **kwargs): |
| 22 | + super().print(*args, **kwargs) |
| 23 | + captured_output_value = super().export_text() |
| 24 | + if not self.log_file.closed: |
| 25 | + self.log_file.write(captured_output_value) |
| 26 | + |
| 27 | + def cleanup(self): |
| 28 | + if not self.log_file.closed: |
| 29 | + self.log_file.close() |
| 30 | + |
| 31 | + def __del__(self): |
| 32 | + self.cleanup() |
| 33 | + |
| 34 | + |
| 35 | +def main(): |
| 36 | + console = MyConsole(width=__SCREEN_WIDTH__,record=True) |
| 37 | + install(console=console, show_locals=True, width=__SCREEN_WIDTH__) |
| 38 | + |
| 39 | + try: |
| 40 | + while True: |
| 41 | + show_start(console) |
| 42 | + new_xxt_instance = NewXxt() |
| 43 | + login(console, new_xxt_instance) |
| 44 | + select_menu(console, new_xxt_instance) |
| 45 | + except Exception as e: |
| 46 | + console.log(f"程序退出: {e}", style="bold red") |
| 47 | + console.log_exception(e) |
| 48 | + console.log("如果需要反馈错误信息,请将对应的日志文件以及问题反映给作者", style="bold red") |
5 | 49 |
|
6 | | -# 设置控制台的宽度 |
7 | | -console = Console(width=120) |
8 | 50 |
|
9 | 51 | if __name__ == '__main__': |
10 | | - while True: |
11 | | - show_start(console) |
12 | | - xxt = NewXxt() |
13 | | - login(console, xxt) |
14 | | - select_menu(console, xxt) |
| 52 | + main() |
0 commit comments