Skip to content
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

cli,helpers: don't always use color #233

Merged
merged 1 commit into from
Mar 24, 2021
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
15 changes: 14 additions & 1 deletion src/cli.nim
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,21 @@ proc showVersion =
echo &"{configletVersion}"
quit(0)

proc shouldUseColor(f: File): bool =
## Returns true if we should write to `f` with color.
existsEnv("CI") or
(isatty(f) and not existsEnv("NO_COLOR") and getEnv("TERM") != "dumb")

let
colorStdout* = shouldUseColor(stdout)
colorStderr* = shouldUseColor(stderr)

proc showError*(s: string) =
stderr.styledWrite(fgRed, "Error: ")
const errorPrefix = "Error: "
if colorStderr:
stderr.styledWrite(fgRed, errorPrefix)
else:
stderr.write(errorPrefix)
stderr.write(s)
stderr.write("\n\n")
showHelp(exitCode = 1)
Expand Down
7 changes: 6 additions & 1 deletion src/helpers.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import std/[algorithm, os, terminal]
import "."/cli

template withDir*(dir: string; body: untyped): untyped =
## Changes the current directory to `dir` temporarily.
Expand All @@ -21,6 +22,10 @@ proc setFalseAndPrint*(b: var bool; description: string; details: string) =
## Sets `b` to `false` and writes a message to stdout containing `description`
## and `details`.
b = false
stdout.styledWriteLine(fgRed, description & ":")
let descriptionPrefix = description & ":"
if colorStdout:
stdout.styledWriteLine(fgRed, descriptionPrefix)
else:
stdout.writeLine(descriptionPrefix)
stdout.writeLine(details)
stdout.write "\n"