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

feat(ext/console): convert colors to supported space #18549

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

marvinhagemeister
Copy link
Contributor

@marvinhagemeister marvinhagemeister commented Apr 1, 2023

The default Terminal on macOS doesn't support 24bit True Colors, which lead to colors being rendered unexpectedly at times. This can be seen in the fresh initialization wizard where the expected colors should look like this (rendered in a True Color terminal):

Screenshot 2023-04-01 at 17 15 30

With the changes in this PR we now check for the supported color space by the terminal and lower the color space if needed.

Before:

Screenshot 2023-04-01 at 17 21 20

After:

Screenshot 2023-04-01 at 17 20 56

Support table:

Terminal OS Ansi Ansi 256 24bit True Color $TERM $COLORTERM
Apple Terminal macOS xterm-256color
iTerm2 macOS xterm-256color truecolor
cmd.exe Windows
PowerShell Windows
BuildKite
GitHub Actions Linux (Ubuntu) dumb
GitLab CI

@marvinhagemeister marvinhagemeister marked this pull request as ready for review April 1, 2023 20:46
@marvinhagemeister marvinhagemeister force-pushed the console-256-colors branch 3 times, most recently from d326722 to 1c6ef5d Compare April 1, 2023 21:35
@marvinhagemeister marvinhagemeister marked this pull request as draft April 1, 2023 21:40
@marvinhagemeister marvinhagemeister marked this pull request as ready for review April 1, 2023 22:33
Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

Looks great @marvinhagemeister! I will wait for @kt3k's review before landing this, but overall LGTM

runtime/worker_bootstrap.rs Outdated Show resolved Hide resolved
@bartlomieju bartlomieju requested a review from kt3k April 2, 2023 16:04
return `\x1b[${kind};2;${r};${g};${b}m`;
} else if (supportLevel === COLOR_SUPPORT_256) {
// Lower colors into 256 color space
// Taken from https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js
Copy link
Member

Choose a reason for hiding this comment

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

👍

@kt3k
Copy link
Member

kt3k commented Apr 4, 2023

Looks very nice feature to have! I'm in favor of the idea.

@marvinhagemeister Can you write a few more integration type test cases which simulate various terminals/environments by setting COLORTERM, TERM, or CI env vars?

@bartlomieju
Copy link
Member

@marvinhagemeister when you find the time, could you please rebase and add a test case mentioned above?

@marvinhagemeister
Copy link
Contributor Author

@kt3k @bartlomieju Tried adding some tests but I have trouble setting the environment variables before a test in rust. It seems like env::set_var does nothing. Not sure why at the moment.

@bartlomieju
Copy link
Member

@kt3k @bartlomieju Tried adding some tests but I have trouble setting the environment variables before a test in rust. It seems like env::set_var does nothing. Not sure why at the moment.

@marvinhagemeister by default tests in Rust are run on multiple threads - I'm pretty sure the tests are overriding env vars set by other tests. I suggest to do an integration test using itest! macro somewhere in run_tests.rs. You can find examples there that use custom environmental variables.

@kt3k
Copy link
Member

kt3k commented Apr 13, 2023

@marvinhagemeister You can create a small script like the below:

console.log("%chello", "color: #123456");

in cli/tests/testdata/run with expected output files next to it. And then execute it from cli/tests/integration/run_test.rs with itest macro, which would look like, for example:

itest!(test_color_support_term_256 {
  envs: vec![("TERM".to_string(), "256".to_string())],
  args: "run run/test_color_support.js",
  output: "run/test_color_support.js.out",
});

(Note: The file names are arbitrary. You probably need several output file patterns.)

@idranme
Copy link
Contributor

idranme commented May 26, 2023

🥰

ext/console/01_colors.js Outdated Show resolved Hide resolved
ext/console/02_console.js Outdated Show resolved Hide resolved
@bartlomieju bartlomieju added this to the 1.35 milestone Jun 25, 2023
runtime/colors.rs Outdated Show resolved Hide resolved
@idranme
Copy link
Contributor

idranme commented Jul 2, 2023

Is it possible to provide an interface for applications to query color levels directly?

Such as: Deno.colorSupportLevel

@bartlomieju bartlomieju self-assigned this Jul 4, 2023
@bartlomieju bartlomieju modified the milestones: 1.35, 1.36 Jul 4, 2023
@dsherret dsherret modified the milestones: 1.36, 1.37 Aug 24, 2023
@bartlomieju bartlomieju removed their assignment Aug 27, 2023
@dsherret dsherret modified the milestones: 1.37, 1.38 Sep 18, 2023
@dsherret dsherret modified the milestones: 1.38, 1.39 Nov 1, 2023
@bartlomieju bartlomieju removed this from the 1.39 milestone Dec 4, 2023
@iuioiua
Copy link
Collaborator

iuioiua commented Dec 5, 2023

Hi guys, I just wanted to check-in. Is this something you guys still want to pursue? This is a blocker for denoland/std#2602. It'd be good to have that one closed off 🙂

@iuioiua iuioiua self-assigned this Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants