Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion trendradar/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,35 @@ def _should_open_browser(self) -> bool:
"""判断是否应该打开浏览器"""
return not self.is_github_actions and not self.is_docker_container

@staticmethod
def _is_wsl() -> bool:
"""检测是否运行在 WSL 中"""
try:
with open("/proc/version", "r") as f:
return "microsoft" in f.read().lower()
except Exception:
return False

@staticmethod
def _open_in_windows_browser(html_file: str) -> None:
"""在 WSL 中用 Windows 默认浏览器打开文件"""
import subprocess
try:
result = subprocess.run(
["wslpath", "-w", html_file],
capture_output=True, text=True
)
win_path = result.stdout.strip()
if win_path:
subprocess.Popen(
["powershell.exe", "-Command", f'Start-Process "{win_path}"'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
else:
print(f"路径转换失败,手动打开: {html_file}")
except Exception as e:
print(f"打开浏览器失败: {e},手动打开: {html_file}")

def _setup_proxy(self) -> None:
"""设置代理配置"""
if not self.is_github_actions and self.ctx.config["USE_PROXY"]:
Expand Down Expand Up @@ -1725,7 +1754,10 @@ def _execute_mode_strategy(
if self._should_open_browser() and html_file:
file_url = "file://" + str(Path(html_file).resolve())
print(f"正在打开HTML报告: {file_url}")
webbrowser.open(file_url)
if self._is_wsl():
self._open_in_windows_browser(html_file)
else:
webbrowser.open(file_url)
elif self.is_docker_container and html_file:
print(f"HTML报告已生成(Docker环境): {html_file}")

Expand Down