Skip to content

Commit

Permalink
Adds deno.noColor (#1716)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Feb 9, 2019
1 parent 4c869dc commit 526497b
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 9 deletions.
11 changes: 11 additions & 0 deletions Docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion js/deno.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion js/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ table StartRes {
version_flag: bool;
deno_version: string;
v8_version: string;
no_color: bool;
}

table WorkerGetMessage {
Expand Down
14 changes: 8 additions & 6 deletions src/ops.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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()
},
);
Expand Down
2 changes: 2 additions & 0 deletions tests/no_color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { noColor } from "deno";
console.log("noColor", noColor);
16 changes: 16 additions & 0 deletions tools/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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
Expand All @@ -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]
Expand Down Expand Up @@ -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))

0 comments on commit 526497b

Please sign in to comment.