Skip to content

Commit

Permalink
feat: adaptive colors
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Jul 26, 2022
1 parent b8f05e5 commit 91ce6ab
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
17 changes: 13 additions & 4 deletions main.go
Expand Up @@ -7,6 +7,7 @@ package main
import "C"

import (
"encoding/json"
"reflect"
"unsafe"

Expand Down Expand Up @@ -55,12 +56,20 @@ func Copy(keyPtr *C.char) *C.char {
}

//export SetColorValue
func SetColorValue(fieldPtr, keyPtr, valuePtr *C.char) {
func SetColorValue(fieldPtr, keyPtr, valuePtr *C.char, adaptive bool) {
style := m[str(fieldPtr)]
key := str(keyPtr)
value := lipgloss.Color(str(valuePtr))
color := reflect.ValueOf(value)
reflect.ValueOf(style).MethodByName(key).Call([]reflect.Value{color})

if adaptive {
adaptiveColor := lipgloss.AdaptiveColor{}
json.Unmarshal([]byte(str(valuePtr)), &adaptiveColor)
color := reflect.ValueOf(adaptiveColor)
reflect.ValueOf(style).MethodByName(key).Call([]reflect.Value{color})
} else {
value := lipgloss.Color(str(valuePtr))
color := reflect.ValueOf(value)
reflect.ValueOf(style).MethodByName(key).Call([]reflect.Value{color})
}
}

//export SetIntValue
Expand Down
11 changes: 5 additions & 6 deletions playground.ts
Expand Up @@ -14,13 +14,12 @@ const style = blipgloss.NewStyle()
console.log(style.Render("Hello, bun."))

const style2 = style.Copy()
.Background({
Light: "#2B2D42",
Dark: "#F8F32B"
})
.Width(30)

console.log(style2.Render("Hello, bun 2."))

const style3 = style2.Copy()
.Width(40)

console.log(style3.Render("Hello, bun 3."))
console.log(style2.Render("Hello, bun clone."))

// style.FreeString()
2 changes: 1 addition & 1 deletion src/ffi.ts
Expand Up @@ -26,7 +26,7 @@ export const { symbols } = dlopen(location, {
returns: FFIType.ptr
},
SetColorValue: {
args: [FFIType.ptr, FFIType.ptr, FFIType.ptr],
args: [FFIType.ptr, FFIType.ptr, FFIType.ptr, FFIType.bool],
returns: FFIType.void
},
SetIntValue: {
Expand Down
9 changes: 7 additions & 2 deletions src/lib.ts
Expand Up @@ -3,7 +3,10 @@ import { symbols } from './ffi'
import { encode } from './utils'

// type Pointer = number
type BlipglossColor = string
type BlipglossColor = string | {
Light: string
Dark: string
}

export class Style {
#handle: number
Expand All @@ -17,7 +20,9 @@ export class Style {
}

private SetColorValue(key: string, value: BlipglossColor) {
symbols.SetColorValue(this.#handle, ptr(encode(key)), ptr(encode(value)))
const adaptive = typeof value !== 'string'
const color = adaptive ? ptr(encode(JSON.stringify(value))) : ptr(encode(value))
symbols.SetColorValue(this.#handle, ptr(encode(key)), color, adaptive)
return this
}

Expand Down

0 comments on commit 91ce6ab

Please sign in to comment.