Skip to content

Your lightweight open-source personal agent.

fix(tui): distinguish fenced code blocks

Xubin Ren455533169d5a
diff --git a/tui/src/app.test.ts b/tui/src/app.test.ts
index 73ae30541..87e7b3dce 100644
--- a/tui/src/app.test.ts
+++ b/tui/src/app.test.ts
@@ -2097,6 +2097,7 @@ describe("NanobotTui layout", () => {
expect(recorder.recordedFrames.length).toBeGreaterThan(0)
expect(recorder.recordedFrames.every(({ frame }) => frame.includes("feat:"))).toBeTrue()
+ expect(recorder.recordedFrames.every(({ frame }) => /│\s+feat:/u.test(frame))).toBeTrue()
expect(recorder.recordedFrames.every(({ frame }) => (
frame.includes("Include the reason in the body.")
))).toBeTrue()
@@ -2130,12 +2131,17 @@ describe("NanobotTui layout", () => {
transcript.history([{ role: "assistant", content: response }])
await setup.flush()
- const code = setup.captureSpans().lines
- .flatMap((line) => line.spans)
- .find((span) => span.text.includes("feat:"))
+ const codeLine = setup.captureSpans().lines.find((line) => (
+ line.spans.some((span) => span.text.includes("feat:"))
+ ))
+ const code = codeLine?.spans.find((span) => span.text.includes("feat:"))
+ const rail = codeLine?.spans.find((span) => span.text.includes("│"))
+ const frame = setup.captureCharFrame()
- expect(setup.captureCharFrame()).toContain("feat:")
+ expect(frame).toContain("feat:")
+ expect(frame).toMatch(/│\s+feat:/u)
expect(code?.fg.toInts().slice(0, 3)).toEqual([24, 24, 27])
+ expect(rail?.fg.toInts().slice(0, 3)).toEqual([212, 212, 216])
})
test("renders assistant LaTeX as Unicode text without changing code", async () => {
diff --git a/tui/src/transcript.ts b/tui/src/transcript.ts
index ec1879871..ff3dd1de8 100644
--- a/tui/src/transcript.ts
+++ b/tui/src/transcript.ts
@@ -1,5 +1,6 @@
import {
BoxRenderable,
+ CodeRenderable,
MarkdownRenderable,
RGBA,
ScrollBoxRenderable,
@@ -64,6 +65,7 @@ const ACTIVITY_PREVIEW_LINES = 4
// additional visible frames. Paint the first token immediately, then coalesce
// subsequent deltas to the renderer cadence.
const STREAM_FLUSH_MS = 32
+const CODE_RAIL_INDENT = 2
export interface UserMessageMedia {
kind?: MediaAttachment["kind"]
@@ -139,6 +141,7 @@ export class Transcript {
private navigationTimer: ReturnType<typeof setTimeout> | null = null
private pendingStream = ""
private streamTimer: ReturnType<typeof setTimeout> | null = null
+ private codeRailColor: RGBA
constructor(
private readonly renderer: CliRenderer,
@@ -147,6 +150,7 @@ export class Transcript {
private readonly onNavigationChange?: (state: TranscriptNavigation) => void,
private readonly workspace = "",
) {
+ this.codeRailColor = RGBA.fromHex(theme.border)
this.root = new ScrollBoxRenderable(renderer, {
id: "nanobot-tui-transcript",
width: "100%",
@@ -175,6 +179,7 @@ export class Transcript {
setTheme(theme: TranscriptTheme): void {
const previousSyntax = this.theme.syntax
this.theme = theme
+ this.codeRailColor = RGBA.fromHex(theme.border)
for (const { renderable, tone } of this.styledText) renderable.fg = theme[tone]
for (const message of this.userMessages) {
message.renderable.content = this.userMessageContent(
@@ -659,6 +664,23 @@ export class Transcript {
return new StyledText(chunks)
}
+ private decorateCodeBlock(code: CodeRenderable): CodeRenderable {
+ code.marginLeft = CODE_RAIL_INDENT
+ const render = code.render.bind(code)
+ code.render = (buffer, deltaTime) => {
+ render(buffer, deltaTime)
+ for (let row = 0; row < code.height; row += 1) {
+ buffer.drawText(
+ "│",
+ code.screenX - CODE_RAIL_INDENT,
+ code.screenY + row,
+ this.codeRailColor,
+ )
+ }
+ }
+ return code
+ }
+
private createMarkdown(content: string, streaming: boolean, id = "markdown"): MarkdownRenderable {
const markdown = new MarkdownRenderable(this.renderer, {
id: this.id(id),
@@ -671,6 +693,12 @@ export class Transcript {
syntaxStyle: this.theme.syntax,
streaming,
internalBlockMode: "top-level",
+ renderNode: (token, context) => {
+ if (token.type !== "code") return undefined
+ const code = context.defaultRender()
+ if (!(code instanceof CodeRenderable)) return code
+ return this.decorateCodeBlock(code)
+ },
tableOptions: {
style: "columns",
widthMode: "full",