Skip to content

Commit d0907e9

Browse files
committed
Turn ansi_nums() into a Display impl
1 parent 3d5dd03 commit d0907e9

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/utils.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,22 @@ impl Attributes {
168168
}
169169

170170
#[inline]
171-
fn ansi_nums(self) -> impl Iterator<Item = u16> {
172-
// Per construction of the enum
173-
self.bits().map(|bit| bit + 1)
171+
fn attrs(self) -> impl Iterator<Item = Attribute> {
172+
self.bits().map(|bit| Attribute::MAP[bit as usize])
174173
}
175174

176175
#[inline]
177-
fn attrs(self) -> impl Iterator<Item = Attribute> {
178-
self.bits().map(|bit| Attribute::MAP[bit as usize])
176+
fn is_empty(self) -> bool {
177+
self.0 == 0
178+
}
179+
}
180+
181+
impl fmt::Display for Attributes {
182+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
183+
for ansi in self.bits().map(|bit| bit + 1) {
184+
write!(f, "\x1b[{ansi}m")?;
185+
}
186+
Ok(())
179187
}
180188
}
181189

@@ -705,8 +713,8 @@ macro_rules! impl_fmt {
705713
}
706714
reset = true;
707715
}
708-
for ansi_num in self.style.attrs.ansi_nums() {
709-
write!(f, "\x1b[{}m", ansi_num)?;
716+
if !self.style.attrs.is_empty() {
717+
write!(f, "{}", self.style.attrs)?;
710718
reset = true;
711719
}
712720
}
@@ -1026,7 +1034,6 @@ fn test_attributes_single() {
10261034
for attr in Attribute::MAP {
10271035
let attrs = Attributes::new().insert(attr);
10281036
assert_eq!(attrs.bits().collect::<Vec<_>>(), [attr as u16]);
1029-
assert_eq!(attrs.ansi_nums().collect::<Vec<_>>(), [attr as u16 + 1]);
10301037
assert_eq!(attrs.attrs().collect::<Vec<_>>(), [attr]);
10311038
assert_eq!(format!("{:?}", attrs), format!("{{{:?}}}", attr));
10321039
}
@@ -1062,13 +1069,6 @@ fn test_attributes_many() {
10621069
.map(|attr| *attr as u16)
10631070
.collect::<Vec<_>>()
10641071
);
1065-
assert_eq!(
1066-
attrs.ansi_nums().collect::<Vec<_>>(),
1067-
test_attrs
1068-
.iter()
1069-
.map(|attr| *attr as u16 + 1)
1070-
.collect::<Vec<_>>()
1071-
);
10721072
assert_eq!(&attrs.attrs().collect::<Vec<_>>(), test_attrs);
10731073
}
10741074
}

0 commit comments

Comments
 (0)