Skip to content

Commit

Permalink
feat: add Border method
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Sep 16, 2023
1 parent 37a8d32 commit e8a388d
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
56 changes: 56 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,62 @@ func Padding(fieldPtr *C.char, paddings *C.char) {
style.Padding(arr...)
}

func whichSidesBool(i ...bool) (top, right, bottom, left bool, ok bool) {
switch len(i) {
case 1:
top = i[0]
bottom = i[0]
left = i[0]
right = i[0]
ok = true
case 2:
top = i[0]
bottom = i[0]
left = i[1]
right = i[1]
ok = true
case 3:
top = i[0]
left = i[1]
right = i[1]
bottom = i[2]
ok = true
case 4:
top = i[0]
right = i[1]
bottom = i[2]
left = i[3]
ok = true
}
return top, right, bottom, left, ok
}

//export Border
func Border(fieldPtr, borderStylePtr *C.char, sides ...bool) {
style := getStyle(fieldPtr)
borderStyle := str(borderStylePtr)

top, right, bottom, left, ok := whichSidesBool(sides...)
if !ok {
top = true
right = true
bottom = true
left = true
}

if borderStyle == "rounded" {
style.Border(lipgloss.RoundedBorder(), top, right, bottom, left)
} else if borderStyle == "double" {
style.Border(lipgloss.DoubleBorder(), top, right, bottom, left)
} else if borderStyle == "normal" {
style.Border(lipgloss.NormalBorder(), top, right, bottom, left)
} else if borderStyle == "hidden" {
style.Border(lipgloss.HiddenBorder(), top, right, bottom, left)
} else if borderStyle == "thick" {
style.Border(lipgloss.ThickBorder(), top, right, bottom, left)
}
}

//export BorderStyle
func BorderStyle(fieldPtr, borderStylePtr *C.char) {
style := getStyle(fieldPtr)
Expand Down
2 changes: 1 addition & 1 deletion playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const divider = blipgloss.NewStyle()
.SetString('.')
.Padding(0, 11)
.Foreground(subtle)
.String(subtle)
.String()

const url = blipgloss.NewStyle().Foreground(special).Render

Expand Down
4 changes: 4 additions & 0 deletions src/ffi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export const { symbols } = dlopen(location, {
args: [FFIType.ptr, FFIType.ptr],
returns: FFIType.void
},
Border: {
args: [FFIType.ptr, FFIType.ptr, FFIType.bool, FFIType.bool, FFIType.bool, FFIType.bool],
returns: FFIType.void
},
BorderStyle: {
args: [FFIType.ptr, FFIType.ptr],
returns: FFIType.void
Expand Down
15 changes: 14 additions & 1 deletion src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CString, ptr } from 'bun:ffi'
import { symbols } from './ffi'
import { encode } from './utils'
import { encode, whichSidesBool } from './utils'

export type AdaptiveColor = {
Light: string
Expand Down Expand Up @@ -202,6 +202,19 @@ export class Style {
return this.SetColorValue('BorderRightForeground', color)
}

Border(style: BorderStyle, ...args: boolean[]) {
let [top, right, bottom, left, ok] = whichSidesBool(...args)

if (!ok) {
top = true
right = true
bottom = true
left = true
}

symbols.Border(this.#handle, ptr(encode(style)), top, right, bottom, left)
}

BorderStyle(style: BorderStyle) {
if (typeof style === 'string') {
symbols.BorderStyle(this.#handle, ptr(encode(style)))
Expand Down
37 changes: 37 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,40 @@ export function toString(ptr: any): string {
symbols.FreeString(str.ptr)
return str.toString()
}

export function whichSidesBool(...args: boolean[]) {
let [top, right, bottom, left, ok] = [false, false, false, false, false];

switch (args.length) {
case 1:
top = args[0];
bottom = args[0];
left = args[0];
right = args[0];
ok = true;
break;
case 2:
top = args[0];
bottom = args[0];
left = args[1];
right = args[1];
ok = true;
break;
case 3:
top = args[0];
left = args[1];
right = args[1];
bottom = args[2];
ok = true;
break;
case 4:
top = args[0];
right = args[1];
bottom = args[2];
left = args[3];
ok = true;
break;
}

return [top, right, bottom, left, ok];
}

0 comments on commit e8a388d

Please sign in to comment.