Crate ansi_str

source ·
Expand description


A library which provides a set of methods to work with strings escaped with ansi sequences.

It’s an agnostic library in regard to different color libraries. Therefore it can be used with any library (e.g. owo-colors, nu-ansi-term).


use owo_colors::*;
use ansi_str::AnsiStr;

let hello = "Hello World!".red().to_string();
let (hello, world) = hello.ansi_split_at(6);

println!("{}", hello);
println!("{}", world);


The library doesn’t guarantee to keep style of usage of ansi sequences.

For example if your string is "\u{1b}[31;40mTEXT\u{1b}[0m" and you will call get method. It may not use "\u{1b}[31;40m" but it use it as "\u{1b}[31m" and "\u{1b}[40m".

Why that matters is because for example the following code example is not guaranteed to be true.

use ansi_str::AnsiStr;

pub fn main() {
    let hello1 = "\u{1b}[31mHello World!\u{1b}[0m";
    let hello2 = hello1.ansi_get(..).unwrap();
    assert_eq!(hello1, hello2)



  • A color is one specific type of ANSI escape code, and can refer to either the foreground or background color.


  • AnsiStr represents a list of functions to work with colored strings defined as ANSI control sequences.