summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorAntonio Musolino <antoniomusolino007@gmail.com>2022-03-01 04:37:50 +0100
committerGitHub <noreply@github.com>2022-03-01 12:37:50 +0900
commit6a030a5396f9c838b4d4523f43ab2d9e2f502e04 (patch)
tree2ad4c356bb5c5ec5b91dbcd5dd9e3442fed0c2bc /runtime
parenta41d399f5f728c73b652c3f5f5c2bf57d2054153 (diff)
fix(runtime): disable console color for non tty stdout (#13782)
Diffstat (limited to 'runtime')
-rw-r--r--runtime/colors.rs7
-rw-r--r--runtime/examples/hello_runtime.rs1
-rw-r--r--runtime/js/99_main.js6
-rw-r--r--runtime/worker.rs1
-rw-r--r--runtime/worker_bootstrap.rs2
5 files changed, 15 insertions, 2 deletions
diff --git a/runtime/colors.rs b/runtime/colors.rs
index f2dbf22bd..196e522e4 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<bool> =
Lazy::new(|| std::env::var_os("NO_COLOR").is_some());
+static IS_TTY: Lazy<bool> = 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 e74920c34..0a0d6fe23 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 fb5de250c..a0f059603 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 1e31b84dc..b3f7b2350 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 9bd519d73..05bde731f 100644
--- a/runtime/worker_bootstrap.rs
+++ b/runtime/worker_bootstrap.rs
@@ -15,6 +15,7 @@ pub struct BootstrapOptions {
pub location: Option<ModuleSpecifier>,
/// 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