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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ color = Lipgloss::CompleteAdaptiveColor.new(light: light, dark: dark)
style = Lipgloss::Style.new.foreground(color)
```

**Color profile:**

Lip Gloss detects how many colors the terminal supports when it first renders. Set the profile to force colors on, for example in CI or golden tests, or off:

```ruby
Lipgloss.color_profile # => :true_color, :ansi256, :ansi, or :ascii
Lipgloss.color_profile = :true_color # always render colors
Lipgloss.color_profile = :ascii # never render colors
Lipgloss.has_dark_background = true # pick the dark variant of adaptive colors
```

### Style Inheritance

**Inherit styles from a parent:**
Expand Down Expand Up @@ -415,6 +426,9 @@ puts tree.render
| `Lipgloss.place_horizontal(width, position, string)` | Place horizontally |
| `Lipgloss.place_vertical(height, position, string)` | Place vertically |
| `Lipgloss.has_dark_background?` | Check terminal background |
| `Lipgloss.has_dark_background = bool` | Set terminal background |
| `Lipgloss.color_profile` | Get the color profile |
| `Lipgloss.color_profile = profile` | Set the color profile |

### Color Blending

Expand Down
17 changes: 17 additions & 0 deletions ext/lipgloss/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ static VALUE lipgloss_has_dark_background_rb(VALUE self) {
return lipgloss_has_dark_background() ? Qtrue : Qfalse;
}

static VALUE lipgloss_color_profile_rb(VALUE self) {
return INT2NUM(lipgloss_color_profile());
}

static VALUE lipgloss_set_color_profile_rb(VALUE self, VALUE profile) {
lipgloss_set_color_profile(NUM2INT(profile));
return profile;
}

static VALUE lipgloss_set_has_dark_background_rb(VALUE self, VALUE dark) {
lipgloss_set_has_dark_background(RTEST(dark) ? 1 : 0);
return dark;
}

static VALUE lipgloss_upstream_version_rb(VALUE self) {
char *version = lipgloss_upstream_version();
VALUE rb_version = rb_utf8_str_new_cstr(version);
Expand Down Expand Up @@ -187,6 +201,9 @@ __attribute__((__visibility__("default"))) void Init_lipgloss(void) {
rb_define_singleton_method(mLipgloss, "_place_horizontal", lipgloss_place_horizontal_rb, 3);
rb_define_singleton_method(mLipgloss, "_place_vertical", lipgloss_place_vertical_rb, 3);
rb_define_singleton_method(mLipgloss, "has_dark_background?", lipgloss_has_dark_background_rb, 0);
rb_define_singleton_method(mLipgloss, "has_dark_background=", lipgloss_set_has_dark_background_rb, 1);
rb_define_singleton_method(mLipgloss, "_color_profile", lipgloss_color_profile_rb, 0);
rb_define_singleton_method(mLipgloss, "_set_color_profile", lipgloss_set_color_profile_rb, 1);
rb_define_singleton_method(mLipgloss, "upstream_version", lipgloss_upstream_version_rb, 0);
rb_define_singleton_method(mLipgloss, "version", lipgloss_version_rb, 0);
}
8 changes: 5 additions & 3 deletions go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ module github.com/marcoroth/lipgloss-ruby/go

go 1.23.0

require github.com/charmbracelet/lipgloss v1.1.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
)

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/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/muesli/termenv v0.16.0 // 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
Expand Down
16 changes: 16 additions & 0 deletions go/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "C"
import (
"encoding/json"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
)

//export lipgloss_join_horizontal
Expand Down Expand Up @@ -111,3 +112,18 @@ func lipgloss_has_dark_background() C.int {

return 0
}

//export lipgloss_color_profile
func lipgloss_color_profile() C.int {
return C.int(lipgloss.ColorProfile())
}

//export lipgloss_set_color_profile
func lipgloss_set_color_profile(profile C.int) {
lipgloss.SetColorProfile(termenv.Profile(profile))
}

//export lipgloss_set_has_dark_background
func lipgloss_set_has_dark_background(dark C.int) {
lipgloss.SetHasDarkBackground(dark != 0)
}
20 changes: 20 additions & 0 deletions lib/lipgloss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ module Lipgloss

NO_TAB_CONVERSION = -1 #: Integer

# @rbs!
# type color_profile = :true_color | :ansi256 | :ansi | :ascii

COLOR_PROFILES = [:true_color, :ansi256, :ansi, :ascii].freeze #: Array[color_profile]

class << self
# @rbs position: Position::position_value
# @rbs *strings: String
Expand Down Expand Up @@ -74,5 +79,20 @@ def place_horizontal(width, position, string)
def place_vertical(height, position, string)
_place_vertical(height, Position.resolve(position), string)
end

# @rbs return: color_profile
def color_profile
COLOR_PROFILES.fetch(_color_profile)
end

# @rbs profile: color_profile
# @rbs return: void
def color_profile=(profile)
index = COLOR_PROFILES.index(profile)

raise ArgumentError, "unknown color profile #{profile.inspect}, expected one of #{COLOR_PROFILES.inspect}" unless index

_set_color_profile(index)
end
end
end
11 changes: 11 additions & 0 deletions sig/lipgloss.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ module Lipgloss

NO_TAB_CONVERSION: Integer

type color_profile = :true_color | :ansi256 | :ansi | :ascii

COLOR_PROFILES: Array[color_profile]

# @rbs position: Position::position_value
# @rbs *strings: String
# @rbs return: String
Expand Down Expand Up @@ -56,4 +60,11 @@ module Lipgloss
# @rbs string: String
# @rbs return: String
def self.place_vertical: (Integer height, Position::position_value position, String string) -> String

# @rbs return: color_profile
def self.color_profile: () -> color_profile

# @rbs profile: color_profile
# @rbs return: void
def self.color_profile=: (color_profile profile) -> void
end
3 changes: 3 additions & 0 deletions sig/lipgloss/lipgloss.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module Lipgloss
def self._place_horizontal: (Integer width, Float position, String string) -> String
def self._place_vertical: (Integer height, Float position, String string) -> String
def self.has_dark_background?: () -> bool
def self.has_dark_background=: (bool dark) -> bool
def self._color_profile: () -> Integer
def self._set_color_profile: (Integer profile) -> Integer
def self.upstream_version: () -> String
def self.version: () -> String

Expand Down
39 changes: 39 additions & 0 deletions test/color_profile_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require_relative "test_helper"

module Lipgloss
class ColorProfileTest < Minitest::Spec
before do
@profile = Lipgloss.color_profile
end

after do
Lipgloss.color_profile = @profile
end

it "renders without colors for the ascii profile" do
Lipgloss.color_profile = :ascii

assert_equal :ascii, Lipgloss.color_profile
assert_equal "hello", Lipgloss::Style.new.foreground("212").bold(true).render("hello")
end

it "renders colors for the true_color profile" do
Lipgloss.color_profile = :true_color

assert_equal :true_color, Lipgloss.color_profile
assert_includes Lipgloss::Style.new.foreground("#ff0000").render("hello"), "\e[38;2;255;0;0m"
end

it "renders 256 colors for the ansi256 profile" do
Lipgloss.color_profile = :ansi256

assert_includes Lipgloss::Style.new.foreground("212").render("hello"), "\e[38;5;212m"
end

it "rejects unknown profiles" do
assert_raises(ArgumentError) { Lipgloss.color_profile = :rainbow }
end
end
end
Loading