"""行为日志(``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}")