Skip to content

Commit d9d43c3

Browse files
committed
Remove unused SgrParser class
- Removed SgrParser class from lib/ghostty.ts (163 lines) - Removed createSgrParser() method from Ghostty class - Cleaned up unused imports (SgrAttribute, SgrAttributeTag, RGBColor) - Updated lib/index.ts exports to remove SgrParser - lib/ghostty.ts: 708 → 545 lines (-163 lines, -23%) All tests passing ✅
1 parent aa59429 commit d9d43c3

File tree

2 files changed

+1
-176
lines changed

2 files changed

+1
-176
lines changed

lib/ghostty.ts

Lines changed: 0 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import {
1414
type KeyEvent,
1515
type KittyKeyFlags,
1616
type RGB,
17-
type RGBColor,
18-
type SgrAttribute,
19-
SgrAttributeTag,
2017
type TerminalHandle,
2118
} from './types';
2219

@@ -50,13 +47,6 @@ export class Ghostty {
5047
return this.memory.buffer;
5148
}
5249

53-
/**
54-
* Create an SGR parser instance
55-
*/
56-
createSgrParser(): SgrParser {
57-
return new SgrParser(this.exports);
58-
}
59-
6050
/**
6151
* Create a key encoder instance
6252
*/
@@ -109,169 +99,6 @@ export class Ghostty {
10999
}
110100
}
111101

