diff options
Diffstat (limited to 'src/attrs.rs')
-rw-r--r-- | src/attrs.rs | 120 |
1 files changed, 41 insertions, 79 deletions
diff --git a/src/attrs.rs b/src/attrs.rs index 95a5e76..dc46646 100644 --- a/src/attrs.rs +++ b/src/attrs.rs @@ -1,3 +1,5 @@ +use std::io::Write as _; + /// Represents a foreground or background color for cells. #[derive(Eq, PartialEq, Debug, Copy, Clone)] pub enum Color { @@ -87,89 +89,49 @@ impl Attrs { } } - pub fn escape_code_diff(&self, other: &Self) -> Vec<u8> { - let mut opts = vec![]; + pub fn write_escape_code_diff( + &self, + contents: &mut Vec<u8>, + other: &Self, + ) { + let attrs = crate::term::Attrs::default(); if self != other && self == &Self::default() { - return b"\x1b[m".to_vec(); - } - - if self.fgcolor != other.fgcolor { - match self.fgcolor { - Color::Default => { - opts.push(39); - } - Color::Idx(i) => { - if i < 8 { - opts.push(i + 30); - } else if i < 16 { - opts.push(i + 82); - } else { - opts.push(38); - opts.push(5); - opts.push(i); - } - } - Color::Rgb(r, g, b) => { - opts.push(38); - opts.push(2); - opts.push(r); - opts.push(g); - opts.push(b); - } - } - } - - if self.bgcolor != other.bgcolor { - match self.bgcolor { - Color::Default => { - opts.push(49); - } - Color::Idx(i) => { - if i < 8 { - opts.push(i + 40); - } else if i < 16 { - opts.push(i + 92); - } else { - opts.push(48); - opts.push(5); - opts.push(i); - } - } - Color::Rgb(r, g, b) => { - opts.push(48); - opts.push(2); - opts.push(r); - opts.push(g); - opts.push(b); - } - } + write!(contents, "{}", attrs).unwrap(); + return; } - if self.bold() != other.bold() { - opts.push(if self.bold() { 1 } else { 22 }); - } - if self.italic() != other.italic() { - opts.push(if self.italic() { 3 } else { 23 }); - } - if self.underline() != other.underline() { - opts.push(if self.underline() { 4 } else { 24 }); - } - if self.inverse() != other.inverse() { - opts.push(if self.inverse() { 7 } else { 27 }); - } + let attrs = if self.fgcolor == other.fgcolor { + attrs + } else { + attrs.fgcolor(self.fgcolor) + }; + let attrs = if self.bgcolor == other.bgcolor { + attrs + } else { + attrs.bgcolor(self.bgcolor) + }; + let attrs = if self.bold() == other.bold() { + attrs + } else { + attrs.bold(self.bold()) + }; + let attrs = if self.italic() == other.italic() { + attrs + } else { + attrs.italic(self.italic()) + }; + let attrs = if self.underline() == other.underline() { + attrs + } else { + attrs.underline(self.underline()) + }; + let attrs = if self.inverse() == other.inverse() { + attrs + } else { + attrs.inverse(self.inverse()) + }; - let mut bytes = b"\x1b[".to_vec(); - let mut first = true; - for opt in opts { - if first { - first = false; - } else { - bytes.push(b';'); - } - bytes.extend(opt.to_string().as_bytes()); - } - bytes.push(b'm'); - bytes + write!(contents, "{}", attrs).unwrap(); } } |