0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

feat: add Deno.colorDepth

This commit is contained in:
Marvin Hagemeister 2025-02-14 17:14:04 +01:00
parent 979e2f7158
commit cce800e3a9
4 changed files with 36 additions and 2 deletions

View file

@ -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.
*

View file

@ -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;
}
}

View file

@ -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,
@ -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: {

View file

@ -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.
@ -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));
},
);