Skip to content

Commit 1ac50cd

Browse files
committed
feat(cli): add word wrap with margins and force eager bootstrap init (#57, #87)
1 parent 41e280d commit 1ac50cd

3 files changed

Lines changed: 64 additions & 11 deletions

File tree

‎packages/opencode/src/cli/lite/index.ts‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,20 @@ import { select } from "./select"
1919
import type { ChatChunk } from "./session"
2020
import { createLiveBlock } from "./liveblock"
2121
import { summarizeInput } from "./summary"
22+
import { wrap } from "./wrap"
2223
export { summarizeInput }
2324

25+
// UI Constants
26+
const PAD = " "
27+
const MAX_WIDTH = Math.min(100, (process.stdout.columns || 80) - 4)
28+
29+
function padLines(text: string): string {
30+
return text
31+
.split("\n")
32+
.map((line) => (line ? `${PAD}${line}` : line))
33+
.join("\n")
34+
}
35+
2436
// UI State
2537
let tasksVisible = false
2638
let isOperationInProgress = false
@@ -72,10 +84,13 @@ async function main() {
7284
// Bootstrap with Instance context for current directory
7385
try {
7486
await bootstrap(process.cwd(), async () => {
87+
// Force eager init of heavy subsystems during spinner
88+
await Promise.all([Provider.list(), Agent.list()])
89+
7590
setup.stop(true)
7691
write(`${fg.green}✓${style.reset} Ready\n\n`)
7792

78-
// Initialize default agent
93+
// Initialize default agent (agentList is cached from eager init)
7994
currentAgent = await Agent.defaultAgent()
8095
const agentList = await Agent.list()
8196

@@ -366,8 +381,8 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
366381
if (first && chunk.type !== "done" && chunk.type !== "start") {
367382
spinner.stop(true)
368383
first = false
369-
write(`${fg.gray}(Esc to cancel)${style.reset}\n`)
370-
const rule = `${style.dim}${"─".repeat(Math.min(process.stdout.columns || 80, 80))}${style.reset}\n`
384+
write(`${PAD}${fg.gray}(Esc to cancel)${style.reset}\n`)
385+
const rule = `${PAD}${style.dim}${"─".repeat(Math.min(process.stdout.columns || 80, 80))}${style.reset}\n`
371386
write(rule)
372387
}
373388

@@ -376,7 +391,9 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
376391
lastToolKey = ""
377392
dedupCount = 0
378393
lastChunkType = "text"
379-
write(md.render(chunk.content))
394+
const rendered = md.render(chunk.content)
395+
const wrapped = wrap(rendered, MAX_WIDTH)
396+
write(padLines(wrapped))
380397
}
381398

382399
if (chunk.type === "tool_start" && chunk.tool?.trim()) {
@@ -424,7 +441,7 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
424441
dedupCount = 0
425442
lastChunkType = "error"
426443
const safeContent = chunk.content.replace(/\x1b\[[0-9;]*m/g, "").replace(/[\x00-\x1f\x7f]/g, "")
427-
write(`\n${fg.red}Error: ${safeContent}${style.reset}\n`)
444+
write(`\n${PAD}${fg.red}Error: ${safeContent}${style.reset}\n`)
428445
}
429446

430447
if (chunk.tokens !== undefined) {
@@ -434,10 +451,10 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
434451

435452
block.freeze()
436453
write(md.flush())
437-
const rule = `\n${style.dim}${"─".repeat(Math.min(process.stdout.columns || 80, 80))}${style.reset}\n`
454+
const rule = `\n${PAD}${style.dim}${"─".repeat(Math.min(process.stdout.columns || 80, 80))}${style.reset}\n`
438455
write(rule)
439456
const duration = Date.now() - startTime
440-
write(`${fg.gray}${formatDuration(duration)} · ${formatTokens(totalTokens)}${style.reset}\n`)
457+
write(`${PAD}${fg.gray}${formatDuration(duration)} · ${formatTokens(totalTokens)}${style.reset}\n`)
441458
} finally {
442459
spinner.stop(true)
443460
}

‎packages/opencode/src/cli/lite/liveblock.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@ export function createLiveBlock() {
4848
const showEllipsis = tool.summary.length > maxLen
4949
const text = showEllipsis ? `${tool.summary.slice(0, maxLen)}…` : tool.summary
5050
const display = `${icon} ${fg.cyan}${tool.name}${style.reset}${fg.gray}${sep}${text}${style.reset}`
51-
lines.push(display)
51+
lines.push(` ${display}`)
5252
}
5353

5454
// Render running tasks
5555
for (const task of tasks.values()) {
5656
const spinner =
5757
task.status === "running" ? `${fg.cyan}${frames[frame]}${style.reset}` : `${fg.green}✓${style.reset}`
5858
const time = task.elapsed > 0 ? ` ${fg.gray}(${task.elapsed}s)${style.reset}` : ""
59-
lines.push(`${spinner} ${fg.gray}@${task.agent}:${style.reset} ${task.description}${time}`)
59+
lines.push(` ${spinner} ${fg.gray}@${task.agent}:${style.reset} ${task.description}${time}`)
6060
}
6161

6262
// Render todos if any
6363
if (todos.length > 0) {
64-
lines.push(`${style.dim}${"─".repeat(Math.min(cols, 60))}${style.reset}`)
64+
lines.push(` ${style.dim}${"─".repeat(Math.min(cols, 60))}${style.reset}`)
6565
for (const todo of todos) {
6666
const isCompleted = todo.status === "completed"
6767
const isInProgress = todo.status === "in_progress"
@@ -75,7 +75,7 @@ export function createLiveBlock() {
7575
: `${fg.gray}○${style.reset}`
7676
const dim = isCompleted || isCancelled ? style.dim : ""
7777
const reset = dim ? style.reset : ""
78-
lines.push(`${check} ${dim}${todo.content}${reset}`)
78+
lines.push(` ${check} ${dim}${todo.content}${reset}`)
7979
}
8080
}
8181

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function visible(str: string): number {
2+
return str.replace(/\x1b\[[0-9;]*m/g, "").length
3+
}
4+
5+
export function wrap(text: string, width: number): string {
6+
if (visible(text) <= width) return text
7+
8+
const lines: string[] = []
9+
const paragraphs = text.split("\n")
10+
11+
for (const para of paragraphs) {
12+
if (visible(para) <= width) {
13+
lines.push(para)
14+
continue
15+
}
16+
17+
const words = para.split(/\s+/)
18+
let line = ""
19+
20+
for (const word of words) {
21+
const wordVisible = visible(word)
22+
const lineVisible = visible(line)
23+
24+
if (lineVisible + wordVisible + 1 > width && lineVisible > 0) {
25+
lines.push(line)
26+
line = word
27+
} else {
28+
line = line ? `${line} ${word}` : word
29+
}
30+
}
31+
32+
if (line) lines.push(line)
33+
}
34+
35+
return lines.join("\n")
36+
}

0 commit comments

Comments
 (0)