diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-10-15 12:32:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-15 12:32:03 +0200 |
commit | bd0c64b9aeeb75eea25402b0ebd5aecc2cec8e3a (patch) | |
tree | f9b52b421b9cd32520359223910a388c95f4d97f | |
parent | 81635c59e66b81a4c85aed6775aff4beedf71f14 (diff) |
Reland feat(cli/console): inspect with colors regardless of Deno.noColor (#7976)
-rw-r--r-- | cli/repl.rs | 2 | ||||
-rw-r--r-- | cli/rt/01_colors.js | 9 | ||||
-rw-r--r-- | cli/rt/02_console.js | 19 | ||||
-rw-r--r-- | cli/rt/40_testing.js | 7 |
4 files changed, 24 insertions, 13 deletions
diff --git a/cli/repl.rs b/cli/repl.rs index 506a79f0d..3540b422b 100644 --- a/cli/repl.rs +++ b/cli/repl.rs @@ -378,7 +378,7 @@ pub async fn run( "Runtime.callFunctionOn", Some(json!({ "executionContextId": context_id, - "functionDeclaration": "function (object) { return Deno[Deno.internal].inspectArgs(['%o', object], { colors: true}); }", + "functionDeclaration": "function (object) { return Deno[Deno.internal].inspectArgs(['%o', object], { colors: !Deno.noColor }); }", "arguments": [ evaluate_result, ], diff --git a/cli/rt/01_colors.js b/cli/rt/01_colors.js index 2dc559186..39e4a7a18 100644 --- a/cli/rt/01_colors.js +++ b/cli/rt/01_colors.js @@ -10,9 +10,7 @@ } function run(str, code) { - return !globalThis || !globalThis.Deno || globalThis.Deno.noColor - ? str - : `${code.open}${str.replace(code.regexp, code.open)}${code.close}`; + return `${code.open}${str.replace(code.regexp, code.open)}${code.close}`; } function bold(str) { @@ -72,6 +70,10 @@ return string.replace(ANSI_PATTERN, ""); } + function maybeColor(fn) { + return !(globalThis.Deno?.noColor ?? false) ? fn : (s) => s; + } + window.__bootstrap.colors = { bold, italic, @@ -85,5 +87,6 @@ magenta, dim, stripColor, + maybeColor, }; })(this); diff --git a/cli/rt/02_console.js b/cli/rt/02_console.js index 0eb7f0aac..bb2b6ea41 100644 --- a/cli/rt/02_console.js +++ b/cli/rt/02_console.js @@ -1425,10 +1425,12 @@ const timerMap = new Map(); const isConsoleInstance = Symbol("isConsoleInstance"); - const CONSOLE_INSPECT_OPTIONS = { - ...DEFAULT_INSPECT_OPTIONS, - colors: true, - }; + function getConsoleInspectOptions() { + return { + ...DEFAULT_INSPECT_OPTIONS, + colors: !(globalThis.Deno?.noColor ?? false), + }; + } class Console { #printFunc = null; @@ -1451,7 +1453,7 @@ log = (...args) => { this.#printFunc( inspectArgs(args, { - ...CONSOLE_INSPECT_OPTIONS, + ...getConsoleInspectOptions(), indentLevel: this.indentLevel, }) + "\n", false, @@ -1463,7 +1465,8 @@ dir = (obj, options = {}) => { this.#printFunc( - inspectArgs([obj], { ...CONSOLE_INSPECT_OPTIONS, ...options }) + "\n", + inspectArgs([obj], { ...getConsoleInspectOptions(), ...options }) + + "\n", false, ); }; @@ -1473,7 +1476,7 @@ warn = (...args) => { this.#printFunc( inspectArgs(args, { - ...CONSOLE_INSPECT_OPTIONS, + ...getConsoleInspectOptions(), indentLevel: this.indentLevel, }) + "\n", true, @@ -1679,7 +1682,7 @@ trace = (...args) => { const message = inspectArgs( args, - { ...CONSOLE_INSPECT_OPTIONS, indentLevel: 0 }, + { ...getConsoleInspectOptions(), indentLevel: 0 }, ); const err = { name: "Trace", diff --git a/cli/rt/40_testing.js b/cli/rt/40_testing.js index c374e0ca6..082d17fe0 100644 --- a/cli/rt/40_testing.js +++ b/cli/rt/40_testing.js @@ -2,7 +2,7 @@ ((window) => { const core = window.Deno.core; - const { gray, green, italic, red, yellow } = window.__bootstrap.colors; + const colors = window.__bootstrap.colors; const { exit } = window.__bootstrap.os; const { Console, inspectArgs } = window.__bootstrap.console; const { stdout } = window.__bootstrap.files; @@ -19,6 +19,8 @@ } function formatDuration(time = 0) { + const gray = colors.maybeColor(colors.gray); + const italic = colors.maybeColor(colors.italic); const timeStr = `(${time}ms)`; return gray(italic(timeStr)); } @@ -139,6 +141,9 @@ finishing test case.`; } function reportToConsole(message) { + const green = colors.maybeColor(colors.green); + const red = colors.maybeColor(colors.red); + const yellow = colors.maybeColor(colors.yellow); const redFailed = red("FAILED"); const greenOk = green("ok"); const yellowIgnored = yellow("ignored"); |