fansi 0.1.0

Simple drop-in ansi support for strings printed to the terminal.
Documentation
  • Coverage
  • 0%
    0 out of 33 items documented0 out of 12 items with examples
  • Size
  • Source code size: 11.46 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 472.19 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • bvanseg

Fansi

This crate allows developers to add color to their terminal output.

Here is an example to make text bold:

use fansi::{style::AnsiStyle, string::AnsiString};

// Create style.
let style = vec![AnsiStyle::Bold];
// Create text with style.
let text = AnsiString::with_styles_vec("world!", style);
// Print text.
println!("Hello, {}", text);

Here is another example to make the foreground text green:

use crate::{color::AnsiColor, style::AnsiStyle, string::AnsiString};

// Create style.
let style = vec![AnsiStyle::ForegroundColor(AnsiColor::Green)];
// Create text with style.
let text = AnsiString::with_styles_vec("world!", style);
// Print text.
println!("Hello, {}", text);

Pre-Computing Style Strings

When writing code such as the above examples, it is important to note that using raw arrays/vectors of styles is sub-optimal due to the styles being converted to strings and joined when creating the AnsiString.

If performance is important for your application and the above method is not acceptable, an AnsiStyleContainer struct is provided which takes in styles and compiles them into a String internally.

The container can then be applied to any String and re-used over and over as shown below:

// Create styles.
let style = vec![AnsiStyle::ForegroundColor(AnsiColor::Green)];
// Create container.
let container = AnsiStyleContainer::new(style);
// Apply container's compiled style string to text.
let text = container.apply("world!");
// Print text.
println!("Hello, {}", text);

Windows Usage

For windows, you will need to do an extra step to enable ANSI support in your Powershell and Command Prompt terminals:

let result: Result<(), i32> = enable_ansi_support();