Skip to content

Commit

Permalink
feat: respect NO_COLOR env var (#5451)
Browse files Browse the repository at this point in the history
* improvement(logger): support NO_COLOR env var

A quick fix to ensure Garden respects the NO_COLOR env var and doesn't
print colored logs if it's set.

We should also this as a CLI flag but this is a start.

* chore(logger): remove unused code

This was accidentally merged but not used
  • Loading branch information
eysi09 committed Nov 20, 2023
1 parent 8d00dc9 commit 889552f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
2 changes: 2 additions & 0 deletions core/src/constants.ts
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion core/src/logger/log-entry.ts
Expand Up @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions core/src/logger/renderers.ts
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions core/test/unit/src/logger/renderers.ts
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions docs/misc/faq.md
Expand Up @@ -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
```

0 comments on commit 889552f

Please sign in to comment.