refactor(gui): 抽 actions_log 视图相关到 components/actions_log_view (4463 → 4337)

This commit is contained in:
Your Name committed 2026-05-30 14:10:30 +08:00
1 parent 8ea676562c
commit 44b091466d
2 files changed
+196 -139

No files matched your search

+183
View File
@@ -0,0 +1,183 @@
"""行为日志(``logs/actions.log``) Tab 的视图刷新与配色逻辑。
从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内的两个闭包
``configure_actions_log_colors`` 和 ``refresh_actions_log``,
搜索相关的小闭包仍保留在 ``gui_app.py`` 中(它们共享 nonlocal 状态)。
依赖通过显式参数注入:
- ``text_widget`` - 展示用的 Text/ScrolledText
- ``theme_getter`` - 一个 ``() -> str`` 回调,返回当前 GUI 主题名
- ``filter_getter`` - 一个 ``() -> str`` 回调,返回筛选关键字(已 strip)
"""
from __future__ import annotations
import os
import tkinter as tk
from typing import Callable
try:
from gui.components.log_color_config import (
apply_operation_color,
get_color_config,
)
except ImportError:
def get_color_config(theme: str = "litera"): # type: ignore[no-redef]
return {}
def apply_operation_color(*args, **kwargs): # type: ignore[no-redef]
pass
def configure_actions_log_colors(
text_widget: tk.Text,
*,
theme_getter: Callable[[], str],
) -> None:
"""根据当前主题配置行为日志的颜色标签"""
try:
# 获取当前主题
try:
current_theme = theme_getter()
except Exception:
current_theme = "litera"
# 获取颜色配置
color_config = get_color_config(current_theme)
# 根据主题设置背景和前景色
light_themes = {"litera", "flatly", "journal", "minty", "cosmo"}
is_light = current_theme.lower() in light_themes
if is_light:
text_widget.configure(
background="#FFFFFF",
foreground="#2C2C2C",
insertbackground="#2C2C2C",
)
else:
text_widget.configure(
background="#1E1E1E",
foreground="#E0E0E0",
insertbackground="#FFFFFF",
)
# 为所有操作类型配置标签
for op_type, colors in color_config.items():
tag_name = f"op_{op_type}"
try:
text_widget.tag_configure(
tag_name,
foreground=colors["foreground"],
background=colors["background"],
)
except Exception:
pass # 忽略标签配置失败
except Exception:
pass # 忽略颜色配置失败
def refresh_actions_log(
text_widget: tk.Text,
*,
theme_getter: Callable[[], str],
filter_getter: Callable[[], str],
) -> None:
"""读取 logs/actions.log 的尾部若干行,在 GUI 中展示关键行为日志(支持简单关键字筛选)。"""
# 使用统一的路径配置
try:
from douyin.core.path_config import get_actions_log_path
path = get_actions_log_path()
except Exception:
# 回退到旧路径(向后兼容)
# 修正路径:GUI应该读取根目录的logs/actions.log
from douyin.core import path_config
path = path_config.get_actions_log_path()
try:
if os.path.exists(path):
# 仿照 web_gui 的实现:只读最后一小段,避免文件巨大
size = os.path.getsize(path)
if size == 0:
text_widget.delete("1.0", "end")
text_widget.insert("1.0", "行为日志文件为空")
return
read_bytes = min(size, 65536)
with open(path, "rb") as f:
if read_bytes < size:
f.seek(-read_bytes, os.SEEK_END)
data = f.read()
# 尝试多种编码方式
try:
# 先尝试 utf-8-sig(带BOM)
text = data.decode("utf-8-sig", errors="replace")
except Exception:
try:
# 再尝试 utf-8
text = data.decode("utf-8", errors="replace")
except Exception:
# 最后尝试 gbk(Windows常见编码)
text = data.decode("gbk", errors="replace")
lines = text.splitlines()
# 只取最近 300 行
tail = lines[-300:]
# 关键字筛选:若填写了筛选词,只保留包含该关键字的行
kw = filter_getter()
if kw:
tail = [ln for ln in tail if kw in ln]
# 保存当前滚动位置,避免刷新时自动置顶
try:
scroll_position = text_widget.yview()[
0
] # 获取当前滚动位置(顶部可见位置的比例)
except Exception:
scroll_position = 0.0
text_widget.delete("1.0", "end")
if tail:
# 获取当前主题(用于颜色配置)
try:
current_theme = theme_getter()
except Exception:
current_theme = "litera"
# 逐行插入并应用颜色标签
for i, line in enumerate(tail):
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:
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_position)
except Exception:
pass
else:
text_widget.delete("1.0", "end")
text_widget.insert(
"1.0", "暂无行为日志(logs/actions.log 不存在)"
)
except Exception as e:
text_widget.delete("1.0", "end")
text_widget.insert("1.0", f"读取行为日志失败:{e}")
+13 -139
View File
@@ -50,6 +50,10 @@ from gui.components.wifi_adb_dialog import open_wifi_adb_dialog as _open_wifi_ad
from gui.components.reservations_view import refresh_reservations as _refresh_reservations_impl
from gui.components.high_value_view import refresh_high_value_rooms as _refresh_high_value_rooms_impl
from gui.components.screenshot_action import do_screenshot as _do_screenshot_impl
from gui.components.actions_log_view import (
configure_actions_log_colors as _configure_actions_log_colors_impl,
refresh_actions_log as _refresh_actions_log_impl,
)
# 导入日志颜色配置模块
try:
@@ -3830,46 +3834,10 @@ def main():
# 配置行为日志颜色标签(使用统一的颜色配置模块)
def configure_actions_log_colors():
"""根据当前主题配置行为日志的颜色标签"""
try:
# 获取当前主题
try:
current_theme = str(gui_theme_var.get() or "litera")
except Exception:
current_theme = "litera"
# 获取颜色配置
color_config = get_color_config(current_theme)
# 根据主题设置背景和前景色
light_themes = {"litera", "flatly", "journal", "minty", "cosmo"}
is_light = current_theme.lower() in light_themes
if is_light:
actions_log_text.configure(
background="#FFFFFF",
foreground="#2C2C2C",
insertbackground="#2C2C2C",
)
else:
actions_log_text.configure(
background="#1E1E1E",
foreground="#E0E0E0",
insertbackground="#FFFFFF",
)
# 为所有操作类型配置标签
for op_type, colors in color_config.items():
tag_name = f"op_{op_type}"
try:
actions_log_text.tag_configure(
tag_name,
foreground=colors["foreground"],
background=colors["background"],
)
except Exception:
pass # 忽略标签配置失败
except Exception:
pass # 忽略颜色配置失败
_configure_actions_log_colors_impl(
actions_log_text,
theme_getter=lambda: str(gui_theme_var.get() or "litera"),
)
# 初始配置
configure_actions_log_colors()
@@ -3877,105 +3845,11 @@ def main():
actions_filter_var = tk.StringVar(value="")
def refresh_actions_log():
"""读取 logs/actions.log 的尾部若干行,在 GUI 中展示关键行为日志(支持简单关键字筛选)。"""
# 使用统一的路径配置
try:
from douyin.core.path_config import get_actions_log_path
path = get_actions_log_path()
except Exception:
# 回退到旧路径(向后兼容)
# 修正路径:GUI应该读取根目录的logs/actions.log
from douyin.core import path_config
path = path_config.get_actions_log_path()
try:
if os.path.exists(path):
# 仿照 web_gui 的实现:只读最后一小段,避免文件巨大
size = os.path.getsize(path)
if size == 0:
actions_log_text.delete("1.0", "end")
actions_log_text.insert("1.0", "行为日志文件为空")
return
read_bytes = min(size, 65536)
with open(path, "rb") as f:
if read_bytes < size:
f.seek(-read_bytes, os.SEEK_END)
data = f.read()
# 尝试多种编码方式
try:
# 先尝试 utf-8-sig(带BOM)
text = data.decode("utf-8-sig", errors="replace")
except Exception:
try:
# 再尝试 utf-8
text = data.decode("utf-8", errors="replace")
except Exception:
# 最后尝试 gbk(Windows常见编码)
text = data.decode("gbk", errors="replace")
lines = text.splitlines()
# 只取最近 300 行
tail = lines[-300:]
# 关键字筛选:若填写了筛选词,只保留包含该关键字的行
kw = actions_filter_var.get().strip()
if kw:
tail = [ln for ln in tail if kw in ln]
# 保存当前滚动位置,避免刷新时自动置顶
try:
scroll_position = actions_log_text.yview()[
0
] # 获取当前滚动位置(顶部可见位置的比例)
except Exception:
scroll_position = 0.0
actions_log_text.delete("1.0", "end")
if tail:
# 获取当前主题(用于颜色配置)
try:
current_theme = str(gui_theme_var.get() or "litera")
except Exception:
current_theme = "litera"
# 逐行插入并应用颜色标签
for i, line in enumerate(tail):
start_pos = actions_log_text.index("end-1c")
# 插入行内容
if i > 0:
actions_log_text.insert("end", "\n")
actions_log_text.insert("end", line)
# 使用统一的颜色配置模块应用颜色
end_pos = actions_log_text.index("end")
try:
apply_operation_color(
actions_log_text,
start_pos,
end_pos,
line,
current_theme,
)
except Exception:
pass # 忽略颜色应用失败
else:
actions_log_text.insert("1.0", "暂无匹配的行为日志")
# 恢复之前的滚动位置,避免自动置顶
try:
actions_log_text.yview_moveto(scroll_position)
except Exception:
pass
else:
actions_log_text.delete("1.0", "end")
actions_log_text.insert(
"1.0", "暂无行为日志(logs/actions.log 不存在)"
)
except Exception as e:
actions_log_text.delete("1.0", "end")
actions_log_text.insert("1.0", f"读取行为日志失败:{e}")
_refresh_actions_log_impl(
actions_log_text,
theme_getter=lambda: str(gui_theme_var.get() or "litera"),
filter_getter=lambda: actions_filter_var.get().strip(),
)
ttk.Label(actions_log_top, text="关键字筛选:").pack(side=LEFT)
ttk.Entry(actions_log_top, textvariable=actions_filter_var, width=16).pack(