112-
/**
113-
* SGR (Select Graphic Rendition) Parser
114-
* Parses ANSI color/style sequences like "1;31" (bold red)
115-
*/
116-
export class SgrParser {
117-
private exports: GhosttyWasmExports;
118-
private parser: number = 0;
119-
120-
constructor(exports: GhosttyWasmExports) {
121-
this.exports = exports;
122-
123-
// Allocate parser
124-
const parserPtrPtr = this.exports.ghostty_wasm_alloc_opaque();
125-
const result = this.exports.ghostty_sgr_new(0, parserPtrPtr);
126-
if (result !== 0) {
127-
throw new Error(`Failed to create SGR parser: ${result}`);
128-
}
129-
130-
// Read the parser pointer
131-
const view = new DataView(this.exports.memory.buffer);
132-
this.parser = view.getUint32(parserPtrPtr, true);
133-
this.exports.ghostty_wasm_free_opaque(parserPtrPtr);
134-
}
135-
136-
/**
137-
* Parse SGR parameters (e.g., [1, 31] for bold red)
138-
* @returns Iterator of SGR attributes
139-
*/
140-
*parse(params: number[]): Generator<SgrAttribute> {
141-
if (params.length === 0) return;
142-
143-
// Allocate parameter array
144-
const paramsPtr = this.exports.ghostty_wasm_alloc_u16_array(params.length);
145-
const view = new DataView(this.exports.memory.buffer);
146-
147-
// Write parameters (uint16_t array)
148-
for (let i = 0; i < params.length; i++) {
149-
view.setUint16(paramsPtr + i * 2, params[i], true);
150-
}
151-
152-
// Set params in parser (null for subs, we don't use subparams yet)
153-
const result = this.exports.ghostty_sgr_set_params(
154-
this.parser,
155-
paramsPtr,
156-
0, // subsPtr - null
157-
params.length
158-
);
159-
160-
this.exports.ghostty_wasm_free_u16_array(paramsPtr, params.length);
161-
162-
if (result !== 0) {
163-
throw new Error(`Failed to set SGR params: ${result}`);
164-
}
165-
166-
// Iterate through attributes
167-
const attrPtr = this.exports.ghostty_wasm_alloc_sgr_attribute();
168-
169-
try {
170-
while (this.exports.ghostty_sgr_next(this.parser, attrPtr)) {
171-
const attr = this.readAttribute(attrPtr);
172-
if (attr) yield attr;
173-
}
174-
} finally {
175-
this.exports.ghostty_wasm_free_sgr_attribute(attrPtr);
176-
}
177-
}
178-
179-
private readAttribute(attrPtr: number): SgrAttribute | null {
180-
const tag = this.exports.ghostty_sgr_attribute_tag(attrPtr);
181-
const view = new DataView(this.exports.memory.buffer);
182-
183-
switch (tag) {
184-
case SgrAttributeTag.BOLD:
185-
return { tag: SgrAttributeTag.BOLD };
186-
case SgrAttributeTag.RESET_BOLD:
187-
return { tag: SgrAttributeTag.RESET_BOLD };
188-
case SgrAttributeTag.ITALIC:
189-
return { tag: SgrAttributeTag.ITALIC };
190-
case SgrAttributeTag.RESET_ITALIC:
191-
return { tag: SgrAttributeTag.RESET_ITALIC };
192-
case SgrAttributeTag.FAINT:
193-
return { tag: SgrAttributeTag.FAINT };
194-
case SgrAttributeTag.RESET_FAINT:
195-
return { tag: SgrAttributeTag.RESET_FAINT };
196-
case SgrAttributeTag.UNDERLINE:
197-
return { tag: SgrAttributeTag.UNDERLINE };
198-
case SgrAttributeTag.RESET_UNDERLINE:
199-
return { tag: SgrAttributeTag.RESET_UNDERLINE };
200-
case SgrAttributeTag.BLINK:
201-
return { tag: SgrAttributeTag.BLINK };
202-
case SgrAttributeTag.RESET_BLINK:
203-
return { tag: SgrAttributeTag.RESET_BLINK };
204-
case SgrAttributeTag.INVERSE:
205-
return { tag: SgrAttributeTag.INVERSE };
206-
case SgrAttributeTag.RESET_INVERSE:
207-
return { tag: SgrAttributeTag.RESET_INVERSE };
208-
case SgrAttributeTag.INVISIBLE:
209-
return { tag: SgrAttributeTag.INVISIBLE };
210-
case SgrAttributeTag.RESET_INVISIBLE:
211-
return { tag: SgrAttributeTag.RESET_INVISIBLE };
212-
case SgrAttributeTag.STRIKETHROUGH:
213-
return { tag: SgrAttributeTag.STRIKETHROUGH };
214-
case SgrAttributeTag.RESET_STRIKETHROUGH:
215-
return { tag: SgrAttributeTag.RESET_STRIKETHROUGH };
216-
217-
case SgrAttributeTag.FG_8:
218-
case SgrAttributeTag.FG_16:
219-
case SgrAttributeTag.FG_256: {
220-
// Color value is stored after the tag (uint8_t)
221-
const color = view.getUint8(attrPtr + 4);
222-
return { tag, color };
223-
}
224-
225-
case SgrAttributeTag.FG_RGB:
226-
case SgrAttributeTag.BG_RGB:
227-
case SgrAttributeTag.UNDERLINE_COLOR_RGB: {
228-
// RGB color stored after the tag (3 bytes: r, g, b)
229-
const r = view.getUint8(attrPtr + 4);
230-
const g = view.getUint8(attrPtr + 5);
231-
const b = view.getUint8(attrPtr + 6);
232-
return { tag, color: { r, g, b } };
233-
}
234-
235-
case SgrAttributeTag.FG_DEFAULT:
236-
return { tag: SgrAttributeTag.FG_DEFAULT };
237-
238-
case SgrAttributeTag.BG_8:
239-
case SgrAttributeTag.BG_16:
240-
case SgrAttributeTag.BG_256: {
241-
const color = view.getUint8(attrPtr + 4);
242-
return { tag, color };
243-
}
244-
245-
case SgrAttributeTag.BG_DEFAULT:
246-
return { tag: SgrAttributeTag.BG_DEFAULT };
247-
248-
case SgrAttributeTag.UNDERLINE_COLOR_DEFAULT:
249-
return { tag: SgrAttributeTag.UNDERLINE_COLOR_DEFAULT };
250-
251-
default:
252-
// Unknown or unhandled
253-
return null;
254-
}
255-
}
256-
257-
/**
258-
* Reset parser state
259-
*/
260-
reset(): void {
261-
this.exports.ghostty_sgr_reset(this.parser);
262-
}
263-
264-
/**
265-
* Free parser resources
266-
*/
267-
dispose(): void {
268-
if (this.parser) {
269-
this.exports.ghostty_sgr_free(this.parser);
270-
this.parser = 0;
271-
}
272-
}
273-
}
274-
275102
/**
276103
* Key Encoder
277104
* Converts keyboard events into terminal escape sequences

lib/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ export type {
1818
} from './interfaces';
1919

2020
// Ghostty WASM components (for advanced usage)
21-
export { Ghostty, GhosttyTerminal, SgrParser, KeyEncoder, CellFlags } from './ghostty';
21+
export { Ghostty, GhosttyTerminal, KeyEncoder, CellFlags, KeyEncoderOption } from './ghostty';
2222
export type {
23-
SgrAttribute,
24-
SgrAttributeTag,
2523
KeyEvent,
2624
KeyAction,
2725
Key,

0 commit comments

Comments
 (0)