refactor(gui): 抽 refresh_run_log 到 components/run_log_view (3715 → 3669)

This commit is contained in:
Your Name committed 2026-05-30 15:12:38 +08:00
1 parent 68d1224b53
commit 64c210dd5a
2 files changed
+96 -53

No files matched your search

+89
View File
@@ -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
+7 -53
View File
@@ -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 # 当前匹配的索引