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§
Trait Implementations§
impl Copy for Encoder
impl Eq for Encoder
impl StructuralPartialEq for Encoder
Auto Trait Implementations§
impl Forget for Encoder
impl Freeze for Encoder
impl Move for Encoder
impl RefUnwindSafe for Encoder
impl Send for Encoder
impl Sync for Encoder
impl Unpin for Encoder
impl UnsafeUnpin for Encoder
impl UnwindSafe for Encoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more