From 8f9be9ba670c00177235c81222a8af99c14c1681 Mon Sep 17 00:00:00 2001 From: Marco Roth Date: Wed, 18 Nov 2026 23:36:45 +0100 Subject: [PATCH] Add `Style#to_h` to read a style's settings --- Steepfile | 2 + ext/lipgloss/style.c | 11 +++ go/style_hash.go | 144 ++++++++++++++++++++++++++++++++++++++ lib/lipgloss.rb | 1 + lib/lipgloss/style.rb | 5 ++ sig/lipgloss/lipgloss.rbs | 1 + sig/lipgloss/style.rbs | 3 + test/style_to_h_test.rb | 48 +++++++++++++ 8 files changed, 215 insertions(+) create mode 100644 go/style_hash.go create mode 100644 test/style_to_h_test.rb diff --git a/Steepfile b/Steepfile index 5cccd28..1844677 100644 --- a/Steepfile +++ b/Steepfile @@ -3,5 +3,7 @@ target :lib do signature "sig" + library "json" + check "lib" end diff --git a/ext/lipgloss/style.c b/ext/lipgloss/style.c index 8e0c263..621f05a 100644 --- a/ext/lipgloss/style.c +++ b/ext/lipgloss/style.c @@ -344,6 +344,16 @@ static VALUE style_to_s(VALUE self) { return rb_result; } +static VALUE style_to_json(VALUE self) { + GET_STYLE(self, style); + char *result = lipgloss_style_to_json(style->handle); + VALUE rb_result = rb_utf8_str_new_cstr(result); + + lipgloss_free(result); + + return rb_result; +} + // Getter methods static VALUE style_get_bold(VALUE self) { @@ -455,6 +465,7 @@ void Init_lipgloss_style(void) { rb_define_method(cStyle, "set_string", style_set_string, 1); rb_define_method(cStyle, "inherit", style_inherit, 1); rb_define_method(cStyle, "to_s", style_to_s, 0); + rb_define_method(cStyle, "_to_json", style_to_json, 0); rb_define_method(cStyle, "bold?", style_get_bold, 0); rb_define_method(cStyle, "italic?", style_get_italic, 0); diff --git a/go/style_hash.go b/go/style_hash.go new file mode 100644 index 0000000..5ea937e --- /dev/null +++ b/go/style_hash.go @@ -0,0 +1,144 @@ +package main + +import "C" + +import ( + "encoding/json" + + "github.com/charmbracelet/lipgloss" +) + +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: + 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}, + } + } + return nil +} + +func borderToValue(b lipgloss.Border) map[string]any { + return map[string]any{ + "top": b.Top, + "bottom": b.Bottom, + "left": b.Left, + "right": b.Right, + "top_left": b.TopLeft, + "top_right": b.TopRight, + "bottom_left": b.BottomLeft, + "bottom_right": b.BottomRight, + "middle_left": b.MiddleLeft, + "middle_right": b.MiddleRight, + "middle": b.Middle, + "middle_top": b.MiddleTop, + "middle_bottom": b.MiddleBottom, + } +} + +func setIf(h map[string]any, key string, value any) { + switch v := value.(type) { + case nil: + return + case bool: + if !v { + return + } + case int: + if v == 0 { + return + } + } + h[key] = value +} + +func styleToHash(s lipgloss.Style) map[string]any { + h := map[string]any{} + + setIf(h, "bold", s.GetBold()) + setIf(h, "italic", s.GetItalic()) + setIf(h, "underline", s.GetUnderline()) + setIf(h, "strikethrough", s.GetStrikethrough()) + setIf(h, "reverse", s.GetReverse()) + setIf(h, "blink", s.GetBlink()) + setIf(h, "faint", s.GetFaint()) + setIf(h, "inline", s.GetInline()) + + setIf(h, "foreground", colorToValue(s.GetForeground())) + setIf(h, "background", colorToValue(s.GetBackground())) + + setIf(h, "width", s.GetWidth()) + setIf(h, "height", s.GetHeight()) + setIf(h, "max_width", s.GetMaxWidth()) + setIf(h, "max_height", s.GetMaxHeight()) + + if horizontal := s.GetAlignHorizontal(); horizontal != lipgloss.Left { + h["align_horizontal"] = float64(horizontal) + } + if vertical := s.GetAlignVertical(); vertical != lipgloss.Top { + h["align_vertical"] = float64(vertical) + } + + if t, r, b, l := s.GetPadding(); t+r+b+l > 0 { + h["padding"] = []int{t, r, b, l} + } + if t, r, b, l := s.GetMargin(); t+r+b+l > 0 { + h["margin"] = []int{t, r, b, l} + } + + border, top, right, bottom, left := s.GetBorder() + if top || right || bottom || left { + h["border"] = borderToValue(border) + h["border_sides"] = []bool{top, right, bottom, left} + + foregrounds := []any{ + colorToValue(s.GetBorderTopForeground()), + colorToValue(s.GetBorderRightForeground()), + colorToValue(s.GetBorderBottomForeground()), + colorToValue(s.GetBorderLeftForeground()), + } + backgrounds := []any{ + colorToValue(s.GetBorderTopBackground()), + colorToValue(s.GetBorderRightBackground()), + colorToValue(s.GetBorderBottomBackground()), + colorToValue(s.GetBorderLeftBackground()), + } + + for _, c := range foregrounds { + if c != nil { + h["border_foreground"] = foregrounds + break + } + } + for _, c := range backgrounds { + if c != nil { + h["border_background"] = backgrounds + break + } + } + } + + return h +} + +//export lipgloss_style_to_json +func lipgloss_style_to_json(id C.ulonglong) *C.char { + data, err := json.Marshal(styleToHash(getStyle(uint64(id)))) + if err != nil { + return C.CString("{}") + } + + return C.CString(string(data)) +} diff --git a/lib/lipgloss.rb b/lib/lipgloss.rb index cbec822..7eb8be4 100644 --- a/lib/lipgloss.rb +++ b/lib/lipgloss.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true # rbs_inline: enabled +require "json" require_relative "lipgloss/version" begin diff --git a/lib/lipgloss/style.rb b/lib/lipgloss/style.rb index 973ea9d..bf804ef 100644 --- a/lib/lipgloss/style.rb +++ b/lib/lipgloss/style.rb @@ -20,5 +20,10 @@ def align_horizontal(position) def align_vertical(position) _align_vertical(Position.resolve(position)) end + + # @rbs return: Hash[Symbol, untyped] + def to_h + JSON.parse(_to_json, symbolize_names: true) + end end end diff --git a/sig/lipgloss/lipgloss.rbs b/sig/lipgloss/lipgloss.rbs index 6c9a735..680f643 100644 --- a/sig/lipgloss/lipgloss.rbs +++ b/sig/lipgloss/lipgloss.rbs @@ -92,6 +92,7 @@ module Lipgloss def set_string: (String string) -> Style def inherit: (Style other) -> Style + def _to_json: () -> String def unset_bold: () -> Style def unset_italic: () -> Style diff --git a/sig/lipgloss/style.rbs b/sig/lipgloss/style.rbs index f5a1c0a..469e362 100644 --- a/sig/lipgloss/style.rbs +++ b/sig/lipgloss/style.rbs @@ -13,5 +13,8 @@ module Lipgloss # @rbs position: Position::position_value # @rbs return: Style def align_vertical: (Position::position_value position) -> Style + + # @rbs return: Hash[Symbol, untyped] + def to_h: () -> Hash[Symbol, untyped] end end diff --git a/test/style_to_h_test.rb b/test/style_to_h_test.rb new file mode 100644 index 0000000..e787403 --- /dev/null +++ b/test/style_to_h_test.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require_relative "test_helper" + +module Lipgloss + class StyleToHTest < Minitest::Spec + it "is empty for a new style" do + assert_equal({}, Lipgloss::Style.new.to_h) + end + + it "includes attributes and colors that are set" do + hash = Lipgloss::Style.new.bold(true).italic(true).foreground("#7aa2f7").background("235").to_h + + assert_equal true, hash[:bold] + assert_equal true, hash[:italic] + assert_equal "#7aa2f7", hash[:foreground] + assert_equal "235", hash[:background] + refute hash.key?(:underline) + end + + it "includes sizes, alignment, padding, and margin" do + hash = Lipgloss::Style.new.width(20).height(3).align(:center, :bottom).padding(1, 2).margin(0, 1).to_h + + assert_equal 20, hash[:width] + assert_equal 3, hash[:height] + assert_in_delta 0.5, hash[:align_horizontal] + assert_in_delta 1.0, hash[:align_vertical] + assert_equal [1, 2, 1, 2], hash[:padding] + assert_equal [0, 1, 0, 1], hash[:margin] + end + + it "includes the border, its sides, and its colors" do + hash = Lipgloss::Style.new.border(:rounded).border_foreground("63").to_h + + assert_equal "╭", hash[:border][:top_left] + assert_equal "─", hash[:border][:top] + assert_equal [true, true, true, true], hash[:border_sides] + assert_equal ["63", "63", "63", "63"], hash[:border_foreground] + refute hash.key?(:border_background) + end + + it "includes adaptive colors as a hash" do + hash = Lipgloss::Style.new.foreground(Lipgloss::AdaptiveColor.new(light: "#000000", dark: "#ffffff")).to_h + + assert_equal({ light: "#000000", dark: "#ffffff" }, hash[:foreground]) + end + end +end