fix(tui): distinguish fenced code blocks
Give retained Markdown code blocks a quiet theme-aware rail and fixed content gap while preserving OpenTUI's in-place streaming renderer. Keep the rail decorative so copied code remains unchanged.
This commit is contained in:
1 parent
ba9f40090f
commit
455533169d
2 files changed
+38
-4
No files matched your search
+10
-4
@@ -2097,6 +2097,7 @@ describe("NanobotTui layout", () => {
|
|||||||
|
|
||||||
expect(recorder.recordedFrames.length).toBeGreaterThan(0)
|
expect(recorder.recordedFrames.length).toBeGreaterThan(0)
|
||||||
expect(recorder.recordedFrames.every(({ frame }) => frame.includes("feat:"))).toBeTrue()
|
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 }) => (
|
expect(recorder.recordedFrames.every(({ frame }) => (
|
||||||
frame.includes("Include the reason in the body.")
|
frame.includes("Include the reason in the body.")
|
||||||
))).toBeTrue()
|
))).toBeTrue()
|
||||||
@@ -2130,12 +2131,17 @@ describe("NanobotTui layout", () => {
|
|||||||
transcript.history([{ role: "assistant", content: response }])
|
transcript.history([{ role: "assistant", content: response }])
|
||||||
await setup.flush()
|
await setup.flush()
|
||||||
|
|
||||||
const code = setup.captureSpans().lines
|
const codeLine = setup.captureSpans().lines.find((line) => (
|
||||||
.flatMap((line) => line.spans)
|
line.spans.some((span) => span.text.includes("feat:"))
|
||||||
.find((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(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 () => {
|
test("renders assistant LaTeX as Unicode text without changing code", async () => {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
BoxRenderable,
|
BoxRenderable,
|
||||||
|
CodeRenderable,
|
||||||
MarkdownRenderable,
|
MarkdownRenderable,
|
||||||
RGBA,
|
RGBA,
|
||||||
ScrollBoxRenderable,
|
ScrollBoxRenderable,
|
||||||
@@ -64,6 +65,7 @@ const ACTIVITY_PREVIEW_LINES = 4
|
|||||||
// additional visible frames. Paint the first token immediately, then coalesce
|
// additional visible frames. Paint the first token immediately, then coalesce
|
||||||
// subsequent deltas to the renderer cadence.
|
// subsequent deltas to the renderer cadence.
|
||||||
const STREAM_FLUSH_MS = 32
|
const STREAM_FLUSH_MS = 32
|
||||||
|
const CODE_RAIL_INDENT = 2
|
||||||
|
|
||||||
export interface UserMessageMedia {
|
export interface UserMessageMedia {
|
||||||
kind?: MediaAttachment["kind"]
|
kind?: MediaAttachment["kind"]
|
||||||
@@ -139,6 +141,7 @@ export class Transcript {
|
|||||||
private navigationTimer: ReturnType<typeof setTimeout> | null = null
|
private navigationTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
private pendingStream = ""
|
private pendingStream = ""
|
||||||
private streamTimer: ReturnType<typeof setTimeout> | null = null
|
private streamTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
|
private codeRailColor: RGBA
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly renderer: CliRenderer,
|
private readonly renderer: CliRenderer,
|
||||||
@@ -147,6 +150,7 @@ export class Transcript {
|
|||||||
private readonly onNavigationChange?: (state: TranscriptNavigation) => void,
|
private readonly onNavigationChange?: (state: TranscriptNavigation) => void,
|
||||||
private readonly workspace = "",
|
private readonly workspace = "",
|
||||||
) {
|
) {
|
||||||
|
this.codeRailColor = RGBA.fromHex(theme.border)
|
||||||
this.root = new ScrollBoxRenderable(renderer, {
|
this.root = new ScrollBoxRenderable(renderer, {
|
||||||
id: "nanobot-tui-transcript",
|
id: "nanobot-tui-transcript",
|
||||||
width: "100%",
|
width: "100%",
|
||||||
@@ -175,6 +179,7 @@ export class Transcript {
|
|||||||
setTheme(theme: TranscriptTheme): void {
|
setTheme(theme: TranscriptTheme): void {
|
||||||
const previousSyntax = this.theme.syntax
|
const previousSyntax = this.theme.syntax
|
||||||
this.theme = theme
|
this.theme = theme
|
||||||
|
this.codeRailColor = RGBA.fromHex(theme.border)
|
||||||
for (const { renderable, tone } of this.styledText) renderable.fg = theme[tone]
|
for (const { renderable, tone } of this.styledText) renderable.fg = theme[tone]
|
||||||
for (const message of this.userMessages) {
|
for (const message of this.userMessages) {
|
||||||
message.renderable.content = this.userMessageContent(
|
message.renderable.content = this.userMessageContent(
|
||||||
@@ -659,6 +664,23 @@ export class Transcript {
|
|||||||
return new StyledText(chunks)
|
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 {
|
private createMarkdown(content: string, streaming: boolean, id = "markdown"): MarkdownRenderable {
|
||||||
const markdown = new MarkdownRenderable(this.renderer, {
|
const markdown = new MarkdownRenderable(this.renderer, {
|
||||||
id: this.id(id),
|
id: this.id(id),
|
||||||
@@ -671,6 +693,12 @@ export class Transcript {
|
|||||||
syntaxStyle: this.theme.syntax,
|
syntaxStyle: this.theme.syntax,
|
||||||
streaming,
|
streaming,
|
||||||
internalBlockMode: "top-level",
|
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: {
|
tableOptions: {
|
||||||
style: "columns",
|
style: "columns",
|
||||||
widthMode: "full",
|
widthMode: "full",
|
||||||
|
|||||||
Reference in new issue
Block a user