Skip to content

Commit

Permalink
fix: whitespace methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Sep 17, 2023
1 parent fe76be6 commit 759fe19
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 63 deletions.
1 change: 1 addition & 0 deletions build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ if (output.success) {
console.log('Compiling native binaries...')
const proc = Bun.spawnSync([
XGO,
"-go", "1.20.3",
"-out", "release/blipgloss",
"--targets=" + TARGETS,
"-ldflags=-s -w",
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ require (
require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
golang.org/x/sys v0.10.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.7.0 // indirect
)
17 changes: 7 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
github.com/charmbracelet/lipgloss v0.8.0 h1:IS00fk4XAHcf8uZKc3eHeMUTCxUH6NkaTrdyCQk84RU=
github.com/charmbracelet/lipgloss v0.8.0/go.mod h1:p4eYUZZJ/0oXTuCQKFF8mqyKCz0ja6y+7DniDDw5KKU=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/jaevor/go-nanoid v1.3.0 h1:nD+iepesZS6pr3uOVf20vR9GdGgJW1HPaR46gtrxzkg=
github.com/jaevor/go-nanoid v1.3.0/go.mod h1:SI+jFaPuddYkqkVQoNGHs81navCtH388TcrH0RqFKgY=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
74 changes: 59 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

var styleMap map[string]lipgloss.Style = map[string]lipgloss.Style{}
var whitespaceMap map[string]lipgloss.WhitespaceOption = map[string]lipgloss.WhitespaceOption{}

func main() {}

Expand Down Expand Up @@ -66,45 +67,74 @@ func Copy(keyPtr *C.char) *C.char {
return ch(uniqueId)
}

//export SetColorValue
func SetColorValue(fieldPtr, keyPtr, valuePtr *C.char, colorType int) {
style := getStyle(fieldPtr)
key := str(keyPtr)
//export WithWhitespaceChars
func WithWhitespaceChars(strPtr *C.char) *C.char {
canonic, _ := nanoid.Standard(10)
uniqueId := canonic()
whitespaceMap[uniqueId] = lipgloss.WithWhitespaceChars(str(strPtr))
return ch(uniqueId)
}

//export WithWhitespaceBackground
func WithWhitespaceBackground(valuePtr *C.char, colorType int) *C.char {
canonic, _ := nanoid.Standard(10)
uniqueId := canonic()
color := SetColor(str(valuePtr), colorType)
whitespaceMap[uniqueId] = lipgloss.WithWhitespaceBackground(color)
return ch(uniqueId)
}

//export WithWhitespaceForeground
func WithWhitespaceForeground(valuePtr *C.char, colorType int) *C.char {
canonic, _ := nanoid.Standard(10)
uniqueId := canonic()
color := SetColor(str(valuePtr), colorType)
whitespaceMap[uniqueId] = lipgloss.WithWhitespaceForeground(color)
return ch(uniqueId)
}

var color reflect.Value
func SetColor(value string, colorType int) lipgloss.TerminalColor {
var color lipgloss.TerminalColor

switch colorType {
case 1:
color = reflect.ValueOf(lipgloss.Color(str(valuePtr)))
color = lipgloss.Color(value)
case 2:
adaptiveColor := lipgloss.AdaptiveColor{}
err := json.Unmarshal([]byte(str(valuePtr)), &adaptiveColor)
err := json.Unmarshal([]byte(value), &adaptiveColor)

if err != nil {
panic("Unable to parse adaptive color")
}

color = reflect.ValueOf(adaptiveColor)
color = adaptiveColor
case 3:
completeColor := lipgloss.CompleteColor{}
err := json.Unmarshal([]byte(str(valuePtr)), &completeColor)
err := json.Unmarshal([]byte(value), &completeColor)

if err != nil {
panic("Unable to parse complete color")
}

color = reflect.ValueOf(completeColor)
color = completeColor
}
return color
}

reflect.ValueOf(style).MethodByName(key).Call([]reflect.Value{color})
//export SetColorValue
func SetColorValue(fieldPtr, keyPtr, valuePtr *C.char, colorType int) {
style := getStyle(fieldPtr)
method := str(keyPtr)

color := reflect.ValueOf(SetColor(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)
key := str(keyPtr)
method := str(keyPtr)
intValue := reflect.ValueOf(value)
reflect.ValueOf(style).MethodByName(key).Call([]reflect.Value{intValue})
reflect.ValueOf(style).MethodByName(method).Call([]reflect.Value{intValue})
}

//export SetStringValue
Expand Down Expand Up @@ -299,3 +329,17 @@ func Inherit(fieldPtr, stylePtr *C.char) {
styleToInherit := getStyle(stylePtr)
style.Inherit(styleToInherit)
}

//export Place
func Place(width, height int, hPos, vPos float64, strPtr, whitespaceOptionPtr *C.char) *C.char {
whitespaceOption := strings.Split(str(whitespaceOptionPtr), ",")
var convertedOptions []lipgloss.WhitespaceOption

// Loop through the array and convert the whitespace options
for _, optionStr := range whitespaceOption {
convertedOptions = append(convertedOptions, whitespaceMap[optionStr])
}

joined := lipgloss.Place(width, height, lipgloss.Position(hPos), lipgloss.Position(vPos), str(strPtr), convertedOptions...)
return ch(joined)
}
16 changes: 16 additions & 0 deletions src/ffi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,20 @@ export const { symbols } = dlopen(location, {
args: [FFIType.ptr, FFIType.ptr],
returns: FFIType.void
},
WithWhitespaceChars: {
args: [FFIType.ptr],
returns: FFIType.ptr
},
WithWhitespaceBackground: {
args: [FFIType.ptr, FFIType.int],
returns: FFIType.ptr
},
WithWhitespaceForeground: {
args: [FFIType.ptr, FFIType.int],
returns: FFIType.ptr
},
Place: {
args: [FFIType.int, FFIType.int, FFIType.f64, FFIType.f64, FFIType.ptr, FFIType.ptr],
returns: FFIType.ptr
}
})
78 changes: 56 additions & 22 deletions src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CString, ptr } from 'bun:ffi'
import { CString, Pointer, ptr } from 'bun:ffi'
import { symbols } from './ffi'
import { encode, whichSidesBool } from './utils'

Expand Down Expand Up @@ -342,21 +342,6 @@ export class Style {
return this.SetIntValue('MaxHeight', val)
}

WithWhitespaceBackground(color: BlipglossColor) {
return this.SetColorValue('WithWhitespaceBackground', color)
}

WithWhitespaceForeground(color: BlipglossColor) {
return this.SetColorValue('WithWhitespaceForeground', color)
}

/**
* Sets the characters to be rendered in the whitespace.
*/
WithWhitespaceChars(char: string) {
return this.SetStringValue('WithWhitespaceChars', char)
}

Inherit(style: Style) {
symbols.Inherit(this.#handle, ptr(encode(style.#handle)))
return this
Expand Down Expand Up @@ -384,18 +369,24 @@ function combineArgs(args: string[]) {
return args.join(",");
}

export function JoinHorizontal(position: Position | number, ...paragraphs: string[]) {
const textPtr = symbols.JoinHorizontal(position, ptr(encode(combineArgs(paragraphs))))
const textStr = new CString(textPtr!)
function getStringAndFreePtr(textPtr: Pointer | null) {
if (!textPtr) {
throw new Error('Pointer required')
}

const textStr = new CString(textPtr)
symbols.FreeString(textStr.ptr)
return textStr.toString()
}

export function JoinHorizontal(position: Position | number, ...paragraphs: string[]) {
const textPtr = symbols.JoinHorizontal(position, ptr(encode(combineArgs(paragraphs))))
return getStringAndFreePtr(textPtr)
}

export function JoinVertical(position: Position | number, ...paragraphs: string[]) {
const textPtr = symbols.JoinVertical(position, ptr(encode(combineArgs(paragraphs))))
const textStr = new CString(textPtr!)
symbols.FreeString(textStr.ptr)
return textStr.toString()
return getStringAndFreePtr(textPtr)
}

export function Width(text: string): number {
Expand All @@ -405,3 +396,46 @@ export function Width(text: string): number {
export function Height(text: string): number {
return symbols.Height(ptr(encode(text)))
}

function getColor(value: BlipglossColor) {
const isObject = typeof value !== 'string'

if (isObject) {
if ('Light' in value) {
// Adaptive color
return 2
}

// Complete color
return 3
}

return 1
}

export function WithWhitespaceBackground(value: BlipglossColor) {
const isObject = typeof value !== 'string'
const colorType = getColor(value)
const color = isObject ? ptr(encode(JSON.stringify(value))) : ptr(encode(value))
const textPtr = symbols.WithWhitespaceBackground(color, colorType)
return getStringAndFreePtr(textPtr)
}

export function WithWhitespaceForeground(value: BlipglossColor) {
const isObject = typeof value !== 'string'
const colorType = getColor(value)
const color = isObject ? ptr(encode(JSON.stringify(value))) : ptr(encode(value))
const textPtr = symbols.WithWhitespaceForeground(color, colorType)
return getStringAndFreePtr(textPtr)
}

export function WithWhitespaceChars(char: string) {
const textPtr = symbols.WithWhitespaceChars(ptr(encode(char)))
return getStringAndFreePtr(textPtr)
}

export function Place(width: number, height: number, hPos: number, vPos: number, str: string, ...whitespaceOptions: string[]) {
const combinedOptions = whitespaceOptions.join(',')
const textPtr = symbols.Place(width, height, hPos, vPos, ptr(encode(str)), ptr(encode(combinedOptions)))
return getStringAndFreePtr(textPtr);
}
12 changes: 0 additions & 12 deletions test/index.test.ts

This file was deleted.

0 comments on commit 759fe19

Please sign in to comment.