Terminal output
Colors and styles in the terminal.
On this page
Every string has .ansi methods that wrap it in terminal color codes. valk.ansi tells whether the output can show them.
Color text#
Valk
fn main() {
println("Saved".ansi.green())
println("Failed".ansi.red(true)) // bold red
println("note: " + "optional".ansi.dim())
}
| Method | Effect |
|---|---|
black, red, green, yellow, blue, purple, cyan, white |
That color, then a reset; pass true for bold |
dim() |
Lower intensity, keeping the current color |
Each method returns a new string with the codes around the text, so colored parts can be joined with +.
Only color a terminal#
Valk
use valk.ansi
fn status(ok: bool) String {
let text = ok ? "PASS" : "FAIL"
if !ansi.supported() : return text
return ok ? text.ansi.green() : text.ansi.red()
}
fn main() {
println(status(true))
}
ansi.supported() is true when standard output is a terminal that shows colors. It is false when output goes to a pipe or a file, or when NO_COLOR is set; FORCE_COLOR or CLICOLOR_FORCE turn colors on anyway, for example in CI logs.
ansi.utf8_supported() tells whether the console expects UTF-8, which matters on Windows before printing symbols such as ✓.
