mirror of
https://github.com/denoland/deno.git
synced 2024-12-26 09:10:40 -05:00
fix(runtime): disable console color for non tty stdout (#13782)
This commit is contained in:
parent
a41d399f5f
commit
6a030a5396
8 changed files with 32 additions and 2 deletions
|
@ -156,6 +156,7 @@ fn create_web_worker_callback(ps: ProcState) -> Arc<CreateWebWorkerCb> {
|
|||
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,
|
||||
|
|
|
@ -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,
|
||||
|
|
14
cli/tests/unit/tty_color_test.ts
Normal file
14
cli/tests/unit/tty_color_test.ts
Normal file
|
@ -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();
|
||||
});
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue