mod color;
mod effect;
#[cfg(doc)]
use crate::Ansi;
use crate::{Color, Coloree, Effect, Toggle};
use bitflags::bitflags;
use std::fmt;
pub(crate) mod private {
pub trait Seal : PartialEq + Eq + Clone + Copy {}
}
#[doc(hidden)]
pub trait Value : private::Seal {}
impl private::Seal for Color {}
impl private::Seal for Effect {}
impl Value for Color {}
impl Value for Effect {}
bitflags! {
#[derive(PartialEq, Eq, Clone, Copy, fmt::Debug)]
struct AttrFlags: u8 {
const Reset = 1 << 0;
const Important = 1 << 1;
const Bg = 1 << 2;
}
}
#[derive(Clone, Copy)]
pub struct Attr<V: Value> {
value: V,
flags: AttrFlags,
}
impl<V: Value> Attr<V> {
#[inline]
pub(super) const fn new(value: V) -> Self { Self { value, flags: AttrFlags::empty() } }
#[inline]
pub const fn value(&self) -> V { let value = self.value; value }
#[inline]
pub const fn is_reset(&self) -> bool { self.flags.contains(AttrFlags::Reset) }
#[inline]
pub const fn reset(&self) -> Self {
Self { value: self.value, flags: self.flags.union(AttrFlags::Reset) }
}
#[inline]
pub const fn not(&self) -> Self {
let reset = if self.flags.intersects(AttrFlags::Reset) { AttrFlags::empty() } else { AttrFlags::Reset };
let flags = self.flags.difference(AttrFlags::Reset).union(reset);
Self { value: self.value, flags }
}
#[inline]
pub const fn is_important(&self) -> bool { self.flags.intersects(AttrFlags::Important) }
#[inline]
pub const fn important(&self) -> Self {
Self { value: self.value, flags: self.flags.union(AttrFlags::Important) }
}
#[inline]
pub const fn unimportant(&self) -> Self {
Self { value: self.value, flags: self.flags.difference(AttrFlags::Important) }
}
#[inline]
pub const fn with_important(&self, is_important: bool) -> Self {
if is_important { self.important() } else { self.unimportant() }
}
#[inline]
pub(crate) const fn get_toggle(&self) -> Toggle {
if self.flags.intersects(AttrFlags::Reset) { Toggle::Reset } else { Toggle::Set }
}
#[inline]
const fn get_coloree(&self) -> Coloree {
if self.flags.intersects(AttrFlags::Bg) { Coloree::Background } else { Coloree::Text }
}
}