1 | /// Reset terminal formatting |
2 | #[derive (Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
3 | pub struct Reset; |
4 | |
5 | impl Reset { |
6 | /// Render the ANSI code |
7 | /// |
8 | /// `Reset` also implements `Display` directly, so calling this method is optional. |
9 | #[inline ] |
10 | pub fn render(self) -> impl core::fmt::Display + Copy + Clone { |
11 | self |
12 | } |
13 | } |
14 | |
15 | impl core::fmt::Display for Reset { |
16 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
17 | write!(f, " {RESET}" ) |
18 | } |
19 | } |
20 | |
21 | pub(crate) const RESET: &str = " \x1B[0m" ; |
22 | |
23 | #[cfg (test)] |
24 | #[cfg (feature = "std" )] |
25 | mod test { |
26 | use super::*; |
27 | |
28 | #[test ] |
29 | fn print_size_of() { |
30 | use std::mem::size_of; |
31 | dbg!(size_of::<Reset>()); |
32 | } |
33 | |
34 | #[test ] |
35 | fn no_align() { |
36 | #[track_caller ] |
37 | fn assert_no_align(d: impl core::fmt::Display) { |
38 | let expected = format!(" {d}" ); |
39 | let actual = format!(" {d:<10}" ); |
40 | assert_eq!(expected, actual); |
41 | } |
42 | |
43 | assert_no_align(Reset); |
44 | assert_no_align(Reset.render()); |
45 | } |
46 | } |
47 | |