|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Automatic ANSI color detection for console output. |
| 5 | +//! |
| 6 | +//! `tracing_subscriber`'s formatter turns ANSI on unless `NO_COLOR` is set. It |
| 7 | +//! does not check whether the output stream is a terminal, so a gateway or |
| 8 | +//! driver running in a container writes escape sequences into its collected |
| 9 | +//! logs. Long-running binaries pass [`stdout_ansi`] or [`stderr_ansi`] to |
| 10 | +//! `with_ansi` so console logs are colored only on a terminal that can render |
| 11 | +//! them. |
| 12 | +//! |
| 13 | +//! Resolution order, highest precedence first: |
| 14 | +//! |
| 15 | +//! 1. `NO_COLOR`, set and non-empty, disables color (<https://no-color.org>). |
| 16 | +//! 2. `FORCE_COLOR`, set and non-empty, forces color on (<https://force-color.org>). |
| 17 | +//! 3. Otherwise the stream is colored only when it is a terminal *and* that |
| 18 | +//! terminal renders ANSI. |
| 19 | +
|
| 20 | +use std::ffi::OsStr; |
| 21 | +use std::io::IsTerminal; |
| 22 | + |
| 23 | +/// Whether console output written to stdout should carry ANSI escapes. |
| 24 | +#[must_use] |
| 25 | +pub fn stdout_ansi() -> bool { |
| 26 | + stream_ansi(std::io::stdout().is_terminal()) |
| 27 | +} |
| 28 | + |
| 29 | +/// Whether console output written to stderr should carry ANSI escapes. |
| 30 | +#[must_use] |
| 31 | +pub fn stderr_ansi() -> bool { |
| 32 | + stream_ansi(std::io::stderr().is_terminal()) |
| 33 | +} |
| 34 | + |
| 35 | +fn stream_ansi(stream_is_terminal: bool) -> bool { |
| 36 | + let term = std::env::var_os("TERM"); |
| 37 | + auto( |
| 38 | + std::env::var_os("NO_COLOR").as_deref(), |
| 39 | + std::env::var_os("FORCE_COLOR").as_deref(), |
| 40 | + terminal_supports_ansi(stream_is_terminal, term.as_deref()), |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +/// Decide whether one stream may carry escapes when no explicit color choice |
| 45 | +/// was made. Pure so the precedence rules are testable without process state. |
| 46 | +#[must_use] |
| 47 | +pub fn auto( |
| 48 | + no_color: Option<&OsStr>, |
| 49 | + force_color: Option<&OsStr>, |
| 50 | + stream_supports_ansi: bool, |
| 51 | +) -> bool { |
| 52 | + // Both conventions key on presence rather than value: set and non-empty |
| 53 | + // means yes, regardless of what the value is. |
| 54 | + if is_set(no_color) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + if is_set(force_color) { |
| 58 | + return true; |
| 59 | + } |
| 60 | + stream_supports_ansi |
| 61 | +} |
| 62 | + |
| 63 | +/// Whether this output stream's terminal renders ANSI escapes. |
| 64 | +/// |
| 65 | +/// Being attached to a terminal is not the same as that terminal rendering |
| 66 | +/// ANSI. `TERM` is the unix signal for it; Windows consoles enable virtual |
| 67 | +/// terminal processing instead and do not set `TERM`, so the check does not |
| 68 | +/// apply there. |
| 69 | +#[must_use] |
| 70 | +pub fn terminal_supports_ansi(stream_is_terminal: bool, term: Option<&OsStr>) -> bool { |
| 71 | + stream_is_terminal |
| 72 | + && if cfg!(unix) { |
| 73 | + term_supports_ansi(term) |
| 74 | + } else { |
| 75 | + true |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// Whether the terminal named by `TERM` renders ANSI escapes on Unix. |
| 80 | +/// |
| 81 | +/// Follows the rule `console` applies on unix: `dumb` means no, and an unset |
| 82 | +/// `TERM` means no because nothing identifies a capable terminal. |
| 83 | +/// |
| 84 | +/// Empty is treated as unset, which is a deliberate divergence: `console` reads |
| 85 | +/// `TERM=""` as `Ok("")`, and since that is not `"dumb"` it counts as capable. |
| 86 | +/// An empty value names no terminal type, and every other variable here already |
| 87 | +/// treats empty as unset, so it is handled the same way. |
| 88 | +fn term_supports_ansi(term: Option<&OsStr>) -> bool { |
| 89 | + is_set(term) && term != Some(OsStr::new("dumb")) |
| 90 | +} |
| 91 | + |
| 92 | +/// Whether an environment variable counts as set: present and not empty. |
| 93 | +fn is_set(value: Option<&OsStr>) -> bool { |
| 94 | + value.is_some_and(|value| !value.is_empty()) |
| 95 | +} |
| 96 | + |
| 97 | +#[cfg(test)] |
| 98 | +mod tests { |
| 99 | + use super::*; |
| 100 | + |
| 101 | + fn env(value: &str) -> &OsStr { |
| 102 | + OsStr::new(value) |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn no_color_disables_when_set_and_non_empty() { |
| 107 | + assert!(!auto(Some(env("1")), None, true)); |
| 108 | + // Presence is what counts; a value that reads as falsy still disables. |
| 109 | + assert!(!auto(Some(env("0")), None, true)); |
| 110 | + // NO_COLOR outranks FORCE_COLOR. |
| 111 | + assert!(!auto(Some(env("1")), Some(env("1")), true)); |
| 112 | + // An empty value is not "set" for the purposes of the convention. |
| 113 | + assert!(auto(Some(env("")), None, true)); |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn force_color_enables_when_set_and_non_empty() { |
| 118 | + assert!(auto(None, Some(env("1")), false)); |
| 119 | + assert!(auto(None, Some(env("0")), false)); |
| 120 | + assert!(!auto(None, Some(env("")), false)); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn otherwise_follows_the_stream() { |
| 125 | + assert!(auto(None, None, true)); |
| 126 | + // A container log stream is not a terminal, so it stays plain. |
| 127 | + assert!(!auto(None, None, false)); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn redirected_stream_is_never_capable() { |
| 132 | + assert!(!terminal_supports_ansi(false, Some(env("xterm-256color")))); |
| 133 | + } |
| 134 | + |
| 135 | + #[cfg(unix)] |
| 136 | + #[test] |
| 137 | + fn terminal_capability_follows_term() { |
| 138 | + assert!(terminal_supports_ansi(true, Some(env("xterm-256color")))); |
| 139 | + assert!(!terminal_supports_ansi(true, Some(env("dumb")))); |
| 140 | + assert!(!terminal_supports_ansi(true, None)); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn term_capability_follows_the_console_rule() { |
| 145 | + assert!(term_supports_ansi(Some(env("xterm-256color")))); |
| 146 | + assert!(term_supports_ansi(Some(env("screen")))); |
| 147 | + assert!(!term_supports_ansi(Some(env("dumb")))); |
| 148 | + // Nothing to suggest a capable terminal, so assume none. |
| 149 | + assert!(!term_supports_ansi(None)); |
| 150 | + // Empty names no terminal type; treated as unset, unlike `console`. |
| 151 | + assert!(!term_supports_ansi(Some(env("")))); |
| 152 | + // Only an exact match counts; `dumb-something` is a different terminal. |
| 153 | + assert!(term_supports_ansi(Some(env("dumb-but-color")))); |
| 154 | + } |
| 155 | +} |
0 commit comments