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

honor CLICOLOR and CLICOLOR_FORCE #302

Closed
wants to merge 6 commits into from
Closed
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
39 changes: 39 additions & 0 deletions src/encode/writer/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use std::{fmt, io};

use once_cell::sync::Lazy;

use crate::encode::{self, Style};
use once_cell::sync::Lazy;

Expand Down Expand Up @@ -42,6 +44,43 @@ pub enum ColorMode {
Never,
}

#[cfg(not(windows))]
static COLOR_MODE: Lazy<ColorMode> = Lazy::new(|| {
let no_color = std::env::var("NO_COLOR")
.map(|var| var != "0")
.unwrap_or(false);
let clicolor_force = std::env::var("CLICOLOR_FORCE")
.map(|var| var != "0")
.unwrap_or(false);
if no_color {
ColorMode::Never
}
else if clicolor_force {
ColorMode::Always
} else {
let clicolor = std::env::var("CLICOLOR")
.map(|var| var != "0")
.unwrap_or(true);
if clicolor {
ColorMode::Auto
} else {
ColorMode::Never
}
}
});

/// The color output mode for a `ConsoleAppender`
#[derive(Clone, Copy, Default)]
pub enum ColorMode {
/// Print color only if the output is recognized as a console
#[default]
Auto,
/// Force color output
Always,
/// Never print color
Never,
}

/// An `encode::Write`r that outputs to a console.
pub struct ConsoleWriter(imp::Writer);

Expand Down
Loading