tale-ndjson 0.4.0

A tail-compatible tool for pretty-printing ndjson files, especially logs.
Documentation
//! Whether ANSI colors should be emitted on stdout.
//!
//! Colors are on by default (preserving behavior for library consumers that
//! never call [`detect_colors`]). The binary calls `detect_colors` once at
//! startup: colors stay on only when stdout is a terminal and the user has
//! not set `NO_COLOR`. When piped, output is plain so downstream tools see
//! clean text.

use std::io::IsTerminal;
use std::sync::atomic::{AtomicBool, Ordering};

static ENABLED: AtomicBool = AtomicBool::new(true);

/// True when log formatting should emit ANSI escape sequences.
#[inline]
pub fn colors_enabled() -> bool {
    ENABLED.load(Ordering::Relaxed)
}

/// Decide color support once at process start: only when stdout is a TTY and
/// `NO_COLOR` is unset or empty (per https://no-color.org).
pub fn detect_colors() {
    let enabled = std::io::stdout().is_terminal() && std::env::var_os("NO_COLOR").is_none_or(|value| value.is_empty());
    ENABLED.store(enabled, Ordering::Relaxed);
}