From 64c210dd5a6a80874aedee63062b7fc95a65f67c Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 30 May 2026 15:12:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor(gui):=20=E6=8A=BD=20refresh=5Frun=5Flo?= =?UTF-8?q?g=20=E5=88=B0=20components/run=5Flog=5Fview=20(3715=20=E2=86=92?= =?UTF-8?q?=203669)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/components/run_log_view.py | 89 ++++++++++++++++++++++++++++++++++ gui/gui_app.py | 60 +++-------------------- 2 files changed, 96 insertions(+), 53 deletions(-) create mode 100644 gui/components/run_log_view.py diff --git a/gui/components/run_log_view.py b/gui/components/run_log_view.py new file mode 100644 index 0000000..6bcd2fe --- /dev/null +++ b/gui/components/run_log_view.py @@ -0,0 +1,89 @@ +"""运行日志 Tab 的刷新逻辑(从 runner 内存读取最近若干行)。 + +从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内的闭包 ``refresh_run_log``。 + +依赖通过显式参数注入: +- ``runner`` - 运行控制器(暴露 ``_lock`` 与 ``_log`` 内部属性) +- ``text_widget`` - 展示用的 Text/ScrolledText +- ``filter_getter`` - 返回筛选关键字 +- ``theme_getter`` - 返回当前主题名 +""" + +from __future__ import annotations + +import tkinter as tk +from typing import Any, Callable + +try: + from gui.components.log_color_config import ( + apply_operation_color, + get_operation_type, + ) +except ImportError: + def get_operation_type(line: str): # type: ignore[no-redef] + return None + + def apply_operation_color(*args, **kwargs): # type: ignore[no-redef] + pass + + +def refresh_run_log( + runner: Any, + *, + text_widget: tk.Text, + filter_getter: Callable[[], str], + theme_getter: Callable[[], str], +) -> None: + """从内存中读取最近若干运行日志并在界面中刷新(行为上与行为日志一致)""" + try: + tail_n = 1000 + # 读取 runner 内存日志(线程安全) + with runner._lock: + total = len(runner._log) + cand = runner._log[-tail_n:] if tail_n > 0 else runner._log[:] + + # 应用筛选关键词 + kw = filter_getter() + if kw: + cand = [ln for ln in cand if kw in (ln or "")] + + # 保存滚动位置 + try: + scroll_pos = text_widget.yview()[0] + except Exception: + scroll_pos = 1.0 + + text_widget.delete("1.0", "end") + if cand: + try: + current_theme = theme_getter() + except Exception: + current_theme = "litera" + + for i, line in enumerate(cand): + start_pos = text_widget.index("end-1c") + if i > 0: + text_widget.insert("end", "\n") + text_widget.insert("end", line) + end_pos = text_widget.index("end") + try: + op_type = get_operation_type(line) + if op_type: + apply_operation_color( + text_widget, start_pos, end_pos, line, current_theme + ) + except Exception: + pass + else: + text_widget.insert("1.0", "暂无匹配的运行日志") + + try: + text_widget.yview_moveto(scroll_pos) + except Exception: + pass + except Exception as e: + try: + text_widget.delete("1.0", "end") + text_widget.insert("1.0", f"刷新运行日志失败:{e}") + except Exception: + pass diff --git a/gui/gui_app.py b/gui/gui_app.py index 1ffc65a..73cae0e 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -79,6 +79,7 @@ from gui.components.wifi_adb_one_click_scan import ( on_scan_one_click as _on_scan_one_click_impl, _handle_scan_result as _handle_scan_result_impl, ) +from gui.components.run_log_view import refresh_run_log as _refresh_run_log_impl # 导入日志颜色配置模块 try: @@ -2885,59 +2886,12 @@ def main(): ) def refresh_run_log(): - """从内存中读取最近若干运行日志并在界面中刷新(行为上与行为日志一致)""" - try: - tail_n = 1000 - # 读取 runner 内存日志(线程安全) - with runner._lock: - total = len(runner._log) - cand = runner._log[-tail_n:] if tail_n > 0 else runner._log[:] - - # 应用筛选关键词 - kw = log_filter_var.get().strip() - if kw: - cand = [ln for ln in cand if kw in (ln or "")] - - # 保存滚动位置 - try: - scroll_pos = log_text.yview()[0] - except Exception: - scroll_pos = 1.0 - - log_text.delete("1.0", "end") - if cand: - try: - current_theme = str(gui_theme_var.get() or "litera") - except Exception: - current_theme = "litera" - - for i, line in enumerate(cand): - start_pos = log_text.index("end-1c") - if i > 0: - log_text.insert("end", "\n") - log_text.insert("end", line) - end_pos = log_text.index("end") - try: - op_type = get_operation_type(line) - if op_type: - apply_operation_color( - log_text, start_pos, end_pos, line, current_theme - ) - except Exception: - pass - else: - log_text.insert("1.0", "暂无匹配的运行日志") - - try: - log_text.yview_moveto(scroll_pos) - except Exception: - pass - except Exception as e: - try: - log_text.delete("1.0", "end") - log_text.insert("1.0", f"刷新运行日志失败:{e}") - except Exception: - pass + _refresh_run_log_impl( + runner, + text_widget=log_text, + filter_getter=lambda: log_filter_var.get().strip(), + theme_getter=lambda: str(gui_theme_var.get() or "litera"), + ) log_search_matches = [] # 存储所有匹配的位置 log_current_match_index = -1 # 当前匹配的索引