Skip to content

Commit

Permalink
feat: add Inherit method
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Sep 16, 2023
1 parent 3c50b41 commit 9ff28d9
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 6 deletions.
19 changes: 13 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/jaevor/go-nanoid"
)

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

func main() {}

Expand All @@ -34,35 +34,35 @@ func FreeString(str *C.char) {
}

func getStyle(fieldPtr *C.char) lipgloss.Style {
return m[str(fieldPtr)]
return styleMap[str(fieldPtr)]
}

//export NewStyle
func NewStyle() *C.char {
canonic, _ := nanoid.Standard(10)
uniqueId := canonic()
m[uniqueId] = lipgloss.NewStyle().Copy()
styleMap[uniqueId] = lipgloss.NewStyle().Copy()
return ch(uniqueId)
}

//export Render
func Render(keyPtr *C.char, text *C.char) *C.char {
key := str(keyPtr)
return ch(m[key].Render(str(text)))
return ch(styleMap[key].Render(str(text)))
}

//export String
func String(keyPtr *C.char) *C.char {
key := str(keyPtr)
return ch(m[key].String())
return ch(styleMap[key].String())
}

//export Copy
func Copy(keyPtr *C.char) *C.char {
key := str(keyPtr)
canonic, _ := nanoid.Standard(10)
uniqueId := canonic()
m[uniqueId] = m[key].Copy()
styleMap[uniqueId] = styleMap[key].Copy()
return ch(uniqueId)
}

Expand Down Expand Up @@ -292,3 +292,10 @@ func CustomBorder(fieldPtr, valuePtr *C.char) {

style.BorderStyle(border)
}

// export Inherit
func Inherit(fieldPtr, stylePtr *C.char) {
style := getStyle(fieldPtr)
styleToInherit := getStyle(stylePtr)
style.Inherit(styleToInherit)
}
114 changes: 114 additions & 0 deletions playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,117 @@ const tabBorder: CustomBorder = {
BottomLeft: "┴",
BottomRight: "┴",
}

const tab = blipgloss.NewStyle()
.Border(tabBorder, true)
.BorderForeground(highlight)
.Padding(0, 1)

const activeTab = tab.Copy().Border(activeTabBorder, true)

const tabGap = tab.Copy()
.BorderTop(false)
.BorderLeft(false)
.BorderRight(false)

// Title.

const titleStyle = blipgloss.NewStyle().
MarginLeft(1).
MarginRight(5).
Padding(0, 1).
Italic(true).
Foreground('#FFF7DB').
SetString('Lip Gloss')

const descStyle = blipgloss.NewStyle().MarginTop(1)

const infoStyle = blipgloss.NewStyle().
BorderStyle('normal').
BorderTop(true).
BorderForeground(subtle)

// Dialog.

const dialogBoxStyle = blipgloss.NewStyle().
Border('rounded').
BorderForeground('#874BFD').
Padding(1, 0).
BorderTop(true).
BorderLeft(true).
BorderRight(true).
BorderBottom(true)

const buttonStyle = blipgloss.NewStyle().
Foreground('#FFF7DB').
Background('#888B7E').
Padding(0, 3).
MarginTop(1)

const activeButtonStyle = buttonStyle.Copy().
Foreground('#FFF7DB').
Background('#F25D94').
MarginRight(2).
Underline(true)

// List.

const list = blipgloss.NewStyle().
Border('normal', false, true, false, false).
BorderForeground(subtle).
MarginRight(2).
Height(8).
Width(columnWidth + 1)

const listHeader = blipgloss.NewStyle().
BorderStyle('normal').
BorderBottom(true).
BorderForeground(subtle).
MarginRight(2).
Render

const listItem = blipgloss.NewStyle().PaddingLeft(2).Render

const checkMark = blipgloss.NewStyle().SetString("✓").
Foreground(special).
PaddingRight(1).
String()

function listDone(s: string) {
return checkMark + blipgloss.NewStyle().
Strikethrough(true).
Foreground({ Light: "#969B86", Dark: "#696969" }).
Render(s)
}

// Paragraphs/History.

const historyStyle = blipgloss.NewStyle().
Align('Left').
Foreground('#FAFAFA').
Background(highlight).
Margin(1, 3, 0, 0).
Padding(1, 2).
Height(19).
Width(columnWidth)

// Status Bar.

const statusNugget = blipgloss.NewStyle().
Foreground('#FFFDF5').
Padding(0, 1)

const statusBarStyle = blipgloss.NewStyle().
Foreground({ Light: "#343433", Dark: "#C1C6B2" }).
Background({ Light: "#D9DCCF", Dark: "#353533" })

const statusStyle = blipgloss.NewStyle().
Inherit(statusBarStyle).
Foreground('#FFFDF5').
Background('#FF5F87').
Padding(0, 1).
MarginRight(1)

const encodingStyle = statusNugget.Copy().
Background('#A550DF').
Align('Right')
4 changes: 4 additions & 0 deletions src/ffi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,9 @@ export const { symbols } = dlopen(location, {
FreeString: {
args: [FFIType.ptr],
returns: FFIType.void
},
Inherit: {
args: [FFIType.ptr, FFIType.ptr],
returns: FFIType.void
}
})
5 changes: 5 additions & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ export class Style {
WithWhitespaceChars(char: string) {
return this.SetStringValue('WithWhitespaceChars', char)
}

Inherit(style: Style) {
symbols.Inherit(this.#handle, ptr(encode(style.#handle)))
return this
}
}

export function NewStyle() {
Expand Down

0 comments on commit 9ff28d9

Please sign in to comment.