|
| 1 | +# xterm.js → Go Porting Specification |
| 2 | + |
| 3 | +This document is the reference for porting the headless xterm.js terminal emulator to Go. |
| 4 | + |
| 5 | +**Source:** https://github.com/xtermjs/xterm.js (MIT license) |
| 6 | +**Target:** This repository (`github.com/gitpod-io/xterm-go`) |
| 7 | + |
| 8 | + |
| 9 | +## Goal |
| 10 | + |
| 11 | +A pure-Go headless terminal emulator that processes VT/ANSI escape sequences and maintains buffer state. No rendering, no DOM, no browser APIs. Stdlib-only dependencies (no third-party packages). |
| 12 | + |
| 13 | +## Architecture Overview |
| 14 | + |
| 15 | +``` |
| 16 | +Terminal (public API) |
| 17 | + └── coreTerminal (internal orchestration) |
| 18 | + ├── inputHandler (sequence → buffer ops) |
| 19 | + │ └── parser (EscapeSequenceParser) |
| 20 | + │ ├── oscParser |
| 21 | + │ ├── dcsParser |
| 22 | + │ └── apcParser |
| 23 | + ├── bufferService |
| 24 | + │ └── bufferSet (normal + alt) |
| 25 | + │ └── buffer |
| 26 | + │ ├── CircularList[*BufferLine] |
| 27 | + │ └── markers |
| 28 | + ├── coreService (data events) |
| 29 | + ├── optionsService |
| 30 | + ├── charsetService |
| 31 | + ├── unicodeService |
| 32 | + └── oscLinkService |
| 33 | +``` |
| 34 | + |
| 35 | +## Porting Rules |
| 36 | + |
| 37 | +### General |
| 38 | +1. **Port behavior, not syntax.** Translate TypeScript idioms to idiomatic Go. |
| 39 | +2. **No dependency injection framework.** xterm.js uses `InstantiationService` — replace with plain struct composition and constructor injection. |
| 40 | +3. **No `interface{}`.** Use concrete types or generics. |
| 41 | +4. **Unexported by default.** Only export the public Terminal API. Internal types are unexported. |
| 42 | +5. **Single package.** Everything lives in `package xterm` at the repository root. |
| 43 | +6. **Tests required.** Every file `foo.go` must have `foo_test.go`. Use table-driven tests with `cmp.Diff`. |
| 44 | + |
| 45 | +### Type Mapping |
| 46 | + |
| 47 | +| TypeScript | Go | |
| 48 | +|---|---| |
| 49 | +| `interface` | Go `interface` (only when needed for polymorphism) | |
| 50 | +| `class` | `struct` with methods | |
| 51 | +| `enum` (const enum) | `const` block with `iota` or explicit values | |
| 52 | +| `Uint16Array` / `Uint32Array` / `Int32Array` | `[]uint16` / `[]uint32` / `[]int32` | |
| 53 | +| `number` (integer context) | `int32` or `uint32` (match xterm.js bit widths) | |
| 54 | +| `string` | `string` or `[]rune` depending on context | |
| 55 | +| `Emitter<T>` / `IEvent<T>` | `EventEmitter[T]` (callback-based, see below) | |
| 56 | +| `IDisposable` | `Disposable` interface with `Dispose()` | |
| 57 | +| `Promise<T>` | Drop async support — Go port is synchronous | |
| 58 | + |
| 59 | +### Event System |
| 60 | + |
| 61 | +xterm.js uses `Emitter<T>` with `.event` property returning `IEvent<T>`. Port as: |
| 62 | + |
| 63 | +```go |
| 64 | +// EventEmitter is a synchronous event emitter. |
| 65 | +type EventEmitter[T any] struct { |
| 66 | + listeners []func(T) |
| 67 | +} |
| 68 | + |
| 69 | +func (e *EventEmitter[T]) Fire(value T) { ... } |
| 70 | +func (e *EventEmitter[T]) Event(listener func(T)) Disposable { ... } |
| 71 | +``` |
| 72 | + |
| 73 | +### Bit Layout Preservation |
| 74 | + |
| 75 | +The attribute bit layouts MUST match xterm.js exactly. This ensures compatibility if we ever need to exchange buffer state. |
| 76 | + |
| 77 | +**fg (uint32):** |
| 78 | +- bits 0-7: blue (RGB) or palette index |
| 79 | +- bits 8-15: green (RGB) |
| 80 | +- bits 16-23: red (RGB) |
| 81 | +- bits 24-25: color mode (0=default, 1=P16, 2=P256, 3=RGB) |
| 82 | +- bit 26: INVERSE |
| 83 | +- bit 27: BOLD |
| 84 | +- bit 28: UNDERLINE |
| 85 | +- bit 29: BLINK |
| 86 | +- bit 30: INVISIBLE |
| 87 | +- bit 31: STRIKETHROUGH |
| 88 | + |
| 89 | +**bg (uint32):** |
| 90 | +- bits 0-25: same color layout as fg |
| 91 | +- bit 26: ITALIC |
| 92 | +- bit 27: DIM |
| 93 | +- bit 28: HAS_EXTENDED |
| 94 | +- bit 29: PROTECTED |
| 95 | +- bit 30: OVERLINE |
| 96 | + |
| 97 | +**content (uint32):** |
| 98 | +- bits 0-20: codepoint (max 0x10FFFF) |
| 99 | +- bit 21: IS_COMBINED (cell has combined string data) |
| 100 | +- bits 22-23: wcwidth (0-2) |
| 101 | + |
| 102 | +### Parser State Machine |
| 103 | + |
| 104 | +The parser is a table-driven VT500 state machine. The transition table is a `[]uint16` of 4095 entries: |
| 105 | +- Index: `state << 8 | charCode` |
| 106 | +- Value: `action << 8 | nextState` |
| 107 | + |
| 108 | +15 states, 18 actions. Port the `VT500_TRANSITION_TABLE` initialization exactly. |
| 109 | + |
| 110 | +### CircularList |
| 111 | + |
| 112 | +Generic circular buffer used for scrollback: |
| 113 | + |
| 114 | +```go |
| 115 | +type CircularList[T any] struct { |
| 116 | + array []T |
| 117 | + length int |
| 118 | + maxLen int |
| 119 | + startIdx int |
| 120 | + // events for insert/delete/trim |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### BufferLine Cell Storage |
| 125 | + |
| 126 | +Each cell is stored as 3 values in parallel slices: |
| 127 | +- `content []uint32` — codepoint + width + combined flag |
| 128 | +- `fg []uint32` — foreground color + text attributes |
| 129 | +- `bg []uint32` — background color + flags |
| 130 | + |
| 131 | +Combined characters (emoji, accented chars) store their string in a side map. |
| 132 | + |
| 133 | +## File Mapping |
| 134 | + |
| 135 | +| xterm.js Source | Go Target | Phase | |
| 136 | +|---|---|---| |
| 137 | +| `src/common/Types.ts` | `types.go` | 1 | |
| 138 | +| `src/common/buffer/Constants.ts` | `constants.go` | 1 | |
| 139 | +| `src/common/parser/Constants.ts` | `constants.go` | 1 | |
| 140 | +| `src/common/CircularList.ts` | `circularlist.go` | 1 | |
| 141 | +| `src/common/Event.ts` | `event.go` | 1 | |
| 142 | +| `src/common/Lifecycle.ts` | `lifecycle.go` | 1 | |
| 143 | +| `src/common/buffer/AttributeData.ts` | `attributedata.go` | 1 | |
| 144 | +| `src/common/buffer/CellData.ts` | `celldata.go` | 1 | |
| 145 | +| `src/common/parser/EscapeSequenceParser.ts` | `parser.go` | 2 | |
| 146 | +| `src/common/parser/Params.ts` | `parser_params.go` | 2 | |
| 147 | +| `src/common/parser/OscParser.ts` | `parser_osc.go` | 2 | |
| 148 | +| `src/common/parser/DcsParser.ts` | `parser_dcs.go` | 2 | |
| 149 | +| `src/common/parser/ApcParser.ts` | `parser_apc.go` | 2 | |
| 150 | +| `src/common/buffer/BufferLine.ts` | `bufferline.go` | 3 | |
| 151 | +| `src/common/buffer/Buffer.ts` | `buffer.go` | 3 | |
| 152 | +| `src/common/buffer/BufferSet.ts` | `bufferset.go` | 3 | |
| 153 | +| `src/common/buffer/Marker.ts` | `marker.go` | 3 | |
| 154 | +| `src/common/buffer/BufferReflow.ts` | `bufferreflow.go` | 3 | |
| 155 | +| `src/common/services/OptionsService.ts` | `options.go` | 4 | |
| 156 | +| `src/common/services/BufferService.ts` | `bufferservice.go` | 4 | |
| 157 | +| `src/common/services/CoreService.ts` | `coreservice.go` | 4 | |
| 158 | +| `src/common/services/CharsetService.ts` | `charset.go` | 4 | |
| 159 | +| `src/common/services/UnicodeService.ts` | `unicode.go` | 4 | |
| 160 | +| `src/common/services/MouseStateService.ts` | `mousestate.go` | 4 | |
| 161 | +| `src/common/services/OscLinkService.ts` | `osclink.go` | 4 | |
| 162 | +| `src/common/data/Charsets.ts` | `charset.go` | 4 | |
| 163 | +| `src/common/InputHandler.ts` | `inputhandler.go` + `inputhandler_*.go` | 5 | |
| 164 | +| `src/common/input/WriteBuffer.ts` | `writebuffer.go` | 5 | |
| 165 | +| `src/common/input/TextDecoder.ts` | `textdecoder.go` | 5 | |
| 166 | +| `src/headless/Terminal.ts` | `terminal.go` | 6 | |
| 167 | +| `src/common/CoreTerminal.ts` | `terminal.go` | 6 | |
| 168 | + |
| 169 | +## Subagent Instructions |
| 170 | + |
| 171 | +Each subagent works on one phase. The subagent should: |
| 172 | + |
| 173 | +1. Clone the xterm.js repo (or read source files via GitHub raw URLs) |
| 174 | +2. Read the relevant TypeScript source files listed in the phase's Linear issue |
| 175 | +3. Create the Go files at the repository root |
| 176 | +4. Write tests for each file |
| 177 | +5. Run `go test ./...` to verify |
| 178 | +6. Run `gofmt` on all files |
| 179 | +7. Commit and push to a feature branch |
| 180 | +8. Create a PR |
| 181 | + |
| 182 | +### Reading xterm.js source |
| 183 | +Use raw GitHub URLs: |
| 184 | +``` |
| 185 | +https://raw.githubusercontent.com/xtermjs/xterm.js/master/src/common/<path> |
| 186 | +``` |
| 187 | + |
| 188 | +### Key xterm.js files to read for context (all phases) |
| 189 | +- `src/common/Types.ts` — all interfaces |
| 190 | +- `src/common/buffer/Types.ts` — buffer interfaces |
| 191 | +- `src/common/parser/Types.ts` — parser interfaces |
| 192 | +- `src/common/buffer/Constants.ts` — bit layout constants |
| 193 | +- `src/common/parser/Constants.ts` — parser state/action enums |
| 194 | + |
| 195 | +## Phase Dependencies |
| 196 | + |
| 197 | +``` |
| 198 | +Phase 1 (types/constants) ──┬──→ Phase 2 (parser) |
| 199 | + ├──→ Phase 3 (buffer) |
| 200 | + └──→ Phase 4 (services) ──→ Phase 5 (input handler) ──→ Phase 6 (terminal) |
| 201 | + ↑ ↑ |
| 202 | + Phase 3 ───────────────────────┘ |
| 203 | + Phase 2 ───────────────────────┘ |
| 204 | +``` |
| 205 | + |
| 206 | +Phases 2 and 3 can run in parallel after Phase 1. |
| 207 | +Phase 4 depends on Phase 1 and Phase 3. |
| 208 | +Phase 5 depends on Phases 2, 3, and 4. |
| 209 | +Phase 6 depends on all previous phases. |
0 commit comments