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 --- ext/node/polyfills/tty.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'ext/node/polyfills') diff --git a/ext/node/polyfills/tty.js b/ext/node/polyfills/tty.js index 3a84a1f94..2e8ecc8e1 100644 --- a/ext/node/polyfills/tty.js +++ b/ext/node/polyfills/tty.js @@ -1,5 +1,6 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +import { op_bootstrap_color_depth } from "ext:core/ops"; import { core, primordials } from "ext:core/mod.js"; const { Error, @@ -105,6 +106,32 @@ export class WriteStream extends Socket { this.rows = rows; this.isTTY = true; } + + /** + * @param {number | Record} [count] + * @param {Record} [env] + * @returns {boolean} + */ + hasColors(count, env) { + if (env === undefined && typeof count === "object") { + env = count; + count = 16; + } + + const depth = this.getColorDepth(env); + return count <= 2 ** depth; + } + + /** + * @param {Record