Skip to content

Commit

Permalink
feat: custom borders
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Aug 1, 2022
1 parent 3363fd3 commit 7c56c14
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
13 changes: 12 additions & 1 deletion README.md
Expand Up @@ -169,7 +169,18 @@ const anotherStyle = NewStyle()
.BorderTop(true).
.BorderLeft(true)

// TODO: Make own border
// Make your own border
const style = NewStyle()
.BorderStyle({
Top: "._.:*:",
Bottom: "._.:*:",
Left: "|*",
Right: "|*",
TopLeft: "*",
TopRight: "*",
BottomLeft: "*",
BottomRight: "*",
})
```

## Copying Styles
Expand Down
13 changes: 13 additions & 0 deletions main.go
Expand Up @@ -206,3 +206,16 @@ func BorderStyle(fieldPtr, borderStylePtr *C.char) {
style.BorderStyle(lipgloss.ThickBorder())
}
}

//export CustomBorder
func CustomBorder(fieldPtr, valuePtr *C.char) {
style := getStyle(fieldPtr)
border := lipgloss.Border{}
err := json.Unmarshal([]byte(str(valuePtr)), &border)

if err != nil {
panic("Unable to parse custom border")
}

style.BorderStyle(border)
}
11 changes: 10 additions & 1 deletion playground.ts
Expand Up @@ -12,7 +12,16 @@ const style = blipgloss.NewStyle()
.Width(30)
.Padding(1, 4, 2)
.Align(blipgloss.Position.Right)
.BorderStyle('rounded')
.BorderStyle({
Top: "._.:*:",
Bottom: "._.:*:",
Left: "|*",
Right: "|*",
TopLeft: "*",
TopRight: "*",
BottomLeft: "*",
BottomRight: "*",
})

// console.log(style.Render("Hello, bun."))

Expand Down
4 changes: 4 additions & 0 deletions src/ffi.ts
Expand Up @@ -89,6 +89,10 @@ export const { symbols } = dlopen(location, {
args: [FFIType.ptr, FFIType.ptr],
returns: FFIType.void
},
CustomBorder: {
args: [FFIType.ptr, FFIType.ptr],
returns: FFIType.void
},
FreeString: {
args: [FFIType.ptr],
returns: FFIType.void
Expand Down
19 changes: 17 additions & 2 deletions src/lib.ts
Expand Up @@ -16,7 +16,18 @@ export enum Position {
Right = 1.0
}

type BorderStyle = 'rounded' | 'double' | 'normal' | 'hidden' | 'thick'
export type CustomBorder = {
Top?: string
Bottom?: string
Left?: string
Right?: string
TopLeft?: string
TopRight?: string
BottomLeft?: string
BottomRight?: string
}

export type BorderStyle = 'rounded' | 'double' | 'normal' | 'hidden' | 'thick' | CustomBorder

export class Style {
#handle: number
Expand Down Expand Up @@ -158,7 +169,11 @@ export class Style {
}

BorderStyle(style: BorderStyle) {
symbols.BorderStyle(this.#handle, ptr(encode(style)))
if (typeof style === 'string') {
symbols.BorderStyle(this.#handle, ptr(encode(style)))
} else {
symbols.CustomBorder(this.#handle, ptr(encode(JSON.stringify(style))))
}
return this
}

Expand Down

0 comments on commit 7c56c14

Please sign in to comment.