Parsing and Managing ANSI-styled Strings
This library allows to:
- Parse ANSI-styled strings.
- Create styled strings from a human-readable format inspired by
rich, including in compile time. - Compare styled strings with rich diff info.
- Manipulate styled strings, e.g. split them into lines, split off parts etc.
One of guiding use cases for the library is hassle-free snapshot testing for styled strings,
without the need to compare literal strings with ANSI escapes (which is brittle and not human-readable),
and outputting more informative diff info than a simple assert_eq! would.
For the example of real-world usage, see the term-transcript crate.
Usage
Add this to your Crate.toml:
[]
= "0.5.0"
Basic usage:
use ;
const STYLED: StyledStr = styled!;
// Get the unstyled text behind the string
assert_eq!;
// Get the style spans info
assert_eq!;
// Print the string with embedded ANSI sequences
println!;
See the crate docs for more examples of usage.
Limitations
- ANSI escape sequences other than SGR ones are either dropped (in case of CSI sequences), or lead to an error.
Alternatives and similar tools
- This crate builds on the
anstylelibrary, using its styling data model.anstyletogether withanstreamprovides tools to create / output ANSI-styled strings in runtime. It doesn't cover creating strings in compile time, parsing ANSI-styled strings, or comparing styled strings. color_printprovides proc macros to create / output ANSI-styled strings usingrich-like syntax.parse-styleallows parsingrich-like style specs.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in term-transcript by you, as defined in the Apache-2.0 license,
shall be dual licensed as above, without any additional terms or conditions.