Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to 256color if COLORTERM != truecolor #604

Merged
merged 2 commits into from
Nov 13, 2023

Conversation

tompng
Copy link
Member

@tompng tompng commented Nov 10, 2023

Face supports hex color '#aabbcc' but some terminal does not support truecolor. (example: macOS Terminal.app)
This pull request adds fallback to 256color if ENV['COLORTERM'] is not 'truecolor' | '24bit'

About 256color

0..15 is standard color and high intensity color (this color table is customizable by terminal emulator's config)
16..231 is 216 color (r,g,b each 6 steps, 6**3 == 216) This pull request uses this part.
232..255 grayscale 24step. This pull request does not use this part event if the specified color is gray.

Each R,G,B in 216 color are 6 steps, [0, 95, 135, 175, 215, 255]
In ruby code, steps are [0, *95.step(255,40)]
when value > 95, color index can be calculated by 1 + ((value-95)/40.0).round or in short, (value - 35) / 40

COLORTERM env

Many tools uses ENV['COLORTERM'] == 'truecolor' or ENV['COLORTERM'] in 'truecolor' | '24bit' to check if truecolor is supported.
https://github.com/search?q=COLORTERM&type=code

Some terminal emulator does not set env COLORTERM even if it supports truecolor. (example: Windows Terminal)
To enable truecolor without COLORTERM env, I added Reline::Face.force_truecolor

Reline::Face.force_truecolor
Reline::Face.config(:foo) do |conf|
  conf.define :bar, foreground: '#123456'
end
# Reline::Face.force_truecolor # Writing here is also ok. It will regenerate escape sequences for truecolor

@hasumikin
Copy link
Collaborator

image

In my environment (Windows terminal | WSL2 | Ubuntu | bash | tmux), $TERMCOLOR is not set, and $TERM is screen-256color.
However, truecolor actually works (see the green gradation in the screenshot).
This means we can not use environment variables to determine if truecolor can be specified or not.

My suggestion is that the #sgr_rgb_256color should only work if the user has explicitly set the Reline not to use truecolor.
Just a casual idea but for example:

Reline::Face.truecolor = false

Copy link
Collaborator

@hasumikin hasumikin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine

@tompng tompng merged commit 090e1e4 into ruby:master Nov 13, 2023
31 checks passed
@tompng tompng deleted the face_256color branch November 13, 2023 12:42
matzbot pushed a commit to ruby/ruby that referenced this pull request Nov 13, 2023
(ruby/reline#604)

* Fallback to 256color if COLORTERM != truecolor

* Add Reline::Face.force_truecolor to force truecolor without COLORTERM env

ruby/reline@090e1e4df0
@ima1zumi ima1zumi mentioned this pull request Nov 28, 2023
@ima1zumi ima1zumi added the enhancement New feature or request label Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

3 participants