diff options
author | Thomas Otto <th1000s@posteo.net> | 2021-11-09 23:22:29 +0100 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2021-11-10 20:08:28 -0500 |
commit | 54bab7051774c82d5af9d6024f03529008254657 (patch) | |
tree | 068de707c136630ae7f5408ad8553c190373b8c9 | |
parent | 9d0d7f0fa5916fb33e420fb7eaa9158b7eb61aaa (diff) |
Add compact debug formatting for Style
-rw-r--r-- | src/style.rs | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/src/style.rs b/src/style.rs index 4f590f0f..693583e7 100644 --- a/src/style.rs +++ b/src/style.rs @@ -6,7 +6,7 @@ use lazy_static::lazy_static; use crate::ansi; use crate::color; -#[derive(Clone, Copy, Debug, PartialEq, Default)] +#[derive(Clone, Copy, PartialEq, Default)] pub struct Style { pub ansi_term_style: ansi_term::Style, pub is_emph: bool, @@ -16,6 +16,42 @@ pub struct Style { pub decoration_style: DecorationStyle, } +// More compact debug output, replace false/empty with lowercase and true with uppercase. +impl fmt::Debug for Style { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let ansi = if self.ansi_term_style.is_plain() { + "<a".into() + } else { + format!("ansi_term_style: {:?}, <", self.ansi_term_style) + }; + + let deco = if self.decoration_style == DecorationStyle::NoDecoration { + "d>".into() + } else { + format!(">, decoration_style: {:?}", self.decoration_style) + }; + + let is_set = |c: char, set: bool| -> String { + if set { + c.to_uppercase().to_string() + } else { + c.to_lowercase().to_string() + } + }; + + write!( + f, + "Style {{ {}{}{}{}{}{} }}", + ansi, + is_set('e', self.is_emph), + is_set('o', self.is_omitted), + is_set('r', self.is_raw), + is_set('s', self.is_syntax_highlighted), + deco + ) + } +} + #[derive(Clone, Copy, Debug, PartialEq)] pub enum DecorationStyle { Box(ansi_term::Style), @@ -318,4 +354,28 @@ pub mod tests { [].iter() )); } + + #[test] + fn test_style_compact_debug_fmt() { + let mut s = Style::new(); + assert_eq!(format!("{:?}", s), "Style { <aeorsd> }"); + s.is_emph = true; + assert_eq!(format!("{:?}", s), "Style { <aEorsd> }"); + s.ansi_term_style = ansi_term::Style::new().bold(); + assert_eq!( + format!("{:?}", s), + "Style { ansi_term_style: Style { bold }, <Eorsd> }" + ); + s.decoration_style = DecorationStyle::Underline(s.ansi_term_style.clone()); + assert_eq!( + format!("{:?}", s), + "Style { ansi_term_style: Style { bold }, <Eors>, \ + decoration_style: Underline(Style { bold }) }" + ); + s.ansi_term_style = ansi_term::Style::default(); + assert_eq!( + format!("{:?}", s), + "Style { <aEors>, decoration_style: Underline(Style { bold }) }" + ); + } } |