Skip to content

Commit

Permalink
feat: add joining paragraphs util
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Jul 27, 2022
1 parent 9973a4c commit d9017cf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions main.go
Expand Up @@ -9,6 +9,7 @@ import "C"
import (
"encoding/json"
"reflect"
"strings"
"unsafe"

"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -122,3 +123,17 @@ func GetBoolValue(fieldPtr, keyPtr *C.char) bool {
func HasDarkBackground() bool {
return lipgloss.HasDarkBackground()
}

//export JoinHorizontal
func JoinHorizontal(position int, paragraphs *C.char) *C.char {
arr := strings.Split(str(paragraphs), ",")
joined := lipgloss.JoinHorizontal(lipgloss.Position(position), arr...)
return ch(joined)
}

//export JoinVertical
func JoinVertical(position int, paragraphs *C.char) *C.char {
arr := strings.Split(str(paragraphs), ",")
joined := lipgloss.JoinVertical(lipgloss.Position(position), arr...)
return ch(joined)
}
8 changes: 8 additions & 0 deletions src/ffi.ts
Expand Up @@ -57,6 +57,14 @@ export const { symbols } = dlopen(location, {
args: [FFIType.ptr, FFIType.ptr],
returns: FFIType.void
},
JoinHorizontal: {
args: [FFIType.int, FFIType.ptr],
returns: FFIType.ptr
},
JoinVertical: {
args: [FFIType.int, FFIType.ptr],
returns: FFIType.ptr
},
FreeString: {
args: [FFIType.ptr],
returns: FFIType.void
Expand Down
24 changes: 24 additions & 0 deletions src/lib.ts
Expand Up @@ -8,6 +8,14 @@ type BlipglossColor = string | {
Dark: string
}

export enum Position {
Top = 0.0,
Bottom = 1.0,
Center = 0.5,
Left = 0.0,
Right = 1.0
}

export class Style {
#handle: number

Expand Down Expand Up @@ -230,6 +238,22 @@ export class Style {
UnsetBorderForeground() {
return this.UnsetRule('UnsetBorderForeground')
}

// Utilities

JoinHorizontal(position: Position, ...paragraphs: string[]) {
const textPtr = symbols.JoinHorizontal(position, ptr(encode(JSON.stringify(paragraphs))))
const textStr = new CString(textPtr)
symbols.FreeString(textStr.ptr)
return textStr
}

JoinVertical(position: Position, ...paragraphs: string[]) {
const textPtr = symbols.JoinVertical(position, ptr(encode(JSON.stringify(paragraphs))))
const textStr = new CString(textPtr)
symbols.FreeString(textStr.ptr)
return textStr
}
}

export function NewStyle() {
Expand Down

0 comments on commit d9017cf

Please sign in to comment.