pi-mono/apps/p2/plugins/terminals/terminal-input.ts
Mario Zechner 05dc8d239d feat: add apps/p2 (imported from pi-private at 7dadff697)
p2 (Pi Mobile on Chord v2) moves from pi-private into pi as one commit. Root scripts that loop all workspaces
use --if-present; npm test, ./test.sh and the release script cover packages/* only (p2: cd apps/p2 && npm test);
sync-versions also updates apps' workspace pins.
2026-11-03 13:39:06 +00:00

15 lines
809 B
TypeScript

/**
* What typing into the terminal's textarea did, as a terminal sees it: `deleted` characters before the cursor go
* (Backspaces), then `inserted` is typed. Everything from the first changed character is retyped, so a correction of
* an earlier word works without moving the cursor. Characters are code points, as a line editor deletes them; a
* newline is Enter (CR).
*
* Example: Gboard corrects "teh" to "the ": deleted 2, inserted "he ".
*/
export function typedChange(before: string, after: string): { deleted: number; inserted: string } {
const old = [...before];
const next = [...after];
let same = 0;
while (same < old.length && same < next.length && old[same] === next[same]) same++;
return { deleted: old.length - same, inserted: next.slice(same).join("").replace(/\r?\n/g, "\r") };
}