From ee2e6933403811d398540e0e8275b2d216546dd8 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 19 Jul 2024 12:39:05 +0200 Subject: fix(node): support `tty.hasColors()` and `tty.getColorDepth()` (#24619) This PR adds support for [`tty.WriteStream.prototype.hasColors()`](https://nodejs.org/api/tty.html#writestreamhascolorscount-env) and [`tty.WriteStream.prototype.getColorDepth()`](https://nodejs.org/api/tty.html#writestreamgetcolordepthenv). I couldn't find any usage on GitHub which passes parameters to it. Therefore I've skipped adding support for the `env` parameter to keep our snapshot size small. Based on https://github.com/denoland/deno_terminal/pull/3 Fixes https://github.com/denoland/deno/issues/24616 --- runtime/ops/bootstrap.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'runtime/ops') diff --git a/runtime/ops/bootstrap.rs b/runtime/ops/bootstrap.rs index c5c193ef3..997bebd76 100644 --- a/runtime/ops/bootstrap.rs +++ b/runtime/ops/bootstrap.rs @@ -2,6 +2,7 @@ use deno_core::op2; use deno_core::OpState; +use deno_terminal::colors::ColorLevel; use serde::Serialize; use crate::BootstrapOptions; @@ -16,6 +17,7 @@ deno_core::extension!( op_bootstrap_language, op_bootstrap_log_level, op_bootstrap_no_color, + op_bootstrap_color_depth, op_bootstrap_is_stdout_tty, op_bootstrap_is_stderr_tty, op_bootstrap_unstable_args, @@ -126,6 +128,17 @@ pub fn op_bootstrap_no_color(state: &mut OpState) -> bool { options.no_color } +#[op2(fast)] +pub fn op_bootstrap_color_depth(state: &mut OpState) -> i32 { + let options = state.borrow::(); + match options.color_level { + ColorLevel::None => 1, + ColorLevel::Ansi => 4, + ColorLevel::Ansi256 => 8, + ColorLevel::TrueColor => 24, + } +} + #[op2(fast)] pub fn op_bootstrap_is_stdout_tty(state: &mut OpState) -> bool { let options = state.borrow::(); -- cgit v1.2.3