import { CustomEditor } from "@earendil-works/pi-coding-agent"; import type { KeybindingsManager, Theme } from "@earendil-works/pi-coding-agent"; import { backgroundAnsi, foregroundAnsi, getTerminalColorMode, parseColor, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui"; import type { EditorTheme, TUI } from "@earendil-works/pi-tui"; import { paintEditorLine } from "./editor-paint.ts"; import type { Palette } from "./palettes.ts"; /** Keep Pi's editing, mouse, autocomplete, shortcuts and cursor positioning unchanged. */ export class TrioEditor extends CustomEditor { private getPalette: () => Palette | undefined; private getTheme: () => Theme; constructor( tui: TUI, theme: EditorTheme, keybindings: KeybindingsManager, getPalette: () => Palette | undefined, getTheme: () => Theme, ) { super(tui, theme, keybindings); this.getPalette = getPalette; this.getTheme = getTheme; } render(width: number): string[] { const lines = super.render(width); const palette = this.getPalette(); if (!palette || width < 1) return lines; const mode = getTerminalColorMode(); const style = { foregroundAnsi: foregroundAnsi(this.getTheme().colors.text, mode), backgroundAnsi: backgroundAnsi(parseColor(palette.inputBg), mode), }; return lines.map((line) => { const fitted = visibleWidth(line) > width ? truncateToWidth(line, width, "") : line; const padded = fitted + " ".repeat(Math.max(0, width - visibleWidth(fitted))); return paintEditorLine(padded, style); }); } }