From 232c3ec7544a67ab4287e3d37a406579387a218f Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 20 Jul 2018 09:11:44 -0700 Subject: [PATCH] replace EncodeEscape trait with plain old Display This makes it easier to use the escape sequence rendering with plain old `format!` --- src/escape/csi.rs | 144 ++++++++++++++++++--------------------- src/escape/esc.rs | 30 ++++---- src/escape/mod.rs | 33 +++------ src/escape/osc.rs | 58 +++++++--------- src/escape/parser/mod.rs | 6 +- src/render/terminfo.rs | 107 ++++++++++++++++------------- 6 files changed, 183 insertions(+), 195 deletions(-) diff --git a/src/escape/csi.rs b/src/escape/csi.rs index a87bad01e..260714a5a 100644 --- a/src/escape/csi.rs +++ b/src/escape/csi.rs @@ -1,8 +1,7 @@ use cell::{Blink, Intensity, Underline}; use color::{AnsiColor, ColorSpec, RgbColor}; -use escape::EncodeEscape; use num::{self, ToPrimitive}; -use std; +use std::fmt::{Display, Error as FmtError, Formatter}; #[derive(Debug, Clone, PartialEq, Eq)] pub enum CSI { @@ -32,18 +31,18 @@ pub enum CSI { __Nonexhaustive, } -impl EncodeEscape for CSI { +impl Display for CSI { // TODO: data size optimization opportunity: if we could somehow know that we // had a run of CSI instances being encoded in sequence, we could // potentially collapse them together. This is a few bytes difference in // practice so it may not be worthwhile with modern networks. - fn encode_escape(&self, w: &mut W) -> Result<(), std::io::Error> { - w.write_all(&[0x1b, b'['])?; + fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> { + write!(f, "\x1b[")?; match self { - CSI::Sgr(sgr) => sgr.encode_escape(w)?, - CSI::Cursor(c) => c.encode_escape(w)?, - CSI::Edit(e) => e.encode_escape(w)?, - CSI::Mode(mode) => mode.encode_escape(w)?, + CSI::Sgr(sgr) => sgr.fmt(f)?, + CSI::Cursor(c) => c.fmt(f)?, + CSI::Edit(e) => e.fmt(f)?, + CSI::Mode(mode) => mode.fmt(f)?, CSI::Unspecified { params, intermediates, @@ -52,15 +51,15 @@ impl EncodeEscape for CSI { } => { for (idx, p) in params.iter().enumerate() { if idx > 0 { - write!(w, ";{}", p)?; + write!(f, ";{}", p)?; } else { - write!(w, "{}", p)?; + write!(f, "{}", p)?; } } for i in intermediates { - write!(w, "{}", i)?; + write!(f, "{}", i)?; } - write!(w, "{}", control)?; + write!(f, "{}", control)?; } CSI::__Nonexhaustive => {} }; @@ -76,20 +75,15 @@ pub enum Mode { RestoreDecPrivateMode(DecPrivateMode), } -impl EncodeEscape for Mode { - fn encode_escape(&self, w: &mut W) -> Result<(), std::io::Error> { +impl Display for Mode { + fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> { macro_rules! emit { ($flag:expr, $mode:expr) => {{ let value = match $mode { - DecPrivateMode::Code(mode) => mode.to_i64().ok_or_else(|| { - std::io::Error::new( - std::io::ErrorKind::Other, - "enum value was not representable as i64!?", - ) - })?, + DecPrivateMode::Code(mode) => mode.to_i64().ok_or_else(|| FmtError)?, DecPrivateMode::Unspecified(mode) => *mode, }; - write!(w, "?{}{}", value, $flag) + write!(f, "?{}{}", value, $flag) }}; } match self { @@ -348,76 +342,71 @@ pub enum Edit { } trait EncodeCSIParam { - fn write_csi(&self, w: &mut W, control: &str) -> Result<(), std::io::Error>; + fn write_csi(&self, f: &mut Formatter, control: &str) -> Result<(), FmtError>; } impl EncodeCSIParam for T { - fn write_csi(&self, w: &mut W, control: &str) -> Result<(), std::io::Error> { + fn write_csi(&self, f: &mut Formatter, control: &str) -> Result<(), FmtError> { if *self == ParamEnum::default() { - write!(w, "{}", control) + write!(f, "{}", control) } else { - let value = self.to_i64().ok_or_else(|| { - std::io::Error::new( - std::io::ErrorKind::Other, - "enum value was not representable as i64!?", - ) - })?; - write!(w, "{}{}", value, control) + let value = self.to_i64().ok_or_else(|| FmtError)?; + write!(f, "{}{}", value, control) } } } impl EncodeCSIParam for u32 { - fn write_csi(&self, w: &mut W, control: &str) -> Result<(), std::io::Error> { + fn write_csi(&self, f: &mut Formatter, control: &str) -> Result<(), FmtError> { if *self == 1 { - write!(w, "{}", control) + write!(f, "{}", control) } else { - write!(w, "{}{}", *self, control) + write!(f, "{}{}", *self, control) } } } -impl EncodeEscape for Edit { - fn encode_escape(&self, w: &mut W) -> Result<(), std::io::Error> { +impl Display for Edit { + fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> { match self { - Edit::DeleteCharacter(n) => n.write_csi(w, "P")?, - Edit::DeleteLine(n) => n.write_csi(w, "M")?, - Edit::EraseCharacter(n) => n.write_csi(w, "X")?, - Edit::EraseInLine(n) => n.write_csi(w, "K")?, - Edit::InsertCharacter(n) => n.write_csi(w, "@")?, - Edit::InsertLine(n) => n.write_csi(w, "L")?, - Edit::ScrollDown(n) => n.write_csi(w, "T")?, - Edit::ScrollUp(n) => n.write_csi(w, "S")?, - Edit::EraseInDisplay(n) => n.write_csi(w, "J")?, + Edit::DeleteCharacter(n) => n.write_csi(f, "P")?, + Edit::DeleteLine(n) => n.write_csi(f, "M")?, + Edit::EraseCharacter(n) => n.write_csi(f, "X")?, + Edit::EraseInLine(n) => n.write_csi(f, "K")?, + Edit::InsertCharacter(n) => n.write_csi(f, "@")?, + Edit::InsertLine(n) => n.write_csi(f, "L")?, + Edit::ScrollDown(n) => n.write_csi(f, "T")?, + Edit::ScrollUp(n) => n.write_csi(f, "S")?, + Edit::EraseInDisplay(n) => n.write_csi(f, "J")?, } Ok(()) } } -impl EncodeEscape for Cursor { - fn encode_escape(&self, w: &mut W) -> Result<(), std::io::Error> { +impl Display for Cursor { + fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> { match self { - Cursor::BackwardTabulation(n) => n.write_csi(w, "Z")?, - Cursor::CharacterAbsolute(col) => col.write_csi(w, "G")?, - Cursor::ForwardTabulation(n) => n.write_csi(w, "I")?, - Cursor::NextLine(n) => n.write_csi(w, "E")?, - Cursor::PrecedingLine(n) => n.write_csi(w, "F")?, - Cursor::ActivePositionReport { line, col } => write!(w, "{};{}R", line, col)?, - Cursor::Left(n) => n.write_csi(w, "D")?, - Cursor::Down(n) => n.write_csi(w, "B")?, - Cursor::Right(n) => n.write_csi(w, "C")?, - Cursor::Up(n) => n.write_csi(w, "A")?, - Cursor::Position { line, col } => write!(w, "{};{}H", line, col)?, - Cursor::LineTabulation(n) => n.write_csi(w, "Y")?, - Cursor::TabulationControl(n) => n.write_csi(w, "W")?, - Cursor::TabulationClear(n) => n.write_csi(w, "g")?, - Cursor::CharacterPositionAbsolute(n) => n.write_csi(w, "`")?, - Cursor::CharacterPositionBackward(n) => n.write_csi(w, "j")?, - Cursor::CharacterPositionForward(n) => n.write_csi(w, "a")?, - Cursor::CharacterAndLinePosition { line, col } => write!(w, "{};{}f", line, col)?, - Cursor::LinePositionAbsolute(n) => n.write_csi(w, "d")?, - Cursor::LinePositionBackward(n) => n.write_csi(w, "k")?, - Cursor::LinePositionForward(n) => n.write_csi(w, "e")?, + Cursor::BackwardTabulation(n) => n.write_csi(f, "Z")?, + Cursor::CharacterAbsolute(col) => col.write_csi(f, "G")?, + Cursor::ForwardTabulation(n) => n.write_csi(f, "I")?, + Cursor::NextLine(n) => n.write_csi(f, "E")?, + Cursor::PrecedingLine(n) => n.write_csi(f, "F")?, + Cursor::ActivePositionReport { line, col } => write!(f, "{};{}R", line, col)?, + Cursor::Left(n) => n.write_csi(f, "D")?, + Cursor::Down(n) => n.write_csi(f, "B")?, + Cursor::Right(n) => n.write_csi(f, "C")?, + Cursor::Up(n) => n.write_csi(f, "A")?, + Cursor::Position { line, col } => write!(f, "{};{}H", line, col)?, + Cursor::LineTabulation(n) => n.write_csi(f, "Y")?, + Cursor::TabulationControl(n) => n.write_csi(f, "W")?, + Cursor::TabulationClear(n) => n.write_csi(f, "g")?, + Cursor::CharacterPositionAbsolute(n) => n.write_csi(f, "`")?, + Cursor::CharacterPositionBackward(n) => n.write_csi(f, "j")?, + Cursor::CharacterPositionForward(n) => n.write_csi(f, "a")?, + Cursor::CharacterAndLinePosition { line, col } => write!(f, "{};{}f", line, col)?, + Cursor::LinePositionAbsolute(n) => n.write_csi(f, "d")?, + Cursor::LinePositionBackward(n) => n.write_csi(f, "k")?, + Cursor::LinePositionForward(n) => n.write_csi(f, "e")?, } Ok(()) } @@ -565,11 +554,11 @@ pub enum Sgr { Background(ColorSpec), } -impl EncodeEscape for Sgr { - fn encode_escape(&self, w: &mut W) -> Result<(), std::io::Error> { +impl Display for Sgr { + fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> { macro_rules! code { ($t:ident) => { - write!(w, "{}m", SgrCode::$t as i64)? + write!(f, "{}m", SgrCode::$t as i64)? }; } @@ -580,7 +569,7 @@ impl EncodeEscape for Sgr { $(AnsiColor::$Ansi => code!($code) ,)* } } else { - write!(w, "{};5;{}m", SgrCode::$eightbit as i64, $idx)? + write!(f, "{};5;{}m", SgrCode::$eightbit as i64, $idx)? } } } @@ -641,7 +630,7 @@ impl EncodeEscape for Sgr { (White, ForegroundBrightWhite) ), Sgr::Foreground(ColorSpec::TrueColor(c)) => write!( - w, + f, "{};2;{};{};{}m", SgrCode::ForegroundColor as i64, c.red, @@ -672,7 +661,7 @@ impl EncodeEscape for Sgr { (White, BackgroundBrightWhite) ), Sgr::Background(ColorSpec::TrueColor(c)) => write!( - w, + f, "{};2;{};{};{}m", SgrCode::BackgroundColor as i64, c.red, @@ -1068,6 +1057,7 @@ impl<'a> Iterator for CSIParser<'a> { #[cfg(test)] mod test { use super::*; + use std::io::Write; fn parse(control: char, params: &[i64], expected: &str) -> Vec { let res = CSI::parse(params, &[], false, control).collect(); @@ -1084,7 +1074,9 @@ mod test { fn encode(seq: &Vec) -> String { let mut res = Vec::new(); - seq.encode_escape(&mut res).unwrap(); + for s in seq { + write!(res, "{}", s).unwrap(); + } String::from_utf8(res).unwrap() } diff --git a/src/escape/esc.rs b/src/escape/esc.rs index 121186e31..bde0347a8 100644 --- a/src/escape/esc.rs +++ b/src/escape/esc.rs @@ -1,6 +1,5 @@ -use escape::EncodeEscape; use num::{self, ToPrimitive}; -use std; +use std::fmt::{Display, Error as FmtError, Formatter, Write as FmtWrite}; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Esc { @@ -104,24 +103,27 @@ impl Esc { } } -impl EncodeEscape for Esc { +impl Display for Esc { // TODO: data size optimization opportunity: if we could somehow know that we // had a run of CSI instances being encoded in sequence, we could // potentially collapse them together. This is a few bytes difference in // practice so it may not be worthwhile with modern networks. - fn encode_escape(&self, w: &mut W) -> Result<(), std::io::Error> { - w.write_all(&[0x1b])?; + fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> { + f.write_char(0x1b as char)?; use self::Esc::*; match self { Code(code) => { let packed = code.to_u16() .expect("num-derive failed to implement ToPrimitive"); if packed > u8::max_value() as u16 { - let buf = [(packed >> 8) as u8, (packed & 0xff) as u8]; - w.write_all(&buf)?; + write!( + f, + "{}{}", + (packed >> 8) as u8 as char, + (packed & 0xff) as u8 as char + )?; } else { - let buf = [(packed & 0xff) as u8]; - w.write_all(&buf)?; + f.write_char((packed & 0xff) as u8 as char)?; } } Unspecified { @@ -129,11 +131,9 @@ impl EncodeEscape for Esc { control, } => { if let Some(i) = intermediate { - let buf = [*i, *control]; - w.write_all(&buf)?; + write!(f, "{}{}", *i as char, *control as char)?; } else { - let buf = [*control]; - w.write_all(&buf)?; + f.write_char(*control as char)?; } } }; @@ -146,9 +146,7 @@ mod test { use super::*; fn encode(osc: &Esc) -> String { - let mut res = Vec::new(); - osc.encode_escape(&mut res).unwrap(); - String::from_utf8(res).unwrap() + format!("{}", osc) } fn parse(esc: &str) -> Esc { diff --git a/src/escape/mod.rs b/src/escape/mod.rs index 64535d0cb..44227bd60 100644 --- a/src/escape/mod.rs +++ b/src/escape/mod.rs @@ -3,7 +3,7 @@ //! escape sequences. It provides encoding and decoding functionality //! only; it does not provide terminal emulation facilities itself. use num; -use std; +use std::fmt::{Display, Error as FmtError, Formatter, Write as FmtWrite}; pub mod csi; pub mod esc; @@ -33,33 +33,20 @@ pub enum Action { /// Encode self as an escape sequence. The escape sequence may potentially /// be clear text with no actual escape sequences. -pub trait EncodeEscape { - fn encode_escape(&self, w: &mut W) -> Result<(), std::io::Error>; -} - -impl EncodeEscape for Action { - fn encode_escape(&self, w: &mut W) -> Result<(), std::io::Error> { +impl Display for Action { + fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> { match self { - Action::Print(c) => write!(w, "{}", c), - Action::Control(Control::Code(c)) => w.write_all(&[c.clone() as u8]), - Action::Control(Control::Unspecified(c)) => w.write_all(&[*c]), + Action::Print(c) => write!(f, "{}", c), + Action::Control(Control::Code(c)) => f.write_char(*c as u8 as char), + Action::Control(Control::Unspecified(c)) => f.write_char(*c as char), Action::DeviceControl(_) => unimplemented!(), - Action::OperatingSystemCommand(osc) => osc.encode_escape(w), - Action::CSI(csi) => csi.encode_escape(w), - Action::Esc(esc) => esc.encode_escape(w), + Action::OperatingSystemCommand(osc) => osc.fmt(f), + Action::CSI(csi) => csi.fmt(f), + Action::Esc(esc) => esc.fmt(f), } } } -impl EncodeEscape for [T] { - fn encode_escape(&self, w: &mut W) -> Result<(), std::io::Error> { - for item in self { - item.encode_escape(w)?; - } - Ok(()) - } -} - #[derive(Debug, Clone, PartialEq, Eq)] pub enum DeviceControlMode { /// Identify device control mode from the encoded parameters. @@ -82,7 +69,7 @@ pub enum DeviceControlMode { } /// C0 or C1 control codes -#[derive(Debug, Clone, PartialEq, Eq, FromPrimitive)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, FromPrimitive)] #[repr(u8)] pub enum ControlCode { Null = 0, diff --git a/src/escape/osc.rs b/src/escape/osc.rs index 593310a9e..4a512fa51 100644 --- a/src/escape/osc.rs +++ b/src/escape/osc.rs @@ -1,8 +1,7 @@ -use escape::EncodeEscape; use failure; use num; -use std; use std::collections::HashMap; +use std::fmt::{Display, Error as FmtError, Formatter}; #[derive(Debug, Clone, PartialEq, Eq)] pub enum OperatingSystemCommand { @@ -76,27 +75,22 @@ impl Hyperlink { } } -impl EncodeEscape for Option { - fn encode_escape(&self, w: &mut W) -> Result<(), std::io::Error> { - match self { - None => write!(w, "8;;"), - Some(link) => { - write!(w, "8;")?; - for (idx, (k, v)) in link.params.iter().enumerate() { - // TODO: protect against k, v containing : or = - if idx > 0 { - write!(w, ":")?; - } - write!(w, "{}={}", k, v)?; - } - // TODO: ensure that link.uri doesn't contain characters - // outside the range 32-126. Need to pull in a URI/URL - // crate to help with this. - write!(w, ";{}", link.uri)?; - - Ok(()) +impl Display for Hyperlink { + fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> { + write!(f, "8;")?; + for (idx, (k, v)) in self.params.iter().enumerate() { + // TODO: protect against k, v containing : or = + if idx > 0 { + write!(f, ":")?; } + write!(f, "{}={}", k, v)?; } + // TODO: ensure that link.uri doesn't contain characters + // outside the range 32-126. Need to pull in a URI/URL + // crate to help with this. + write!(f, ";{}", self.uri)?; + + Ok(()) } } @@ -171,13 +165,13 @@ pub enum OperatingSystemCommandCode { ITermProprietary = 1337, } -impl EncodeEscape for OperatingSystemCommand { - fn encode_escape(&self, w: &mut W) -> Result<(), std::io::Error> { - write!(w, "\x1b]")?; +impl Display for OperatingSystemCommand { + fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> { + write!(f, "\x1b]")?; macro_rules! single_string { ($variant:ident, $s:expr) => { - write!(w, "{};{}", OperatingSystemCommandCode::$variant as u8, $s)? + write!(f, "{};{}", OperatingSystemCommandCode::$variant as u8, $s)? }; }; @@ -186,18 +180,20 @@ impl EncodeEscape for OperatingSystemCommand { SetIconNameAndWindowTitle(title) => single_string!(SetIconNameAndWindowTitle, title), SetWindowTitle(title) => single_string!(SetWindowTitle, title), SetIconName(title) => single_string!(SetIconName, title), - SetHyperlink(link) => link.encode_escape(w)?, + SetHyperlink(Some(link)) => link.fmt(f)?, + SetHyperlink(None) => write!(f, "8;;")?, Unspecified(v) => { for (idx, item) in v.iter().enumerate() { if idx > 0 { - write!(w, ";")?; + write!(f, ";")?; } - w.write_all(item.as_slice())?; + f.write_str(&String::from_utf8_lossy(item))?; + //f.write_all(item.as_slice())?; } } __Nonexhaustive => {} }; - write!(w, "\x07")?; + write!(f, "\x07")?; Ok(()) } } @@ -207,9 +203,7 @@ mod test { use super::*; fn encode(osc: &OperatingSystemCommand) -> String { - let mut res = Vec::new(); - osc.encode_escape(&mut res).unwrap(); - String::from_utf8(res).unwrap() + format!("{}", osc) } fn parse(osc: &[&str], expected: &str) -> OperatingSystemCommand { diff --git a/src/escape/parser/mod.rs b/src/escape/parser/mod.rs index a764658df..3ad722cef 100644 --- a/src/escape/parser/mod.rs +++ b/src/escape/parser/mod.rs @@ -106,12 +106,14 @@ mod test { use super::*; use cell::Intensity; use escape::csi::Sgr; - use escape::EncodeEscape; use escape::EscCode; + use std::io::Write; fn encode(seq: &Vec) -> String { let mut res = Vec::new(); - seq.encode_escape(&mut res).unwrap(); + for s in seq { + write!(res, "{}", s).unwrap(); + } String::from_utf8(res).unwrap() } diff --git a/src/render/terminfo.rs b/src/render/terminfo.rs index d1ebf6e16..5c08baef2 100644 --- a/src/render/terminfo.rs +++ b/src/render/terminfo.rs @@ -4,7 +4,6 @@ use cell::{AttributeChange, Blink, CellAttributes, Intensity, Underline}; use color::{ColorAttribute, ColorSpec}; use escape::csi::{Cursor, Edit, EraseInDisplay, Sgr, CSI}; use escape::osc::OperatingSystemCommand; -use escape::EncodeEscape; use failure; use render::Renderer; use screen::{Change, Position}; @@ -56,13 +55,13 @@ impl TerminfoRenderer { if let Some(attr) = self.get_capability::() { attr.expand().to(out)?; } else { - CSI::Sgr(Sgr::$sgr(value)).encode_escape(&mut out)?; + write!(out, "{}", CSI::Sgr(Sgr::$sgr(value)))?; } } else { if let Some(attr) = self.get_capability::() { attr.expand().to(out)?; } else { - CSI::Sgr(Sgr::$sgr(value)).encode_escape(&mut out)?; + write!(out, "{}", CSI::Sgr(Sgr::$sgr(value)))?; } } } @@ -84,7 +83,7 @@ impl TerminfoRenderer { if let Some(exit) = self.get_capability::() { exit.expand().to(WriteWrapper::new(out))?; } else { - CSI::Sgr(Sgr::Reset).encode_escape(&mut WriteWrapper::new(out))?; + write!(out, "{}", CSI::Sgr(Sgr::Reset))?; } if attr.intensity() != self.current_attr.intensity() { @@ -93,16 +92,14 @@ impl TerminfoRenderer { if let Some(bold) = self.get_capability::() { bold.expand().to(WriteWrapper::new(out))?; } else { - CSI::Sgr(Sgr::Intensity(attr.intensity())) - .encode_escape(&mut WriteWrapper::new(out))?; + write!(out, "{}", CSI::Sgr(Sgr::Intensity(attr.intensity())))?; } } Intensity::Half => { if let Some(dim) = self.get_capability::() { dim.expand().to(WriteWrapper::new(out))?; } else { - CSI::Sgr(Sgr::Intensity(attr.intensity())) - .encode_escape(&mut WriteWrapper::new(out))?; + write!(out, "{}", CSI::Sgr(Sgr::Intensity(attr.intensity())))?; } } _ => {} @@ -129,8 +126,7 @@ impl TerminfoRenderer { if let Some(attr) = self.get_capability::() { attr.expand().to(WriteWrapper::new(out))?; } else { - CSI::Sgr(Sgr::Blink(attr.blink())) - .encode_escape(&mut WriteWrapper::new(out))?; + write!(out, "{}", CSI::Sgr(Sgr::Blink(attr.blink())))?; } } @@ -138,14 +134,12 @@ impl TerminfoRenderer { if let Some(attr) = self.get_capability::() { attr.expand().to(WriteWrapper::new(out))?; } else { - CSI::Sgr(Sgr::Inverse(attr.reverse())) - .encode_escape(&mut WriteWrapper::new(out))?; + write!(out, "{}", CSI::Sgr(Sgr::Inverse(attr.reverse())))?; } } if attr.invisible() != self.current_attr.invisible() { - CSI::Sgr(Sgr::Invisible(attr.invisible())) - .encode_escape(&mut WriteWrapper::new(out))?; + write!(out, "{}", CSI::Sgr(Sgr::Invisible(attr.invisible())))?; } } @@ -153,8 +147,11 @@ impl TerminfoRenderer { // TODO: add strikethrough to Capabilities if attr.strikethrough() != self.current_attr.strikethrough() { - CSI::Sgr(Sgr::StrikeThrough(attr.strikethrough())) - .encode_escape(&mut WriteWrapper::new(out))?; + write!( + out, + "{}", + CSI::Sgr(Sgr::StrikeThrough(attr.strikethrough())) + )?; } } @@ -164,23 +161,28 @@ impl TerminfoRenderer { match (has_true_color, attr.foreground) { (true, ColorAttribute::TrueColorWithPaletteFallback(tc, _)) | (true, ColorAttribute::TrueColorWithDefaultFallback(tc)) => { - CSI::Sgr(Sgr::Foreground(ColorSpec::TrueColor(tc))) - .encode_escape(&mut WriteWrapper::new(out))?; + write!( + out, + "{}", + CSI::Sgr(Sgr::Foreground(ColorSpec::TrueColor(tc))) + )?; } (false, ColorAttribute::TrueColorWithDefaultFallback(_)) | (_, ColorAttribute::Default) => { // Terminfo doesn't define a reset color to default, so // we use the ANSI code. - CSI::Sgr(Sgr::Foreground(ColorSpec::Default)) - .encode_escape(&mut WriteWrapper::new(out))?; + write!(out, "{}", CSI::Sgr(Sgr::Foreground(ColorSpec::Default)))?; } (false, ColorAttribute::TrueColorWithPaletteFallback(_, idx)) | (_, ColorAttribute::PaletteIndex(idx)) => { if let Some(set) = self.get_capability::() { set.expand().color(idx).to(WriteWrapper::new(out))?; } else { - CSI::Sgr(Sgr::Foreground(ColorSpec::PaletteIndex(idx))) - .encode_escape(&mut WriteWrapper::new(out))?; + write!( + out, + "{}", + CSI::Sgr(Sgr::Foreground(ColorSpec::PaletteIndex(idx))) + )?; } } } @@ -190,23 +192,28 @@ impl TerminfoRenderer { match (has_true_color, attr.background) { (true, ColorAttribute::TrueColorWithPaletteFallback(tc, _)) | (true, ColorAttribute::TrueColorWithDefaultFallback(tc)) => { - CSI::Sgr(Sgr::Background(ColorSpec::TrueColor(tc))) - .encode_escape(&mut WriteWrapper::new(out))?; + write!( + out, + "{}", + CSI::Sgr(Sgr::Background(ColorSpec::TrueColor(tc))) + )?; } (false, ColorAttribute::TrueColorWithDefaultFallback(_)) | (_, ColorAttribute::Default) => { // Terminfo doesn't define a reset color to default, so // we use the ANSI code. - CSI::Sgr(Sgr::Background(ColorSpec::Default)) - .encode_escape(&mut WriteWrapper::new(out))?; + write!(out, "{}", CSI::Sgr(Sgr::Background(ColorSpec::Default)))?; } (false, ColorAttribute::TrueColorWithPaletteFallback(_, idx)) | (_, ColorAttribute::PaletteIndex(idx)) => { if let Some(set) = self.get_capability::() { set.expand().color(idx).to(WriteWrapper::new(out))?; } else { - CSI::Sgr(Sgr::Background(ColorSpec::PaletteIndex(idx))) - .encode_escape(&mut WriteWrapper::new(out))?; + write!( + out, + "{}", + CSI::Sgr(Sgr::Background(ColorSpec::PaletteIndex(idx))) + )?; } } } @@ -215,11 +222,11 @@ impl TerminfoRenderer { if self.caps.hyperlinks() { if let Some(link) = attr.hyperlink.as_ref() { let osc = OperatingSystemCommand::SetHyperlink(Some((**link).clone())); - osc.encode_escape(&mut WriteWrapper::new(out))?; + write!(out, "{}", osc)?; } else if self.current_attr.hyperlink.is_some() { // Close out the old hyperlink let osc = OperatingSystemCommand::SetHyperlink(None); - osc.encode_escape(&mut WriteWrapper::new(out))?; + write!(out, "{}", osc)?; } } @@ -294,12 +301,18 @@ impl Renderer for TerminfoRenderer { if let Some(attr) = self.get_capability::() { attr.expand().to(WriteWrapper::new(out))?; } else { - CSI::Cursor(Cursor::Position { line: 1, col: 1 }) - .encode_escape(&mut WriteWrapper::new(out))? + write!( + out, + "{}", + CSI::Cursor(Cursor::Position { line: 1, col: 1 }) + )?; } - CSI::Edit(Edit::EraseInDisplay(EraseInDisplay::EraseDisplay)) - .encode_escape(&mut WriteWrapper::new(out))?; + write!( + out, + "{}", + CSI::Edit(Edit::EraseInDisplay(EraseInDisplay::EraseDisplay)) + )?; } } else { // We're setting the background to a specific color, so we get to @@ -308,8 +321,7 @@ impl Renderer for TerminfoRenderer { if let Some(attr) = self.get_capability::() { attr.expand().to(WriteWrapper::new(out))?; } else { - CSI::Cursor(Cursor::Position { line: 1, col: 1 }) - .encode_escape(&mut WriteWrapper::new(out))? + write!(out, "{}", CSI::Cursor(Cursor::Position { line: 1, col: 1 }))?; } let size = out.get_screen_size()?; @@ -369,8 +381,7 @@ impl Renderer for TerminfoRenderer { if let Some(attr) = self.get_capability::() { attr.expand().to(WriteWrapper::new(out))?; } else { - CSI::Cursor(Cursor::Position { line: 1, col: 1 }) - .encode_escape(&mut WriteWrapper::new(out))? + write!(out, "{}", CSI::Cursor(Cursor::Position { line: 1, col: 1 }))?; } } Change::CursorPosition { @@ -380,7 +391,7 @@ impl Renderer for TerminfoRenderer { if let Some(attr) = self.get_capability::() { attr.expand().to(WriteWrapper::new(out))?; } else { - CSI::Cursor(Cursor::Down(1)).encode_escape(&mut WriteWrapper::new(out))? + write!(out, "{}", CSI::Cursor(Cursor::Down(1)))?; } } Change::CursorPosition { @@ -390,7 +401,7 @@ impl Renderer for TerminfoRenderer { if let Some(attr) = self.get_capability::() { attr.expand().to(WriteWrapper::new(out))?; } else { - CSI::Cursor(Cursor::Up(1)).encode_escape(&mut WriteWrapper::new(out))? + write!(out, "{}", CSI::Cursor(Cursor::Up(1)))?; } } Change::CursorPosition { @@ -400,7 +411,7 @@ impl Renderer for TerminfoRenderer { if let Some(attr) = self.get_capability::() { attr.expand().to(WriteWrapper::new(out))?; } else { - CSI::Cursor(Cursor::Left(1)).encode_escape(&mut WriteWrapper::new(out))? + write!(out, "{}", CSI::Cursor(Cursor::Left(1)))?; } } Change::CursorPosition { @@ -410,7 +421,7 @@ impl Renderer for TerminfoRenderer { if let Some(attr) = self.get_capability::() { attr.expand().to(WriteWrapper::new(out))?; } else { - CSI::Cursor(Cursor::Right(1)).encode_escape(&mut WriteWrapper::new(out))? + write!(out, "{}", CSI::Cursor(Cursor::Right(1)))?; } } Change::CursorPosition { @@ -426,10 +437,14 @@ impl Renderer for TerminfoRenderer { } else { // We need to manually convert to 1-based as the CSI representation // requires it and there's no automatic conversion. - CSI::Cursor(Cursor::Position { - line: x + 1, - col: y + 1, - }).encode_escape(&mut WriteWrapper::new(out))? + write!( + out, + "{}", + CSI::Cursor(Cursor::Position { + line: x + 1, + col: y + 1, + }) + )?; } } Change::CursorPosition { .. } => {