Files
oh-my-pi/packages/tui/src/kill-ring.ts
T
can1357 dcc0dc62cc fix(tui): made editor cursor math grapheme-aware and markdown nesting structural
vertical movement walks graphemes and snaps to cluster boundaries (no more surrogate splits/wide-glyph drift); wrap-trimmed whitespace keeps a cursor home; kill ops extend over atomic paste markers; undo capped+coalesced, kill ring capped, wrap layout cached, pastes batched; nested-list detection tags structurally instead of sniffing chalk cyan; ordered lists hang by actual bullet width.
2026-06-10 01:28:23 +02:00

52 lines
1.4 KiB
TypeScript

/**
* Ring buffer for Emacs-style kill/yank operations.
*
* Tracks killed (deleted) text entries. Consecutive kills can accumulate
* into a single entry. Supports yank (paste most recent) and yank-pop
* (cycle through older entries).
*/
const MAX_ENTRIES = 60;
export class KillRing {
#ring: string[] = [];
/**
* Add text to the kill ring.
*
* @param text - The killed text to add
* @param opts - Push options
* @param opts.prepend - If accumulating, prepend (backward deletion) or append (forward deletion)
* @param opts.accumulate - Merge with the most recent entry instead of creating a new one
*/
push(text: string, opts: { prepend: boolean; accumulate?: boolean }): void {
if (!text) return;
if (opts.accumulate && this.#ring.length > 0) {
const last = this.#ring.pop()!;
this.#ring.push(opts.prepend ? text + last : last + text);
} else {
this.#ring.push(text);
if (this.#ring.length > MAX_ENTRIES) {
this.#ring.shift();
}
}
}
/** Get most recent entry without modifying the ring. */
peek(): string | undefined {
return this.#ring.length > 0 ? this.#ring[this.#ring.length - 1] : undefined;
}
/** Move last entry to front (for yank-pop cycling). */
rotate(): void {
if (this.#ring.length > 1) {
const last = this.#ring.pop()!;
this.#ring.unshift(last);
}
}
get length(): number {
return this.#ring.length;
}
}