mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
Codemode and write rows keep their last highlighting in a CodeHighlighter that checks theme and grammar changes itself. Markdown reuses the top-level blocks of its last render, held weakly, and assistant messages reuse their Markdown instances while streaming. Text.setText() keeps its wrapped lines for unchanged text. closes #10971
83 lines
2.2 KiB
TypeScript
83 lines
2.2 KiB
TypeScript
import { Container, Markdown, type MarkdownTheme } from "@earendil-works/pi-tui";
|
|
import type { MarkdownTransformer } from "../../../core/extensions/types.ts";
|
|
import { getMarkdownTheme, theme } from "../theme/theme.ts";
|
|
import { createMarkdownTransform } from "./markdown-transform.ts";
|
|
|
|
const OSC133_ZONE_START = "\x1b]133;A\x07";
|
|
const OSC133_ZONE_END = "\x1b]133;B\x07";
|
|
const OSC133_ZONE_FINAL = "\x1b]133;C\x07";
|
|
|
|
/**
|
|
* Component that renders a user message
|
|
*/
|
|
export class UserMessageComponent extends Container {
|
|
private text: string;
|
|
private markdownTheme: MarkdownTheme;
|
|
private outputPad: number;
|
|
private markdownTransformers: readonly MarkdownTransformer[];
|
|
private messageId?: string;
|
|
private timestamp?: string;
|
|
|
|
constructor(
|
|
text: string,
|
|
markdownTheme: MarkdownTheme = getMarkdownTheme(),
|
|
outputPad = 1,
|
|
markdownTransformers: readonly MarkdownTransformer[] = [],
|
|
messageId?: string,
|
|
timestamp?: string,
|
|
) {
|
|
super();
|
|
this.text = text;
|
|
this.markdownTheme = markdownTheme;
|
|
this.outputPad = outputPad;
|
|
this.markdownTransformers = markdownTransformers;
|
|
this.messageId = messageId;
|
|
this.timestamp = timestamp;
|
|
this.rebuild();
|
|
}
|
|
|
|
setOutputPad(padding: number): void {
|
|
this.outputPad = padding;
|
|
this.rebuild();
|
|
}
|
|
|
|
private rebuild(): void {
|
|
this.clear();
|
|
// The Markdown pads and colors its own background: a Box around it would keep a second full-width copy of every
|
|
// line, with identical output.
|
|
this.addChild(
|
|
new Markdown(
|
|
this.text,
|
|
this.outputPad,
|
|
1,
|
|
this.markdownTheme,
|
|
{
|
|
color: (content: string) => theme.fg("userMessageText", content),
|
|
bgColor: (content: string) => theme.bg("userMessageBg", content),
|
|
},
|
|
{
|
|
preserveOrderedListMarkers: true,
|
|
preserveBackslashEscapes: true,
|
|
transform: createMarkdownTransform(
|
|
"user",
|
|
() => false,
|
|
this.markdownTransformers,
|
|
this.messageId,
|
|
this.timestamp,
|
|
),
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
override render(width: number): string[] {
|
|
const lines = super.render(width);
|
|
if (lines.length === 0) {
|
|
return lines;
|
|
}
|
|
|
|
lines[0] = OSC133_ZONE_START + lines[0];
|
|
lines[lines.length - 1] = OSC133_ZONE_END + OSC133_ZONE_FINAL + lines[lines.length - 1];
|
|
return lines;
|
|
}
|
|
}
|