import { charWidth } from './liveViewport.js'; /** Visual cropping only: the editor retains the complete text and logical cursor. */ export function inputViewport(lines: string[], cursorRow: number, cursorCol: number, width: number, height: number) { width = Number.isFinite(width) ? Math.max(2, Math.floor(width)) : 2; height = Number.isFinite(height) ? Math.max(1, Math.floor(height)) : 1; const start = Math.max(0, Math.min(cursorRow - height + 1, lines.length - height)); return lines.slice(start, start + height).map((line, index) => { const row = start + index; // One display character per source code point keeps logical cursor offsets intact. // Tabs use a space and controls a visible '?' only in the viewport, never in the draft. const points = Array.from(line, char => char === '\t' ? ' ' : /[\x00-\x1f\x7f-\x9f]/.test(char) ? '?' : char); const safeCol = Number.isFinite(cursorCol) ? Math.max(0, Math.min(points.length, Math.floor(cursorCol))) : 0; let from = row === cursorRow ? safeCol : 0; let used = 1; while (from > 0 && used + charWidth(points[from - 1].codePointAt(0)!) < width) { used += charWidth(points[--from].codePointAt(0)!); } let to = from; used = 0; while (to < points.length && used + charWidth(points[to].codePointAt(0)!) <= width - 1) { used += charWidth(points[to++].codePointAt(0)!); } return { row, line: points.slice(from, to).join(''), col: safeCol - from }; }); }