Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ghostty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ export class GhosttyTerminal {

write(data: string | Uint8Array): void {
const bytes = typeof data === 'string' ? new TextEncoder().encode(data) : data;
if (bytes.length === 0) return; // a zero-length allocation makes the copy below throw
const ptr = this.exports.ghostty_wasm_alloc_u8_array(bytes.length);
new Uint8Array(this.memory.buffer).set(bytes, ptr);
this.exports.ghostty_terminal_write(this.handle, ptr, bytes.length);
Expand Down
34 changes: 34 additions & 0 deletions lib/terminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,40 @@ describe('Terminal', () => {
term.dispose();
});

test('write() with an empty string is a no-op', async () => {
const term = await createIsolatedTerminal();
term.open(container!);

expect(() => term.write('')).not.toThrow();
term.write('after');
expect(term.buffer.active.getLine(0)?.translateToString(true)).toBe('after');

term.dispose();
});

test('write() with an empty Uint8Array is a no-op', async () => {
const term = await createIsolatedTerminal();
term.open(container!);

expect(() => term.write(new Uint8Array(0))).not.toThrow();

term.dispose();
});

test('write() with empty data still calls the callback', async () => {
const term = await createIsolatedTerminal();
term.open(container!);

let called = false;
term.write('', () => {
called = true;
});
await new Promise((r) => setTimeout(r, 50));
expect(called).toBe(true);

term.dispose();
});

test('writeln() adds newline', async () => {
const term = await createIsolatedTerminal();
term.open(container!);
Expand Down
7 changes: 7 additions & 0 deletions lib/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,13 @@ export class Terminal implements ITerminalCore {
* Internal write implementation (extracted from write())
*/
private writeInternal(data: string | Uint8Array, callback?: () => void): void {
// Nothing to write: the WASM side throws a RangeError on an empty buffer. Stay a no-op (as in
// xterm.js), but keep the callback contract.
if (data.length === 0) {
if (callback) requestAnimationFrame(callback);
return;
}

// Note: We intentionally do NOT clear selection on write - most modern terminals
// preserve selection when new data arrives. Selection is cleared by user actions
// like clicking or typing, not by incoming data.
Expand Down