Skip to content

Commit

Permalink
feat: Complete colors support
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Aug 3, 2023
1 parent 2ab6d68 commit 62a268e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 25 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ Background({
})
```

### Complete Colors

CompleteColor specifies exact values for truecolor, ANSI256, and ANSI color profiles.

```js
Background({
True: "#0000FF",
ANSI256: "86",
ANSI: "5"
})
```

The terminal's background color will automatically be detected and the appropriate color will be chosen at runtime.

## Inline Formatting
Expand Down
29 changes: 20 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,36 @@ func Copy(keyPtr *C.char) *C.char {
}

//export SetColorValue
func SetColorValue(fieldPtr, keyPtr, valuePtr *C.char, adaptive bool) {
func SetColorValue(fieldPtr, keyPtr, valuePtr *C.char, colorType int) {
style := getStyle(fieldPtr)
key := str(keyPtr)

if adaptive {
var color reflect.Value

switch colorType {
case 1:
color = reflect.ValueOf(lipgloss.Color(str(valuePtr)))
case 2:
adaptiveColor := lipgloss.AdaptiveColor{}
err := json.Unmarshal([]byte(str(valuePtr)), &adaptiveColor)

if err != nil {
panic("Unable to parse color")
panic("Unable to parse adaptive color")
}

color := reflect.ValueOf(adaptiveColor)
reflect.ValueOf(style).MethodByName(key).Call([]reflect.Value{color})
} else {
value := lipgloss.Color(str(valuePtr))
color := reflect.ValueOf(value)
reflect.ValueOf(style).MethodByName(key).Call([]reflect.Value{color})
color = reflect.ValueOf(adaptiveColor)
case 3:
completeColor := lipgloss.CompleteColor{}
err := json.Unmarshal([]byte(str(valuePtr)), &completeColor)

if err != nil {
panic("Unable to parse complete color")
}

color = reflect.ValueOf(completeColor)
}

reflect.ValueOf(style).MethodByName(key).Call([]reflect.Value{color})
}

//export SetIntValue
Expand Down
10 changes: 0 additions & 10 deletions playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,4 @@ const style = blipgloss.NewStyle()
BottomRight: "*",
})

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

// const style2 = style.Copy()
// .Background({
// Light: "#2B2D42",
// Dark: "#F8F32B"
// })
// .Width(20)
// .Align(blipgloss.Position.Top)

console.log(style.Render("Hello, bun clone."))
2 changes: 1 addition & 1 deletion src/ffi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const { symbols } = dlopen(location, {
returns: FFIType.ptr
},
SetColorValue: {
args: [FFIType.ptr, FFIType.ptr, FFIType.ptr, FFIType.bool],
args: [FFIType.ptr, FFIType.ptr, FFIType.ptr, FFIType.int],
returns: FFIType.void
},
SetIntValue: {
Expand Down
30 changes: 25 additions & 5 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import { CString, ptr } from 'bun:ffi'
import { symbols } from './ffi'
import { encode } from './utils'

// type Pointer = number
type BlipglossColor = string | {
type AdaptiveColor = {
Light: string
Dark: string
}

type CompleteColor = {
True: string
ANSI256: string
ANSI: string
}

// type Pointer = number
type BlipglossColor = string | AdaptiveColor | CompleteColor

export enum Position {
Top = 0.0,
Bottom = 1.0,
Expand Down Expand Up @@ -41,9 +49,21 @@ export class Style {
}

private SetColorValue(key: string, value: BlipglossColor) {
const adaptive = typeof value !== 'string'
const color = adaptive ? ptr(encode(JSON.stringify(value))) : ptr(encode(value))
symbols.SetColorValue(this.#handle, ptr(encode(key)), color, adaptive)
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)
} else {
// Complete color
symbols.SetColorValue(this.#handle, ptr(encode(key)), color, 3)
}
} else {
symbols.SetColorValue(this.#handle, ptr(encode(key)), color, 1)
}

return this
}

Expand Down

0 comments on commit 62a268e

Please sign in to comment.