Skip to content

Commit

Permalink
Add Reline::Face.force_truecolor to force truecolor without COLORTERM…
Browse files Browse the repository at this point in the history
… env
  • Loading branch information
tompng committed Nov 12, 2023
1 parent 9fe3001 commit e86124b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/reline/face.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ def define(name, **values)
@definition[name] = values
end

def reconfigure
@definition.each_value do |values|
values.delete(:escape_sequence)
values[:escape_sequence] = format_to_sgr(values.to_a).freeze
end
end

def [](name)
@definition.dig(name, :escape_sequence) or raise ArgumentError, "unknown face: #{name}"
end
Expand All @@ -82,7 +89,7 @@ def [](name)

def sgr_rgb(key, value)
return nil unless rgb_expression?(value)
if ENV['COLORTERM'] == 'truecolor'
if Reline::Face.truecolor?
sgr_rgb_truecolor(key, value)
else
sgr_rgb_256color(key, value)
Expand Down Expand Up @@ -150,6 +157,15 @@ def rgb_expression?(color)

private_constant :SGR_PARAMETERS, :Config

def self.truecolor?
@force_truecolor || %w[truecolor 24bit].include?(ENV['COLORTERM'])
end

def self.force_truecolor
@force_truecolor = true
@configs&.each_value(&:reconfigure)
end

def self.[](name)
@configs[name]
end
Expand Down
33 changes: 33 additions & 0 deletions test/reline/test_face.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ def setup
@config = Reline::Face.const_get(:Config).new(:my_config) { }
end

def teardown
super
Reline::Face.instance_variable_set(:@force_truecolor, nil)
end

def test_rgb?
assert_equal true, @config.send(:rgb_expression?, "#FFFFFF")
end
Expand Down Expand Up @@ -199,6 +204,17 @@ def test_format_to_sgr_with_single_style
)
end

def test_truecolor
ENV['COLORTERM'] = 'truecolor'
assert_equal true, Reline::Face.truecolor?
ENV['COLORTERM'] = '24bit'
assert_equal true, Reline::Face.truecolor?
ENV['COLORTERM'] = nil
assert_equal false, Reline::Face.truecolor?
Reline::Face.force_truecolor
assert_equal true, Reline::Face.truecolor?
end

def test_sgr_rgb_truecolor
ENV['COLORTERM'] = 'truecolor'
assert_equal "38;2;255;255;255", @config.send(:sgr_rgb, :foreground, "#ffffff")
Expand All @@ -220,5 +236,22 @@ def test_sgr_rgb_256color
assert_equal '48;5;110', @config.send(:sgr_rgb, :background, '#9ac2ea')
assert_equal '48;5;153', @config.send(:sgr_rgb, :background, '#9bc3eb')
end

def test_force_truecolor_reconfigure
ENV['COLORTERM'] = nil

Reline::Face.config(:my_config) do |face|
face.define :default, foreground: '#005f87'
face.define :enhanced, background: '#afd7ff'
end

assert_equal "\e[0m\e[38;5;24m", Reline::Face[:my_config][:default]
assert_equal "\e[0m\e[48;5;153m", Reline::Face[:my_config][:enhanced]

Reline::Face.force_truecolor

assert_equal "\e[0m\e[38;2;0;95;135m", Reline::Face[:my_config][:default]
assert_equal "\e[0m\e[48;2;175;215;255m", Reline::Face[:my_config][:enhanced]
end
end
end

0 comments on commit e86124b

Please sign in to comment.