diff --git a/go/compat.go b/go/compat.go new file mode 100644 index 0000000..69aee79 --- /dev/null +++ b/go/compat.go @@ -0,0 +1,237 @@ +package main + +import ( + "bytes" + "fmt" + "image/color" + "os" + "strconv" + "sync" + + "charm.land/lipgloss/v2" + "github.com/charmbracelet/colorprofile" + "github.com/charmbracelet/x/ansi" +) + +// Lip Gloss v2 no longer has a global renderer: styles always render +// full-color sequences and adaptive colors are resolved by the caller. The Ruby +// API still exposes the v1 model (a global color profile, a global dark +// background flag and AdaptiveColor/CompleteColor types that resolve at render +// time), so this file keeps that state and resolves it the way v1 did. + +var ( + profileMu sync.RWMutex + profile = colorprofile.Detect(os.Stdout, os.Environ()) + + darkMu sync.RWMutex + darkDetected bool + darkValue bool +) + +func currentProfile() colorprofile.Profile { + profileMu.RLock() + defer profileMu.RUnlock() + + return profile +} + +func setProfile(p colorprofile.Profile) { + profileMu.Lock() + defer profileMu.Unlock() + + profile = p +} + +// hasDarkBackground queries the terminal on first use, like v1 did. Querying at +// load time would put the terminal in raw mode and read from stdin as soon as +// the gem is required. +func hasDarkBackground() bool { + darkMu.RLock() + if darkDetected { + defer darkMu.RUnlock() + return darkValue + } + darkMu.RUnlock() + + darkMu.Lock() + defer darkMu.Unlock() + + if !darkDetected { + darkValue = os.Getenv("NO_COLOR") != "" || lipgloss.HasDarkBackground(os.Stdin, os.Stdout) + darkDetected = true + } + + return darkValue +} + +func setHasDarkBackground(dark bool) { + darkMu.Lock() + defer darkMu.Unlock() + + darkValue = dark + darkDetected = true +} + +// downsample converts the full-color output of Lip Gloss v2 to the current +// color profile. The ascii profile strips all styling, matching v1. +func downsample(s string) string { + p := currentProfile() + + if p == colorprofile.TrueColor { + return s + } + + if p <= colorprofile.ASCII { + return ansi.Strip(s) + } + + var buf bytes.Buffer + w := &colorprofile.Writer{Forward: &buf, Profile: p} + _, _ = w.WriteString(s) + + return buf.String() +} + +// Ruby passes profiles as indexes into Lipgloss::COLOR_PROFILES, which follow +// the termenv order used by v1. +var rubyProfiles = []colorprofile.Profile{ + colorprofile.TrueColor, + colorprofile.ANSI256, + colorprofile.ANSI, + colorprofile.ASCII, +} + +func profileToRuby(p colorprofile.Profile) int { + switch p { + case colorprofile.TrueColor: + return 0 + case colorprofile.ANSI256: + return 1 + case colorprofile.ANSI: + return 2 + default: + return 3 + } +} + +func profileFromRuby(i int) colorprofile.Profile { + if i < 0 || i >= len(rubyProfiles) { + return colorprofile.ASCII + } + + return rubyProfiles[i] +} + +// colorNames remembers how each color was spelled when it was set, so getters +// and to_h can hand back "#7aa2f7" or "212" instead of a reformatted value. +var ( + colorNames = map[color.Color]string{} + colorNamesMu sync.RWMutex +) + +// parseColor turns a v1 color string into a v2 color. Empty strings are +// treated as "no color", as in v1. +func parseColor(s string) color.Color { + if s == "" { + return lipgloss.NoColor{} + } + + c := lipgloss.Color(s) + + if _, ok := c.(lipgloss.NoColor); !ok { + colorNamesMu.Lock() + colorNames[c] = s + colorNamesMu.Unlock() + } + + return c +} + +// colorString is the inverse of parseColor. It returns "" for no color. +func colorString(c color.Color) string { + switch c.(type) { + case nil, lipgloss.NoColor: + return "" + case adaptiveColor: + return "" + case completeColor, completeAdaptiveColor: + return "" + } + + colorNamesMu.RLock() + name, ok := colorNames[c] + colorNamesMu.RUnlock() + + if ok { + return name + } + + switch v := c.(type) { + case ansi.BasicColor: + return strconv.Itoa(int(v)) + case ansi.IndexedColor: + return strconv.Itoa(int(v)) + } + + r, g, b, _ := c.RGBA() + + return fmt.Sprintf("#%02x%02x%02x", r>>8, g>>8, b>>8) +} + +// adaptiveColor picks Light or Dark based on the terminal background at render +// time, like lipgloss.AdaptiveColor in v1. +type adaptiveColor struct { + Light string + Dark string +} + +func (c adaptiveColor) resolve() color.Color { + if hasDarkBackground() { + return parseColor(c.Dark) + } + + return parseColor(c.Light) +} + +func (c adaptiveColor) RGBA() (r, g, b, a uint32) { + return c.resolve().RGBA() +} + +// completeColor picks an exact value for the current color profile, like +// lipgloss.CompleteColor in v1. +type completeColor struct { + TrueColor string + ANSI256 string + ANSI string +} + +func (c completeColor) resolve() color.Color { + switch currentProfile() { + case colorprofile.TrueColor: + return parseColor(c.TrueColor) + case colorprofile.ANSI256: + return parseColor(c.ANSI256) + case colorprofile.ANSI: + return parseColor(c.ANSI) + default: + return lipgloss.NoColor{} + } +} + +func (c completeColor) RGBA() (r, g, b, a uint32) { + return c.resolve().RGBA() +} + +// completeAdaptiveColor combines completeColor with light/dark selection. +type completeAdaptiveColor struct { + Light completeColor + Dark completeColor +} + +func (c completeAdaptiveColor) RGBA() (r, g, b, a uint32) { + if hasDarkBackground() { + return c.Dark.RGBA() + } + + return c.Light.RGBA() +} diff --git a/go/go.mod b/go/go.mod index bceb598..4c8a5f1 100644 --- a/go/go.mod +++ b/go/go.mod @@ -1,22 +1,25 @@ module github.com/marcoroth/lipgloss-ruby/go -go 1.23.0 +go 1.26.0 require ( - github.com/charmbracelet/lipgloss v1.1.0 - github.com/lucasb-eyer/go-colorful v1.2.0 - github.com/muesli/termenv v0.16.0 + charm.land/lipgloss/v2 v2.1.1 + github.com/charmbracelet/colorprofile v0.4.3 + github.com/charmbracelet/x/ansi v0.11.8 + github.com/lucasb-eyer/go-colorful v1.4.1 ) require ( - github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect - github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect - github.com/charmbracelet/x/ansi v0.8.0 // indirect - github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect - github.com/charmbracelet/x/term v0.2.1 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/charmbracelet/ultraviolet v0.0.0-20260811164956-006e29f97886 // indirect + github.com/charmbracelet/x/term v0.2.2 // indirect + github.com/charmbracelet/x/termios v0.1.1 // indirect + github.com/charmbracelet/x/windows v0.2.2 // indirect + github.com/clipperhouse/displaywidth v0.11.0 // indirect + github.com/clipperhouse/uax29/v2 v2.7.0 // indirect + github.com/mattn/go-runewidth v0.0.24 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect - golang.org/x/sys v0.30.0 // indirect + golang.org/x/sync v0.22.0 // indirect + golang.org/x/sys v0.47.0 // indirect ) diff --git a/go/go.sum b/go/go.sum index 97c706d..69cdaba 100644 --- a/go/go.sum +++ b/go/go.sum @@ -1,34 +1,38 @@ -github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= -github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= -github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8= -github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA= -github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs= -github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk= -github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY= -github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30= -github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2llXn7xE= -github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q= -github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8= -github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs= -github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a h1:G99klV19u0QnhiizODirwVksQB91TJKV/UaTnACcG30= -github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U= -github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ= -github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg= -github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= -github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= -github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc= -github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +charm.land/lipgloss/v2 v2.1.1 h1:F+vKWis9Kz3MFRsbICSDFtT1ZwVL+8f+36Dtg+OpKwc= +charm.land/lipgloss/v2 v2.1.1/go.mod h1:k79y/56yThpkJvbwyrcHr8MyZF42e84Q5fW9Ibu6LFc= +github.com/aymanbagabas/go-udiff v0.4.1 h1:OEIrQ8maEeDBXQDoGCbbTTXYJMYRCRO1fnodZ12Gv5o= +github.com/aymanbagabas/go-udiff v0.4.1/go.mod h1:0L9PGwj20lrtmEMeyw4WKJ/TMyDtvAoK9bf2u/mNo3w= +github.com/charmbracelet/colorprofile v0.4.3 h1:QPa1IWkYI+AOB+fE+mg/5/4HRMZcaXex9t5KX76i20Q= +github.com/charmbracelet/colorprofile v0.4.3/go.mod h1:/zT4BhpD5aGFpqQQqw7a+VtHCzu+zrQtt1zhMt9mR4Q= +github.com/charmbracelet/ultraviolet v0.0.0-20260811164956-006e29f97886 h1:rdnVWKgJpTVXKuKuJyxDJ+NFJdUaUqGvyGy61OcvlbA= +github.com/charmbracelet/ultraviolet v0.0.0-20260811164956-006e29f97886/go.mod h1:nAw0d9PhFp1qdzi2xhQU5YOu5sVpDIHWlaW2Uz/bCro= +github.com/charmbracelet/x/ansi v0.11.8 h1:JMFwp0CgDC2+jcOB162HH5k7I3FVbgFSMMYg7dSPBQQ= +github.com/charmbracelet/x/ansi v0.11.8/go.mod h1:ZNN+3mXny/516oTQPLMPIBeSINvNJJQ8uQXDgbeJxY0= +github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f h1:pk6gmGpCE7F3FcjaOEKYriCvpmIN4+6OS/RD0vm4uIA= +github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f/go.mod h1:IfZAMTHB6XkZSeXUqriemErjAWCCzT0LwjKFYCZyw0I= +github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk= +github.com/charmbracelet/x/term v0.2.2/go.mod h1:kF8CY5RddLWrsgVwpw4kAa6TESp6EB5y3uxGLeCqzAI= +github.com/charmbracelet/x/termios v0.1.1 h1:o3Q2bT8eqzGnGPOYheoYS8eEleT5ZVNYNy8JawjaNZY= +github.com/charmbracelet/x/termios v0.1.1/go.mod h1:rB7fnv1TgOPOyyKRJ9o+AsTU/vK5WHJ2ivHeut/Pcwo= +github.com/charmbracelet/x/windows v0.2.2 h1:IofanmuvaxnKHuV04sC0eBy/smG6kIKrWG2/jYn2GuM= +github.com/charmbracelet/x/windows v0.2.2/go.mod h1:/8XtdKZzedat74NQFn0NGlGL4soHB0YQZrETF96h75k= +github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSEFgwIwO+UVM8= +github.com/clipperhouse/displaywidth v0.11.0/go.mod h1:bkrFNkf81G8HyVqmKGxsPufD3JhNl3dSqnGhOoSD/o0= +github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk= +github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM= +github.com/lucasb-eyer/go-colorful v1.4.1 h1:1EO+WB73+EH8EVbzlrG3KLAfEypQWVHIBqlTf+2hNss= +github.com/lucasb-eyer/go-colorful v1.4.1/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-runewidth v0.0.24 h1:cpokDiIn0MGnhdHwuWnJBITySJ20QyNGnY2kR/ay2DU= +github.com/mattn/go-runewidth v0.0.24/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= -golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= -golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= -golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek= +golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= +golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= +golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= diff --git a/go/layout.go b/go/layout.go index ef449ee..9f60943 100644 --- a/go/layout.go +++ b/go/layout.go @@ -3,9 +3,8 @@ package main import "C" import ( + "charm.land/lipgloss/v2" "encoding/json" - "github.com/charmbracelet/lipgloss" - "github.com/muesli/termenv" ) //export lipgloss_join_horizontal @@ -18,7 +17,7 @@ func lipgloss_join_horizontal(position C.double, stringsJSON *C.char) *C.char { result := lipgloss.JoinHorizontal(lipgloss.Position(position), strings...) - return C.CString(result) + return C.CString(downsample(result)) } //export lipgloss_join_vertical @@ -31,7 +30,7 @@ func lipgloss_join_vertical(position C.double, stringsJSON *C.char) *C.char { result := lipgloss.JoinVertical(lipgloss.Position(position), strings...) - return C.CString(result) + return C.CString(downsample(result)) } //export lipgloss_width @@ -47,7 +46,7 @@ func lipgloss_height(text *C.char) C.int { //export lipgloss_place func lipgloss_place(width C.int, height C.int, horizontalPosition C.double, verticalPosition C.double, text *C.char) *C.char { result := lipgloss.Place(int(width), int(height), lipgloss.Position(horizontalPosition), lipgloss.Position(verticalPosition), C.GoString(text)) - return C.CString(result) + return C.CString(downsample(result)) } //export lipgloss_place_with_whitespace @@ -61,11 +60,11 @@ func lipgloss_place_with_whitespace(width C.int, height C.int, horizontalPositio wsFg := C.GoString(whitespaceForeground) if wsFg != "" { - opts = append(opts, lipgloss.WithWhitespaceForeground(lipgloss.Color(wsFg))) + opts = append(opts, lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Foreground(parseColor(wsFg)))) } result := lipgloss.Place(int(width), int(height), lipgloss.Position(horizontalPosition), lipgloss.Position(verticalPosition), C.GoString(text), opts...) - return C.CString(result) + return C.CString(downsample(result)) } //export lipgloss_place_with_whitespace_adaptive @@ -81,32 +80,32 @@ func lipgloss_place_with_whitespace_adaptive(width C.int, height C.int, horizont wsFgDark := C.GoString(whitespaceForegroundDark) if wsFgLight != "" || wsFgDark != "" { - opts = append(opts, lipgloss.WithWhitespaceForeground(lipgloss.AdaptiveColor{ + opts = append(opts, lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Foreground(adaptiveColor{ Light: wsFgLight, Dark: wsFgDark, - })) + }))) } result := lipgloss.Place(int(width), int(height), lipgloss.Position(horizontalPosition), lipgloss.Position(verticalPosition), C.GoString(text), opts...) - return C.CString(result) + return C.CString(downsample(result)) } //export lipgloss_place_horizontal func lipgloss_place_horizontal(width C.int, position C.double, text *C.char) *C.char { result := lipgloss.PlaceHorizontal(int(width), lipgloss.Position(position), C.GoString(text)) - return C.CString(result) + return C.CString(downsample(result)) } //export lipgloss_place_vertical func lipgloss_place_vertical(height C.int, position C.double, text *C.char) *C.char { result := lipgloss.PlaceVertical(int(height), lipgloss.Position(position), C.GoString(text)) - return C.CString(result) + return C.CString(downsample(result)) } //export lipgloss_has_dark_background func lipgloss_has_dark_background() C.int { - if lipgloss.HasDarkBackground() { + if hasDarkBackground() { return 1 } @@ -115,15 +114,15 @@ func lipgloss_has_dark_background() C.int { //export lipgloss_color_profile func lipgloss_color_profile() C.int { - return C.int(lipgloss.ColorProfile()) + return C.int(profileToRuby(currentProfile())) } //export lipgloss_set_color_profile func lipgloss_set_color_profile(profile C.int) { - lipgloss.SetColorProfile(termenv.Profile(profile)) + setProfile(profileFromRuby(int(profile))) } //export lipgloss_set_has_dark_background func lipgloss_set_has_dark_background(dark C.int) { - lipgloss.SetHasDarkBackground(dark != 0) + setHasDarkBackground(dark != 0) } diff --git a/go/lipgloss.go b/go/lipgloss.go index b7fbf13..b78cd84 100644 --- a/go/lipgloss.go +++ b/go/lipgloss.go @@ -6,10 +6,10 @@ package main import "C" import ( - "github.com/charmbracelet/lipgloss" - lipglosslist "github.com/charmbracelet/lipgloss/list" - lipglosstable "github.com/charmbracelet/lipgloss/table" - lipglosstree "github.com/charmbracelet/lipgloss/tree" + "charm.land/lipgloss/v2" + lipglosslist "charm.land/lipgloss/v2/list" + lipglosstable "charm.land/lipgloss/v2/table" + lipglosstree "charm.land/lipgloss/v2/tree" "runtime/debug" "sync" "unsafe" @@ -67,7 +67,7 @@ func lipgloss_upstream_version() *C.char { } for _, dep := range info.Deps { - if dep.Path == "github.com/charmbracelet/lipgloss" { + if dep.Path == "charm.land/lipgloss/v2" { return C.CString(dep.Version) } } diff --git a/go/list.go b/go/list.go index a750b21..077616d 100644 --- a/go/list.go +++ b/go/list.go @@ -3,8 +3,8 @@ package main import "C" import ( + lipglosslist "charm.land/lipgloss/v2/list" "encoding/json" - lipglosslist "github.com/charmbracelet/lipgloss/list" ) func allocList(list *lipglosslist.List) uint64 { @@ -114,5 +114,5 @@ func lipgloss_list_item_style(id C.ulonglong, styleID C.ulonglong) C.ulonglong { func lipgloss_list_render(id C.ulonglong) *C.char { list := getList(uint64(id)) - return C.CString(list.String()) + return C.CString(downsample(list.String())) } diff --git a/go/style.go b/go/style.go index eb1191b..14aa21d 100644 --- a/go/style.go +++ b/go/style.go @@ -3,9 +3,10 @@ package main import "C" import ( + "image/color" "unsafe" - "github.com/charmbracelet/lipgloss" + "charm.land/lipgloss/v2" ) func allocStyle(style lipgloss.Style) uint64 { @@ -41,7 +42,7 @@ func lipgloss_style_render(id C.ulonglong, text *C.char) *C.char { style := getStyle(uint64(id)) result := style.Render(C.GoString(text)) - return C.CString(result) + return C.CString(downsample(result)) } // Text formatting methods @@ -92,19 +93,19 @@ func lipgloss_style_faint(id C.ulonglong, value C.int) C.ulonglong { //export lipgloss_style_foreground func lipgloss_style_foreground(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).Foreground(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).Foreground(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } //export lipgloss_style_background func lipgloss_style_background(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).Background(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).Background(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } //export lipgloss_style_foreground_adaptive func lipgloss_style_foreground_adaptive(id C.ulonglong, light *C.char, dark *C.char) C.ulonglong { - style := getStyle(uint64(id)).Foreground(lipgloss.AdaptiveColor{ + style := getStyle(uint64(id)).Foreground(adaptiveColor{ Light: C.GoString(light), Dark: C.GoString(dark), }) @@ -114,7 +115,7 @@ func lipgloss_style_foreground_adaptive(id C.ulonglong, light *C.char, dark *C.c //export lipgloss_style_background_adaptive func lipgloss_style_background_adaptive(id C.ulonglong, light *C.char, dark *C.char) C.ulonglong { - style := getStyle(uint64(id)).Background(lipgloss.AdaptiveColor{ + style := getStyle(uint64(id)).Background(adaptiveColor{ Light: C.GoString(light), Dark: C.GoString(dark), }) @@ -124,7 +125,7 @@ func lipgloss_style_background_adaptive(id C.ulonglong, light *C.char, dark *C.c //export lipgloss_style_foreground_complete func lipgloss_style_foreground_complete(id C.ulonglong, trueColor *C.char, ansi256 *C.char, ansi *C.char) C.ulonglong { - style := getStyle(uint64(id)).Foreground(lipgloss.CompleteColor{ + style := getStyle(uint64(id)).Foreground(completeColor{ TrueColor: C.GoString(trueColor), ANSI256: C.GoString(ansi256), ANSI: C.GoString(ansi), @@ -135,7 +136,7 @@ func lipgloss_style_foreground_complete(id C.ulonglong, trueColor *C.char, ansi2 //export lipgloss_style_background_complete func lipgloss_style_background_complete(id C.ulonglong, trueColor *C.char, ansi256 *C.char, ansi *C.char) C.ulonglong { - style := getStyle(uint64(id)).Background(lipgloss.CompleteColor{ + style := getStyle(uint64(id)).Background(completeColor{ TrueColor: C.GoString(trueColor), ANSI256: C.GoString(ansi256), ANSI: C.GoString(ansi), @@ -146,13 +147,13 @@ func lipgloss_style_background_complete(id C.ulonglong, trueColor *C.char, ansi2 //export lipgloss_style_foreground_complete_adaptive func lipgloss_style_foreground_complete_adaptive(id C.ulonglong, lightTrue *C.char, lightAnsi256 *C.char, lightAnsi *C.char, darkTrue *C.char, darkAnsi256 *C.char, darkAnsi *C.char) C.ulonglong { - style := getStyle(uint64(id)).Foreground(lipgloss.CompleteAdaptiveColor{ - Light: lipgloss.CompleteColor{ + style := getStyle(uint64(id)).Foreground(completeAdaptiveColor{ + Light: completeColor{ TrueColor: C.GoString(lightTrue), ANSI256: C.GoString(lightAnsi256), ANSI: C.GoString(lightAnsi), }, - Dark: lipgloss.CompleteColor{ + Dark: completeColor{ TrueColor: C.GoString(darkTrue), ANSI256: C.GoString(darkAnsi256), ANSI: C.GoString(darkAnsi), @@ -164,13 +165,13 @@ func lipgloss_style_foreground_complete_adaptive(id C.ulonglong, lightTrue *C.ch //export lipgloss_style_background_complete_adaptive func lipgloss_style_background_complete_adaptive(id C.ulonglong, lightTrue *C.char, lightAnsi256 *C.char, lightAnsi *C.char, darkTrue *C.char, darkAnsi256 *C.char, darkAnsi *C.char) C.ulonglong { - style := getStyle(uint64(id)).Background(lipgloss.CompleteAdaptiveColor{ - Light: lipgloss.CompleteColor{ + style := getStyle(uint64(id)).Background(completeAdaptiveColor{ + Light: completeColor{ TrueColor: C.GoString(lightTrue), ANSI256: C.GoString(lightAnsi256), ANSI: C.GoString(lightAnsi), }, - Dark: lipgloss.CompleteColor{ + Dark: completeColor{ TrueColor: C.GoString(darkTrue), ANSI256: C.GoString(darkAnsi256), ANSI: C.GoString(darkAnsi), @@ -277,7 +278,7 @@ func lipgloss_style_inherit(id C.ulonglong, inheritFromID C.ulonglong) C.ulonglo //export lipgloss_style_string func lipgloss_style_string(id C.ulonglong) *C.char { style := getStyle(uint64(id)) - return C.CString(style.String()) + return C.CString(downsample(style.String())) } // Getter methods @@ -345,20 +346,10 @@ func lipgloss_style_get_faint(id C.ulonglong) C.int { return 0 } -// terminalColorToString converts a TerminalColor to its string representation -func terminalColorToString(tc lipgloss.TerminalColor) string { - if tc == nil { - return "" - } - switch c := tc.(type) { - case lipgloss.Color: - return string(c) - case lipgloss.NoColor: - return "" - default: - // For adaptive/complete colors, we can't easily extract a single value - return "" - } +// terminalColorToString converts a color to the string it was set with. +// Adaptive and complete colors have no single value and return "". +func terminalColorToString(c color.Color) string { + return colorString(c) } //export lipgloss_style_get_foreground diff --git a/go/style_border.go b/go/style_border.go index 996f0e6..2e7e18e 100644 --- a/go/style_border.go +++ b/go/style_border.go @@ -5,7 +5,7 @@ import "C" import ( "unsafe" - "github.com/charmbracelet/lipgloss" + "charm.land/lipgloss/v2" ) //export lipgloss_style_border @@ -110,13 +110,13 @@ func lipgloss_style_border_custom(id C.ulonglong, top, bottom, left, right, topL //export lipgloss_style_border_foreground func lipgloss_style_border_foreground(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).BorderForeground(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).BorderForeground(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } //export lipgloss_style_border_foreground_adaptive func lipgloss_style_border_foreground_adaptive(id C.ulonglong, light *C.char, dark *C.char) C.ulonglong { - style := getStyle(uint64(id)).BorderForeground(lipgloss.AdaptiveColor{ + style := getStyle(uint64(id)).BorderForeground(adaptiveColor{ Light: C.GoString(light), Dark: C.GoString(dark), }) @@ -126,13 +126,13 @@ func lipgloss_style_border_foreground_adaptive(id C.ulonglong, light *C.char, da //export lipgloss_style_border_background func lipgloss_style_border_background(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).BorderBackground(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).BorderBackground(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } //export lipgloss_style_border_background_adaptive func lipgloss_style_border_background_adaptive(id C.ulonglong, light *C.char, dark *C.char) C.ulonglong { - style := getStyle(uint64(id)).BorderBackground(lipgloss.AdaptiveColor{ + style := getStyle(uint64(id)).BorderBackground(adaptiveColor{ Light: C.GoString(light), Dark: C.GoString(dark), }) @@ -168,25 +168,25 @@ func lipgloss_style_border_left(id C.ulonglong, value C.int) C.ulonglong { //export lipgloss_style_border_top_foreground func lipgloss_style_border_top_foreground(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).BorderTopForeground(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).BorderTopForeground(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } //export lipgloss_style_border_right_foreground func lipgloss_style_border_right_foreground(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).BorderRightForeground(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).BorderRightForeground(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } //export lipgloss_style_border_bottom_foreground func lipgloss_style_border_bottom_foreground(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).BorderBottomForeground(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).BorderBottomForeground(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } //export lipgloss_style_border_left_foreground func lipgloss_style_border_left_foreground(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).BorderLeftForeground(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).BorderLeftForeground(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } @@ -194,24 +194,24 @@ func lipgloss_style_border_left_foreground(id C.ulonglong, color *C.char) C.ulon //export lipgloss_style_border_top_background func lipgloss_style_border_top_background(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).BorderTopBackground(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).BorderTopBackground(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } //export lipgloss_style_border_right_background func lipgloss_style_border_right_background(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).BorderRightBackground(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).BorderRightBackground(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } //export lipgloss_style_border_bottom_background func lipgloss_style_border_bottom_background(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).BorderBottomBackground(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).BorderBottomBackground(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } //export lipgloss_style_border_left_background func lipgloss_style_border_left_background(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).BorderLeftBackground(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).BorderLeftBackground(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } diff --git a/go/style_hash.go b/go/style_hash.go index 5ea937e..fcd66b9 100644 --- a/go/style_hash.go +++ b/go/style_hash.go @@ -4,32 +4,35 @@ import "C" import ( "encoding/json" + "image/color" - "github.com/charmbracelet/lipgloss" + "charm.land/lipgloss/v2" ) -func colorToValue(tc lipgloss.TerminalColor) any { - switch c := tc.(type) { - case nil, lipgloss.NoColor: - return nil - case lipgloss.Color: - if c == "" { - return nil - } - return string(c) - case lipgloss.AdaptiveColor: - return map[string]any{"light": c.Light, "dark": c.Dark} - case lipgloss.CompleteColor: - return map[string]any{"true_color": c.TrueColor, "ansi256": c.ANSI256, "ansi": c.ANSI} - case lipgloss.CompleteAdaptiveColor: +func colorToValue(c color.Color) any { + switch v := c.(type) { + case adaptiveColor: + return map[string]any{"light": v.Light, "dark": v.Dark} + case completeColor: + return completeColorToValue(v) + case completeAdaptiveColor: return map[string]any{ - "light": map[string]any{"true_color": c.Light.TrueColor, "ansi256": c.Light.ANSI256, "ansi": c.Light.ANSI}, - "dark": map[string]any{"true_color": c.Dark.TrueColor, "ansi256": c.Dark.ANSI256, "ansi": c.Dark.ANSI}, + "light": completeColorToValue(v.Light), + "dark": completeColorToValue(v.Dark), } } + + if s := colorString(c); s != "" { + return s + } + return nil } +func completeColorToValue(c completeColor) map[string]any { + return map[string]any{"true_color": c.TrueColor, "ansi256": c.ANSI256, "ansi": c.ANSI} +} + func borderToValue(b lipgloss.Border) map[string]any { return map[string]any{ "top": b.Top, diff --git a/go/style_spacing.go b/go/style_spacing.go index 3b47819..e4ff016 100644 --- a/go/style_spacing.go +++ b/go/style_spacing.go @@ -3,7 +3,6 @@ package main import "C" import ( - "github.com/charmbracelet/lipgloss" "unsafe" ) @@ -89,6 +88,6 @@ func lipgloss_style_margin_left(id C.ulonglong, value C.int) C.ulonglong { //export lipgloss_style_margin_background func lipgloss_style_margin_background(id C.ulonglong, color *C.char) C.ulonglong { - style := getStyle(uint64(id)).MarginBackground(lipgloss.Color(C.GoString(color))) + style := getStyle(uint64(id)).MarginBackground(parseColor(C.GoString(color))) return C.ulonglong(allocStyle(style)) } diff --git a/go/table.go b/go/table.go index fdb3744..b0ed15a 100644 --- a/go/table.go +++ b/go/table.go @@ -3,10 +3,10 @@ package main import "C" import ( + "charm.land/lipgloss/v2" + lipglosstable "charm.land/lipgloss/v2/table" "encoding/json" "fmt" - "github.com/charmbracelet/lipgloss" - lipglosstable "github.com/charmbracelet/lipgloss/table" ) func allocTable(table *lipglosstable.Table) uint64 { @@ -27,7 +27,8 @@ func getTable(id uint64) *lipglosstable.Table { //export lipgloss_table_new func lipgloss_table_new() C.ulonglong { - return C.ulonglong(allocTable(lipglosstable.New())) + // v2 defaults to the normal border; keep v1's rounded default. + return C.ulonglong(allocTable(lipglosstable.New().Border(lipgloss.RoundedBorder()))) } //export lipgloss_table_free @@ -173,7 +174,7 @@ func lipgloss_table_height(id C.ulonglong, height C.int) C.ulonglong { //export lipgloss_table_offset func lipgloss_table_offset(id C.ulonglong, offset C.int) C.ulonglong { - table := getTable(uint64(id)).Offset(int(offset)) + table := getTable(uint64(id)).YOffset(int(offset)) return C.ulonglong(allocTable(table)) } @@ -192,7 +193,7 @@ func lipgloss_table_clear_rows(id C.ulonglong) C.ulonglong { //export lipgloss_table_render func lipgloss_table_render(id C.ulonglong) *C.char { table := getTable(uint64(id)) - return C.CString(table.Render()) + return C.CString(downsample(table.Render())) } //export lipgloss_table_style_func diff --git a/go/tree.go b/go/tree.go index 6b82bed..85aaf33 100644 --- a/go/tree.go +++ b/go/tree.go @@ -3,8 +3,8 @@ package main import "C" import ( + lipglosstree "charm.land/lipgloss/v2/tree" "encoding/json" - lipglosstree "github.com/charmbracelet/lipgloss/tree" ) func allocTree(tree *lipglosstree.Tree) uint64 { @@ -102,7 +102,8 @@ func lipgloss_tree_enumerator(id C.ulonglong, enumType C.int) C.ulonglong { //export lipgloss_tree_enumerator_style func lipgloss_tree_enumerator_style(id C.ulonglong, styleID C.ulonglong) C.ulonglong { style := getStyle(uint64(styleID)) - tree := getTree(uint64(id)).EnumeratorStyle(style) + // v1 styled the indentation with the enumerator style; v2 splits them. + tree := getTree(uint64(id)).EnumeratorStyle(style).IndenterStyle(style) return C.ulonglong(allocTree(tree)) } @@ -134,5 +135,5 @@ func lipgloss_tree_offset(id C.ulonglong, start C.int, end C.int) C.ulonglong { func lipgloss_tree_render(id C.ulonglong) *C.char { tree := getTree(uint64(id)) - return C.CString(tree.String()) + return C.CString(downsample(tree.String())) } diff --git a/test/tree_test.rb b/test/tree_test.rb index 43353cb..edd9ea3 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -92,6 +92,23 @@ class TreeTest < Minitest::Spec assert_equal expected, result end + it "applies the enumerator style to nested indentation" do + style = Lipgloss::Style.new.foreground("5") + tree = Lipgloss::Tree.root("Root") + .child("A", Lipgloss::Tree.root("B").child("C")) + .enumerator_style(style) + + previous = Lipgloss.color_profile + Lipgloss.color_profile = :ansi + + result = tree.render + + assert_equal "Root\n├──A\n└──B\n └──C", strip_ansi(result) + assert_includes result, "\e[35m \e[m" + ensure + Lipgloss.color_profile = previous if previous + end + it "renders with item style" do style = Lipgloss::Style.new.italic(true) tree = Lipgloss::Tree.root("Root")