Skip to content

Use FORCE_COLOR environmental variable to force colorized output #7033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions cli/rescript_bsb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"),
);
Expand Down
8 changes: 6 additions & 2 deletions rescript
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`);
}
}

Expand Down