Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
target :lib do
signature "sig"

library "json"

check "lib"
end
11 changes: 11 additions & 0 deletions ext/lipgloss/style.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
144 changes: 144 additions & 0 deletions go/style_hash.go
Original file line number Diff line number Diff line change
@@ -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))
}
1 change: 1 addition & 0 deletions lib/lipgloss.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
# rbs_inline: enabled

require "json"
require_relative "lipgloss/version"

begin
Expand Down
5 changes: 5 additions & 0 deletions lib/lipgloss/style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions sig/lipgloss/lipgloss.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions sig/lipgloss/style.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 48 additions & 0 deletions test/style_to_h_test.rb
Original file line number Diff line number Diff line change
@@ -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
Loading