背景
extensions/ui-customization/ 负责 OpenPI 终端 TUI 状态栏(Footer)及自定义视觉渲染。在状态栏布局中,cwd(当前工作区目录)是核心组件之一,通过 formatDirectory(cwd) 将处于用户 Home 目录下的工作路径智能缩写为 ~/...,以节省有限的终端状态栏列宽,避免挤占右侧模型名称、Context 占用百分比、Git 分支等关键指标。
问题与代码定位
在 extensions/ui-customization/footer.ts 中,formatDirectory 目前硬编码使用正斜杠判断前缀:
export function formatDirectory(cwd: string) {
const home = homedir();
if (cwd === home) return "~";
const display = cwd.startsWith(`${home}/`) ? `~/${relative(home, cwd)}` : cwd;
return sanitizeTerminalLabel(display);
}
该实现在 Windows 环境下存在跨平台路径分隔符缺陷:
- 在 Windows 上,
homedir() 返回反斜杠路径(如 C:\Users\username),而 Windows 系统的 cwd 同样为反斜杠分隔(如 C:\Users\username\openprojects\openpi)。
${home}/(如 C:\Users\username/)与实际的 cwd 反斜杠无法匹配,cwd.startsWith(${home}/) 永远判定为 false。
- 导致 Windows 用户的主目录路径永远无法被缩写为
~/...,始终输出完整绝对路径。
影响
在标准宽度终端(如 80 ~ 120 列)下,未缩写的长路径(如 📁 C:\Users\Administrator\Documents\workspaces\...)会过度霸占状态栏宽度:
- 挤压右侧其他重要 Footer Segment(如
model、context、git、thinking);
- 在窄屏下触发激进的尾部截断或丢弃低优先级指标,恶化 Windows 用户的 TUI 交互体验。
最小复现证据
在 Node/Bun 环境下模拟 Windows 路径:
import path from "node:path";
function formatDirectory(cwd: string, home: string) {
if (cwd === home) return "~";
const display = cwd.startsWith(`${home}/`) ? `~/${path.win32.relative(home, cwd)}` : cwd;
return display;
}
const winHome = "C:\\Users\\Administrator";
const winCwd = "C:\\Users\\Administrator\\openprojects\\openpi";
console.log(formatDirectory(winCwd, winHome));
// 实际输出: "C:\Users\Administrator\openprojects\openpi" (未缩写)
// 预期输出: "~/openprojects/openpi"
建议的最小修复方向
- 兼容 Windows 路径分隔符:
- 增加对
${home}\\(或 path.sep)的前缀匹配支持:
const isSubdir = cwd.startsWith(`${home}/`) || cwd.startsWith(`${home}\\`);
- 统一跨平台展示格式:
- 将
relative(home, cwd) 后的路径分隔符统一规范化为 / 展示(如 ~/openprojects/openpi),保持不同操作系统间状态栏输出风格的一致性。
- 补充单测:
- 在
tests/extensions/ui-customization/footer.test.ts 中增加针对 Windows 路径前缀(C:\Users\...)和 POSIX 路径前缀的格式化测试用例。
建议验证
背景
extensions/ui-customization/负责 OpenPI 终端 TUI 状态栏(Footer)及自定义视觉渲染。在状态栏布局中,cwd(当前工作区目录)是核心组件之一,通过formatDirectory(cwd)将处于用户 Home 目录下的工作路径智能缩写为~/...,以节省有限的终端状态栏列宽,避免挤占右侧模型名称、Context 占用百分比、Git 分支等关键指标。问题与代码定位
在
extensions/ui-customization/footer.ts中,formatDirectory目前硬编码使用正斜杠判断前缀:该实现在 Windows 环境下存在跨平台路径分隔符缺陷:
homedir()返回反斜杠路径(如C:\Users\username),而 Windows 系统的cwd同样为反斜杠分隔(如C:\Users\username\openprojects\openpi)。${home}/(如C:\Users\username/)与实际的cwd反斜杠无法匹配,cwd.startsWith(${home}/)永远判定为false。~/...,始终输出完整绝对路径。影响
在标准宽度终端(如 80 ~ 120 列)下,未缩写的长路径(如
📁 C:\Users\Administrator\Documents\workspaces\...)会过度霸占状态栏宽度:model、context、git、thinking);最小复现证据
在 Node/Bun 环境下模拟 Windows 路径:
建议的最小修复方向
${home}\\(或path.sep)的前缀匹配支持:relative(home, cwd)后的路径分隔符统一规范化为/展示(如~/openprojects/openpi),保持不同操作系统间状态栏输出风格的一致性。tests/extensions/ui-customization/footer.test.ts中增加针对 Windows 路径前缀(C:\Users\...)和 POSIX 路径前缀的格式化测试用例。建议验证
~/...缩写行为保持不变;~/...;cwd === home保持返回~;bun run check与全量测试套件通过.