import type { InitCodeDiff, InitEncryptionSummary, InitRuntimeState, InitStreamingOutput } from '../runtime' import { exit } from 'node:process' import { Alert } from '@inkjs/ui' import { Box, Text, useInput, useStdout } from 'ink' import Spinner from 'ink-spinner' import React, { useSyncExternalStore } from 'react' import { INIT_CANCEL } from '../runtime' import { CurrentStepSection, InitHeader, ProgressSection, PromptArea, ScreenIntro, SpinnerArea } from './components' function StreamingOutputPanel({ output, width, rows }: Readonly<{ output: InitStreamingOutput, width: number, rows: number }>) { // Reserve rows for: header (3) + panel borders/title (4) + footer status (2) // + a tiny safety margin (2). The rest is log body. const visibleLineCount = Math.max(5, rows - 11) const visibleLines = output.lines.slice(-visibleLineCount) const borderColor = output.status === 'success' ? 'green' : output.status === 'error' ? 'red' : 'cyan' useInput((input, key) => { const continuePrompt = output.continuePrompt if (key.ctrl && input === 'c') { if (continuePrompt) { continuePrompt.resolve(INIT_CANCEL) } else if (output.onCancel) { output.onCancel?.() } else { exit(130) } return } if (continuePrompt && key.return) continuePrompt.resolve() }) return ( {output.title} {` $ ${output.command}`} {visibleLines.length === 0 ? ( (waiting for output...) ) : ( visibleLines.map((line, index) => { const trimmed = line.trimStart() const isSuccess = trimmed.startsWith('✔') || trimmed.startsWith('✓') || trimmed.startsWith('[success]') const isError = trimmed.startsWith('✖') || trimmed.startsWith('❌') || trimmed.startsWith('[error]') || trimmed.startsWith('ERROR') const isWarn = trimmed.startsWith('⚠') || trimmed.startsWith('[warn]') || trimmed.startsWith('WARN') const isInfo = trimmed.startsWith('ℹ') || trimmed.startsWith('[info]') const isCap = trimmed.startsWith('[capacitor]') const isPath = /^\s+[a-z]/.test(line) && (line.includes('/') || line.includes('\\')) const color = isSuccess ? 'green' : isError ? 'red' : isWarn ? 'yellow' : isInfo ? 'cyan' : isCap ? 'blue' : isPath ? 'gray' : undefined const dim = isPath return ( {line} ) }) )} {output.status === 'running' ? ( {' '} {output.statusMessage ?? 'Running...'} ) : ( {output.status === 'success' ? '✓ ' : '✖ '} {output.statusMessage ?? (output.status === 'success' ? 'Done' : 'Failed')} )} {output.continuePrompt && ( {output.continuePrompt.message} )} ) } function encryptionPhaseColor(phase: InitEncryptionSummary['phase']): 'green' | 'yellow' | 'red' { switch (phase) { case 'enabled': return 'green' case 'failed': return 'red' case 'pending-sync': case 'skipped': default: return 'yellow' } } function EncryptionSummaryPanel({ summary, width }: Readonly<{ summary: InitEncryptionSummary, width: number }>) { const borderColor = encryptionPhaseColor(summary.phase) return ( {summary.title} {summary.lines.length > 0 && ( {summary.lines.map((line, index) => ( {line} ))} )} ) } function CodeDiffPanel({ diff, width }: Readonly<{ diff: InitCodeDiff, width: number }>) { const title = diff.created ? `Created ${diff.filePath}` : `Updated ${diff.filePath}` const maxLineNumber = diff.lines.reduce((max, line) => Math.max(max, line.lineNumber), 0) const gutterWidth = Math.max(2, String(maxLineNumber).length) return ( {`📝 ${title}`} {diff.note !== undefined && ( {` ${diff.note}`} )} {diff.lines.length > 0 && ( {diff.lines.map((line, index) => { const marker = line.kind === 'add' ? '+' : ' ' const lineNum = String(line.lineNumber).padStart(gutterWidth, ' ') const color = line.kind === 'add' ? 'green' : 'gray' return ( {`${marker} ${lineNum} │ ${line.text}`} ) })} )} ) } interface InitInkAppProps { getSnapshot: () => InitRuntimeState subscribe: (listener: () => void) => () => void updatePromptError: (error?: string) => void } export default function InitInkApp({ getSnapshot, subscribe, updatePromptError }: Readonly) { const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot) const { stdout } = useStdout() const columns = stdout?.columns ?? 96 const rows = stdout?.rows ?? 24 const contentWidth = Math.max(0, columns - 6) useInput((input, key) => { const skip = snapshot.logSkip if (!skip) return if (key.ctrl && input === 'c') { skip.resolve() return } if (!key.ctrl && (input === 'c' || input === 'C')) skip.resolve() }, { isActive: Boolean(snapshot.logSkip) && !snapshot.prompt && !snapshot.streamingOutput }) // Estimate how many terminal rows the code diff panel consumes so the log // area (and the prompt/spinner rendered after it) still fit in the viewport // on short terminals. Overhead covers the panel's marginTop, top/bottom // borders, title line, and the marginTop between title and line content. // Long lines that wrap are approximated by counting each line's wrap count. const diffPanelHeight = (() => { const diff = snapshot.codeDiff if (!diff) return 0 const innerWidth = Math.max(1, contentWidth - 4) const wrappedLineRows = diff.lines.reduce((sum, line) => { const rendered = ` ${String(line.lineNumber)} │ ${line.text}` return sum + Math.max(1, Math.ceil(rendered.length / innerWidth)) }, 0) const noteRows = diff.note !== undefined ? 1 : 0 const linesBlockRows = diff.lines.length > 0 ? wrappedLineRows + 1 : 0 // 1 (panel marginTop) + 2 (borders) + 1 (title) + noteRows + linesBlockRows return 4 + noteRows + linesBlockRows })() // Same overhead math as the diff panel: marginTop + borders + title + // (optional) lines block. Wrap-aware so long bullet lines don't push the // prompt off-screen on narrow terminals. const encryptionPanelHeight = (() => { const summary = snapshot.encryptionSummary if (!summary) return 0 const innerWidth = Math.max(1, contentWidth - 4) const wrappedLineRows = summary.lines.reduce((sum, line) => { return sum + Math.max(1, Math.ceil(line.length / innerWidth)) }, 0) const linesBlockRows = summary.lines.length > 0 ? wrappedLineRows + 1 : 0 // 1 (panel marginTop) + 2 (borders) + 1 (title) + linesBlockRows return 4 + linesBlockRows })() // `Array.prototype.slice(-0)` returns the full array because `-0` coerces // to `0`, so we cannot feed a zero clamp into slice — explicitly short- // circuit to an empty array when there's no viewport budget left for logs. const visibleLogCount = Math.max(0, rows - 14 - diffPanelHeight - encryptionPanelHeight) const visibleLogs = visibleLogCount === 0 ? [] : snapshot.logs.slice(-visibleLogCount) const screen = snapshot.screen // When a streaming command is running we hand the entire viewport over to // the streaming panel — no progress bar, no logs, no prompt. This keeps // long-lived `cap sync` output visible without fighting the normal // onboarding chrome for space. The InitHeader stays so the user knows // they're still inside `capgo init`. if (snapshot.streamingOutput) { return ( {screen ? : null} ) } return ( {screen ? : null} {snapshot.versionWarning && ( You are using @capgo/cli@{snapshot.versionWarning.currentVersion} — update to @capgo/cli@{snapshot.versionWarning.latestVersion} or @capgo/cli@{snapshot.versionWarning.majorVersion} )} {screen?.introLines?.length || screen?.title ? : null} {screen && } {screen && } {snapshot.codeDiff && ( )} {snapshot.encryptionSummary && ( )} {visibleLogs.length > 0 && ( {visibleLogs.map((entry, index) => ( {entry.message} ))} )} ) }