Adds deno.noColor (#1716)

This commit is contained in:
Ryan Dahl 2019-02-08 22:13:04 -05:00 committed by GitHub
parent 4c869dc885
commit 526497bc29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 45 additions and 9 deletions

11
Docs.md
View file

@ -305,6 +305,17 @@ import { test, assertEqual } from "./package.ts";
This design circumvents a plethora of complexity spawned by package management
software, centralized code repositories, and superfluous file formats.
## Environmental Variables
There are several env vars that control how Deno behaves:
`DENO_DIR` defaults to `$HOME/.deno` but can be set to any path to control where
generated and cached source code is written and read to.
`NO_COLOR` will turn off color output if set. See https://no-color.org/. User
code can test if `NO_COLOR` was set without having `--allow-env` by using the
boolean constant `deno.noColor`.
## Browser compatibility
The subset of Deno programs which are written completely in JavaScript and do

View file

@ -1,7 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
// Public deno module.
export { pid, env, exit, isTTY } from "./os";
export { noColor, pid, env, exit, isTTY } from "./os";
export { chdir, cwd } from "./dir";
export {
File,

View file

@ -39,7 +39,7 @@ export default function denoMain() {
os.exit(0);
}
os.setPid(startResMsg.pid());
os.setGlobals(startResMsg.pid(), startResMsg.noColor());
const cwd = startResMsg.cwd();
log("cwd", cwd);

View file

@ -9,9 +9,13 @@ import * as util from "./util";
/** process id */
export let pid: number;
export function setPid(pid_: number): void {
/** Reflects the NO_COLOR enviromental variable: https://no-color.org/ */
export let noColor: boolean;
export function setGlobals(pid_: number, noColor_: boolean): void {
assert(!pid);
pid = pid_;
noColor = noColor_;
}
interface CodeInfo {

View file

@ -160,6 +160,7 @@ table StartRes {
version_flag: bool;
deno_version: string;
v8_version: string;
no_color: bool;
}
table WorkerGetMessage {

View file

@ -1,5 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use atty;
use crate::ansi;
use crate::errors;
use crate::errors::{DenoError, DenoResult, ErrorKind};
use crate::fs as deno_fs;
@ -18,8 +20,6 @@ use crate::resources::table_entries;
use crate::resources::Resource;
use crate::tokio_util;
use crate::version;
use atty;
use flatbuffers::FlatBufferBuilder;
use futures;
use futures::Async;
@ -33,10 +33,6 @@ use std;
use std::convert::From;
use std::fs;
use std::net::Shutdown;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
@ -48,6 +44,11 @@ use tokio::net::TcpStream;
use tokio_process::CommandExt;
use tokio_threadpool;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;
type OpResult = DenoResult<Buf>;
// TODO Ideally we wouldn't have to box the Op being returned.
@ -266,6 +267,7 @@ fn op_start(
version_flag: state.flags.version,
v8_version: Some(v8_version_off),
deno_version: Some(deno_version_off),
no_color: !ansi::use_color(),
..Default::default()
},
);

2
tests/no_color.js Normal file
View file

@ -0,0 +1,2 @@
import { noColor } from "deno";
console.log("noColor", noColor);

View file

@ -8,6 +8,7 @@ from integration_tests import integration_tests
from deno_dir_test import deno_dir_test
from setup_test import setup_test
from util import build_path, enable_ansi_colors, executable_suffix, run, rmtree
from util import run_output, tests_path, green_ok
from unit_tests import unit_tests
from util_test import util_test
from benchmark_test import benchmark_test
@ -25,6 +26,19 @@ def check_exists(filename):
sys.exit(1)
def test_no_color(deno_exe):
sys.stdout.write("no_color test...")
sys.stdout.flush()
t = os.path.join(tests_path, "no_color.js")
output = run_output([deno_exe, t], merge_env={"NO_COLOR": "1"})
assert output.strip() == "noColor true"
t = os.path.join(tests_path, "no_color.js")
output = run_output([deno_exe, t])
assert output.strip() == "noColor false"
print green_ok()
def main(argv):
if len(argv) == 2:
build_dir = sys.argv[1]
@ -81,6 +95,8 @@ def main(argv):
deno_dir_test(deno_exe, deno_dir)
test_no_color(deno_exe)
if __name__ == '__main__':
sys.exit(main(sys.argv))