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 effects (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.bold().applied_to("text")). - Immutability: All styling types are immutable and most implement
Copy.
Installation
Run this command in your project directory:
Usage
The primary way to use fluent-ansi is through its fluent API. You can combine colors and effecs to create a Style, and then apply it to any type that implements Display.
use ;
// Create a style
let style: Style = RED.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_effect.set_color;
let stl: Style = new.add.add;
let stl: Style = new.effect.color;
let stl: Style = new.bold.fg;
let stl: Style = Bold.fg;
let stl: Style = RED.bold;
Styling Elements
Effects
Effects can be used on their own, combined with other elements, or applied to content:
use *;
assert_eq!;
assert_eq!;
Underline Styles
In addition to standard effects, specific underline styles are supported (and are mutually exclusive):
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 ColorTarget, and
can also be applied to some content.
use *;
let red_foreground = RED.for_fg;
assert_eq!;
assert_eq!;
let blue_background = BLUE.for_bg;
assert_eq!;
assert_eq!;
Changelog
v0.3.0
- Use colors with
Foregroundas default target - Add underline effects and color
- Extract trait methods tests into macros
- Move some items to their own modules
- Review
Effectvariants - Rename some types
v0.2.1
- Fix version in the README file
v0.2.0
- Define blanket implementation of
ColorKindinstead of implementing for each color type - Turn a few methods const
- Rename Forma to Style
v0.1.0
- Initial version
License
This project is licensed under the MIT License - see the LICENSE file for details.