forked from anomalyco/opentui
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes.go
More file actions
215 lines (185 loc) · 4.61 KB
/
Copy pathtypes.go
File metadata and controls
215 lines (185 loc) · 4.61 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
package opentui
/*
#include "opentui.h"
*/
import "C"
import (
"runtime"
"unsafe"
)
// Cell represents a single terminal cell with character, colors, and attributes
type Cell struct {
Char rune // Unicode character
Foreground RGBA // Foreground color
Background RGBA // Background color
Attributes uint8 // Text attributes (bold, italic, etc.)
}
// Text attributes constants
const (
AttrBold uint8 = 1 << 0
AttrDim uint8 = 1 << 1
AttrItalic uint8 = 1 << 2
AttrUnderline uint8 = 1 << 3
AttrBlink uint8 = 1 << 4
AttrReverse uint8 = 1 << 5
AttrStrike uint8 = 1 << 6
)
// ClipRect defines a rectangular clipping region
type ClipRect struct {
X int32
Y int32
Width uint32
Height uint32
}
// Stats holds renderer statistics
type Stats struct {
Time float64
FPS uint32
FrameCallbackTime float64
}
// MemoryStats holds memory usage statistics
type MemoryStats struct {
HeapUsed uint32
HeapTotal uint32
ArrayBuffers uint32
}
// BoxOptions holds options for drawing boxes
type BoxOptions struct {
Sides BorderSides
Fill bool
Title string
TitleAlignment TextAlignment
BorderChars [8]rune // Top-left, top, top-right, right, bottom-right, bottom, bottom-left, left
}
// DefaultBoxChars provides default Unicode box drawing characters
var DefaultBoxChars = [8]rune{
'┌', '─', '┐',
'│', '│',
'└', '─', '┘',
}
// SuperSampleFormat defines pixel formats for super-sampling
type SuperSampleFormat uint8
const (
FormatRGBA SuperSampleFormat = iota
FormatRGB
FormatBGRA
FormatBGR
)
// TextChunk represents a styled text fragment
type TextChunk struct {
Text string
Foreground *RGBA
Background *RGBA
Attributes *uint8
}
// LineInfo represents information about a line in a text buffer
type LineInfo struct {
StartIndex uint32
Width uint32
}
// HitTestResult represents the result of a mouse hit test
type HitTestResult struct {
ID uint32
Found bool
}
// Error represents an OpenTUI error
type Error struct {
Message string
}
func (e *Error) Error() string {
return e.Message
}
// newError creates a new OpenTUI error
func newError(msg string) error {
return &Error{Message: msg}
}
// finalizer is a helper to set up automatic cleanup for CGO objects
func setFinalizer[T any](obj *T, cleanup func(*T)) {
if obj != nil {
runtime.SetFinalizer(obj, func(o *T) { cleanup(o) })
}
}
// clearFinalizer removes the finalizer from an object
func clearFinalizer[T any](obj *T) {
if obj != nil {
runtime.SetFinalizer(obj, nil)
}
}
// sliceToC converts a Go slice to C array parameters
func sliceToC[T any](slice []T) (*T, C.size_t) {
if len(slice) == 0 {
return nil, 0
}
return (*T)(unsafe.Pointer(&slice[0])), C.size_t(len(slice))
}
// cArrayToSlice converts a C array to a Go slice (read-only view)
func cArrayToSlice[T any](ptr *T, length int) []T {
if ptr == nil || length == 0 {
return nil
}
return unsafe.Slice(ptr, length)
}
// runesToC converts a rune slice to uint32 C array
func runesToC(runes []rune) *C.uint32_t {
if len(runes) == 0 {
return nil
}
// Convert runes to uint32
uint32s := make([]uint32, len(runes))
for i, r := range runes {
uint32s[i] = uint32(r)
}
return (*C.uint32_t)(unsafe.Pointer(&uint32s[0]))
}
// Position represents a 2D coordinate
type Position struct {
X int32
Y int32
}
// Size represents 2D dimensions
type Size struct {
Width uint32
Height uint32
}
// Rect combines position and size
type Rect struct {
Position
Size
}
// Contains checks if a point is inside the rectangle
func (r Rect) Contains(x, y int32) bool {
return x >= r.X && x < r.X+int32(r.Width) &&
y >= r.Y && y < r.Y+int32(r.Height)
}
// Overlaps checks if two rectangles overlap
func (r Rect) Overlaps(other Rect) bool {
return r.X < other.X+int32(other.Width) &&
r.X+int32(r.Width) > other.X &&
r.Y < other.Y+int32(other.Height) &&
r.Y+int32(r.Height) > other.Y
}
// MouseEvent represents a mouse interaction
type MouseEvent struct {
Position Position
Button uint8
Pressed bool
}
// KeyEvent represents a keyboard interaction
type KeyEvent struct {
Key rune
Modifiers uint8
}
// Key modifier constants
const (
ModShift uint8 = 1 << 0
ModCtrl uint8 = 1 << 1
ModAlt uint8 = 1 << 2
ModSuper uint8 = 1 << 3
)
// Capabilities represents terminal capabilities
type Capabilities struct {
SupportsTruecolor bool // Terminal supports 24-bit color
SupportsMouse bool // Terminal supports mouse events
SupportsKittyKeyboard bool // Terminal supports Kitty keyboard protocol
SupportsAlternateScreen bool // Terminal supports alternate screen buffer
}