diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-11-10 06:17:09 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-11-10 08:08:14 -0500 |
commit | 168a91a9ddc331cd3b8e5c32f7701f2b0973b147 (patch) | |
tree | 6b2df47bf18a731a9814bb875ce231e28cad1bf0 /src/term.rs | |
parent | b04c0e6e97765aeb888479c5e0bc27d54de60659 (diff) | |
download | vt100-rust-168a91a9ddc331cd3b8e5c32f7701f2b0973b147.tar.gz vt100-rust-168a91a9ddc331cd3b8e5c32f7701f2b0973b147.zip |
optimize attribute setting a bit
Diffstat (limited to 'src/term.rs')
-rw-r--r-- | src/term.rs | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/term.rs b/src/term.rs index ef6c546..074f177 100644 --- a/src/term.rs +++ b/src/term.rs @@ -56,6 +56,21 @@ impl std::fmt::Display for MoveTo { } #[derive(Default, Debug)] +pub struct ClearAttrs; + +impl ClearAttrs { + pub fn new() -> Self { + Self::default() + } +} + +impl std::fmt::Display for ClearAttrs { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("\x1b[m") + } +} + +#[derive(Default, Debug)] pub struct Attrs { fgcolor: Option<crate::attrs::Color>, bgcolor: Option<crate::attrs::Color>, @@ -66,10 +81,6 @@ pub struct Attrs { } impl Attrs { - pub fn new() -> Self { - Self::default() - } - pub fn fgcolor(mut self, fgcolor: crate::attrs::Color) -> Self { self.fgcolor = Some(fgcolor); self @@ -102,8 +113,22 @@ impl Attrs { } impl std::fmt::Display for Attrs { - #[allow(unused_assignments, clippy::cognitive_complexity)] + #[allow( + unused_assignments, + clippy::cognitive_complexity, + clippy::too_many_lines + )] fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if self.fgcolor.is_none() + && self.bgcolor.is_none() + && self.bold.is_none() + && self.italic.is_none() + && self.underline.is_none() + && self.inverse.is_none() + { + return Ok(()); + } + f.write_str("\x1b[")?; let mut first = true; |