1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use core::sync::atomic::{AtomicU8, Ordering};

/// Set an override value for whether or not colors are supported using
/// [`set_override`] while executing the closure provided.
///
/// Once the function has executed the value will be reset to the previous set
/// (or unset) override.
///
/// This is especially useful in use-cases where one would like to temporarily
/// override the supported color set, without impacting previous configurations.
///
/// ```
/// # use owo_colors::{Stream, OwoColorize, set_override, unset_override, with_override};
/// # use owo_colors::colors::Black;
/// #
/// set_override(false);
/// assert_eq!("example".if_supports_color(Stream::Stdout, |value| value.bg::<Black>()).to_string(), "example");
///
/// with_override(true, || {
///     assert_eq!("example".if_supports_color(Stream::Stdout, |value| value.bg::<Black>()).to_string(), "\x1b[40mexample\x1b[49m");
/// });
///
/// assert_eq!("example".if_supports_color(Stream::Stdout, |value| value.bg::<Black>()).to_string(), "example");
/// # unset_override() // make sure that other doc tests are not impacted
/// ```
#[cfg(feature = "supports-colors")]
pub fn with_override<T, F: FnOnce() -> T>(enabled: bool, f: F) -> T {
    let previous = OVERRIDE.inner();
    OVERRIDE.set_force(enabled);

    let value = f();

    OVERRIDE.set_unchecked(previous);

    value
}

/// Set an override value for whether or not colors are supported.
///
/// If `true` is passed,
/// [`if_supports_color`](crate::OwoColorize::if_supports_color) will always act
/// as if colors are supported.
///
/// If `false` is passed,
/// [`if_supports_color`](crate::OwoColorize::if_supports_color) will always act
/// as if colors are **not** supported.
///
/// This behavior can be disabled using [`unset_override`], allowing
/// `owo-colors` to return to inferring if colors are supported.
#[cfg(feature = "supports-colors")]
pub fn set_override(enabled: bool) {
    OVERRIDE.set_force(enabled);
}

/// Remove any override value for whether or not colors are supported. This
/// means [`if_supports_color`](crate::OwoColorize::if_supports_color) will
/// resume checking if the given terminal output ([`Stream`](crate::Stream))
/// supports colors.
///
/// This override can be set using [`set_override`].
#[cfg(feature = "supports-colors")]
pub fn unset_override() {
    OVERRIDE.unset();
}

pub(crate) static OVERRIDE: Override = Override::none();

pub(crate) struct Override(AtomicU8);

const FORCE_MASK: u8 = 0b10;
const FORCE_ENABLE: u8 = 0b11;
const FORCE_DISABLE: u8 = 0b10;
const NO_FORCE: u8 = 0b00;

impl Override {
    const fn none() -> Self {
        Self(AtomicU8::new(NO_FORCE))
    }

    fn inner(&self) -> u8 {
        self.0.load(Ordering::SeqCst)
    }

    pub(crate) fn is_force_enabled_or_disabled(&self) -> (bool, bool) {
        let inner = self.inner();

        (inner == FORCE_ENABLE, inner == FORCE_DISABLE)
    }

    fn set_force(&self, enable: bool) {
        self.set_unchecked(FORCE_MASK | (enable as u8));
    }

    fn unset(&self) {
        self.set_unchecked(0);
    }

    fn set_unchecked(&self, value: u8) {
        self.0.store(value, Ordering::SeqCst);
    }
}