crossterm/style/
attributes.rs1use std::ops::{BitAnd, BitOr, BitXor};
2
3use crate::style::Attribute;
4
5#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
7pub struct Attributes(u32);
8
9impl From<Attribute> for Attributes {
10 fn from(attribute: Attribute) -> Self {
11 Self(attribute.bytes())
12 }
13}
14
15impl From<&[Attribute]> for Attributes {
16 fn from(arr: &[Attribute]) -> Self {
17 let mut attributes = Attributes::default();
18 for &attr in arr {
19 attributes.set(attr);
20 }
21 attributes
22 }
23}
24
25impl BitAnd<Attribute> for Attributes {
26 type Output = Self;
27 fn bitand(self, rhs: Attribute) -> Self {
28 Self(self.0 & rhs.bytes())
29 }
30}
31impl BitAnd for Attributes {
32 type Output = Self;
33 fn bitand(self, rhs: Self) -> Self {
34 Self(self.0 & rhs.0)
35 }
36}
37
38impl BitOr<Attribute> for Attributes {
39 type Output = Self;
40 fn bitor(self, rhs: Attribute) -> Self {
41 Self(self.0 | rhs.bytes())
42 }
43}
44impl BitOr for Attributes {
45 type Output = Self;
46 fn bitor(self, rhs: Self) -> Self {
47 Self(self.0 | rhs.0)
48 }
49}
50
51impl BitXor<Attribute> for Attributes {
52 type Output = Self;
53 fn bitxor(self, rhs: Attribute) -> Self {
54 Self(self.0 ^ rhs.bytes())
55 }
56}
57impl BitXor for Attributes {
58 type Output = Self;
59 fn bitxor(self, rhs: Self) -> Self {
60 Self(self.0 ^ rhs.0)
61 }
62}
63
64impl Attributes {
65 #[inline(always)]
67 pub const fn none() -> Self {
68 Self(0)
69 }
70
71 #[inline(always)]
74 pub const fn with(self, attribute: Attribute) -> Self {
75 Self(self.0 | attribute.bytes())
76 }
77
78 #[inline(always)]
81 pub const fn without(self, attribute: Attribute) -> Self {
82 Self(self.0 & !attribute.bytes())
83 }
84
85 #[inline(always)]
88 pub fn set(&mut self, attribute: Attribute) {
89 self.0 |= attribute.bytes();
90 }
91
92 #[inline(always)]
95 pub fn unset(&mut self, attribute: Attribute) {
96 self.0 &= !attribute.bytes();
97 }
98
99 #[inline(always)]
102 pub fn toggle(&mut self, attribute: Attribute) {
103 self.0 ^= attribute.bytes();
104 }
105
106 #[inline(always)]
108 pub const fn has(self, attribute: Attribute) -> bool {
109 self.0 & attribute.bytes() != 0
110 }
111
112 #[inline(always)]
114 pub fn extend(&mut self, attributes: Attributes) {
115 self.0 |= attributes.0;
116 }
117
118 #[inline(always)]
120 pub const fn is_empty(self) -> bool {
121 self.0 == 0
122 }
123}
124
125#[cfg(test)]
126mod tests {
127 use super::{Attribute, Attributes};
128
129 #[test]
130 fn test_attributes() {
131 let mut attributes: Attributes = Attribute::Bold.into();
132 assert!(attributes.has(Attribute::Bold));
133 attributes.set(Attribute::Italic);
134 assert!(attributes.has(Attribute::Italic));
135 attributes.unset(Attribute::Italic);
136 assert!(!attributes.has(Attribute::Italic));
137 attributes.toggle(Attribute::Bold);
138 assert!(attributes.is_empty());
139 }
140
141 #[test]
142 fn test_attributes_const() {
143 const ATTRIBUTES: Attributes = Attributes::none()
144 .with(Attribute::Bold)
145 .with(Attribute::Italic)
146 .without(Attribute::Bold);
147 assert!(!ATTRIBUTES.has(Attribute::Bold));
148 assert!(ATTRIBUTES.has(Attribute::Italic));
149 }
150}