diff --git a/core/src/constants.ts b/core/src/constants.ts index 4bf5af7b04..b541ee0874 100644 --- a/core/src/constants.ts +++ b/core/src/constants.ts @@ -62,6 +62,8 @@ export const DEFAULT_BROWSER_DIVIDER_WIDTH = 80 */ export const gardenEnv = { ANALYTICS_DEV: env.get("ANALYTICS_DEV").required(false).asBool(), + // Support the NO_COLOR env var (see https://no-color.org/) + NO_COLOR: env.get("NO_COLOR").required(false).default("false").asBool(), GARDEN_AUTH_TOKEN: env.get("GARDEN_AUTH_TOKEN").required(false).asString(), GARDEN_CACHE_TTL: env.get("GARDEN_CACHE_TTL").required(false).asInt(), GARDEN_DB_DIR: env.get("GARDEN_DB_DIR").required(false).default(GARDEN_GLOBAL_PATH).asString(), diff --git a/core/src/logger/log-entry.ts b/core/src/logger/log-entry.ts index 5437fe741a..9827d7257d 100644 --- a/core/src/logger/log-entry.ts +++ b/core/src/logger/log-entry.ts @@ -20,7 +20,7 @@ import { renderDuration } from "./util.js" import { styles } from "./styles.js" import { getStyle } from "./renderers.js" -export type LogSymbol = keyof typeof logSymbols | "empty" | "cached" +export type LogSymbol = keyof typeof logSymbols | "empty" export type TaskLogStatus = "active" | "success" | "error" export interface LogMetadata { diff --git a/core/src/logger/renderers.ts b/core/src/logger/renderers.ts index f3a6928403..7c2851749b 100644 --- a/core/src/logger/renderers.ts +++ b/core/src/logger/renderers.ts @@ -20,6 +20,7 @@ import { logLevelMap, LogLevel } from "./logger.js" import { toGardenError } from "../exceptions.js" import { styles } from "./styles.js" import type { Chalk } from "chalk" +import { gardenEnv } from "../constants.js" type RenderFn = (entry: LogEntry, logger: Logger) => string @@ -70,11 +71,6 @@ export function renderSymbol(entry: LogEntry): string { return " " } - if (symbol === "cached") { - return styles.highlightSecondary.bold("🞦 ") - // return styles.highlightSecondary.bold("🌸") - } - // Always show symbol with sections if (!symbol && section) { symbol = "info" @@ -162,7 +158,7 @@ export function formatForTerminal(entry: LogEntry, logger: Logger): string { return "" } - return combineRenders(entry, logger, [ + const formattedMsg = combineRenders(entry, logger, [ renderTimestamp, renderSymbol, renderSection, @@ -171,6 +167,11 @@ export function formatForTerminal(entry: LogEntry, logger: Logger): string { renderData, () => "\n", ]) + + if (gardenEnv.NO_COLOR) { + return stripAnsi(formattedMsg) + } + return formattedMsg } export function cleanForJSON(input?: string | string[]): string { diff --git a/core/test/unit/src/logger/renderers.ts b/core/test/unit/src/logger/renderers.ts index 590cffdbb8..a8ff9dd5db 100644 --- a/core/test/unit/src/logger/renderers.ts +++ b/core/test/unit/src/logger/renderers.ts @@ -29,6 +29,7 @@ import { highlightYaml, safeDumpYaml } from "../../../../src/util/serialization. import { freezeTime } from "../../../helpers.js" import format from "date-fns/format/index.js" import { styles } from "../../../../src/logger/styles.js" +import { gardenEnv } from "../../../../src/constants.js" const logger: Logger = getRootLogger() @@ -122,6 +123,20 @@ describe("renderers", () => { expect(formatForTerminal(entry, logger)).to.equal(`${styles.secondary("[debug] hello world")}\n`) }) + context("NO_COLOR=true", () => { + before(() => { + gardenEnv.NO_COLOR = true + }) + after(() => { + gardenEnv.NO_COLOR = false + }) + it("should not use ANSI terminal colors", () => { + const entry = logger.createLog({ name: "test-log" }).info({ msg: "hello world" }).getLatestEntry() + + const sectionWithPadding = "test-log".padEnd(SECTION_PADDING, " ") + expect(formatForTerminal(entry, logger)).to.equal(`ℹ ${sectionWithPadding} → hello world\n`) + }) + }) context("basic", () => { before(() => { logger.showTimestamps = true diff --git a/docs/misc/faq.md b/docs/misc/faq.md index c7c2e9f72d..13e9b793b6 100644 --- a/docs/misc/faq.md +++ b/docs/misc/faq.md @@ -258,12 +258,8 @@ local kubernetes provider it might work. ### How do I disable terminal colors? -Garden uses the [Chalk NPM package](https://github.com/chalk/chalk) under the hood which picks a color setting based on what colors your terminal supports. - -You can override this behavior with the `FORCE_COLOR` environment variable. For example: - -- `FORCE_COLOR=0 garden deploy # No colors` -- `FORCE_COLOR=1 garden deploy # 16 colors` -- `FORCE_COLOR=2 garden deploy # 256 colors` -- `FORCE_COLOR=3 garden deploy # Truecolor (16 million)` +You can disable terminal colors with the `NO_COLOR` environment variable. For example: +```console +NO_COLOR=1 garden deploy +```