Skip to main content

Encoder

Struct Encoder 

Source
pub struct Encoder { /* private fields */ }
Expand description

Turns lines of styled spans into the bytes a terminal reads.

Layout never sees an escape byte: it produces Lines, and this encoder adds the styling afterwards.

  • It writes a style sequence only where the style changes between spans, with every colour converted to the encoder’s depth.
  • It skips a span with no text.
  • It writes a reset where a style ends. At the end of a line it writes one only when a style is still open, so each line stands alone in head, less -R, and a log.

An encoder with no depth writes the text of each line byte for byte, with no escape.

It needs no terminal, because the depth is an argument. A program that renders to a string or to bytes builds one from a depth it chose.

use ctl_term::ctl_text::anstyle::AnsiColor;
use ctl_term::ctl_text::{Line, Span, Style};
use ctl_term::{ColorDepth, Encoder};

let red = Style::new().fg_color(Some(AnsiColor::Red.into()));
let lines = [Line::from(Span::new("failed", red))];

let text = Encoder::new(Some(ColorDepth::TrueColor)).encode(&lines);
assert_eq!(text, "\u{1b}[31mfailed\u{1b}[0m\n");
assert_eq!(text.into_bytes(), b"\x1b[31mfailed\x1b[0m\n");

Implementations§

Source§

impl Encoder

Source

pub const fn new(depth: Option<ColorDepth>) -> Self

An encoder that writes colour at depth, or none for None.

Source

pub fn encode(self, lines: &[Line]) -> String

lines as text, each line ended by a newline.

Each line starts with no style on and ends with none on.

Trait Implementations§

Source§

impl Clone for Encoder

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Encoder

Source§

impl Debug for Encoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Encoder

Source§

impl PartialEq for Encoder

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Encoder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.