From 6a030a5396f9c838b4d4523f43ab2d9e2f502e04 Mon Sep 17 00:00:00 2001 From: Antonio Musolino Date: Tue, 1 Mar 2022 04:37:50 +0100 Subject: [PATCH] fix(runtime): disable console color for non tty stdout (#13782) --- cli/main.rs | 2 ++ cli/standalone.rs | 1 + cli/tests/unit/tty_color_test.ts | 14 ++++++++++++++ runtime/colors.rs | 7 +++++++ runtime/examples/hello_runtime.rs | 1 + runtime/js/99_main.js | 6 ++++-- runtime/worker.rs | 1 + runtime/worker_bootstrap.rs | 2 ++ 8 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 cli/tests/unit/tty_color_test.ts diff --git a/cli/main.rs b/cli/main.rs index d1f521a816..1bbc839e8e 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -156,6 +156,7 @@ fn create_web_worker_callback(ps: ProcState) -> Arc { enable_testing_features: ps.flags.enable_testing_features, location: Some(args.main_module.clone()), no_color: !colors::use_color(), + is_tty: colors::is_tty(), runtime_version: version::deno(), ts_version: version::TYPESCRIPT.to_string(), unstable: ps.flags.unstable, @@ -256,6 +257,7 @@ pub fn create_main_worker( enable_testing_features: ps.flags.enable_testing_features, location: ps.flags.location.clone(), no_color: !colors::use_color(), + is_tty: colors::is_tty(), runtime_version: version::deno(), ts_version: version::TYPESCRIPT.to_string(), unstable: ps.flags.unstable, diff --git a/cli/standalone.rs b/cli/standalone.rs index bd7bef8acb..bea29f2636 100644 --- a/cli/standalone.rs +++ b/cli/standalone.rs @@ -276,6 +276,7 @@ pub async fn run( enable_testing_features: false, location: metadata.location, no_color: !colors::use_color(), + is_tty: colors::is_tty(), runtime_version: version::deno(), ts_version: version::TYPESCRIPT.to_string(), unstable: metadata.unstable, diff --git a/cli/tests/unit/tty_color_test.ts b/cli/tests/unit/tty_color_test.ts new file mode 100644 index 0000000000..d64c278bf2 --- /dev/null +++ b/cli/tests/unit/tty_color_test.ts @@ -0,0 +1,14 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +import { assertEquals } from "./test_util.ts"; + +// Note tests for Deno.setRaw is in integration tests. + +Deno.test({ permissions: { run: true } }, async function noColorIfNotTty() { + const p = Deno.run({ + cmd: [Deno.execPath(), "eval", "console.log(1)"], + stdout: "piped", + }); + const output = new TextDecoder().decode(await p.output()); + assertEquals(output, "1\n"); + p.close(); +}); diff --git a/runtime/colors.rs b/runtime/colors.rs index f2dbf22bd6..196e522e4d 100644 --- a/runtime/colors.rs +++ b/runtime/colors.rs @@ -1,5 +1,6 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +use atty; use once_cell::sync::Lazy; use std::fmt; use std::io::Write; @@ -12,6 +13,12 @@ use termcolor::{BufferWriter, ColorChoice}; static NO_COLOR: Lazy = Lazy::new(|| std::env::var_os("NO_COLOR").is_some()); +static IS_TTY: Lazy = Lazy::new(|| atty::is(atty::Stream::Stdout)); + +pub fn is_tty() -> bool { + *IS_TTY +} + pub fn use_color() -> bool { !(*NO_COLOR) } diff --git a/runtime/examples/hello_runtime.rs b/runtime/examples/hello_runtime.rs index e74920c340..0a0d6fe236 100644 --- a/runtime/examples/hello_runtime.rs +++ b/runtime/examples/hello_runtime.rs @@ -35,6 +35,7 @@ async fn main() -> Result<(), AnyError> { enable_testing_features: false, location: None, no_color: false, + is_tty: false, runtime_version: "x".to_string(), ts_version: "x".to_string(), unstable: false, diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index fb5de250c0..a0f0596031 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -574,13 +574,14 @@ delete Object.prototype.__proto__; args, location: locationHref, noColor, + isTty, pid, ppid, unstableFlag, cpuCount, } = runtimeOptions; - colors.setNoColor(noColor); + colors.setNoColor(noColor || !isTty); if (locationHref != null) { location.setLocationHref(locationHref); } @@ -666,12 +667,13 @@ delete Object.prototype.__proto__; unstableFlag, pid, noColor, + isTty, args, location: locationHref, cpuCount, } = runtimeOptions; - colors.setNoColor(noColor); + colors.setNoColor(noColor || !isTty); location.setLocationHref(locationHref); numCpus = cpuCount; registerErrors(); diff --git a/runtime/worker.rs b/runtime/worker.rs index 1e31b84dc5..b3f7b2350d 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -360,6 +360,7 @@ mod tests { enable_testing_features: false, location: None, no_color: true, + is_tty: false, runtime_version: "x".to_string(), ts_version: "x".to_string(), unstable: false, diff --git a/runtime/worker_bootstrap.rs b/runtime/worker_bootstrap.rs index 9bd519d735..05bde731f9 100644 --- a/runtime/worker_bootstrap.rs +++ b/runtime/worker_bootstrap.rs @@ -15,6 +15,7 @@ pub struct BootstrapOptions { pub location: Option, /// Sets `Deno.noColor` in JS runtime. pub no_color: bool, + pub is_tty: bool, /// Sets `Deno.version.deno` in JS runtime. pub runtime_version: String, /// Sets `Deno.version.typescript` in JS runtime. @@ -33,6 +34,7 @@ impl BootstrapOptions { "denoVersion": self.runtime_version, "location": self.location, "noColor": self.no_color, + "isTty": self.is_tty, "tsVersion": self.ts_version, "unstableFlag": self.unstable, // Web worker only