Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/activity/view.ts

Raw
import type { IBufferCell, Terminal } from "@xterm/headless";

export interface TerminalView {
	lines: string[];
	truncated: boolean;
	cursor?: {
		x: number;
		y: number;
		text: string;
		width: number;
		style: string;
		inverse: boolean;
	};
}

const RESET = "\x1b[0m";
const MAX_VIEW_BYTES = 1024 * 1024;
const MAX_CELL_CHARS = 128;
const CELL_CONTROLS = /[\x00-\x1f\x7f-\x9f]/g;

function colorStyle(
	channel: 38 | 48,
	rgb: boolean,
	palette: boolean,
	color: number,
): string {
	if (rgb)
		return `;${channel};2;${(color >>> 16) & 255};${(color >>> 8) & 255};${color & 255}`;
	return palette ? `;${channel};5;${color}` : "";
}

type StyleCache = {
	flags: number;
	fgMode: number;
	bgMode: number;
	fg: number;
	bg: number;
	style: string;
};

function cellStyle(cell: IBufferCell, cache: StyleCache): string {
	const flags =
		Number(Boolean(cell.isBold())) |
		(Number(Boolean(cell.isDim())) << 1) |
		(Number(Boolean(cell.isItalic())) << 2) |
		(Number(Boolean(cell.isUnderline())) << 3) |
		(Number(Boolean(cell.isInverse())) << 4) |
		(Number(Boolean(cell.isInvisible())) << 5) |
		(Number(Boolean(cell.isStrikethrough())) << 6);
	const fgMode = cell.getFgColorMode();
	const bgMode = cell.getBgColorMode();
	const fg = cell.getFgColor();
	const bg = cell.getBgColor();
	// Uniform styled regions dominate human views; reuse SGR without private xterm fields.
	if (
		cache.flags === flags &&
		cache.fgMode === fgMode &&
		cache.bgMode === bgMode &&
		cache.fg === fg &&
		cache.bg === bg
	)
		return cache.style;
	let style = "\x1b[0";
	if (flags & 1) style += ";1";
	if (flags & 2) style += ";2";
	if (flags & 4) style += ";3";
	if (flags & 8) style += ";4";
	if (flags & 16) style += ";7";
	if (flags & 32) style += ";8";
	if (flags & 64) style += ";9";
	cache.flags = flags;
	cache.fgMode = fgMode;
	cache.bgMode = bgMode;
	cache.fg = fg;
	cache.bg = bg;
	cache.style =
		style +
		colorStyle(38, cell.isFgRGB(), cell.isFgPalette(), fg) +
		colorStyle(48, cell.isBgRGB(), cell.isBgPalette(), bg) +
		"m";
	return cache.style;
}

/** Human-only projection: generated SGR, never replayed child terminal controls. */
export function terminalView(
	terminal: Terminal,
	cursorVisible: boolean,
): TerminalView {
	const buffer = terminal.buffer.active;
	const cell = buffer.getNullCell();
	const view: TerminalView = { lines: [], truncated: false };
	const styleCache: StyleCache = {
		flags: -1,
		fgMode: -1,
		bgMode: -1,
		fg: -1,
		bg: -1,
		style: "",
	};
	const cursorX = Math.min(buffer.cursorX, terminal.cols - 1);
	const contentBudget = MAX_VIEW_BYTES - terminal.rows * RESET.length;
	let bytes = 0;
	for (let y = 0; y < terminal.rows; y++) {
		const source = buffer.getLine(buffer.baseY + y);
		let line = "";
		let previousStyle = "";
		for (let x = 0; source && x < terminal.cols; x++) {
			if (!source.getCell(x, cell)) continue;
			const width = cell.getWidth();
			if (width === 0) continue;
			const chars = cell.getChars();
			if (chars.length > MAX_CELL_CHARS) view.truncated = true;
			const text =
				chars.slice(0, MAX_CELL_CHARS).replace(CELL_CONTROLS, "") || " ";
			const style = cell.isAttributeDefault()
				? RESET
				: cellStyle(cell, styleCache);
			const segment = (style === previousStyle ? "" : style) + text;
			bytes += Buffer.byteLength(segment);
			if (bytes > contentBudget) {
				view.truncated = true;
				break;
			}
			line += segment;
			previousStyle = style;
			if (
				cursorVisible &&
				y === buffer.cursorY &&
				x <= cursorX &&
				x + width > cursorX
			) {
				view.cursor = {
					x,
					y,
					text,
					width,
					style,
					inverse: Boolean(cell.isInverse()),
				};
			}
		}
		view.lines.push(line + RESET);
		if (bytes > contentBudget) break;
	}
	if (view.truncated) view.cursor = undefined;
	return view;
}