ansi_to_tui/
lib.rs

1#![allow(unused_imports)]
2#![warn(missing_docs)]
3//! Parses a `Vec<u8>` as an byte sequence with ansi colors to
4//! [`tui::text::Text`][Text].  
5//!
6//! Invalid ansi colors / sequences will be ignored.  
7//!
8//!
9//! Supported features
10//! - UTF-8 using `String::from_utf8` or [`simdutf8`][simdutf8].
11//! - Most stuff like **Bold** / *Italic* / <u>Underline</u> / ~~Strikethrough~~.
12//! - Supports 4-bit color palletes.
13//! - Supports 8-bit color.
14//! - Supports True color ( RGB / 24-bit color ).
15//!
16//!
17//! ## Example
18//! The argument to the function `ansi_to_text` implements `IntoIterator` so it will be consumed on
19//! use.
20//! ```rust
21//! use ansi_to_tui::IntoText;
22//! let bytes = b"\x1b[38;2;225;192;203mAAAAA\x1b[0m".to_owned().to_vec();
23//! let text = bytes.into_text().unwrap();
24//! ```
25//! Example parsing from a file.
26//! ```rust
27//! use ansi_to_tui::IntoText;
28//! let buffer = std::fs::read("ascii/text.ascii").unwrap();
29//! let text = buffer.into_text().unwrap();
30//! ```
31//!
32//! If you want to use [`simdutf8`][simdutf8] instead of `String::from_utf8()`  
33//! for parsing UTF-8 then enable optional feature `simd`  
34//!  
35//! [Text]: https://docs.rs/tui/0.15.0/tui/text/struct.Text.html
36//! [ansi-to-tui]: https://github.com/uttarayan21/ansi-to-tui
37//! [simdutf8]: https://github.com/rusticstuff/simdutf8
38
39// mod ansi;
40mod code;
41mod error;
42mod parser;
43pub use error::Error;
44use tui::text::Text;
45
46/// IntoText will convert any type that has a AsRef<[u8]> to a Text.
47pub trait IntoText {
48    /// Convert the type to a Text.
49    #[allow(clippy::wrong_self_convention)]
50    fn into_text(&self) -> Result<Text<'static>, Error>;
51    /// Convert the type to a Text while trying to copy as less as possible
52    #[cfg(feature = "zero-copy")]
53    fn to_text(&self) -> Result<Text<'_>, Error>;
54}
55impl<T> IntoText for T
56where
57    T: AsRef<[u8]>,
58{
59    fn into_text(&self) -> Result<Text<'static>, Error> {
60        Ok(crate::parser::text(self.as_ref())?.1)
61    }
62
63    #[cfg(feature = "zero-copy")]
64    fn to_text(&self) -> Result<Text<'_>, Error> {
65        Ok(crate::parser::text_fast(self.as_ref())?.1)
66    }
67}