@ebowwa/coder 0.7.64 → 0.7.66
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +36233 -32
- package/dist/interfaces/ui/terminal/cli/index.js +34318 -158
- package/dist/interfaces/ui/terminal/native/README.md +53 -0
- package/dist/interfaces/ui/terminal/native/claude_code_native.darwin-x64.node +0 -0
- package/dist/interfaces/ui/terminal/native/claude_code_native.dylib +0 -0
- package/dist/interfaces/ui/terminal/native/index.d.ts +0 -0
- package/dist/interfaces/ui/terminal/native/index.darwin-arm64.node +0 -0
- package/dist/interfaces/ui/terminal/native/index.js +43 -0
- package/dist/interfaces/ui/terminal/native/index.node +0 -0
- package/dist/interfaces/ui/terminal/native/package.json +34 -0
- package/dist/native/README.md +53 -0
- package/dist/native/claude_code_native.darwin-x64.node +0 -0
- package/dist/native/claude_code_native.dylib +0 -0
- package/dist/native/index.d.ts +0 -480
- package/dist/native/index.darwin-arm64.node +0 -0
- package/dist/native/index.js +43 -1625
- package/dist/native/index.node +0 -0
- package/dist/native/package.json +34 -0
- package/native/index.darwin-arm64.node +0 -0
- package/native/index.js +33 -19
- package/package.json +3 -2
- package/packages/src/core/agent-loop/__tests__/compaction.test.ts +17 -14
- package/packages/src/core/agent-loop/compaction.ts +6 -2
- package/packages/src/core/agent-loop/index.ts +2 -0
- package/packages/src/core/agent-loop/loop-state.ts +1 -1
- package/packages/src/core/agent-loop/turn-executor.ts +4 -0
- package/packages/src/core/agent-loop/types.ts +4 -0
- package/packages/src/core/api-client-impl.ts +377 -176
- package/packages/src/core/cognitive-security/hooks.ts +2 -1
- package/packages/src/core/config/todo +7 -0
- package/packages/src/core/context/__tests__/integration.test.ts +334 -0
- package/packages/src/core/context/compaction.ts +170 -0
- package/packages/src/core/context/constants.ts +58 -0
- package/packages/src/core/context/extraction.ts +85 -0
- package/packages/src/core/context/index.ts +66 -0
- package/packages/src/core/context/summarization.ts +251 -0
- package/packages/src/core/context/token-estimation.ts +98 -0
- package/packages/src/core/context/types.ts +59 -0
- package/packages/src/core/models.ts +81 -4
- package/packages/src/core/normalizers/todo +5 -1
- package/packages/src/core/providers/README.md +230 -0
- package/packages/src/core/providers/__tests__/providers.test.ts +135 -0
- package/packages/src/core/providers/index.ts +419 -0
- package/packages/src/core/providers/types.ts +132 -0
- package/packages/src/core/retry.ts +10 -0
- package/packages/src/ecosystem/tools/index.ts +174 -0
- package/packages/src/index.ts +23 -2
- package/packages/src/interfaces/ui/index.ts +17 -20
- package/packages/src/interfaces/ui/spinner.ts +2 -2
- package/packages/src/interfaces/ui/terminal/bridge/index.ts +370 -0
- package/packages/src/interfaces/ui/terminal/bridge/ipc.ts +829 -0
- package/packages/src/interfaces/ui/terminal/bridge/screen-export.ts +968 -0
- package/packages/src/interfaces/ui/terminal/bridge/types.ts +226 -0
- package/packages/src/interfaces/ui/terminal/bridge/useBridge.ts +210 -0
- package/packages/src/interfaces/ui/terminal/cli/bootstrap.ts +132 -0
- package/packages/src/interfaces/ui/terminal/cli/index.ts +200 -13
- package/packages/src/interfaces/ui/terminal/cli/interactive/index.ts +110 -0
- package/packages/src/interfaces/ui/terminal/cli/interactive/input-handler.ts +402 -0
- package/packages/src/interfaces/ui/terminal/cli/interactive/interactive-runner.ts +820 -0
- package/packages/src/interfaces/ui/terminal/cli/interactive/message-store.ts +299 -0
- package/packages/src/interfaces/ui/terminal/cli/interactive/types.ts +274 -0
- package/packages/src/interfaces/ui/terminal/shared/index.ts +13 -0
- package/packages/src/interfaces/ui/terminal/shared/query.ts +9 -3
- package/packages/src/interfaces/ui/terminal/shared/setup.ts +5 -1
- package/packages/src/interfaces/ui/terminal/shared/spinner-frames.ts +73 -0
- package/packages/src/interfaces/ui/terminal/shared/status-line.ts +10 -2
- package/packages/src/native/index.ts +404 -27
- package/packages/src/native/tui_v2_types.ts +39 -0
- package/packages/src/teammates/coordination.test.ts +279 -0
- package/packages/src/teammates/coordination.ts +646 -0
- package/packages/src/teammates/index.ts +95 -25
- package/packages/src/teammates/integration.test.ts +272 -0
- package/packages/src/teammates/runner.test.ts +235 -0
- package/packages/src/teammates/runner.ts +750 -0
- package/packages/src/teammates/schemas.ts +673 -0
- package/packages/src/types/index.ts +1 -0
- package/packages/src/core/context-compaction.ts +0 -578
- package/packages/src/interfaces/ui/Screenshot 2026-03-02 at 9.23.10/342/200/257PM.png +0 -0
- package/packages/src/interfaces/ui/Screenshot 2026-03-03 at 10.55.11/342/200/257AM.png +0 -0
- package/packages/src/interfaces/ui/terminal/tui/HelpPanel.tsx +0 -262
- package/packages/src/interfaces/ui/terminal/tui/InputContext.tsx +0 -232
- package/packages/src/interfaces/ui/terminal/tui/InputField.tsx +0 -62
- package/packages/src/interfaces/ui/terminal/tui/InteractiveTUI.tsx +0 -537
- package/packages/src/interfaces/ui/terminal/tui/MessageArea.tsx +0 -107
- package/packages/src/interfaces/ui/terminal/tui/MessageStore.tsx +0 -240
- package/packages/src/interfaces/ui/terminal/tui/StatusBar.tsx +0 -54
- package/packages/src/interfaces/ui/terminal/tui/commands.ts +0 -438
- package/packages/src/interfaces/ui/terminal/tui/components/InteractiveElements.tsx +0 -584
- package/packages/src/interfaces/ui/terminal/tui/components/MultilineInput.tsx +0 -614
- package/packages/src/interfaces/ui/terminal/tui/components/PaneManager.tsx +0 -333
- package/packages/src/interfaces/ui/terminal/tui/components/Sidebar.tsx +0 -604
- package/packages/src/interfaces/ui/terminal/tui/components/index.ts +0 -118
- package/packages/src/interfaces/ui/terminal/tui/console.ts +0 -49
- package/packages/src/interfaces/ui/terminal/tui/index.ts +0 -90
- package/packages/src/interfaces/ui/terminal/tui/run.tsx +0 -42
- package/packages/src/interfaces/ui/terminal/tui/spinner.ts +0 -69
- package/packages/src/interfaces/ui/terminal/tui/tui-app.tsx +0 -390
- package/packages/src/interfaces/ui/terminal/tui/tui-footer.ts +0 -422
- package/packages/src/interfaces/ui/terminal/tui/types.ts +0 -186
- package/packages/src/interfaces/ui/terminal/tui/useInputHandler.ts +0 -104
- package/packages/src/interfaces/ui/terminal/tui/useNativeInput.ts +0 -239
|
@@ -0,0 +1,968 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Screen Export API for TUI Bridge
|
|
3
|
+
* Provides screen capture, ANSI parsing, and export utilities
|
|
4
|
+
*
|
|
5
|
+
* @module screen-export
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
ScreenCell,
|
|
10
|
+
ScreenBuffer,
|
|
11
|
+
ParsedScreen,
|
|
12
|
+
UIElement,
|
|
13
|
+
UIElementType,
|
|
14
|
+
} from "./types.js";
|
|
15
|
+
|
|
16
|
+
// ============================================
|
|
17
|
+
// HELPER FUNCTIONS
|
|
18
|
+
// ============================================
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Safely get a cell from the screen buffer
|
|
22
|
+
*/
|
|
23
|
+
function getCell(buffer: ScreenBuffer, x: number, y: number): ScreenCell {
|
|
24
|
+
const row = buffer.cells[y];
|
|
25
|
+
if (!row) return { char: " ", fg: undefined, bg: undefined, bold: false, italic: false, underline: false, dim: false, inverse: false };
|
|
26
|
+
const cell = row[x];
|
|
27
|
+
if (!cell) return { char: " ", fg: undefined, bg: undefined, bold: false, italic: false, underline: false, dim: false, inverse: false };
|
|
28
|
+
return cell;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Check if a cell exists at a buffer at position
|
|
33
|
+
*/
|
|
34
|
+
function hasCell(buffer: ScreenBuffer, x: number, y: number): boolean {
|
|
35
|
+
return buffer.cells[y]?.[x] !== undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ============================================
|
|
39
|
+
// ANSI PARSING CONSTANTS
|
|
40
|
+
// ============================================
|
|
41
|
+
|
|
42
|
+
/** ANSI escape sequence patterns */
|
|
43
|
+
const ANSI_PATTERNS = {
|
|
44
|
+
/** Full ANSI escape sequence */
|
|
45
|
+
escape: /\x1b\[[^@-~]*[@-~]/g,
|
|
46
|
+
/** SGR (Select Graphic Rendition) sequence */
|
|
47
|
+
sgr: /\x1b\[([0-9;]*)m/g,
|
|
48
|
+
/** Cursor position sequence */
|
|
49
|
+
cursor: /\x1b\[(\d+);(\d+)([Hf])/g,
|
|
50
|
+
/** Erase sequence */
|
|
51
|
+
erase: /\x1b\[(\??[0-9;]*)([JK])/g,
|
|
52
|
+
/** 256 color foreground */
|
|
53
|
+
color256Fg: /\x1b\[38;5;(\d+)m/g,
|
|
54
|
+
/** 256 color background */
|
|
55
|
+
color256Bg: /\x1b\[48;5;(\d+)m/g,
|
|
56
|
+
/** True color foreground */
|
|
57
|
+
colorTrueFg: /\x1b\[38;2;(\d+);(\d+);(\d+)m/g,
|
|
58
|
+
/** True color background */
|
|
59
|
+
colorTrueBg: /\x1b\[48;2;(\d+);(\d+);(\d+)m/g,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** SGR attribute codes */
|
|
63
|
+
const SGR_CODES = {
|
|
64
|
+
RESET: 0,
|
|
65
|
+
BOLD: 1,
|
|
66
|
+
DIM: 2,
|
|
67
|
+
ITALIC: 3,
|
|
68
|
+
UNDERLINE: 4,
|
|
69
|
+
INVERSE: 7,
|
|
70
|
+
BOLD_OFF: 22,
|
|
71
|
+
DIM_OFF: 22,
|
|
72
|
+
ITALIC_OFF: 23,
|
|
73
|
+
UNDERLINE_OFF: 24,
|
|
74
|
+
INVERSE_OFF: 27,
|
|
75
|
+
FG_BLACK: 30,
|
|
76
|
+
FG_RED: 31,
|
|
77
|
+
FG_GREEN: 32,
|
|
78
|
+
FG_YELLOW: 33,
|
|
79
|
+
FG_BLUE: 34,
|
|
80
|
+
FG_MAGENTA: 35,
|
|
81
|
+
FG_CYAN: 36,
|
|
82
|
+
FG_WHITE: 37,
|
|
83
|
+
FG_DEFAULT: 39,
|
|
84
|
+
BG_BLACK: 40,
|
|
85
|
+
BG_RED: 41,
|
|
86
|
+
BG_GREEN: 42,
|
|
87
|
+
BG_YELLOW: 43,
|
|
88
|
+
BG_BLUE: 44,
|
|
89
|
+
BG_MAGENTA: 45,
|
|
90
|
+
BG_CYAN: 46,
|
|
91
|
+
BG_WHITE: 47,
|
|
92
|
+
BG_DEFAULT: 49,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/** Standard ANSI color names to hex mapping */
|
|
96
|
+
const ANSI_COLORS: Record<number, string> = {
|
|
97
|
+
30: "#000000", // Black
|
|
98
|
+
31: "#cd0000", // Red
|
|
99
|
+
32: "#00cd00", // Green
|
|
100
|
+
33: "#cdcd00", // Yellow
|
|
101
|
+
34: "#0000ee", // Blue
|
|
102
|
+
35: "#cd00cd", // Magenta
|
|
103
|
+
36: "#00cdcd", // Cyan
|
|
104
|
+
37: "#e5e5e5", // White
|
|
105
|
+
90: "#7f7f7f", // Bright Black (Gray)
|
|
106
|
+
91: "#ff0000", // Bright Red
|
|
107
|
+
92: "#00ff00", // Bright Green
|
|
108
|
+
93: "#ffff00", // Bright Yellow
|
|
109
|
+
94: "#5c5cff", // Bright Blue
|
|
110
|
+
95: "#ff00ff", // Bright Magenta
|
|
111
|
+
96: "#00ffff", // Bright Cyan
|
|
112
|
+
97: "#ffffff", // Bright White
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// ============================================
|
|
116
|
+
// DEFAULT CELL
|
|
117
|
+
// ============================================
|
|
118
|
+
|
|
119
|
+
/** Default empty cell state */
|
|
120
|
+
export const DEFAULT_CELL: ScreenCell = {
|
|
121
|
+
char: " ",
|
|
122
|
+
fg: undefined,
|
|
123
|
+
bg: undefined,
|
|
124
|
+
bold: false,
|
|
125
|
+
italic: false,
|
|
126
|
+
underline: false,
|
|
127
|
+
dim: false,
|
|
128
|
+
inverse: false,
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// ============================================
|
|
132
|
+
// ANSI PARSER
|
|
133
|
+
// ============================================
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Parse result for a single character with attributes
|
|
137
|
+
*/
|
|
138
|
+
interface ParsedChar {
|
|
139
|
+
char: string;
|
|
140
|
+
attrs: ScreenCell;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* ANSI Parser state
|
|
145
|
+
*/
|
|
146
|
+
interface ParserState {
|
|
147
|
+
attrs: ScreenCell;
|
|
148
|
+
pos: number;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Parse ANSI color code to hex string
|
|
153
|
+
*/
|
|
154
|
+
function parseColorCode(code: number, is256: boolean, r?: number, g?: number, b?: number): string | undefined {
|
|
155
|
+
if (is256) {
|
|
156
|
+
// 256 color palette
|
|
157
|
+
if (code < 16) {
|
|
158
|
+
// Standard colors
|
|
159
|
+
return ANSI_COLORS[30 + code] ?? ANSI_COLORS[90 + code - 8];
|
|
160
|
+
} else if (code < 232) {
|
|
161
|
+
// 216 color cube
|
|
162
|
+
const n = code - 16;
|
|
163
|
+
const r = Math.floor(n / 36) * 51;
|
|
164
|
+
const g = Math.floor((n % 36) / 6) * 51;
|
|
165
|
+
const b = (n % 6) * 51;
|
|
166
|
+
return `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}`;
|
|
167
|
+
} else {
|
|
168
|
+
// Grayscale
|
|
169
|
+
const gray = (code - 232) * 10 + 8;
|
|
170
|
+
return `#${gray.toString(16).padStart(2, "0")}${gray.toString(16).padStart(2, "0")}${gray.toString(16).padStart(2, "0")}`;
|
|
171
|
+
}
|
|
172
|
+
} else if (r !== undefined && g !== undefined && b !== undefined) {
|
|
173
|
+
// True color
|
|
174
|
+
return `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}`;
|
|
175
|
+
}
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Parse SGR sequence and update attributes
|
|
181
|
+
*/
|
|
182
|
+
function parseSGR(params: string, state: ParserState): void {
|
|
183
|
+
if (params === "" || params === "0") {
|
|
184
|
+
// Reset
|
|
185
|
+
state.attrs = { ...DEFAULT_CELL };
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const codes = params.split(";").map((s) => parseInt(s, 10) || 0);
|
|
190
|
+
let i = 0;
|
|
191
|
+
|
|
192
|
+
while (i < codes.length) {
|
|
193
|
+
const code = codes[i];
|
|
194
|
+
|
|
195
|
+
switch (code) {
|
|
196
|
+
case SGR_CODES.RESET:
|
|
197
|
+
state.attrs = { ...DEFAULT_CELL };
|
|
198
|
+
break;
|
|
199
|
+
case SGR_CODES.BOLD:
|
|
200
|
+
state.attrs.bold = true;
|
|
201
|
+
break;
|
|
202
|
+
case SGR_CODES.DIM:
|
|
203
|
+
state.attrs.dim = true;
|
|
204
|
+
break;
|
|
205
|
+
case SGR_CODES.ITALIC:
|
|
206
|
+
state.attrs.italic = true;
|
|
207
|
+
break;
|
|
208
|
+
case SGR_CODES.UNDERLINE:
|
|
209
|
+
state.attrs.underline = true;
|
|
210
|
+
break;
|
|
211
|
+
case SGR_CODES.INVERSE:
|
|
212
|
+
state.attrs.inverse = true;
|
|
213
|
+
break;
|
|
214
|
+
case SGR_CODES.BOLD_OFF:
|
|
215
|
+
case SGR_CODES.DIM_OFF:
|
|
216
|
+
state.attrs.bold = false;
|
|
217
|
+
state.attrs.dim = false;
|
|
218
|
+
break;
|
|
219
|
+
case SGR_CODES.ITALIC_OFF:
|
|
220
|
+
state.attrs.italic = false;
|
|
221
|
+
break;
|
|
222
|
+
case SGR_CODES.UNDERLINE_OFF:
|
|
223
|
+
state.attrs.underline = false;
|
|
224
|
+
break;
|
|
225
|
+
case SGR_CODES.INVERSE_OFF:
|
|
226
|
+
state.attrs.inverse = false;
|
|
227
|
+
break;
|
|
228
|
+
// Standard foreground colors
|
|
229
|
+
case 30:
|
|
230
|
+
case 31:
|
|
231
|
+
case 32:
|
|
232
|
+
case 33:
|
|
233
|
+
case 34:
|
|
234
|
+
case 35:
|
|
235
|
+
case 36:
|
|
236
|
+
case 37:
|
|
237
|
+
state.attrs.fg = ANSI_COLORS[code];
|
|
238
|
+
break;
|
|
239
|
+
case 90:
|
|
240
|
+
case 91:
|
|
241
|
+
case 92:
|
|
242
|
+
case 93:
|
|
243
|
+
case 94:
|
|
244
|
+
case 95:
|
|
245
|
+
case 96:
|
|
246
|
+
case 97:
|
|
247
|
+
state.attrs.fg = ANSI_COLORS[code];
|
|
248
|
+
break;
|
|
249
|
+
case SGR_CODES.FG_DEFAULT:
|
|
250
|
+
state.attrs.fg = undefined;
|
|
251
|
+
break;
|
|
252
|
+
// Standard background colors
|
|
253
|
+
case 40:
|
|
254
|
+
case 41:
|
|
255
|
+
case 42:
|
|
256
|
+
case 43:
|
|
257
|
+
case 44:
|
|
258
|
+
case 45:
|
|
259
|
+
case 46:
|
|
260
|
+
case 47:
|
|
261
|
+
state.attrs.bg = ANSI_COLORS[code - 10];
|
|
262
|
+
break;
|
|
263
|
+
case 100:
|
|
264
|
+
case 101:
|
|
265
|
+
case 102:
|
|
266
|
+
case 103:
|
|
267
|
+
case 104:
|
|
268
|
+
case 105:
|
|
269
|
+
case 106:
|
|
270
|
+
case 107:
|
|
271
|
+
state.attrs.bg = ANSI_COLORS[code];
|
|
272
|
+
break;
|
|
273
|
+
case SGR_CODES.BG_DEFAULT:
|
|
274
|
+
state.attrs.bg = undefined;
|
|
275
|
+
break;
|
|
276
|
+
// 256 color
|
|
277
|
+
case 38:
|
|
278
|
+
if (codes[i + 1] === 5 && codes[i + 2] !== undefined) {
|
|
279
|
+
state.attrs.fg = parseColorCode(codes[i + 2] as number, true);
|
|
280
|
+
i += 2;
|
|
281
|
+
} else if (codes[i + 1] === 2 && codes[i + 4] !== undefined) {
|
|
282
|
+
state.attrs.fg = parseColorCode(0, false, codes[i + 2] as number, codes[i + 3] as number, codes[i + 4] as number);
|
|
283
|
+
i += 4;
|
|
284
|
+
}
|
|
285
|
+
break;
|
|
286
|
+
case 48:
|
|
287
|
+
if (codes[i + 1] === 5 && codes[i + 2] !== undefined) {
|
|
288
|
+
state.attrs.bg = parseColorCode(codes[i + 2] as number, true);
|
|
289
|
+
i += 2;
|
|
290
|
+
} else if (codes[i + 1] === 2 && codes[i + 4] !== undefined) {
|
|
291
|
+
state.attrs.bg = parseColorCode(0, false, codes[i + 2] as number, codes[i + 3] as number, codes[i + 4] as number);
|
|
292
|
+
i += 4;
|
|
293
|
+
}
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
i++;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Parse ANSI text into characters with attributes
|
|
302
|
+
*/
|
|
303
|
+
export function parseANSIText(text: string): ParsedChar[] {
|
|
304
|
+
const result: ParsedChar[] = [];
|
|
305
|
+
const state: ParserState = {
|
|
306
|
+
attrs: { ...DEFAULT_CELL },
|
|
307
|
+
pos: 0,
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
let remaining = text;
|
|
311
|
+
|
|
312
|
+
while (remaining.length > 0) {
|
|
313
|
+
// Check for ANSI escape sequence
|
|
314
|
+
if (remaining.startsWith("\x1b[")) {
|
|
315
|
+
// Find the end of the sequence
|
|
316
|
+
const match = remaining.match(/^\x1b\[([0-9;]*)([A-Za-z])/);
|
|
317
|
+
if (match) {
|
|
318
|
+
const [, params = "", cmd] = match;
|
|
319
|
+
if (cmd === "m") {
|
|
320
|
+
parseSGR(params, state);
|
|
321
|
+
}
|
|
322
|
+
remaining = remaining.slice(match[0].length);
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Handle UTF-8 characters properly
|
|
328
|
+
const char = remaining[0];
|
|
329
|
+
remaining = remaining.slice(1);
|
|
330
|
+
|
|
331
|
+
// Skip control characters except newline
|
|
332
|
+
if (char === "\n") {
|
|
333
|
+
result.push({ char: "\n", attrs: { ...state.attrs } });
|
|
334
|
+
} else if (char && char.charCodeAt(0) >= 32 || (char && char.charCodeAt(0) === 9)) {
|
|
335
|
+
result.push({ char, attrs: { ...state.attrs } });
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return result;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Strip ANSI escape sequences from text
|
|
344
|
+
*/
|
|
345
|
+
export function stripANSI(text: string): string {
|
|
346
|
+
return text.replace(ANSI_PATTERNS.escape, "");
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ============================================
|
|
350
|
+
// SCREEN BUFFER CAPTURE
|
|
351
|
+
// ============================================
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Create an empty screen buffer
|
|
355
|
+
*/
|
|
356
|
+
export function createScreenBuffer(width: number, height: number): ScreenBuffer {
|
|
357
|
+
const cells: ScreenCell[][] = [];
|
|
358
|
+
for (let y = 0; y < height; y++) {
|
|
359
|
+
cells.push(Array(width).fill(null).map(() => ({ ...DEFAULT_CELL })));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return {
|
|
363
|
+
width,
|
|
364
|
+
height,
|
|
365
|
+
cells,
|
|
366
|
+
cursor: { x: 0, y: 0, visible: true },
|
|
367
|
+
timestamp: Date.now(),
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Render parsed characters to a screen buffer
|
|
373
|
+
*/
|
|
374
|
+
export function renderToScreenBuffer(
|
|
375
|
+
chars: ParsedChar[],
|
|
376
|
+
width: number,
|
|
377
|
+
height: number
|
|
378
|
+
): ScreenBuffer {
|
|
379
|
+
const buffer = createScreenBuffer(width, height);
|
|
380
|
+
let x = 0;
|
|
381
|
+
let y = 0;
|
|
382
|
+
|
|
383
|
+
for (const { char, attrs } of chars) {
|
|
384
|
+
if (char === "\n") {
|
|
385
|
+
x = 0;
|
|
386
|
+
y++;
|
|
387
|
+
if (y >= height) break;
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (char === "\t") {
|
|
392
|
+
x = Math.min(x + 4 - (x % 4), width - 1);
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (x < width && y < height) {
|
|
397
|
+
const row = buffer.cells[y];
|
|
398
|
+
if (row && row[x] !== undefined) {
|
|
399
|
+
row[x] = { ...attrs, char };
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
x++;
|
|
404
|
+
if (x >= width) {
|
|
405
|
+
x = 0;
|
|
406
|
+
y++;
|
|
407
|
+
if (y >= height) break;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
buffer.cursor = { x, y: Math.min(y, height - 1), visible: true };
|
|
412
|
+
return buffer;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Capture terminal screen as buffer
|
|
417
|
+
* Note: This requires terminal access via Bun's APIs or external capture
|
|
418
|
+
*/
|
|
419
|
+
export async function captureScreen(
|
|
420
|
+
width: number,
|
|
421
|
+
height: number,
|
|
422
|
+
rawContent?: string
|
|
423
|
+
): Promise<ScreenBuffer> {
|
|
424
|
+
// If raw content provided, parse and render it
|
|
425
|
+
if (rawContent) {
|
|
426
|
+
const chars = parseANSIText(rawContent);
|
|
427
|
+
return renderToScreenBuffer(chars, width, height);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// Otherwise return empty buffer (actual capture done by TUI Bridge MCP)
|
|
431
|
+
return createScreenBuffer(width, height);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// ============================================
|
|
435
|
+
// UI ELEMENT DETECTION
|
|
436
|
+
// ============================================
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Detect UI elements in a screen buffer
|
|
440
|
+
*/
|
|
441
|
+
export function detectUIElements(buffer: ScreenBuffer): UIElement[] {
|
|
442
|
+
const elements: UIElement[] = [];
|
|
443
|
+
const visited: boolean[][] = buffer.cells.map((row) => row.map(() => false));
|
|
444
|
+
|
|
445
|
+
for (let y = 0; y < buffer.height; y++) {
|
|
446
|
+
for (let x = 0; x < buffer.width; x++) {
|
|
447
|
+
if (visited[y]?.[x]) continue;
|
|
448
|
+
|
|
449
|
+
const cell = getCell(buffer, x, y);
|
|
450
|
+
if (cell.char === " ") continue;
|
|
451
|
+
|
|
452
|
+
// Try to detect element type based on surrounding context
|
|
453
|
+
const elementType = detectElementType(buffer, x, y);
|
|
454
|
+
const element = extractElement(buffer, x, y, elementType, visited);
|
|
455
|
+
|
|
456
|
+
if (element) {
|
|
457
|
+
elements.push(element);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
return elements;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Detect the type of UI element at a position
|
|
467
|
+
*/
|
|
468
|
+
function detectElementType(buffer: ScreenBuffer, x: number, y: number): UIElementType {
|
|
469
|
+
const row = buffer.cells[y];
|
|
470
|
+
if (!row) return "text";
|
|
471
|
+
const cell = row[x];
|
|
472
|
+
if (!cell) return "text";
|
|
473
|
+
|
|
474
|
+
// Check for button-like patterns (text in brackets or with borders)
|
|
475
|
+
if (cell.char === "[" || cell.char === "<") {
|
|
476
|
+
return "button";
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// Check for menu items (lines with > or * prefixes)
|
|
480
|
+
if (cell.char === ">" || cell.char === "*") {
|
|
481
|
+
return "menu_item";
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// Check for list items (lines with - or numbers)
|
|
485
|
+
if (cell.char === "-" || /^\d$/.test(cell.char)) {
|
|
486
|
+
return "list_item";
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// Check for borders (box drawing characters)
|
|
490
|
+
if (/[\u2500-\u257F]/.test(cell.char)) {
|
|
491
|
+
return "dialog";
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// Check for header (first few lines, often bold or colored)
|
|
495
|
+
if (y < 3 && (cell.bold || cell.inverse)) {
|
|
496
|
+
return "header";
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// Check for footer (last few lines)
|
|
500
|
+
if (y >= buffer.height - 3) {
|
|
501
|
+
return "footer";
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// Default to text
|
|
505
|
+
return "text";
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Extract a UI element starting at a position
|
|
510
|
+
*/
|
|
511
|
+
function extractElement(
|
|
512
|
+
buffer: ScreenBuffer,
|
|
513
|
+
startX: number,
|
|
514
|
+
startY: number,
|
|
515
|
+
type: UIElementType,
|
|
516
|
+
visited: boolean[][]
|
|
517
|
+
): UIElement | null {
|
|
518
|
+
const text = extractText(buffer, startX, startY, visited);
|
|
519
|
+
if (!text || text.trim().length === 0) {
|
|
520
|
+
return null;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// Calculate bounds
|
|
524
|
+
let minX = buffer.width;
|
|
525
|
+
let maxX = 0;
|
|
526
|
+
let minY = buffer.height;
|
|
527
|
+
let maxY = 0;
|
|
528
|
+
|
|
529
|
+
// Re-scan for bounds based on text length
|
|
530
|
+
let x = startX;
|
|
531
|
+
let y = startY;
|
|
532
|
+
for (const char of text) {
|
|
533
|
+
if (char === "\n") {
|
|
534
|
+
x = startX;
|
|
535
|
+
y++;
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
if (x < minX) minX = x;
|
|
539
|
+
if (x > maxX) maxX = x;
|
|
540
|
+
if (y < minY) minY = y;
|
|
541
|
+
if (y > maxY) maxY = y;
|
|
542
|
+
x++;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
return {
|
|
546
|
+
type,
|
|
547
|
+
bounds: {
|
|
548
|
+
x: minX,
|
|
549
|
+
y: minY,
|
|
550
|
+
width: Math.max(1, maxX - minX + 1),
|
|
551
|
+
height: Math.max(1, maxY - minY + 1),
|
|
552
|
+
},
|
|
553
|
+
text: text.trim(),
|
|
554
|
+
focused: isFocused(buffer, startX, startY),
|
|
555
|
+
clickable: type === "button" || type === "menu_item",
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Extract continuous text from a position
|
|
561
|
+
*/
|
|
562
|
+
function extractText(
|
|
563
|
+
buffer: ScreenBuffer,
|
|
564
|
+
startX: number,
|
|
565
|
+
startY: number,
|
|
566
|
+
visited: boolean[][]
|
|
567
|
+
): string {
|
|
568
|
+
let text = "";
|
|
569
|
+
let x = startX;
|
|
570
|
+
let y = startY;
|
|
571
|
+
|
|
572
|
+
// Extract horizontal line of text
|
|
573
|
+
while (y < buffer.height) {
|
|
574
|
+
x = startX;
|
|
575
|
+
while (x < buffer.width) {
|
|
576
|
+
if (visited[y]?.[x]) {
|
|
577
|
+
x++;
|
|
578
|
+
continue;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
const cell = getCell(buffer, x, y);
|
|
582
|
+
if (cell.char === " " && text.length > 0) {
|
|
583
|
+
// Check if this is end of element
|
|
584
|
+
const nextNonSpace = findNextNonSpace(buffer, x, y);
|
|
585
|
+
if (!nextNonSpace || nextNonSpace.y > y) {
|
|
586
|
+
break;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
const visitedRow = visited[y];
|
|
591
|
+
if (visitedRow) visitedRow[x] = true;
|
|
592
|
+
text += cell.char;
|
|
593
|
+
x++;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// Check if next line continues this element
|
|
597
|
+
if (y + 1 < buffer.height && shouldContinueElement(buffer, startX, y)) {
|
|
598
|
+
text += "\n";
|
|
599
|
+
y++;
|
|
600
|
+
} else {
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
return text;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Find next non-space character
|
|
610
|
+
*/
|
|
611
|
+
function findNextNonSpace(
|
|
612
|
+
buffer: ScreenBuffer,
|
|
613
|
+
startX: number,
|
|
614
|
+
y: number
|
|
615
|
+
): { x: number; y: number } | null {
|
|
616
|
+
for (let x = startX; x < buffer.width; x++) {
|
|
617
|
+
const cell = getCell(buffer, x, y);
|
|
618
|
+
if (cell.char !== " ") {
|
|
619
|
+
return { x, y };
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
if (y + 1 < buffer.height) {
|
|
623
|
+
return findNextNonSpace(buffer, 0, y + 1);
|
|
624
|
+
}
|
|
625
|
+
return null;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Check if element continues to next line
|
|
630
|
+
*/
|
|
631
|
+
function shouldContinueElement(buffer: ScreenBuffer, startX: number, y: number): boolean {
|
|
632
|
+
// Check if next line has similar indentation and content
|
|
633
|
+
if (y + 1 >= buffer.height) return false;
|
|
634
|
+
|
|
635
|
+
const currentRow = buffer.cells[y];
|
|
636
|
+
if (!currentRow) return false;
|
|
637
|
+
|
|
638
|
+
const currentLineHasContent = currentRow.some((c) => c.char !== " ");
|
|
639
|
+
const nextRow = buffer.cells[y + 1];
|
|
640
|
+
if (!nextRow) return false;
|
|
641
|
+
const nextLineHasContent = nextRow.some((c) => c.char !== " ");
|
|
642
|
+
|
|
643
|
+
return currentLineHasContent && nextLineHasContent;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Check if element appears focused
|
|
648
|
+
*/
|
|
649
|
+
function isFocused(buffer: ScreenBuffer, x: number, y: number): boolean {
|
|
650
|
+
const cell = getCell(buffer, x, y);
|
|
651
|
+
return cell.inverse || cell.bold || false;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// ============================================
|
|
655
|
+
// EXPORT FORMATS
|
|
656
|
+
// ============================================
|
|
657
|
+
|
|
658
|
+
/** Export format options */
|
|
659
|
+
export type ExportFormat = "text" | "json" | "html" | "markdown";
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* Export options
|
|
663
|
+
*/
|
|
664
|
+
export interface ExportOptions {
|
|
665
|
+
/** Export format */
|
|
666
|
+
format: ExportFormat;
|
|
667
|
+
/** Include cursor position (for JSON) */
|
|
668
|
+
includeCursor?: boolean;
|
|
669
|
+
/** Include UI elements (for JSON) */
|
|
670
|
+
includeElements?: boolean;
|
|
671
|
+
/** CSS class prefix for HTML */
|
|
672
|
+
cssPrefix?: string;
|
|
673
|
+
/** Use inline styles for HTML */
|
|
674
|
+
inlineStyles?: boolean;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* Export screen buffer to plain text
|
|
679
|
+
*/
|
|
680
|
+
export function exportToText(buffer: ScreenBuffer): string {
|
|
681
|
+
const lines: string[] = [];
|
|
682
|
+
|
|
683
|
+
for (let y = 0; y < buffer.height; y++) {
|
|
684
|
+
const row = buffer.cells[y];
|
|
685
|
+
if (!row) continue;
|
|
686
|
+
let line = "";
|
|
687
|
+
for (let x = 0; x < buffer.width; x++) {
|
|
688
|
+
const cell = row[x];
|
|
689
|
+
line += cell?.char ?? " ";
|
|
690
|
+
}
|
|
691
|
+
// Trim trailing spaces but preserve leading
|
|
692
|
+
lines.push(line.trimEnd());
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
// Remove trailing empty lines
|
|
696
|
+
while (lines.length > 0 && lines[lines.length - 1] === "") {
|
|
697
|
+
lines.pop();
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
return lines.join("\n");
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* Export screen buffer to JSON
|
|
705
|
+
*/
|
|
706
|
+
export function exportToJSON(
|
|
707
|
+
buffer: ScreenBuffer,
|
|
708
|
+
options: { includeCursor?: boolean; includeElements?: boolean } = {}
|
|
709
|
+
): string {
|
|
710
|
+
const result: Record<string, unknown> = {
|
|
711
|
+
width: buffer.width,
|
|
712
|
+
height: buffer.height,
|
|
713
|
+
cells: buffer.cells,
|
|
714
|
+
timestamp: buffer.timestamp,
|
|
715
|
+
};
|
|
716
|
+
|
|
717
|
+
if (options.includeCursor) {
|
|
718
|
+
result.cursor = buffer.cursor;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
if (options.includeElements) {
|
|
722
|
+
result.elements = detectUIElements(buffer);
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
return JSON.stringify(result, null, 2);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* Export screen buffer to HTML
|
|
730
|
+
*/
|
|
731
|
+
export function exportToHTML(
|
|
732
|
+
buffer: ScreenBuffer,
|
|
733
|
+
options: { cssPrefix?: string; inlineStyles?: boolean } = {}
|
|
734
|
+
): string {
|
|
735
|
+
const prefix = options.cssPrefix || "tui";
|
|
736
|
+
const inline = options.inlineStyles !== false;
|
|
737
|
+
|
|
738
|
+
let html = `<!DOCTYPE html>
|
|
739
|
+
<html>
|
|
740
|
+
<head>
|
|
741
|
+
<meta charset="UTF-8">
|
|
742
|
+
<title>Terminal Screen</title>
|
|
743
|
+
<style>
|
|
744
|
+
.${prefix}-screen {
|
|
745
|
+
font-family: monospace;
|
|
746
|
+
white-space: pre;
|
|
747
|
+
line-height: 1.2;
|
|
748
|
+
background: #1a1a1a;
|
|
749
|
+
color: #e5e5e5;
|
|
750
|
+
padding: 8px;
|
|
751
|
+
}
|
|
752
|
+
.${prefix}-line {
|
|
753
|
+
display: block;
|
|
754
|
+
}
|
|
755
|
+
.${prefix}-cell {
|
|
756
|
+
display: inline;
|
|
757
|
+
}
|
|
758
|
+
.${prefix}-bold { font-weight: bold; }
|
|
759
|
+
.${prefix}-italic { font-style: italic; }
|
|
760
|
+
.${prefix}-underline { text-decoration: underline; }
|
|
761
|
+
.${prefix}-dim { opacity: 0.6; }
|
|
762
|
+
.${prefix}-inverse { filter: invert(1); }
|
|
763
|
+
</style>
|
|
764
|
+
</head>
|
|
765
|
+
<body>
|
|
766
|
+
<div class="${prefix}-screen">
|
|
767
|
+
`;
|
|
768
|
+
|
|
769
|
+
for (let y = 0; y < buffer.height; y++) {
|
|
770
|
+
html += `<span class="${prefix}-line">`;
|
|
771
|
+
for (let x = 0; x < buffer.width; x++) {
|
|
772
|
+
const cell = getCell(buffer, x, y);
|
|
773
|
+
if (cell.char === " ") {
|
|
774
|
+
html += " ";
|
|
775
|
+
continue;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
const classes: string[] = [`${prefix}-cell`];
|
|
779
|
+
const styles: string[] = [];
|
|
780
|
+
|
|
781
|
+
if (cell.bold) classes.push(`${prefix}-bold`);
|
|
782
|
+
if (cell.italic) classes.push(`${prefix}-italic`);
|
|
783
|
+
if (cell.underline) classes.push(`${prefix}-underline`);
|
|
784
|
+
if (cell.dim) classes.push(`${prefix}-dim`);
|
|
785
|
+
if (cell.inverse) classes.push(`${prefix}-inverse`);
|
|
786
|
+
|
|
787
|
+
if (cell.fg) styles.push(`color: ${cell.fg}`);
|
|
788
|
+
if (cell.bg) styles.push(`background-color: ${cell.bg}`);
|
|
789
|
+
|
|
790
|
+
const styleAttr = inline && styles.length > 0 ? ` style="${styles.join("; ")}"` : "";
|
|
791
|
+
const classAttr = classes.length > 1 ? ` class="${classes.join(" ")}"` : "";
|
|
792
|
+
|
|
793
|
+
if (classAttr || styleAttr) {
|
|
794
|
+
html += `<span${classAttr}${styleAttr}>${escapeHTML(cell.char)}</span>`;
|
|
795
|
+
} else {
|
|
796
|
+
html += escapeHTML(cell.char);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
html += `</span>\n`;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
html += `</div>
|
|
803
|
+
</body>
|
|
804
|
+
</html>`;
|
|
805
|
+
|
|
806
|
+
return html;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* Escape HTML special characters
|
|
811
|
+
*/
|
|
812
|
+
function escapeHTML(char: string): string {
|
|
813
|
+
switch (char) {
|
|
814
|
+
case "&": return "&";
|
|
815
|
+
case "<": return "<";
|
|
816
|
+
case ">": return ">";
|
|
817
|
+
case '"': return """;
|
|
818
|
+
case "'": return "'";
|
|
819
|
+
default: return char;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/**
|
|
824
|
+
* Export screen buffer to Markdown
|
|
825
|
+
*/
|
|
826
|
+
export function exportToMarkdown(buffer: ScreenBuffer): string {
|
|
827
|
+
let md = "```\n";
|
|
828
|
+
|
|
829
|
+
for (let y = 0; y < buffer.height; y++) {
|
|
830
|
+
let line = "";
|
|
831
|
+
for (let x = 0; x < buffer.width; x++) {
|
|
832
|
+
line += getCell(buffer, x, y).char;
|
|
833
|
+
}
|
|
834
|
+
md += line.trimEnd() + "\n";
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
md += "```\n";
|
|
838
|
+
|
|
839
|
+
// Add detected elements as a list
|
|
840
|
+
const elements = detectUIElements(buffer);
|
|
841
|
+
if (elements.length > 0) {
|
|
842
|
+
md += "\n### Detected Elements\n\n";
|
|
843
|
+
for (const el of elements) {
|
|
844
|
+
const emoji = getElementEmoji(el.type);
|
|
845
|
+
md += `- ${emoji} **${el.type}**: "${el.text}" at (${el.bounds.x}, ${el.bounds.y})\n`;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
return md;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* Get emoji for element type
|
|
854
|
+
*/
|
|
855
|
+
function getElementEmoji(type: UIElementType): string {
|
|
856
|
+
switch (type) {
|
|
857
|
+
case "button": return "[ ]";
|
|
858
|
+
case "input": return "[_]";
|
|
859
|
+
case "menu": return "||";
|
|
860
|
+
case "menu_item": return ">";
|
|
861
|
+
case "list": return "-";
|
|
862
|
+
case "list_item": return "*";
|
|
863
|
+
case "dialog": return "+";
|
|
864
|
+
case "header": return "#";
|
|
865
|
+
case "footer": return "_";
|
|
866
|
+
default: return " ";
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* Export screen buffer to specified format
|
|
872
|
+
*/
|
|
873
|
+
export function exportScreen(
|
|
874
|
+
buffer: ScreenBuffer,
|
|
875
|
+
format: ExportFormat,
|
|
876
|
+
options: ExportOptions = { format: "text" }
|
|
877
|
+
): string {
|
|
878
|
+
switch (format) {
|
|
879
|
+
case "text":
|
|
880
|
+
return exportToText(buffer);
|
|
881
|
+
case "json":
|
|
882
|
+
return exportToJSON(buffer, {
|
|
883
|
+
includeCursor: options.includeCursor,
|
|
884
|
+
includeElements: options.includeElements,
|
|
885
|
+
});
|
|
886
|
+
case "html":
|
|
887
|
+
return exportToHTML(buffer, {
|
|
888
|
+
cssPrefix: options.cssPrefix,
|
|
889
|
+
inlineStyles: options.inlineStyles,
|
|
890
|
+
});
|
|
891
|
+
case "markdown":
|
|
892
|
+
return exportToMarkdown(buffer);
|
|
893
|
+
default:
|
|
894
|
+
throw new Error(`Unknown export format: ${format}`);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
// ============================================
|
|
899
|
+
// PARSED SCREEN
|
|
900
|
+
// ============================================
|
|
901
|
+
|
|
902
|
+
/**
|
|
903
|
+
* Parse raw terminal output into a full ParsedScreen
|
|
904
|
+
*/
|
|
905
|
+
export function parseScreen(
|
|
906
|
+
rawContent: string,
|
|
907
|
+
width: number,
|
|
908
|
+
height: number
|
|
909
|
+
): ParsedScreen {
|
|
910
|
+
const chars = parseANSIText(rawContent);
|
|
911
|
+
const buffer = renderToScreenBuffer(chars, width, height);
|
|
912
|
+
const elements = detectUIElements(buffer);
|
|
913
|
+
const text = exportToText(buffer);
|
|
914
|
+
|
|
915
|
+
return {
|
|
916
|
+
text,
|
|
917
|
+
buffer,
|
|
918
|
+
elements,
|
|
919
|
+
timestamp: Date.now(),
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
// ============================================
|
|
924
|
+
// INTEGRATION WITH TUI BRIDGE
|
|
925
|
+
// ============================================
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* Screen export API for TUIBridge
|
|
929
|
+
*/
|
|
930
|
+
export interface ScreenExportAPI {
|
|
931
|
+
/** Get screen buffer */
|
|
932
|
+
getScreenBuffer: () => Promise<ScreenBuffer>;
|
|
933
|
+
/** Export screen to format */
|
|
934
|
+
exportScreen: (format: ExportFormat, options?: ExportOptions) => Promise<string>;
|
|
935
|
+
/** Parse raw content */
|
|
936
|
+
parseRawContent: (content: string, width: number, height: number) => ParsedScreen;
|
|
937
|
+
/** Get current screen dimensions */
|
|
938
|
+
getDimensions: () => { width: number; height: number };
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
/**
|
|
942
|
+
* Create screen export API for TUIBridge integration
|
|
943
|
+
*/
|
|
944
|
+
export function createScreenExportAPI(
|
|
945
|
+
getRawContent: () => Promise<string>,
|
|
946
|
+
getDimensions: () => { width: number; height: number }
|
|
947
|
+
): ScreenExportAPI {
|
|
948
|
+
return {
|
|
949
|
+
async getScreenBuffer(): Promise<ScreenBuffer> {
|
|
950
|
+
const { width, height } = getDimensions();
|
|
951
|
+
const content = await getRawContent();
|
|
952
|
+
return captureScreen(width, height, content);
|
|
953
|
+
},
|
|
954
|
+
|
|
955
|
+
async exportScreen(format: ExportFormat, options: ExportOptions = { format }): Promise<string> {
|
|
956
|
+
const buffer = await this.getScreenBuffer();
|
|
957
|
+
return exportScreen(buffer, format, options);
|
|
958
|
+
},
|
|
959
|
+
|
|
960
|
+
parseRawContent(content: string, width: number, height: number): ParsedScreen {
|
|
961
|
+
return parseScreen(content, width, height);
|
|
962
|
+
},
|
|
963
|
+
|
|
964
|
+
getDimensions(): { width: number; height: number } {
|
|
965
|
+
return getDimensions();
|
|
966
|
+
},
|
|
967
|
+
};
|
|
968
|
+
}
|