-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrender_state_data.go
More file actions
339 lines (294 loc) · 12.7 KB
/
render_state_data.go
File metadata and controls
339 lines (294 loc) · 12.7 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
package libghostty
// Render state data getters and setters wrapping
// ghostty_render_state_get() and ghostty_render_state_set().
// Functions are ordered alphabetically.
/*
#include <ghostty/vt.h>
// Helper to create a properly initialized GhosttyRenderStateColors (sized struct).
static inline GhosttyRenderStateColors init_render_state_colors() {
GhosttyRenderStateColors c = GHOSTTY_INIT_SIZED(GhosttyRenderStateColors);
return c;
}
*/
import "C"
import (
"errors"
"unsafe"
)
// RenderStateData identifies a data field for render state queries.
// C: GhosttyRenderStateData
type RenderStateData int
const (
// RenderStateDataInvalid is an invalid / sentinel value.
RenderStateDataInvalid RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_INVALID
// RenderStateDataCols is the viewport width in cells (uint16_t).
RenderStateDataCols RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_COLS
// RenderStateDataRows is the viewport height in cells (uint16_t).
RenderStateDataRows RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_ROWS
// RenderStateDataDirty is the current dirty state
// (GhosttyRenderStateDirty).
RenderStateDataDirty RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_DIRTY
// RenderStateDataRowIterator populates a pre-allocated row iterator
// (GhosttyRenderStateRowIterator).
RenderStateDataRowIterator RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_ROW_ITERATOR
// RenderStateDataColorBackground is the default/current background
// color (GhosttyColorRgb).
RenderStateDataColorBackground RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_COLOR_BACKGROUND
// RenderStateDataColorForeground is the default/current foreground
// color (GhosttyColorRgb).
RenderStateDataColorForeground RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_COLOR_FOREGROUND
// RenderStateDataColorCursor is the cursor color when explicitly set
// (GhosttyColorRgb).
RenderStateDataColorCursor RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_COLOR_CURSOR
// RenderStateDataColorCursorHasValue indicates whether an explicit
// cursor color is set (bool).
RenderStateDataColorCursorHasValue RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_COLOR_CURSOR_HAS_VALUE
// RenderStateDataColorPalette is the active 256-color palette
// (GhosttyColorRgb[256]).
RenderStateDataColorPalette RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_COLOR_PALETTE
// RenderStateDataCursorVisualStyle is the visual style of the cursor
// (GhosttyRenderStateCursorVisualStyle).
RenderStateDataCursorVisualStyle RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_CURSOR_VISUAL_STYLE
// RenderStateDataCursorVisible indicates whether the cursor is visible
// based on terminal modes (bool).
RenderStateDataCursorVisible RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_CURSOR_VISIBLE
// RenderStateDataCursorBlinking indicates whether the cursor should
// blink based on terminal modes (bool).
RenderStateDataCursorBlinking RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_CURSOR_BLINKING
// RenderStateDataCursorPasswordInput indicates whether the cursor is
// at a password input field (bool).
RenderStateDataCursorPasswordInput RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_CURSOR_PASSWORD_INPUT
// RenderStateDataCursorViewportHasValue indicates whether the cursor
// is visible within the viewport (bool).
RenderStateDataCursorViewportHasValue RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_HAS_VALUE
// RenderStateDataCursorViewportX is the cursor viewport x position
// in cells (uint16_t).
RenderStateDataCursorViewportX RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_X
// RenderStateDataCursorViewportY is the cursor viewport y position
// in cells (uint16_t).
RenderStateDataCursorViewportY RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_Y
// RenderStateDataCursorViewportWideTail indicates whether the cursor
// is on the tail of a wide character (bool).
RenderStateDataCursorViewportWideTail RenderStateData = C.GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_WIDE_TAIL
)
// Cols returns the viewport width in cells.
func (rs *RenderState) Cols() (uint16, error) {
var v C.uint16_t
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_COLS, unsafe.Pointer(&v))); err != nil {
return 0, err
}
return uint16(v), nil
}
// ColorBackground returns the default/current background color.
func (rs *RenderState) ColorBackground() (ColorRGB, error) {
var v C.GhosttyColorRgb
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_COLOR_BACKGROUND, unsafe.Pointer(&v))); err != nil {
return ColorRGB{}, err
}
return ColorRGB{R: uint8(v.r), G: uint8(v.g), B: uint8(v.b)}, nil
}
// ColorCursor returns the cursor color when explicitly set by terminal
// state. Returns nil (without error) when no explicit cursor color is set.
func (rs *RenderState) ColorCursor() (*ColorRGB, error) {
// Check whether a cursor color is set first.
var has C.bool
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_COLOR_CURSOR_HAS_VALUE, unsafe.Pointer(&has))); err != nil {
return nil, err
}
if !bool(has) {
return nil, nil
}
var v C.GhosttyColorRgb
err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_COLOR_CURSOR, unsafe.Pointer(&v)))
if err != nil {
var ge *Error
if errors.As(err, &ge) && ge.Result == ResultInvalidValue {
return nil, nil
}
return nil, err
}
c := ColorRGB{R: uint8(v.r), G: uint8(v.g), B: uint8(v.b)}
return &c, nil
}
// ColorForeground returns the default/current foreground color.
func (rs *RenderState) ColorForeground() (ColorRGB, error) {
var v C.GhosttyColorRgb
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_COLOR_FOREGROUND, unsafe.Pointer(&v))); err != nil {
return ColorRGB{}, err
}
return ColorRGB{R: uint8(v.r), G: uint8(v.g), B: uint8(v.b)}, nil
}
// ColorPalette returns the active 256-color palette.
func (rs *RenderState) ColorPalette() (*Palette, error) {
var cp [PaletteSize]C.GhosttyColorRgb
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_COLOR_PALETTE, unsafe.Pointer(&cp[0]))); err != nil {
return nil, err
}
var p Palette
for i, c := range cp {
p[i] = ColorRGB{R: uint8(c.r), G: uint8(c.g), B: uint8(c.b)}
}
return &p, nil
}
// Colors returns all color information from the render state in a
// single call using the sized-struct API.
func (rs *RenderState) Colors() (*RenderStateColors, error) {
cc := C.init_render_state_colors()
if err := resultError(C.ghostty_render_state_colors_get(rs.ptr, &cc)); err != nil {
return nil, err
}
result := &RenderStateColors{
Background: ColorRGB{R: uint8(cc.background.r), G: uint8(cc.background.g), B: uint8(cc.background.b)},
Foreground: ColorRGB{R: uint8(cc.foreground.r), G: uint8(cc.foreground.g), B: uint8(cc.foreground.b)},
Cursor: ColorRGB{R: uint8(cc.cursor.r), G: uint8(cc.cursor.g), B: uint8(cc.cursor.b)},
CursorHasValue: bool(cc.cursor_has_value),
}
for i, c := range cc.palette {
result.Palette[i] = ColorRGB{R: uint8(c.r), G: uint8(c.g), B: uint8(c.b)}
}
return result, nil
}
// GetMulti queries multiple render state data fields in a single cgo
// call. This is a low-level function; prefer the typed getters (Cols,
// Rows, CursorVisible, etc.) for normal use. GetMulti is useful when
// you need many fields at once and want to avoid per-field cgo overhead.
//
// Each element in keys specifies a data kind, and the corresponding
// element in values must be an unsafe.Pointer to a variable whose type
// matches the "Output type" documented for that key in the upstream C
// header (ghostty/vt/render.h, GhosttyRenderStateData enum).
//
// Example:
//
// var cols, rows C.uint16_t
// err := rs.GetMulti(
// []RenderStateData{RenderStateDataCols, RenderStateDataRows},
// []unsafe.Pointer{unsafe.Pointer(&cols), unsafe.Pointer(&rows)},
// )
//
// C: ghostty_render_state_get_multi
func (rs *RenderState) GetMulti(keys []RenderStateData, values []unsafe.Pointer) error {
if len(keys) != len(values) {
return errors.New("libghostty: keys and values must have the same length")
}
if len(keys) == 0 {
return nil
}
// Allocate the void** array in C memory to satisfy cgo pointer-passing rules.
cVals, cValsSize := cValuesArray(values)
defer Free(unsafe.Pointer(cVals), cValsSize)
return resultError(C.ghostty_render_state_get_multi(
rs.ptr,
C.size_t(len(keys)),
(*C.GhosttyRenderStateData)(unsafe.Pointer(&keys[0])),
cVals,
nil,
))
}
// CursorBlinking reports whether the cursor should blink based on
// terminal modes.
func (rs *RenderState) CursorBlinking() (bool, error) {
var v C.bool
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_CURSOR_BLINKING, unsafe.Pointer(&v))); err != nil {
return false, err
}
return bool(v), nil
}
// CursorPasswordInput reports whether the cursor is at a password
// input field.
func (rs *RenderState) CursorPasswordInput() (bool, error) {
var v C.bool
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_CURSOR_PASSWORD_INPUT, unsafe.Pointer(&v))); err != nil {
return false, err
}
return bool(v), nil
}
// CursorVisible reports whether the cursor is visible based on
// terminal modes.
func (rs *RenderState) CursorVisible() (bool, error) {
var v C.bool
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_CURSOR_VISIBLE, unsafe.Pointer(&v))); err != nil {
return false, err
}
return bool(v), nil
}
// CursorVisualStyle returns the visual style of the cursor.
func (rs *RenderState) CursorVisualStyle() (CursorVisualStyle, error) {
var v C.GhosttyRenderStateCursorVisualStyle
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_CURSOR_VISUAL_STYLE, unsafe.Pointer(&v))); err != nil {
return 0, err
}
return CursorVisualStyle(v), nil
}
// CursorViewportHasValue reports whether the cursor is visible within
// the viewport. If false, the cursor viewport position values are
// undefined.
func (rs *RenderState) CursorViewportHasValue() (bool, error) {
var v C.bool
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_HAS_VALUE, unsafe.Pointer(&v))); err != nil {
return false, err
}
return bool(v), nil
}
// CursorViewportWideTail reports whether the cursor is on the tail
// of a wide character. Only valid when CursorViewportHasValue
// returns true.
func (rs *RenderState) CursorViewportWideTail() (bool, error) {
var v C.bool
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_WIDE_TAIL, unsafe.Pointer(&v))); err != nil {
return false, err
}
return bool(v), nil
}
// CursorViewportX returns the cursor viewport x position in cells.
// Only valid when CursorViewportHasValue returns true.
func (rs *RenderState) CursorViewportX() (uint16, error) {
var v C.uint16_t
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_X, unsafe.Pointer(&v))); err != nil {
return 0, err
}
return uint16(v), nil
}
// CursorViewportY returns the cursor viewport y position in cells.
// Only valid when CursorViewportHasValue returns true.
func (rs *RenderState) CursorViewportY() (uint16, error) {
var v C.uint16_t
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_Y, unsafe.Pointer(&v))); err != nil {
return 0, err
}
return uint16(v), nil
}
// Dirty returns the current dirty state.
func (rs *RenderState) Dirty() (RenderStateDirty, error) {
var v C.GhosttyRenderStateDirty
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_DIRTY, unsafe.Pointer(&v))); err != nil {
return 0, err
}
return RenderStateDirty(v), nil
}
// RowIterator populates a pre-allocated row iterator with row data
// from the render state. The iterator can then be advanced with Next
// and queried with getter methods.
//
// The iterator can be reused across multiple calls. Row data is only
// valid until the next call to Update.
func (rs *RenderState) RowIterator(ri *RenderStateRowIterator) error {
return resultError(C.ghostty_render_state_get(
rs.ptr,
C.GHOSTTY_RENDER_STATE_DATA_ROW_ITERATOR,
unsafe.Pointer(&ri.ptr),
))
}
// Rows returns the viewport height in cells.
func (rs *RenderState) Rows() (uint16, error) {
var v C.uint16_t
if err := resultError(C.ghostty_render_state_get(rs.ptr, C.GHOSTTY_RENDER_STATE_DATA_ROWS, unsafe.Pointer(&v))); err != nil {
return 0, err
}
return uint16(v), nil
}
// SetDirty sets the dirty state.
func (rs *RenderState) SetDirty(dirty RenderStateDirty) error {
v := C.GhosttyRenderStateDirty(dirty)
return resultError(C.ghostty_render_state_set(rs.ptr, C.GHOSTTY_RENDER_STATE_OPTION_DIRTY, unsafe.Pointer(&v)))
}