Skip to content

Commit

Permalink
feat: support for complete adaptive colors
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Sep 18, 2023
1 parent 7a18455 commit e5f05d6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 25 deletions.
38 changes: 23 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ func getStyle(fieldPtr *C.char) lipgloss.Style {
return styleMap[str(fieldPtr)]
}

func generateUniqueId() string {
canonic, _ := nanoid.Standard(10)
return canonic()
}

//export NewStyle
func NewStyle() *C.char {
canonic, _ := nanoid.Standard(10)
uniqueId := canonic()
uniqueId := generateUniqueId()
styleMap[uniqueId] = lipgloss.NewStyle().Copy()
return ch(uniqueId)
}
Expand All @@ -61,39 +65,35 @@ func String(keyPtr *C.char) *C.char {
//export Copy
func Copy(keyPtr *C.char) *C.char {
key := str(keyPtr)
canonic, _ := nanoid.Standard(10)
uniqueId := canonic()
uniqueId := generateUniqueId()
styleMap[uniqueId] = styleMap[key].Copy()
return ch(uniqueId)
}

//export WithWhitespaceChars
func WithWhitespaceChars(strPtr *C.char) *C.char {
canonic, _ := nanoid.Standard(10)
uniqueId := canonic()
uniqueId := generateUniqueId()
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)
uniqueId := generateUniqueId()
color := GetColorByType(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)
uniqueId := generateUniqueId()
color := GetColorByType(str(valuePtr), colorType)
whitespaceMap[uniqueId] = lipgloss.WithWhitespaceForeground(color)
return ch(uniqueId)
}

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

switch colorType {
Expand All @@ -115,6 +115,14 @@ func SetColor(value string, colorType int) lipgloss.TerminalColor {
panic("Unable to parse complete color")
}
color = completeColor
case 4:
completeAdaptiveColor := lipgloss.CompleteAdaptiveColor{}
err := json.Unmarshal([]byte(value), &completeAdaptiveColor)

if err != nil {
panic("Unable to parse complete adaptive color")
}
color = completeAdaptiveColor
}
return color
}
Expand All @@ -124,7 +132,7 @@ func SetColorValue(fieldPtr, keyPtr, valuePtr *C.char, colorType int) {
style := getStyle(fieldPtr)
method := str(keyPtr)

color := reflect.ValueOf(SetColor(str(valuePtr), colorType))
color := reflect.ValueOf(GetColorByType(str(valuePtr), colorType))

reflect.ValueOf(style).MethodByName(method).Call([]reflect.Value{color})
}
Expand Down Expand Up @@ -267,7 +275,7 @@ func Border(fieldPtr, valuePtr *C.char, top, right, bottom, left bool) {
err := json.Unmarshal([]byte(value), &jsonBorder)

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

border = jsonBorder
Expand Down
44 changes: 34 additions & 10 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ export type CompleteColor = {
ANSI: string
}

export type BlipglossColor = string | AdaptiveColor | CompleteColor
export type CompleteAdaptiveColor = {
Light: CompleteColor
Dark: CompleteColor
}

export type BlipglossColor = string | AdaptiveColor | CompleteColor | CompleteAdaptiveColor

export enum Position {
Top = 0.0,
Expand Down Expand Up @@ -59,19 +64,38 @@ export class Style {
const isObject = typeof value !== 'string'
const color = isObject ? ptr(encode(JSON.stringify(value))) : ptr(encode(value))

if (isObject) {
if ('Light' in value) {
// Adaptive color
symbols.SetColorValue(this.#handle, ptr(encode(key)), color, 2)
if (!isObject){
symbols.SetColorValue(this.#handle, ptr(encode(key)), color, 1)
return this
}

if ('Light' in value && 'Dark' in value) {
if (
// @ts-expect-error: Better type checking
'True' in value.Light &&
'ANSI256' in value.Light &&
'ANSI' in value.Light &&
// @ts-expect-error: Better type checking
'True' in value.Dark &&
'ANSI256' in value.Dark &&
'ANSI' in value.Dark
) {
// value is of type CompleteAdaptiveColor
symbols.SetColorValue(this.#handle, ptr(encode(key)), color, 4)
} else {
// Complete color
symbols.SetColorValue(this.#handle, ptr(encode(key)), color, 3)
// value is of type AdaptiveColor
symbols.SetColorValue(this.#handle, ptr(encode(key)), color, 2)
}
} else {
symbols.SetColorValue(this.#handle, ptr(encode(key)), color, 1)
return this
}

if ('True' in value && 'ANSI256' in value && 'ANSI' in value) {
// value is of type CompleteColor
symbols.SetColorValue(this.#handle, ptr(encode(key)), color, 3)
return this
}

return this
throw new Error('Incorrect color type: Must be of type string, AdaptiveColor, CompleteColor, or CompleteAdaptiveColor.');
}

private SetBooleanValue(key: string, value: boolean) {
Expand Down

0 comments on commit e5f05d6

Please sign in to comment.