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

Support controls of colored output by CARGO_TERM_COLOR #110

Merged
merged 1 commit into from
Jan 19, 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
16 changes: 11 additions & 5 deletions src/term.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
use anyhow::{bail, Result};
use std::{
env,
io::{self, Write},
sync::atomic::{AtomicU8, Ordering},
sync::atomic::{AtomicU8, Ordering::Relaxed},
};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

static COLORING: AtomicU8 = AtomicU8::new(0);
static COLORING: AtomicU8 = AtomicU8::new(AUTO);

const AUTO: u8 = 0;
const ALWAYS: u8 = 1;
const NEVER: u8 = 2;

pub(crate) fn set_coloring(color: Option<&str>) -> Result<()> {
let coloring = match color {
// https://doc.rust-lang.org/cargo/reference/config.html#termcolor
let mut cargo_term_color = None;
if color.is_none() {
cargo_term_color = env::var("CARGO_TERM_COLOR").ok();
}
let coloring = match color.or_else(|| cargo_term_color.as_ref().map(|s| &**s)) {
Some("auto") | None => AUTO,
Some("always") => ALWAYS,
Some("never") => NEVER,
Some(other) => bail!("must be auto, always, or never, but found `{}`", other),
};
COLORING.store(coloring, Ordering::Relaxed);
COLORING.store(coloring, Relaxed);
Ok(())
}

fn coloring() -> ColorChoice {
match COLORING.load(Ordering::Relaxed) {
match COLORING.load(Relaxed) {
AUTO => ColorChoice::Auto,
ALWAYS => ColorChoice::Always,
NEVER => ColorChoice::Never,
Expand Down
4 changes: 2 additions & 2 deletions tests/auxiliary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
ffi::OsStr,
path::{self, Path},
process::{Command, ExitStatus},
sync::atomic::{AtomicUsize, Ordering},
sync::atomic::{AtomicUsize, Ordering::Relaxed},
};
use tempfile::{Builder, TempDir};
use walkdir::WalkDir;
Expand Down Expand Up @@ -236,7 +236,7 @@ fn test_project(model: &str) -> Result<(TempDir, PathBuf)> {
static COUNTER: AtomicUsize = AtomicUsize::new(0);

let tmpdir = Builder::new()
.prefix(&format!("test_project{}", COUNTER.fetch_add(1, Ordering::Relaxed)))
.prefix(&format!("test_project{}", COUNTER.fetch_add(1, Relaxed)))
.tempdir()?;
let tmpdir_path = tmpdir.path();

Expand Down