-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
339 lines (275 loc) Β· 8.3 KB
/
main.go
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 main
/*
#include <stdlib.h>
#include <string.h>
*/
import "C"
import (
"encoding/json"
"reflect"
"strings"
"unsafe"
"github.com/charmbracelet/lipgloss"
"github.com/jaevor/go-nanoid"
)
var styleMap map[string]lipgloss.Style = map[string]lipgloss.Style{}
var whitespaceMap map[string]lipgloss.WhitespaceOption = map[string]lipgloss.WhitespaceOption{}
func main() {}
func ch(str string) *C.char {
return C.CString(str)
}
func str(ch *C.char) string {
return C.GoString(ch)
}
//export FreeString
func FreeString(str *C.char) {
C.free(unsafe.Pointer(str))
}
func getStyle(fieldPtr *C.char) lipgloss.Style {
return styleMap[str(fieldPtr)]
}
func generateUniqueId() string {
canonic, _ := nanoid.Standard(10)
return canonic()
}
//export NewStyle
func NewStyle() *C.char {
uniqueId := generateUniqueId()
styleMap[uniqueId] = lipgloss.NewStyle().Copy()
return ch(uniqueId)
}
//export Render
func Render(keyPtr *C.char, text *C.char) *C.char {
style := getStyle(keyPtr)
return ch(style.Render(str(text)))
}
//export String
func String(keyPtr *C.char) *C.char {
key := str(keyPtr)
return ch(styleMap[key].String())
}
//export Copy
func Copy(keyPtr *C.char) *C.char {
key := str(keyPtr)
uniqueId := generateUniqueId()
styleMap[uniqueId] = styleMap[key].Copy()
return ch(uniqueId)
}
//export WithWhitespaceChars
func WithWhitespaceChars(strPtr *C.char) *C.char {
uniqueId := generateUniqueId()
whitespaceMap[uniqueId] = lipgloss.WithWhitespaceChars(str(strPtr))
return ch(uniqueId)
}
//export WithWhitespaceBackground
func WithWhitespaceBackground(valuePtr *C.char, colorType int) *C.char {
uniqueId := generateUniqueId()
color := GetColorByType(str(valuePtr), colorType)
whitespaceMap[uniqueId] = lipgloss.WithWhitespaceBackground(color)
return ch(uniqueId)
}
//export WithWhitespaceForeground
func WithWhitespaceForeground(valuePtr *C.char, colorType int) *C.char {
uniqueId := generateUniqueId()
color := GetColorByType(str(valuePtr), colorType)
whitespaceMap[uniqueId] = lipgloss.WithWhitespaceForeground(color)
return ch(uniqueId)
}
func GetColorByType(value string, colorType int) lipgloss.TerminalColor {
var color lipgloss.TerminalColor
switch colorType {
case 1:
color = lipgloss.Color(value)
case 2:
adaptiveColor := lipgloss.AdaptiveColor{}
err := json.Unmarshal([]byte(value), &adaptiveColor)
if err != nil {
panic("Unable to parse adaptive color")
}
color = adaptiveColor
case 3:
completeColor := lipgloss.CompleteColor{}
err := json.Unmarshal([]byte(value), &completeColor)
if err != nil {
panic("Unable to parse complete color")
}
color = completeColor
case 4:
completeAdaptiveColor := lipgloss.CompleteAdaptiveColor{}
err := json.Unmarshal([]byte(value), &completeAdaptiveColor)
if err != nil {
panic("Unable to parse complete adaptive color")
}
color = completeAdaptiveColor
}
return color
}
//export SetColorValue
func SetColorValue(fieldPtr, keyPtr, valuePtr *C.char, colorType int) {
style := getStyle(fieldPtr)
method := str(keyPtr)
color := reflect.ValueOf(GetColorByType(str(valuePtr), colorType))
reflect.ValueOf(style).MethodByName(method).Call([]reflect.Value{color})
}
//export SetIntValue
func SetIntValue(fieldPtr, keyPtr *C.char, value int) {
style := getStyle(fieldPtr)
method := str(keyPtr)
intValue := reflect.ValueOf(value)
reflect.ValueOf(style).MethodByName(method).Call([]reflect.Value{intValue})
}
//export SetString
func SetString(fieldPtr, valuePtr *C.char) {
style := getStyle(fieldPtr)
style.SetString(str(valuePtr))
}
//export SetBooleanValue
func SetBooleanValue(fieldPtr, keyPtr *C.char, value bool) {
style := getStyle(fieldPtr)
key := str(keyPtr)
boolValue := reflect.ValueOf(value)
reflect.ValueOf(style).MethodByName(key).Call([]reflect.Value{boolValue})
}
//export UnsetRule
func UnsetRule(fieldPtr, keyPtr *C.char) {
style := getStyle(fieldPtr)
key := str(keyPtr)
reflect.ValueOf(style).MethodByName(key).Call([]reflect.Value{})
}
//export GetIntValue
func GetIntValue(fieldPtr, keyPtr *C.char) int {
style := getStyle(fieldPtr)
key := str(keyPtr)
return reflect.ValueOf(style).MethodByName(key).Interface().(int)
}
//export GetBoolValue
func GetBoolValue(fieldPtr, keyPtr *C.char) bool {
style := getStyle(fieldPtr)
key := str(keyPtr)
return reflect.ValueOf(style).MethodByName(key).Interface().(bool)
}
//export HasDarkBackground
func HasDarkBackground() bool {
return lipgloss.HasDarkBackground()
}
//export JoinHorizontal
func JoinHorizontal(position float64, paragraphs *C.char) *C.char {
var arr []string
err := json.Unmarshal([]byte(str(paragraphs)), &arr)
if err != nil {
panic("Unable to parse paragraphs")
}
joined := lipgloss.JoinHorizontal(lipgloss.Position(position), arr...)
return ch(joined)
}
//export JoinVertical
func JoinVertical(position float64, paragraphs *C.char) *C.char {
var arr []string
err := json.Unmarshal([]byte(str(paragraphs)), &arr)
if err != nil {
panic("Unable to parse paragraphs")
}
joined := lipgloss.JoinVertical(lipgloss.Position(position), arr...)
return ch(joined)
}
//export Width
func Width(text *C.char) int {
return lipgloss.Width(str(text))
}
//export Height
func Height(text *C.char) int {
return lipgloss.Height(str(text))
}
//export Align
func Align(fieldPtr *C.char, position float64) {
style := getStyle(fieldPtr)
style.Align(lipgloss.Position(position))
}
//export Margin
func Margin(fieldPtr *C.char, margins *C.char) {
style := getStyle(fieldPtr)
var arr []int
err := json.Unmarshal([]byte(str(margins)), &arr)
if err != nil {
panic("Unable to parse margins")
}
style.Margin(arr...)
}
//export Padding
func Padding(fieldPtr *C.char, paddings *C.char) {
style := getStyle(fieldPtr)
var arr []int
err := json.Unmarshal([]byte(str(paddings)), &arr)
if err != nil {
panic("Unable to parse paddings")
}
style.Padding(arr...)
}
func GetBorderStyleByPointerValue(valuePtr *C.char) lipgloss.Border {
var border lipgloss.Border
value := str(valuePtr)
if value == "rounded" {
border = lipgloss.RoundedBorder()
} else if value == "double" {
border = lipgloss.DoubleBorder()
} else if value == "normal" {
border = lipgloss.NormalBorder()
} else if value == "hidden" {
border = lipgloss.HiddenBorder()
} else if value == "thick" {
border = lipgloss.ThickBorder()
} else {
jsonBorder := lipgloss.Border{}
err := json.Unmarshal([]byte(value), &jsonBorder)
if err != nil {
panic("Unable to parse custom border")
}
border = jsonBorder
}
return border
}
//export Border
func Border(fieldPtr, valuePtr *C.char, top, right, bottom, left bool) {
style := getStyle(fieldPtr)
border := GetBorderStyleByPointerValue(valuePtr)
style.Border(border, top, right, bottom, left)
}
//export BorderStyle
func BorderStyle(fieldPtr, valuePtr *C.char) {
style := getStyle(fieldPtr)
border := GetBorderStyleByPointerValue(valuePtr)
style.BorderStyle(border)
}
//export Inherit
func Inherit(fieldPtr, stylePtr *C.char) {
style := getStyle(fieldPtr)
styleToInherit := getStyle(stylePtr)
style.Inherit(styleToInherit)
}
func ConvertStringsToWhitespaceOptions(strPtr *C.char) []lipgloss.WhitespaceOption {
whitespaceOptionKeys := strings.Split(str(strPtr), ",")
var convertedOptions []lipgloss.WhitespaceOption
// Loop through the array and convert the whitespace options
for _, optionStr := range whitespaceOptionKeys {
convertedOptions = append(convertedOptions, whitespaceMap[optionStr])
}
return convertedOptions
}
//export Place
func Place(width, height int, hPos, vPos float64, strPtr, whitespaceOptionPtr *C.char) *C.char {
whitespaceOptions := ConvertStringsToWhitespaceOptions(whitespaceOptionPtr)
joined := lipgloss.Place(width, height, lipgloss.Position(hPos), lipgloss.Position(vPos), str(strPtr), whitespaceOptions...)
return ch(joined)
}
//export PlaceHorizontal
func PlaceHorizontal(width int, pos float64, strPtr, whitespaceOptionPtr *C.char) *C.char {
whitespaceOptions := ConvertStringsToWhitespaceOptions(whitespaceOptionPtr)
joined := lipgloss.PlaceHorizontal(width, lipgloss.Position(pos), str(strPtr), whitespaceOptions...)
return ch(joined)
}
//export PlaceVertical
func PlaceVertical(height int, pos float64, strPtr, whitespaceOptionPtr *C.char) *C.char {
whitespaceOptions := ConvertStringsToWhitespaceOptions(whitespaceOptionPtr)
joined := lipgloss.PlaceVertical(height, lipgloss.Position(pos), str(strPtr), whitespaceOptions...)
return ch(joined)
}