television_previewers/
ansi.rs

1#![allow(unused_imports)]
2//! This module provides a way to parse ansi escape codes and convert them to ratatui objects.
3//!
4//! This code is a modified version of [ansi_to_tui](https://github.com/ratatui/ansi-to-tui).
5
6// mod ansi;
7pub mod code;
8pub mod error;
9pub mod parser;
10pub use error::Error;
11use tui::text::Text;
12
13/// `IntoText` will convert any type that has a `AsRef<[u8]>` to a Text.
14pub trait IntoText {
15    /// Convert the type to a Text.
16    #[allow(clippy::wrong_self_convention)]
17    fn into_text(&self) -> Result<Text<'static>, Error>;
18    /// Convert the type to a Text while trying to copy as less as possible
19    #[cfg(feature = "zero-copy")]
20    fn to_text(&self) -> Result<Text<'_>, Error>;
21}
22impl<T> IntoText for T
23where
24    T: AsRef<[u8]>,
25{
26    fn into_text(&self) -> Result<Text<'static>, Error> {
27        Ok(crate::ansi::parser::text(self.as_ref())?.1)
28    }
29
30    #[cfg(feature = "zero-copy")]
31    fn to_text(&self) -> Result<Text<'_>, Error> {
32        Ok(crate::ansi::parser::text_fast(self.as_ref())?.1)
33    }
34}