Skip to main content

Encode

Trait Encode 

Source
pub trait Encode: Surface {
    // Provided methods
    fn encode<W: Write>(&self, w: &mut W) -> Result<()> { ... }
    fn encode_with<W: Write>(&self, w: &mut W, profile: Profile) -> Result<()> { ... }
    fn display(&self) -> SurfaceDisplay<'_, Self> { ... }
    fn display_with(&self, profile: Profile) -> SurfaceDisplay<'_, Self> { ... }
}
Expand description

Serialize a Surface into escape sequences and text.

This extension trait is implemented for every Surface, so any cell grid (a Buffer, Window, TextBuffer, or Screen) can be rendered back to its escape-code form.

Use encode to stream straight into an io::Write, or display to borrow the surface as a Display for format!, to_string, and the write! macros. The encode_with and display_with variants downsample every cell’s colors to a Profile first, the same way the renderer adapts output to a terminal’s color capability.

Provided Methods§

Source

fn encode<W: Write>(&self, w: &mut W) -> Result<()>

Write the surface to w as escape sequences and text.

One terminal row is written per line, separated by CRLF (\r\n) with no trailing newline after the final row. Each row starts and ends in the default style: any open SGR state or hyperlink is reset at the end of the row.

Trailing unstyled blank cells are trimmed from each row, so a row with nothing but default spaces emits an empty line. A blank cell still counts as visible when it carries a style (for example a space with a background color), so styled trailing space is preserved.

Wide-cell continuation placeholders are skipped because the wide primary already carries the full grapheme cluster. Colors are written as stored; use encode_with to downsample them to a color Profile.

§Errors

Propagates any io::Error from w.

Source

fn encode_with<W: Write>(&self, w: &mut W, profile: Profile) -> Result<()>

Write the surface to w, downsampling colors to profile.

Each cell’s style is converted for profile before it is emitted, so the output matches what the renderer would produce for a terminal with that color capability: Profile::Ansi and Profile::Ansi256 quantize to the nearest palette color, Profile::Ascii drops colors but keeps attributes, and Profile::Disabled drops all styling (including hyperlinks). Profile::TrueColor is identical to encode.

§Errors

Propagates any io::Error from w.

Source

fn display(&self) -> SurfaceDisplay<'_, Self>

Borrow the surface as a Display adapter.

The returned value renders the same bytes as encode when formatted, so surface.display().to_string() produces the encoded string and write!(w, "{}", surface.display()) writes it to any io::Write or fmt::Write sink.

Source

fn display_with(&self, profile: Profile) -> SurfaceDisplay<'_, Self>

Borrow the surface as a Display adapter that downsamples colors to profile.

Formatting it renders the same bytes as encode_with with the same profile.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<S: Surface + ?Sized> Encode for S