#ansi #csi #ansi-csi

fyi_ansi

Compile-time ANSI formatting macros for FYI

2 stable releases

Uses new Rust 2024

new 2.0.2 Apr 10, 2025
2.0.1 Apr 9, 2025

#3 in #csi

37 downloads per month
Used in fyi_msg

WTFPL license

30KB
489 lines

FYI ANSI

docs.rs
crates.io ci deps.rs
license

This crate exports two simple compile-time ANSI formatting macros — csi and ansi — as well as shortcut helpers for bold, dim, and underline.

Examples

use fyi_ansi::{ansi, csi};

// The `csi` macro generates (only) the ANSI formatting sequence.
assert_eq!(csi!(bold, underline), "\x1b[1;4m");

// The `ansi` macro generates formatted content strings.
assert_eq!(
    concat!(ansi!((bold, light_red) "Error:"), " Oh no!"),
    "\x1b[1;91mError:\x1b[0m Oh no!",
);

The dim, bold, and underline macros are only shortcuts, but can help declutter your code when there's only the one style being toggled.

use fyi_ansi::{bold, dim, underline};

// Same as with `ansi`, they terminate with a blanket reset by default.
assert_eq!(bold!("I'm bold!"),              "\x1b[1mI'm bold!\x1b[0m");
assert_eq!(dim!("I'm dim!"),                "\x1b[2mI'm dim!\x1b[0m");
assert_eq!(underline!("I'm underlined!"),   "\x1b[4mI'm underlined!\x1b[0m");

// And same as `ansi`, the ">" modifier will make them terminate more
// selectively instead.
assert_eq!(bold!(> "I'm bold!"),            "\x1b[1mI'm bold!\x1b[21m");
assert_eq!(dim!(> "I'm dim!"),              "\x1b[2mI'm dim!\x1b[22m");
assert_eq!(underline!(> "I'm underlined!"), "\x1b[4mI'm underlined!\x1b[24m");

No runtime deps