use std::fmt::Display;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::super::SetAttribute;
macro_rules! Attribute {
(
$(
$(#[$inner:ident $($args:tt)*])*
$name:ident = $sgr:expr,
)*
) => {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[non_exhaustive]
pub enum Attribute {
$(
$(#[$inner $($args)*])*
$name,
)*
}
pub static SGR: &'static[i16] = &[
$($sgr,)*
];
impl Attribute {
pub fn iterator() -> impl Iterator<Item = Attribute> {
use self::Attribute::*;
[ $($name,)* ].iter().copied()
}
}
}
}
Attribute! {
Reset = 0,
Bold = 1,
Dim = 2,
Italic = 3,
Underlined = 4,
DoubleUnderlined = 2,
Undercurled = 3,
Underdotted = 4,
Underdashed = 5,
SlowBlink = 5,
RapidBlink = 6,
Reverse = 7,
Hidden = 8,
CrossedOut = 9,
Fraktur = 20,
NoBold = 21,
NormalIntensity = 22,
NoItalic = 23,
NoUnderline = 24,
NoBlink = 25,
NoReverse = 27,
NoHidden = 28,
NotCrossedOut = 29,
Framed = 51,
Encircled = 52,
OverLined = 53,
NotFramedOrEncircled = 54,
NotOverLined = 55,
}
impl Display for Attribute {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", SetAttribute(*self))?;
Ok(())
}
}
impl Attribute {
#[inline(always)]
pub const fn bytes(self) -> u32 {
1 << ((self as u32) + 1)
}
pub fn sgr(self) -> String {
if (self as usize) > 4 && (self as usize) < 9 {
return "4:".to_string() + SGR[self as usize].to_string().as_str();
}
SGR[self as usize].to_string()
}
}