From 5c0bda5a70a9a7005277e6ac3f7e5e30be509aa4 Mon Sep 17 00:00:00 2001 From: Marco Roth Date: Wed, 18 Nov 2026 23:49:36 +0100 Subject: [PATCH] Add `Lipgloss.color_profile=` to force colors on or off --- README.md | 14 ++++++++++++++ ext/lipgloss/extension.c | 17 +++++++++++++++++ go/go.mod | 8 +++++--- go/layout.go | 16 ++++++++++++++++ lib/lipgloss.rb | 20 +++++++++++++++++++ sig/lipgloss.rbs | 11 +++++++++++ sig/lipgloss/lipgloss.rbs | 3 +++ test/color_profile_test.rb | 39 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 test/color_profile_test.rb diff --git a/README.md b/README.md index ddd57cc..20534cd 100644 --- a/README.md +++ b/README.md @@ -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:** @@ -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 diff --git a/ext/lipgloss/extension.c b/ext/lipgloss/extension.c index 9fdfcd7..166ae85 100644 --- a/ext/lipgloss/extension.c +++ b/ext/lipgloss/extension.c @@ -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); @@ -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); } diff --git a/go/go.mod b/go/go.mod index 4d035de..bceb598 100644 --- a/go/go.mod +++ b/go/go.mod @@ -2,7 +2,11 @@ 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 @@ -10,10 +14,8 @@ require ( 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 diff --git a/go/layout.go b/go/layout.go index 1eb696e..ef449ee 100644 --- a/go/layout.go +++ b/go/layout.go @@ -5,6 +5,7 @@ import "C" import ( "encoding/json" "github.com/charmbracelet/lipgloss" + "github.com/muesli/termenv" ) //export lipgloss_join_horizontal @@ -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) +} diff --git a/lib/lipgloss.rb b/lib/lipgloss.rb index 7eb8be4..08e905b 100644 --- a/lib/lipgloss.rb +++ b/lib/lipgloss.rb @@ -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 @@ -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 diff --git a/sig/lipgloss.rbs b/sig/lipgloss.rbs index 9669f1c..f248287 100644 --- a/sig/lipgloss.rbs +++ b/sig/lipgloss.rbs @@ -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 @@ -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 diff --git a/sig/lipgloss/lipgloss.rbs b/sig/lipgloss/lipgloss.rbs index 680f643..43c8571 100644 --- a/sig/lipgloss/lipgloss.rbs +++ b/sig/lipgloss/lipgloss.rbs @@ -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 diff --git a/test/color_profile_test.rb b/test/color_profile_test.rb new file mode 100644 index 0000000..deaeead --- /dev/null +++ b/test/color_profile_test.rb @@ -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