Repository navigation
Expand file tree
/
Copy pathsnapshot.go
More file actions
421 lines (366 loc) · 11.4 KB
/
Copy pathsnapshot.go
File metadata and controls
421 lines (366 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package headlessterm
import (
"encoding/base64"
"fmt"
"image/color"
)
// SnapshotDetail specifies the level of detail in a snapshot.
type SnapshotDetail string
const (
// SnapshotDetailText returns plain text only.
SnapshotDetailText SnapshotDetail = "text"
// SnapshotDetailStyled returns text with style segments per line.
SnapshotDetailStyled SnapshotDetail = "styled"
// SnapshotDetailFull returns full cell-by-cell data.
SnapshotDetailFull SnapshotDetail = "full"
)
// Snapshot represents a complete terminal screen capture.
type Snapshot struct {
Size SnapshotSize `json:"size"`
Cursor SnapshotCursor `json:"cursor"`
Lines []SnapshotLine `json:"lines"`
Images []SnapshotImage `json:"images,omitempty"`
}
// SnapshotSize holds terminal dimensions.
type SnapshotSize struct {
Rows int `json:"rows"`
Cols int `json:"cols"`
}
// SnapshotCursor holds cursor state.
type SnapshotCursor struct {
Row int `json:"row"`
Col int `json:"col"`
Visible bool `json:"visible"`
Style string `json:"style"`
}
// SnapshotLine represents a single line in the snapshot.
type SnapshotLine struct {
Text string `json:"text"`
Segments []SnapshotSegment `json:"segments,omitempty"`
Cells []SnapshotCell `json:"cells,omitempty"`
}
// SnapshotSegment represents a styled text segment within a line.
type SnapshotSegment struct {
Text string `json:"text"`
Fg string `json:"fg,omitempty"`
Bg string `json:"bg,omitempty"`
UnderlineColor string `json:"underline_color,omitempty"`
Attributes SnapshotAttrs `json:"attrs,omitempty"`
Hyperlink *SnapshotLink `json:"hyperlink,omitempty"`
}
// SnapshotCell represents a single cell with full attributes.
type SnapshotCell struct {
Char string `json:"char"`
Fg string `json:"fg"`
Bg string `json:"bg"`
UnderlineColor string `json:"underline_color,omitempty"`
Attributes SnapshotAttrs `json:"attrs,omitempty"`
Hyperlink *SnapshotLink `json:"hyperlink,omitempty"`
Image *SnapshotCellImage `json:"image,omitempty"`
Wide bool `json:"wide,omitempty"`
WideSpacer bool `json:"wide_spacer,omitempty"`
}
// SnapshotCellImage holds image reference information for a cell.
type SnapshotCellImage struct {
ImageID uint32 `json:"image_id"`
PlacementID uint32 `json:"placement_id"`
U0 float32 `json:"u0"` // Texture coordinates
V0 float32 `json:"v0"`
U1 float32 `json:"u1"`
V1 float32 `json:"v1"`
ScaleX float32 `json:"scale_x"`
ScaleY float32 `json:"scale_y"`
}
// SnapshotAttrs holds text formatting attributes.
type SnapshotAttrs struct {
Bold bool `json:"bold,omitempty"`
Dim bool `json:"dim,omitempty"`
Italic bool `json:"italic,omitempty"`
Underline string `json:"underline,omitempty"` // "", "single", "double", "curly", "dotted", "dashed"
Blink string `json:"blink,omitempty"` // "", "slow", "fast"
Reverse bool `json:"reverse,omitempty"`
Hidden bool `json:"hidden,omitempty"`
Strikethrough bool `json:"strikethrough,omitempty"`
}
// SnapshotLink holds hyperlink information.
type SnapshotLink struct {
ID string `json:"id,omitempty"`
URI string `json:"uri"`
}
// SnapshotImage holds image placement metadata (without pixel data).
type SnapshotImage struct {
ID uint32 `json:"id"` // Unique image ID
PlacementID uint32 `json:"placement_id"` // Unique placement ID
Row int `json:"row"` // Position row (cells)
Col int `json:"col"` // Position column (cells)
Rows int `json:"rows"` // Size in rows (cells)
Cols int `json:"cols"` // Size in columns (cells)
PixelWidth uint32 `json:"pixel_width"` // Original image width (pixels)
PixelHeight uint32 `json:"pixel_height"` // Original image height (pixels)
ZIndex int32 `json:"z_index"` // Z-index for layering
}
// ImageSnapshot holds complete image data for retrieval.
type ImageSnapshot struct {
ID uint32 `json:"id"`
Width uint32 `json:"width"`
Height uint32 `json:"height"`
Format string `json:"format"` // "rgba" (raw RGBA pixels, base64 encoded)
Data string `json:"data"` // Base64 encoded image data
}
// GetImageData returns the image data for the given ID, or nil if not found.
func (t *Terminal) GetImageData(id uint32) *ImageSnapshot {
t.mu.RLock()
defer t.mu.RUnlock()
img := t.images.Image(id)
if img == nil {
return nil
}
return &ImageSnapshot{
ID: img.ID,
Width: img.Width,
Height: img.Height,
Format: "rgba",
Data: base64.StdEncoding.EncodeToString(img.Data),
}
}
// Snapshot creates a snapshot of the current terminal state.
// The detail parameter controls how much information is included.
func (t *Terminal) Snapshot(detail SnapshotDetail) *Snapshot {
t.mu.RLock()
defer t.mu.RUnlock()
snap := &Snapshot{
Size: SnapshotSize{
Rows: t.rows,
Cols: t.cols,
},
Cursor: SnapshotCursor{
Row: t.cursor.Row,
Col: t.cursor.Col,
Visible: t.cursor.Visible,
Style: cursorStyleToString(t.cursor.Style),
},
Lines: make([]SnapshotLine, t.rows),
}
for row := 0; row < t.rows; row++ {
snap.Lines[row] = t.snapshotLine(row, detail)
}
// Include image placements
snap.Images = t.snapshotImages()
return snap
}
// snapshotImages returns all image placements with metadata.
func (t *Terminal) snapshotImages() []SnapshotImage {
placements := t.images.Placements()
if len(placements) == 0 {
return nil
}
images := make([]SnapshotImage, 0, len(placements))
for _, p := range placements {
img := t.images.Image(p.ImageID)
if img == nil {
continue
}
images = append(images, SnapshotImage{
ID: p.ImageID,
PlacementID: p.ID,
Row: p.Row,
Col: p.Col,
Rows: p.Rows,
Cols: p.Cols,
PixelWidth: img.Width,
PixelHeight: img.Height,
ZIndex: p.ZIndex,
})
}
return images
}
// snapshotLine creates a snapshot of a single line.
func (t *Terminal) snapshotLine(row int, detail SnapshotDetail) SnapshotLine {
line := SnapshotLine{
Text: t.activeBuffer.LineContent(row),
}
switch detail {
case SnapshotDetailText:
// Just text, already set
case SnapshotDetailStyled:
line.Segments = t.lineToSegments(row)
case SnapshotDetailFull:
line.Cells = t.lineToCells(row)
}
return line
}
// lineToSegments converts a line to styled segments (runs of same style).
func (t *Terminal) lineToSegments(row int) []SnapshotSegment {
var segments []SnapshotSegment
var current *SnapshotSegment
var currentChars []rune
for col := 0; col < t.cols; col++ {
cell := t.activeBuffer.Cell(row, col)
if cell == nil {
continue
}
if cell.IsWideSpacer() {
continue
}
fg := colorToHex(cell.Fg)
bg := colorToHex(cell.Bg)
underlineColor := colorToHex(cell.UnderlineColor)
attrs := cellAttrsToSnapshot(cell)
link := cellHyperlinkToSnapshot(cell)
// Check if we need to start a new segment
if current == nil || !segmentMatches(current, fg, bg, underlineColor, attrs, link) {
// Save current segment if exists
if current != nil && len(currentChars) > 0 {
current.Text = string(currentChars)
segments = append(segments, *current)
}
// Start new segment
current = &SnapshotSegment{
Fg: fg,
Bg: bg,
UnderlineColor: underlineColor,
Attributes: attrs,
Hyperlink: link,
}
currentChars = nil
}
ch := cell.Char
if ch == 0 {
ch = ' '
}
currentChars = append(currentChars, ch)
}
// Don't forget the last segment
if current != nil && len(currentChars) > 0 {
current.Text = string(currentChars)
segments = append(segments, *current)
}
return segments
}
// lineToCells converts a line to full cell data.
func (t *Terminal) lineToCells(row int) []SnapshotCell {
cells := make([]SnapshotCell, 0, t.cols)
for col := 0; col < t.cols; col++ {
cell := t.activeBuffer.Cell(row, col)
if cell == nil {
cells = append(cells, SnapshotCell{
Char: " ",
Fg: colorToHex(nil),
Bg: colorToHex(nil),
})
continue
}
ch := cell.Char
if ch == 0 {
ch = ' '
}
sc := SnapshotCell{
Char: string(ch),
Fg: colorToHex(cell.Fg),
Bg: colorToHex(cell.Bg),
UnderlineColor: colorToHex(cell.UnderlineColor),
Attributes: cellAttrsToSnapshot(cell),
Hyperlink: cellHyperlinkToSnapshot(cell),
Image: cellImageToSnapshot(cell),
Wide: cell.IsWide(),
WideSpacer: cell.IsWideSpacer(),
}
cells = append(cells, sc)
}
return cells
}
// cellImageToSnapshot extracts cell image info.
func cellImageToSnapshot(cell *Cell) *SnapshotCellImage {
if cell.Image == nil {
return nil
}
return &SnapshotCellImage{
ImageID: cell.Image.ImageID,
PlacementID: cell.Image.PlacementID,
U0: cell.Image.U0,
V0: cell.Image.V0,
U1: cell.Image.U1,
V1: cell.Image.V1,
ScaleX: cell.Image.ScaleX,
ScaleY: cell.Image.ScaleY,
}
}
// segmentMatches checks if segment matches the given style.
func segmentMatches(seg *SnapshotSegment, fg, bg, underlineColor string, attrs SnapshotAttrs, link *SnapshotLink) bool {
if seg.Fg != fg || seg.Bg != bg || seg.UnderlineColor != underlineColor {
return false
}
if seg.Attributes != attrs {
return false
}
// Compare hyperlinks
if seg.Hyperlink == nil && link == nil {
return true
}
if seg.Hyperlink == nil || link == nil {
return false
}
return seg.Hyperlink.URI == link.URI && seg.Hyperlink.ID == link.ID
}
// colorToHex converts a color to hex string.
func colorToHex(c color.Color) string {
if c == nil {
return ""
}
rgba := resolveDefaultColor(c, true)
return fmt.Sprintf("#%02x%02x%02x", rgba.R, rgba.G, rgba.B)
}
// cellAttrsToSnapshot extracts cell attributes.
func cellAttrsToSnapshot(cell *Cell) SnapshotAttrs {
attrs := SnapshotAttrs{
Bold: cell.HasFlag(CellFlagBold),
Dim: cell.HasFlag(CellFlagDim),
Italic: cell.HasFlag(CellFlagItalic),
Reverse: cell.HasFlag(CellFlagReverse),
Hidden: cell.HasFlag(CellFlagHidden),
Strikethrough: cell.HasFlag(CellFlagStrike),
}
// Determine underline style
switch {
case cell.HasFlag(CellFlagCurlyUnderline):
attrs.Underline = "curly"
case cell.HasFlag(CellFlagDoubleUnderline):
attrs.Underline = "double"
case cell.HasFlag(CellFlagDottedUnderline):
attrs.Underline = "dotted"
case cell.HasFlag(CellFlagDashedUnderline):
attrs.Underline = "dashed"
case cell.HasFlag(CellFlagUnderline):
attrs.Underline = "single"
}
// Determine blink type
if cell.HasFlag(CellFlagBlinkFast) {
attrs.Blink = "fast"
} else if cell.HasFlag(CellFlagBlinkSlow) {
attrs.Blink = "slow"
}
return attrs
}
// cellHyperlinkToSnapshot extracts hyperlink info.
func cellHyperlinkToSnapshot(cell *Cell) *SnapshotLink {
if cell.Hyperlink == nil {
return nil
}
return &SnapshotLink{
ID: cell.Hyperlink.ID,
URI: cell.Hyperlink.URI,
}
}
// cursorStyleToString converts cursor style to string.
func cursorStyleToString(style CursorStyle) string {
switch style {
case CursorStyleBlinkingBlock, CursorStyleSteadyBlock:
return "block"
case CursorStyleBlinkingUnderline, CursorStyleSteadyUnderline:
return "underline"
case CursorStyleBlinkingBar, CursorStyleSteadyBar:
return "bar"
default:
return "block"
}
}