fluent-ansi
fluent-ansi is a Rust library designed to handle ANSI escape sequences for the terminal. It provides a modular, composable, and fluent API for styling text with colors and flags (like bold, italic).
Key Features
no_stdCompatible: Designed to work without the standard library, relying oncore::fmt::Display.- Fluent API: Allows method chaining (e.g.,
Color::RED.in_fg().bold().applied_to("text")). - Immutability: All styling types are immutable and most implement
Copy.
Installation
Add this to your Cargo.toml:
[]
= "0.2.1"
Usage
The primary way to use fluent-ansi is through its fluent API. You can combine colors and flags to create a Style, and then apply it to any type that implements Display.
use ;
// Create a style
let style: Style = RED.in_fg.bold;
// Apply it to some content
let styled: = style.applied_to;
// Print it directly
println!;
// Or get the string with escape sequences
let content_with_escape_sequences = format!;
assert_eq!;
Composable API
There are several ways to reach the same result, depending on your preference:
use ;
let stl: Style = new.set.set;
let stl: Style = new.set_flag.set_color;
let stl: Style = new.add.add;
let stl: Style = new.flag.color;
let stl: Style = new.bold.fg;
let stl: Style = Bold.fg;
let stl: Style = RED.in_fg.bold;
Styling Elements
Flags
Flags can be used on their own, combined with other elements, or applied to content:
use *;
assert_eq!;
assert_eq!;
Colors
The library supports Basic (3/4-bit), 8-bit (256 colors), and RGB (TrueColor) colors. Colors must be associated with a Plane (Foreground or Background), and
can also be applied to some content.
use *;
let red_in_foreground = RED.in_fg;
assert_eq!;
assert_eq!;
let blue_in_background = BLUE.in_bg;
assert_eq!;
assert_eq!;
License
This project is licensed under the MIT License - see the LICENSE file for details.