@@ -168,14 +168,22 @@ impl Attributes {
168
168
}
169
169
170
170
#[ 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 ] )
174
173
}
175
174
176
175
#[ 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 ( ( ) )
179
187
}
180
188
}
181
189
@@ -705,8 +713,8 @@ macro_rules! impl_fmt {
705
713
}
706
714
reset = true ;
707
715
}
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 ) ?;
710
718
reset = true ;
711
719
}
712
720
}
@@ -1026,7 +1034,6 @@ fn test_attributes_single() {
1026
1034
for attr in Attribute :: MAP {
1027
1035
let attrs = Attributes :: new ( ) . insert ( attr) ;
1028
1036
assert_eq ! ( attrs. bits( ) . collect:: <Vec <_>>( ) , [ attr as u16 ] ) ;
1029
- assert_eq ! ( attrs. ansi_nums( ) . collect:: <Vec <_>>( ) , [ attr as u16 + 1 ] ) ;
1030
1037
assert_eq ! ( attrs. attrs( ) . collect:: <Vec <_>>( ) , [ attr] ) ;
1031
1038
assert_eq ! ( format!( "{:?}" , attrs) , format!( "{{{:?}}}" , attr) ) ;
1032
1039
}
@@ -1062,13 +1069,6 @@ fn test_attributes_many() {
1062
1069
. map( |attr| * attr as u16 )
1063
1070
. collect:: <Vec <_>>( )
1064
1071
) ;
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
- ) ;
1072
1072
assert_eq ! ( & attrs. attrs( ) . collect:: <Vec <_>>( ) , test_attrs) ;
1073
1073
}
1074
1074
}
0 commit comments