import { Box } from "ink"; import { CLI_GLYPHS } from "@/cli/helpers/glyphs"; import { useTerminalWidth } from "@/cli/hooks/use-terminal-width"; import { Text } from "./Text"; const PREFIX_WIDTH = 5; const LABEL_WIDTH = 8; export interface EnterWorktreeDisplayResult { action: "created" | "switched"; path?: string; branch?: string; base?: string; switchedCwd?: boolean; } export function parseEnterWorktreeResult( text: string, ): EnterWorktreeDisplayResult | null { const normalized = text.replace(/\r\n/g, "\n"); const firstLine = normalized.split("\n").find((line) => line.trim()); const action = firstLine?.trim() === "Created worktree." ? "created" : firstLine?.trim() === "Switched to existing worktree." ? "switched" : null; if (!action) { return null; } const field = (name: string): string | undefined => { const match = normalized.match(new RegExp(`^${name}:\\s*(.+)$`, "m")); return match?.[1]?.trim(); }; const result: EnterWorktreeDisplayResult = { action, path: field("Path"), branch: field("Branch"), base: field("Base"), }; if ( normalized.includes( "This conversation's working directory is now the new worktree.", ) || normalized.includes( "This conversation's working directory is now this worktree.", ) ) { result.switchedCwd = true; } else if ( normalized.includes( "The conversation working directory was left unchanged.", ) ) { result.switchedCwd = false; } if (!result.path && !result.branch && !result.base) { return null; } return result; } function DetailRow({ label, value }: { label: string; value: string }) { const columns = useTerminalWidth(); const contentWidth = Math.max(0, columns - PREFIX_WIDTH); const valueWidth = Math.max(0, contentWidth - LABEL_WIDTH); return ( {" ".repeat(PREFIX_WIDTH)} {label}: {value} ); } export function EnterWorktreeResultRenderer({ resultText, }: { resultText: string; }) { const parsed = parseEnterWorktreeResult(resultText); const columns = useTerminalWidth(); const contentWidth = Math.max(0, columns - PREFIX_WIDTH); if (!parsed) { return null; } const cwdStatus = parsed.switchedCwd === undefined ? undefined : parsed.switchedCwd ? "switched to worktree" : "unchanged"; return ( {` ${CLI_GLYPHS.result} `} {parsed.action === "created" ? "Created worktree" : "Switched to existing worktree"} {parsed.path ? : null} {parsed.branch ? ( ) : null} {parsed.base ? : null} {cwdStatus ? : null} ); }