Skip to content

Commit

Permalink
Use atomic for COLOR
Browse files Browse the repository at this point in the history
  • Loading branch information
HanabishiRecca committed May 31, 2023
1 parent 1e7adf4 commit 6512e1d
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/print.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{fmt::Arguments, sync::RwLock};
use std::{
fmt::Arguments,
sync::atomic::{AtomicBool, Ordering::Relaxed},
};

static COLOR: RwLock<bool> = RwLock::new(false);
static COLOR: AtomicBool = AtomicBool::new(false);

#[cfg_attr(test, derive(Debug, PartialEq))]
pub enum ColorMode {
Expand All @@ -16,16 +19,19 @@ fn isatty() -> bool {

pub fn set_color_mode(mode: ColorMode) {
use ColorMode::*;
*COLOR.write().unwrap() = match mode {
Auto => isatty(),
Always => true,
Never => false,
};
COLOR.store(
match mode {
Auto => isatty(),
Always => true,
Never => false,
},
Relaxed,
);
}

macro_rules! print_to {
($p: ident, $pl: ident, $c: expr, $n: expr) => {{
match *COLOR.read().unwrap() {
match COLOR.load(Relaxed) {
true => {
$p!($c);
$pl!("\x1b[0m");
Expand Down

0 comments on commit 6512e1d

Please sign in to comment.