From 2ce14e5b6d87a5c8ecbc658921176fb50c987f75 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 21 Aug 2026 21:22:40 -0400 Subject: [PATCH] feat: update() no-op on same dimensions, throw on invalid --- term.ts | 19 ++++++++++++++++--- test/term.test.ts | 12 ++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/term.ts b/term.ts index ec78594..de84d4b 100644 --- a/term.ts +++ b/term.ts @@ -230,12 +230,25 @@ export async function createTerm(options: TermOptions): Promise { return { output, events, info, errors, animating }; }, update(options: UpdateOptions): void { + let w: number; + let h: number; if ("events" in options) { return; + } else { + w = options.width; + h = options.height; + } + if ( + !Number.isInteger(w) || !Number.isInteger(h) || w <= 0 || h <= 0 + ) { + throw new RangeError(`invalid terminal dimensions ${w}x${h}`); + } + if (w === width && h === height) { + return; } - width = options.width; - height = options.height; - native.update(width, height); + width = w; + height = h; + native.update(w, h); }, }; } diff --git a/test/term.test.ts b/test/term.test.ts index c3f2671..601e2da 100644 --- a/test/term.test.ts +++ b/test/term.test.ts @@ -358,6 +358,18 @@ describe("term", () => { expect(out.length).toBeGreaterThan(0); }); + it("is a no-op when dimensions are unchanged", () => { + term.render(frame); + term.update({ width: 40, height: 10 }); + expect(term.render(frame).output.length).toBe(0); + }); + + it("throws on non-positive or non-integer dimensions", () => { + expect(() => term.update({ width: 0, height: 10 })).toThrow(RangeError); + expect(() => term.update({ width: 40, height: -1 })).toThrow(RangeError); + expect(() => term.update({ width: 40.5, height: 10 })).toThrow(RangeError); + }); + it("resizes in place and lays out at the new dimensions", () => { term.render(frame); term.update({ width: 12, height: 4 }); -- 2.51.2