Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(glyphgrid): packed cell instance format

enes73ecb3f91d84
diff --git a/src/renderer/glyphgrid/cells.test.ts b/src/renderer/glyphgrid/cells.test.ts
new file mode 100644
index 000000000..236c03a1c
--- /dev/null
+++ b/src/renderer/glyphgrid/cells.test.ts
@@ -0,0 +1,42 @@
+import { describe, expect, it } from 'vitest'
+import {
+ CELL_STRIDE,
+ FLAG_BOLD,
+ FLAG_SELECTED,
+ packColor,
+ readCell,
+ unpackColor,
+ writeCell
+} from './cells'
+
+describe('glyphgrid cells', () => {
+ it('packColor/unpackColor round-trip all channels', () => {
+ const c = packColor(0x12, 0x34, 0x56, 0xff)
+ expect(unpackColor(c)).toEqual({ r: 0x12, g: 0x34, b: 0x56, a: 0xff })
+ })
+
+ it('writeCell/readCell round-trip through a shared buffer', () => {
+ const buf = new Uint32Array(3 * CELL_STRIDE)
+ writeCell(buf, 2, 4242, packColor(1, 2, 3, 255), packColor(9, 8, 7, 255), FLAG_BOLD | FLAG_SELECTED)
+ const cell = readCell(buf, 2)
+ expect(cell.glyph).toBe(4242)
+ expect(cell.flags).toBe(FLAG_BOLD | FLAG_SELECTED)
+ expect(unpackColor(cell.fg)).toEqual({ r: 1, g: 2, b: 3, a: 255 })
+ // Neighboring cells untouched:
+ expect(readCell(buf, 1)).toEqual({ glyph: 0, fg: 0, bg: 0, flags: 0 })
+ })
+
+ it('a packed color equals the lane it was written to, even with alpha >= 128', () => {
+ // `0xff << 24` is negative in JS, so an unguarded packColor returns a signed int while the
+ // Uint32Array lane reads back unsigned — the two would never compare equal, and a
+ // dirty-check comparing a fresh packColor() against readCell() would fire forever.
+ const opaque = packColor(1, 2, 3, 255)
+ expect(opaque).toBeGreaterThan(0)
+
+ const buf = new Uint32Array(2 * CELL_STRIDE)
+ writeCell(buf, 0, 65, opaque, packColor(9, 8, 7, 200), FLAG_BOLD)
+ const cell = readCell(buf, 0)
+ expect(cell.fg).toBe(opaque)
+ expect(cell.bg).toBe(packColor(9, 8, 7, 200))
+ })
+})
diff --git a/src/renderer/glyphgrid/cells.ts b/src/renderer/glyphgrid/cells.ts
new file mode 100644
index 000000000..93633afcd
--- /dev/null
+++ b/src/renderer/glyphgrid/cells.ts
@@ -0,0 +1,47 @@
+/** One cell = CELL_STRIDE uint32 lanes: [glyph slot, fg RGBA8, bg RGBA8, flags]. This exact
+ * layout is what gl-webgl2.ts binds as instanced vertex attributes — change both together. */
+export const CELL_STRIDE = 4
+
+export const FLAG_BOLD = 1
+export const FLAG_ITALIC = 2
+export const FLAG_UNDERLINE = 4
+export const FLAG_CURSOR = 8
+export const FLAG_WIDE = 16
+export const FLAG_SELECTED = 32
+
+/** RGBA8 packed little-endian style: r in the low byte (matches UNSIGNED_BYTE attr upload).
+ * The trailing `>>> 0` is load-bearing, not cosmetic: `0xff << 24` is NEGATIVE in JS, so
+ * without it every color with alpha >= 128 packs to a signed int while readCell() returns the
+ * buffer's unsigned lane — `readCell(buf, i).fg !== packColor(...)` for the very same color,
+ * which silently defeats any dirty-check written the obvious way. Bytes are identical either
+ * way (Uint32Array assignment does ToUint32); only the JS sign changes. */
+export function packColor(r: number, g: number, b: number, a: number): number {
+ return (((a & 0xff) << 24) | ((b & 0xff) << 16) | ((g & 0xff) << 8) | (r & 0xff)) >>> 0
+}
+
+export function unpackColor(c: number): { r: number; g: number; b: number; a: number } {
+ return { r: c & 0xff, g: (c >>> 8) & 0xff, b: (c >>> 16) & 0xff, a: (c >>> 24) & 0xff }
+}
+
+export function writeCell(
+ buf: Uint32Array,
+ cellIndex: number,
+ glyph: number,
+ fg: number,
+ bg: number,
+ flags: number
+): void {
+ const o = cellIndex * CELL_STRIDE
+ buf[o] = glyph >>> 0
+ buf[o + 1] = fg >>> 0
+ buf[o + 2] = bg >>> 0
+ buf[o + 3] = flags >>> 0
+}
+
+export function readCell(
+ buf: Uint32Array,
+ cellIndex: number
+): { glyph: number; fg: number; bg: number; flags: number } {
+ const o = cellIndex * CELL_STRIDE
+ return { glyph: buf[o], fg: buf[o + 1], bg: buf[o + 2], flags: buf[o + 3] }
+}