forked from anomalyco/opentui
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtextbuffer.go
More file actions
362 lines (309 loc) · 9.96 KB
/
textbuffer.go
File metadata and controls
362 lines (309 loc) · 9.96 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
package opentui
/*
#include "opentui.h"
#include <stdlib.h>
*/
import "C"
import (
"unsafe"
)
// TextBuffer wraps the TextBuffer from the C library.
// It represents a buffer of styled text fragments with efficient line tracking.
type TextBuffer struct {
ptr *C.TextBuffer
}
// NewTextBuffer creates a new text buffer with the specified initial capacity.
// The widthMethod parameter controls how text width is calculated (use WidthMethodUnicode for full Unicode support).
func NewTextBuffer(length uint32, widthMethod uint8) *TextBuffer {
if length == 0 {
length = 1024 // Default capacity
}
ptr := C.createTextBuffer(C.uint32_t(length), C.uint8_t(widthMethod))
if ptr == nil {
return nil
}
tb := &TextBuffer{ptr: ptr}
setFinalizer(tb, func(tb *TextBuffer) { tb.Close() })
return tb
}
// Close releases the text buffer's resources.
// After calling Close, the text buffer should not be used.
func (tb *TextBuffer) Close() error {
if tb.ptr != nil {
clearFinalizer(tb)
C.destroyTextBuffer(tb.ptr)
tb.ptr = nil
}
return nil
}
// Length returns the current length of the text buffer in characters.
func (tb *TextBuffer) Length() (uint32, error) {
if tb.ptr == nil {
return 0, newError("text buffer is closed")
}
return uint32(C.textBufferGetLength(tb.ptr)), nil
}
// Capacity returns the current capacity of the text buffer.
func (tb *TextBuffer) Capacity() (uint32, error) {
if tb.ptr == nil {
return 0, newError("text buffer is closed")
}
return uint32(C.textBufferGetCapacity(tb.ptr)), nil
}
// SetCell sets a single character at the specified index with styling.
func (tb *TextBuffer) SetCell(index uint32, char rune, fg, bg RGBA, attributes uint16) error {
if tb.ptr == nil {
return newError("text buffer is closed")
}
C.textBufferSetCell(tb.ptr, C.uint32_t(index), C.uint32_t(char), fg.toCFloat(), bg.toCFloat(), C.uint16_t(attributes))
return nil
}
// WriteChunk appends a text chunk with optional styling to the buffer.
// Returns the number of characters written.
func (tb *TextBuffer) WriteChunk(chunk TextChunk) (uint32, error) {
if tb.ptr == nil {
return 0, newError("text buffer is closed")
}
textPtr, textLen := stringToC(chunk.Text)
if textPtr == nil {
return 0, nil // Empty string
}
var fgPtr, bgPtr *C.float
var attrPtr *C.uint8_t
if chunk.Foreground != nil {
fgPtr = chunk.Foreground.toCFloat()
}
if chunk.Background != nil {
bgPtr = chunk.Background.toCFloat()
}
if chunk.Attributes != nil {
attrPtr = (*C.uint8_t)(unsafe.Pointer(chunk.Attributes))
}
written := C.textBufferWriteChunk(tb.ptr, textPtr, C.uint32_t(textLen), fgPtr, bgPtr, attrPtr)
return uint32(written), nil
}
// WriteString is a convenience method to write a string with default styling.
func (tb *TextBuffer) WriteString(text string) (uint32, error) {
return tb.WriteChunk(TextChunk{Text: text})
}
// WriteStyledString writes a string with the specified colors and attributes.
func (tb *TextBuffer) WriteStyledString(text string, fg, bg *RGBA, attributes *uint8) (uint32, error) {
return tb.WriteChunk(TextChunk{
Text: text,
Foreground: fg,
Background: bg,
Attributes: attributes,
})
}
// Concat concatenates this text buffer with another text buffer.
// Returns a new text buffer containing the combined content.
func (tb *TextBuffer) Concat(other *TextBuffer) (*TextBuffer, error) {
if tb.ptr == nil {
return nil, newError("text buffer is closed")
}
if other == nil || other.ptr == nil {
return nil, newError("other text buffer is nil or closed")
}
resultPtr := C.textBufferConcat(tb.ptr, other.ptr)
if resultPtr == nil {
return nil, newError("failed to concatenate text buffers")
}
result := &TextBuffer{ptr: resultPtr}
setFinalizer(result, func(tb *TextBuffer) { tb.Close() })
return result, nil
}
// Resize changes the capacity of the text buffer.
func (tb *TextBuffer) Resize(newLength uint32) error {
if tb.ptr == nil {
return newError("text buffer is closed")
}
C.textBufferResize(tb.ptr, C.uint32_t(newLength))
return nil
}
// Reset clears the text buffer content while preserving capacity.
func (tb *TextBuffer) Reset() error {
if tb.ptr == nil {
return newError("text buffer is closed")
}
C.textBufferReset(tb.ptr)
return nil
}
// SetSelection sets a text selection range with optional highlighting colors.
func (tb *TextBuffer) SetSelection(start, end uint32, bgColor, fgColor *RGBA) error {
if tb.ptr == nil {
return newError("text buffer is closed")
}
var bgPtr, fgPtr *C.float
if bgColor != nil {
bgPtr = bgColor.toCFloat()
}
if fgColor != nil {
fgPtr = fgColor.toCFloat()
}
C.textBufferSetSelection(tb.ptr, C.uint32_t(start), C.uint32_t(end), bgPtr, fgPtr)
return nil
}
// ResetSelection clears any active text selection.
func (tb *TextBuffer) ResetSelection() error {
if tb.ptr == nil {
return newError("text buffer is closed")
}
C.textBufferResetSelection(tb.ptr)
return nil
}
// SetDefaultForeground sets the default foreground color for new text.
func (tb *TextBuffer) SetDefaultForeground(fg *RGBA) error {
if tb.ptr == nil {
return newError("text buffer is closed")
}
var fgPtr *C.float
if fg != nil {
fgPtr = fg.toCFloat()
}
C.textBufferSetDefaultFg(tb.ptr, fgPtr)
return nil
}
// SetDefaultBackground sets the default background color for new text.
func (tb *TextBuffer) SetDefaultBackground(bg *RGBA) error {
if tb.ptr == nil {
return newError("text buffer is closed")
}
var bgPtr *C.float
if bg != nil {
bgPtr = bg.toCFloat()
}
C.textBufferSetDefaultBg(tb.ptr, bgPtr)
return nil
}
// SetDefaultAttributes sets the default text attributes for new text.
func (tb *TextBuffer) SetDefaultAttributes(attributes *uint8) error {
if tb.ptr == nil {
return newError("text buffer is closed")
}
var attrPtr *C.uint8_t
if attributes != nil {
attrPtr = (*C.uint8_t)(unsafe.Pointer(attributes))
}
C.textBufferSetDefaultAttributes(tb.ptr, attrPtr)
return nil
}
// ResetDefaults clears all default styling settings.
func (tb *TextBuffer) ResetDefaults() error {
if tb.ptr == nil {
return newError("text buffer is closed")
}
C.textBufferResetDefaults(tb.ptr)
return nil
}
// FinalizeLineInfo processes the text buffer to generate line information.
// This should be called after adding text and before querying line information.
func (tb *TextBuffer) FinalizeLineInfo() error {
if tb.ptr == nil {
return newError("text buffer is closed")
}
C.textBufferFinalizeLineInfo(tb.ptr)
return nil
}
// LineCount returns the number of lines in the text buffer.
// FinalizeLineInfo must be called first.
func (tb *TextBuffer) LineCount() (uint32, error) {
if tb.ptr == nil {
return 0, newError("text buffer is closed")
}
return uint32(C.textBufferGetLineCount(tb.ptr)), nil
}
// GetLineInfo returns information about all lines in the text buffer.
// FinalizeLineInfo must be called first.
func (tb *TextBuffer) GetLineInfo() ([]LineInfo, error) {
if tb.ptr == nil {
return nil, newError("text buffer is closed")
}
lineCount := uint32(C.textBufferGetLineCount(tb.ptr))
if lineCount == 0 {
return []LineInfo{}, nil
}
startsPtr := C.textBufferGetLineStartsPtr(tb.ptr)
widthsPtr := C.textBufferGetLineWidthsPtr(tb.ptr)
starts := cArrayToSlice((*uint32)(startsPtr), int(lineCount))
widths := cArrayToSlice((*uint32)(widthsPtr), int(lineCount))
lines := make([]LineInfo, lineCount)
for i := uint32(0); i < lineCount; i++ {
lines[i] = LineInfo{
StartIndex: starts[i],
Width: widths[i],
}
}
return lines, nil
}
// GetDirectAccess returns direct access to the text buffer's internal arrays.
// This is an advanced feature for performance-critical operations.
func (tb *TextBuffer) GetDirectAccess() (*TextBufferDirectAccess, error) {
if tb.ptr == nil {
return nil, newError("text buffer is closed")
}
length := uint32(C.textBufferGetLength(tb.ptr))
if length == 0 {
return &TextBufferDirectAccess{
Chars: []uint32{},
Foreground: []RGBA{},
Background: []RGBA{},
Attributes: []uint16{},
Length: 0,
}, nil
}
charPtr := C.textBufferGetCharPtr(tb.ptr)
fgPtr := C.textBufferGetFgPtr(tb.ptr)
bgPtr := C.textBufferGetBgPtr(tb.ptr)
attrPtr := C.textBufferGetAttributesPtr(tb.ptr)
return &TextBufferDirectAccess{
Chars: cArrayToSlice((*uint32)(charPtr), int(length)),
Foreground: cArrayToSlice((*RGBA)(unsafe.Pointer(fgPtr)), int(length)),
Background: cArrayToSlice((*RGBA)(unsafe.Pointer(bgPtr)), int(length)),
Attributes: cArrayToSlice((*uint16)(attrPtr), int(length)),
Length: length,
}, nil
}
// TextBufferDirectAccess provides direct access to text buffer internal arrays.
type TextBufferDirectAccess struct {
Chars []uint32 // Character codes (Unicode code points)
Foreground []RGBA // Foreground colors
Background []RGBA // Background colors
Attributes []uint16 // Text attributes
Length uint32 // Buffer length
}
// GetChar returns the character at the specified index.
func (da *TextBufferDirectAccess) GetChar(index uint32) (rune, error) {
if index >= da.Length {
return 0, newError("index out of bounds")
}
return rune(da.Chars[index]), nil
}
// SetChar sets the character at the specified index.
func (da *TextBufferDirectAccess) SetChar(index uint32, char rune) error {
if index >= da.Length {
return newError("index out of bounds")
}
da.Chars[index] = uint32(char)
return nil
}
// GetStyle returns the styling at the specified index.
func (da *TextBufferDirectAccess) GetStyle(index uint32) (RGBA, RGBA, uint16, error) {
if index >= da.Length {
return RGBA{}, RGBA{}, 0, newError("index out of bounds")
}
return da.Foreground[index], da.Background[index], da.Attributes[index], nil
}
// SetStyle sets the styling at the specified index.
func (da *TextBufferDirectAccess) SetStyle(index uint32, fg, bg RGBA, attributes uint16) error {
if index >= da.Length {
return newError("index out of bounds")
}
da.Foreground[index] = fg
da.Background[index] = bg
da.Attributes[index] = attributes
return nil
}
// Valid checks if the text buffer is still valid (not closed).
func (tb *TextBuffer) Valid() bool {
return tb.ptr != nil
}