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: add Deno.colorDepth #28122

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cli/tsc/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,13 @@ declare namespace Deno {
*/
export const noColor: boolean;

/**
* Get the terminal's color depth.
*
* @category Runtime
*/
export const colorDepth: 1 | 4 | 8 | 24;

/**
* Returns the release version of the Operating System.
*
Expand Down
2 changes: 1 addition & 1 deletion ext/node/polyfills/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class WriteStream extends Socket {
// TODO(@marvinhagemeister): Ignore env parameter.
// Haven't seen it used anywhere, seems more done
// to make testing easier in Node
return op_bootstrap_color_depth();
return Deno.colorDepth;
}
}

Expand Down
2 changes: 2 additions & 0 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { core, internals, primordials } from "ext:core/mod.js";
const ops = core.ops;
import {
op_bootstrap_args,
op_bootstrap_color_depth,
op_bootstrap_is_stderr_tty,
op_bootstrap_is_stdout_tty,
op_bootstrap_no_color,
Expand Down Expand Up @@ -599,6 +600,7 @@ ObjectDefineProperties(finalDenoNs, {
// https://github.com/denoland/deno/issues/23004
ppid: core.propGetterOnly(() => op_ppid()),
noColor: core.propGetterOnly(() => op_bootstrap_no_color()),
colorDepth: core.propGetterOnly(() => op_bootstrap_color_depth()),
args: core.propGetterOnly(opArgs),
mainModule: core.propGetterOnly(() => op_main_module()),
exitCode: {
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/tty_color_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2025 the Deno authors. MIT license.
import { assertEquals } from "./test_util.ts";
import { assert, assertEquals } from "./test_util.ts";

// Note tests for Deno.stdin.setRaw is in integration tests.

Expand Down Expand Up @@ -53,3 +53,28 @@ Deno.test(
assertEquals(output, "true\n");
},
);

Deno.test(
{ permissions: { run: true, read: true } },
async function denoColorDepth() {
const { stdout } = await new Deno.Command(Deno.execPath(), {
args: ["eval", "console.log(Deno.colorDepth)"],
}).output();
const output = new TextDecoder().decode(stdout).trim();
assert([1, 4, 8, 24].includes(+output));
},
);

Deno.test(
{ permissions: { run: true, read: true } },
async function denoColorDepthDisabled() {
const { stdout } = await new Deno.Command(Deno.execPath(), {
args: ["eval", "console.log(Deno.colorDepth)"],
env: {
NO_COLOR: "1",
},
}).output();
const output = new TextDecoder().decode(stdout).trim();
assert([1, 4, 8, 24].includes(+output));
},
);