File size: 1,938 Bytes
f500658 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import { Box, 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[];
constructor(
text: string,
markdownTheme: MarkdownTheme = getMarkdownTheme(),
outputPad = 1,
markdownTransformers: readonly MarkdownTransformer[] = [],
) {
super();
this.text = text;
this.markdownTheme = markdownTheme;
this.outputPad = outputPad;
this.markdownTransformers = markdownTransformers;
this.rebuild();
}
setOutputPad(padding: number): void {
this.outputPad = padding;
this.rebuild();
}
private rebuild(): void {
this.clear();
const contentBox = new Box(this.outputPad, 1, (content: string) => theme.bg("userMessageBg", content));
contentBox.addChild(
new Markdown(
this.text,
0,
0,
this.markdownTheme,
{
color: (content: string) => theme.fg("userMessageText", content),
},
{
preserveOrderedListMarkers: true,
preserveBackslashEscapes: true,
transform: createMarkdownTransform("user", false, this.markdownTransformers),
},
),
);
this.addChild(contentBox);
}
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;
}
}
|