From b58d0f29594ad9f1a63853de82ddd198a385f4ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Wed, 11 Sep 2024 22:24:26 +0100 Subject: [PATCH 1/2] Use FORCE_COLOR environmental variable to force colorized output --- cli/rescript_bsb.js | 12 ++++++------ rescript | 8 ++++++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cli/rescript_bsb.js b/cli/rescript_bsb.js index a18f3ef0ac..294979ddd1 100644 --- a/cli/rescript_bsb.js +++ b/cli/rescript_bsb.js @@ -110,8 +110,8 @@ function clean(args) { delegate(["clean", ...args]); } -const isTtyError = process.stderr.isTTY; -const isTtyStd = process.stdout.isTTY; +const shouldColorizeError = process.stderr.isTTY || process.env.FORCE_COLOR == "1"; +const shouldColorize = process.stdout.isTTY || process.env.FORCE_COLOR == "1"; /** * @type {[number,number]} @@ -133,7 +133,7 @@ function logFinishCompiling(code) { if (code) { log = log + " (exit: " + code + ")"; } - if (isTtyStd) { + if (shouldColorize) { log = "\x1b[36m" + log + "\x1b[0m"; } if (code) { @@ -146,7 +146,7 @@ function logFinishCompiling(code) { function logStartCompiling() { updateStartTime(); let log = `>>>> Start compiling`; - if (isTtyStd) { + if (shouldColorize) { log = "\x1b[36m" + log + "\x1b[0m"; } console.log(log); @@ -265,7 +265,7 @@ function watch(args) { .on("error", function (err) { // @ts-ignore if (err !== undefined && err.code === "EADDRINUSE") { - var error = isTtyStd ? `\x1b[1;31mERROR:\x1b[0m` : `ERROR:`; + var error = shouldColorize ? `\x1b[1;31mERROR:\x1b[0m` : `ERROR:`; console.error(`${error} The websocket port number ${webSocketPort} is in use. Please pick a different one using the \`-ws [host:]port\` flag from bsb.`); } else { @@ -358,7 +358,7 @@ Please pick a different one using the \`-ws [host:]port\` flag from bsb.`); * @param highlight {string} */ function outputError(error, highlight) { - if (isTtyError && highlight) { + if (shouldColorizeError && highlight) { process.stderr.write( error.replace(highlight, "\x1b[1;31m" + highlight + "\x1b[0m"), ); diff --git a/rescript b/rescript index ea84b0f8b4..0c2b51639d 100755 --- a/rescript +++ b/rescript @@ -14,13 +14,17 @@ var bsb = require("./cli/rescript_bsb.js"); var cwd = process.cwd(); process.env.BSB_PROJECT_ROOT = cwd; -if (process.env.NINJA_ANSI_FORCED === undefined) { +if (process.env.FORCE_COLOR === undefined) { if (require("tty").isatty(1)) { + process.env.FORCE_COLOR = "1"; process.env.NINJA_ANSI_FORCED = "1"; } } else { + if (process.env.FORCE_COLOR === "1" && process.env.NINJA_ANSI_FORCED === undefined) { + process.env.NINJA_ANSI_FORCED = "1"; + } if (process.argv.includes("-verbose")) { - console.log(`NINJA_ANSI_FORCED: "${process.env.NINJA_ANSI_FORCED}"`); + console.log(`FORCE_COLOR: "${process.env.FORCE_COLOR}"`); } } From f3289bfb8b74c16fb04e83eb79b138d50056153d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Thu, 12 Sep 2024 12:32:09 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f1be5ebd6..2ce1d598dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ # 12.0.0-alpha.4 (Unreleased) +#### :rocket: New Feature + +- Use FORCE_COLOR environmental variable to force colorized output https://github.com/rescript-lang/rescript-compiler/pull/7033 + #### :bug: Bug fix - Fix tuple coercion. https://github.com/rescript-lang/rescript-compiler/pull/7024