Skip to content

Commit d9b2d66

Browse files
committed
fix(cli): render spinner immediately on Enter and clean up cursor/tool display (#78, #79)
1 parent 0e2ebde commit d9b2d66

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ async function listSessions(): Promise<Session.Info[]> {
376376

377377
async function handleCustomCommand(name: string, args: string) {
378378
const spinner = new Spinner("Thinking")
379+
spinner.start()
379380

380381
let first = true
381382
let totalTokens = 0
@@ -396,8 +397,6 @@ async function handleCustomCommand(name: string, args: string) {
396397
sessionID: currentSessionID || undefined,
397398
}
398399

399-
spinner.start()
400-
401400
try {
402401
for await (const chunk of command(name, args, options)) {
403402
if (chunk.type === "start" && chunk.sessionID && !currentSessionID) {
@@ -424,14 +423,15 @@ async function handleCustomCommand(name: string, args: string) {
424423
if (chunk.type === "tool_start" && chunk.tool?.trim()) {
425424
const tool = chunk.tool.trim()
426425
const arg = summarizeInput(tool, chunk.input)
426+
const cols = process.stdout.columns || 80
427427

428428
if (tool === lastToolName && arg === lastToolArg) {
429429
repeatCount += 1
430430
const count = repeatCount + 1
431431
const countText = count > 1 ? ` (×${count})` : ""
432-
write(
433-
`\x1b[1A\r\x1b[2K${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${arg ? ` ${arg}` : ""}${countText}${style.reset}\n`,
434-
)
432+
const line = `${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${arg ? ` ${arg}` : ""}${countText}${style.reset}`
433+
const display = line.length > cols ? line.slice(0, cols - 1) + "…" : line
434+
write(`\x1b[1A\r\x1b[2K${display}\n`)
435435
} else {
436436
if (lastChunkType === "text") {
437437
write("\n")
@@ -440,7 +440,9 @@ async function handleCustomCommand(name: string, args: string) {
440440
lastToolArg = arg
441441
repeatCount = 0
442442
const summary = arg ? ` ${arg}` : ""
443-
write(`${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${summary}${style.reset}\n`)
443+
const line = `${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${summary}${style.reset}`
444+
const display = line.length > cols ? line.slice(0, cols - 1) + "…" : line
445+
write(`${display}\n`)
444446
}
445447

446448
lastToolSummary = arg
@@ -451,13 +453,14 @@ async function handleCustomCommand(name: string, args: string) {
451453
if (chunk.type === "tool_end" && chunk.tool?.trim()) {
452454
const tool = chunk.tool.trim()
453455
const arg = summarizeInput(tool, chunk.input)
456+
const cols = process.stdout.columns || 80
454457

455458
if (lastChunkWasToolStart && lastToolSummary === arg) {
456459
const count = repeatCount + 1
457460
const countText = count > 1 ? ` (×${count})` : ""
458-
write(
459-
`\x1b[1A\r\x1b[2K${fg.green}✓${style.reset} ${fg.cyan}${tool}${style.reset}${fg.gray}${arg ? ` ${arg}` : ""}${countText}${style.reset}\n`,
460-
)
461+
const line = `${fg.green}✓${style.reset} ${fg.cyan}${tool}${style.reset}${fg.gray}${arg ? ` ${arg}` : ""}${countText}${style.reset}`
462+
const display = line.length > cols ? line.slice(0, cols - 1) + "…" : line
463+
write(`\x1b[1A\r\x1b[2K${display}\n`)
461464
} else {
462465
write(`${fg.green}✓${style.reset} ${fg.cyan}${tool}${style.reset}\n`)
463466
}
@@ -484,7 +487,7 @@ async function handleCustomCommand(name: string, args: string) {
484487
const duration = Date.now() - startTime
485488
write(`${fg.gray}${formatDuration(duration)} · ${formatTokens(totalTokens)}${style.reset}\n`)
486489
} finally {
487-
write(cursor.show)
490+
spinner.stop(true)
488491
}
489492
} catch (err) {
490493
if (first) spinner.stop(false)
@@ -499,6 +502,7 @@ async function handleCustomCommand(name: string, args: string) {
499502

500503
async function handleMessage(message: string) {
501504
const spinner = new Spinner("Thinking")
505+
spinner.start()
502506

503507
let first = true
504508
let totalTokens = 0
@@ -519,8 +523,6 @@ async function handleMessage(message: string) {
519523
sessionID: currentSessionID || undefined,
520524
}
521525

522-
spinner.start()
523-
524526
try {
525527
for await (const chunk of chat(message, options)) {
526528
if (chunk.type === "start" && chunk.sessionID && !currentSessionID) {
@@ -547,14 +549,15 @@ async function handleMessage(message: string) {
547549
if (chunk.type === "tool_start" && chunk.tool?.trim()) {
548550
const tool = chunk.tool.trim()
549551
const arg = summarizeInput(tool, chunk.input)
552+
const cols = process.stdout.columns || 80
550553

551554
if (tool === lastToolName && arg === lastToolArg) {
552555
repeatCount += 1
553556
const count = repeatCount + 1
554557
const countText = count > 1 ? ` (×${count})` : ""
555-
write(
556-
`\x1b[1A\r\x1b[2K${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${arg ? ` ${arg}` : ""}${countText}${style.reset}\n`,
557-
)
558+
const line = `${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${arg ? ` ${arg}` : ""}${countText}${style.reset}`
559+
const display = line.length > cols ? line.slice(0, cols - 1) + "…" : line
560+
write(`\x1b[1A\r\x1b[2K${display}\n`)
558561
} else {
559562
if (lastChunkType === "text") {
560563
write("\n")
@@ -563,7 +566,9 @@ async function handleMessage(message: string) {
563566
lastToolArg = arg
564567
repeatCount = 0
565568
const summary = arg ? ` ${arg}` : ""
566-
write(`${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${summary}${style.reset}\n`)
569+
const line = `${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${summary}${style.reset}`
570+
const display = line.length > cols ? line.slice(0, cols - 1) + "…" : line
571+
write(`${display}\n`)
567572
}
568573

569574
lastToolSummary = arg
@@ -574,13 +579,14 @@ async function handleMessage(message: string) {
574579
if (chunk.type === "tool_end" && chunk.tool?.trim()) {
575580
const tool = chunk.tool.trim()
576581
const arg = summarizeInput(tool, chunk.input)
582+
const cols = process.stdout.columns || 80
577583

578584
if (lastChunkWasToolStart && lastToolSummary === arg) {
579585
const count = repeatCount + 1
580586
const countText = count > 1 ? ` (×${count})` : ""
581-
write(
582-
`\x1b[1A\r\x1b[2K${fg.green}✓${style.reset} ${fg.cyan}${tool}${style.reset}${fg.gray}${arg ? ` ${arg}` : ""}${countText}${style.reset}\n`,
583-
)
587+
const line = `${fg.green}✓${style.reset} ${fg.cyan}${tool}${style.reset}${fg.gray}${arg ? ` ${arg}` : ""}${countText}${style.reset}`
588+
const display = line.length > cols ? line.slice(0, cols - 1) + "…" : line
589+
write(`\x1b[1A\r\x1b[2K${display}\n`)
584590
} else {
585591
write(`${fg.green}✓${style.reset} ${fg.cyan}${tool}${style.reset}\n`)
586592
}
@@ -607,7 +613,7 @@ async function handleMessage(message: string) {
607613
const duration = Date.now() - startTime
608614
write(`${fg.gray}${formatDuration(duration)} · ${formatTokens(totalTokens)}${style.reset}\n`)
609615
} finally {
610-
write(cursor.show)
616+
spinner.stop(true)
611617
}
612618
} catch (err) {
613619
if (first) spinner.stop(false)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export class Spinner {
1414

1515
start() {
1616
write(cursor.hide)
17+
this.frame = 0
1718
this.startTime = Date.now()
19+
write(`\r${clear.line}${fg.cyan}${frames[0]}${style.reset} ${fg.gray}${this.text}${style.reset}`)
1820
this.interval = setInterval(() => {
1921
this.frame = (this.frame + 1) % frames.length
2022
const elapsed = Math.floor((Date.now() - this.startTime) / 1000)

0 commit comments

Comments
 (0)