Crate cansi

Source
Expand description

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).


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"] }

Modules§

v3
Update API for version 3.0 of the crate.

Structs§

CategorisedLineIteratorDeprecated
An iterator structure for CategorisedSlices, iterating over each new line (\n or \r\n) and returns the categorised slices within those. CategorisedSlices that include a new line are split with the same style.
CategorisedSliceDeprecated
Data structure that holds information about colouring and styling of a text slice.
Match
A match.

Enums§

Color
The 8 standard colors.
Intensity
The emphasis (bold, faint) states.

Functions§

categorise_textDeprecated
Parses the text and returns each formatted slice in order. The ANSI escape codes are not included in the text slices.
construct_text_no_codesDeprecated
Constructs a string of the categorised text without the ANSI escape characters.
line_iterDeprecated
Construct an iterator over each new line (\n or \r\n) and returns the categorised slices within those. CategorisedSlices that include a new line are split with the same style.
parse
Parses ANSI escape codes from the given text, returning a vector of Match.

Type Aliases§

CategorisedLineDeprecated
The item type of CategorisedLineIterator.
CategorisedSlicesDeprecated
Type definition of the collection of CategorisedSlices.