#ansi #parsing #terminal #no-std

no-std cansi

Catergorise ANSI - ANSI escape code parser and categoriser

8 stable releases

Uses new Rust 2021

2.2.1 Nov 3, 2022
2.2.0 Jul 28, 2022
2.1.1 Aug 20, 2020
2.1.0 Aug 23, 2019
1.0.0 Dec 31, 2018

#185 in Parser implementations

Download history 117/week @ 2022-08-18 196/week @ 2022-08-25 268/week @ 2022-09-01 129/week @ 2022-09-08 68/week @ 2022-09-15 67/week @ 2022-09-22 76/week @ 2022-09-29 212/week @ 2022-10-06 84/week @ 2022-10-13 56/week @ 2022-10-20 156/week @ 2022-10-27 274/week @ 2022-11-03 259/week @ 2022-11-10 194/week @ 2022-11-17 169/week @ 2022-11-24 175/week @ 2022-12-01

836 downloads per month
Used in 5 crates (2 directly)

MIT license

54KB
1K SLoC

Build Status Latest Version Rust Documentation codecov

Catergorise ANSI - ANSI escape code parser and categoriser

See the rs docs. Look at progress and contribute on github.

cansi will parse text with ANSI escape sequences in it and return a deconstructed text with metadata around the colouring and styling. cansi is only concerned with CSI sequences, particuarly the SGR parameters. cansi will not construct escaped text, there are crates such as colored that do a great job of colouring and styling text.

Example usage

This example was done using the colored crate to help with constructing the escaped text string. It will work with other tools that inject escape sequences into text strings (given they follow ANSI specification).

extern crate cansi;
extern crate colored;

use cansi::*;
use colored::Colorize;
use std::io::Write;

let v = &mut Vec::new();
write!(
  v,
  "Hello, {}{}{}{}{}{}",
  "w".white().on_red(),
  "o".cyan().on_green(),
  "r".magenta().on_yellow(),
  "l".blue().on_white(),
  "d".yellow().on_bright_cyan(),
  "!".bright_red().on_bright_yellow(),
)
.unwrap();

let text = String::from_utf8_lossy(&v);
let result = categorise_text(&text); // cansi function

assert_eq!(result.len(), 7); // there should be seven differently styled components

assert_eq!("Hello, world!", &construct_text_no_codes(&result));

// 'Hello, ' is just defaults
assert_eq!(
  result[0],
  CategorisedSlice {
    text: "Hello, ",
    start: 0,
    end: 7,
    fg_colour: Color::White,
    bg_colour: Color::Black,
    intensity: Intensity::Normal,
    italic: false,
    underline: false,
    blink: false,
    reversed: false,
    hidden: false,
    strikethrough: false
  }
);

// 'w' is coloured differently
assert_eq!(
  result[1],
  CategorisedSlice {
    text: "w",
    start: 15,
    end: 16,
    fg_colour: Color::White,
    bg_colour: Color::Red,
    intensity: Intensity::Normal,
    italic: false,
    underline: false,
    blink: false,
    reversed: false,
    hidden: false,
    strikethrough: false
  }
);

Targeting no_std

This crate can use alloc in place of the standard library for no_std targets. The standard library is enabled by default, so disabling default features and enabling the alloc feature is required to use the crate this way.

[dependencies]
cansi = { version = "2.1.0", default-features = false, features = ["alloc"] }

No runtime deps