| Crates.io | fluent-ansi |
| lib.rs | fluent-ansi |
| version | 0.4.0 |
| created_at | 2025-12-20 18:37:42.01788+00 |
| updated_at | 2026-01-08 02:37:26.822668+00 |
| description | A fluent interface for ANSI terminal colors and styles in Rust. |
| homepage | |
| repository | https://github.com/erdavila/fluent-ansi |
| max_upload_size | |
| id | 1996836 |
| size | 136,215 |
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).
no_std Compatible: Designed to work without the standard library, relying on core::fmt::Display.Color::RED.bold().applied_to("text")).Copy.Run this command in your project directory:
cargo add fluent-ansi
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 fluent_ansi::{prelude::*, Style, Styled};
// Create a style
let style: Style = Color::RED.bold();
// Apply it to some content
let styled: Styled<&str> = style.applied_to("Some content");
// Print it directly
println!("{}", styled);
// Or get the string with escape sequences
let content_with_escape_sequences = format!("{}", styled);
assert_eq!(content_with_escape_sequences, "\x1b[1;31mSome content\x1b[0m");
There are several ways to reach the same result, depending on your preference:
use fluent_ansi::{prelude::*, ColorTarget, Style, TargetedColor};
let style: Style = Style::new().set(Effect::Bold, true).set(ColorTarget::Foreground, Some(Color::RED.to_color()));
let style: Style = Style::new().set_effect(Effect::Bold, true).set_color(ColorTarget::Foreground, Some(Color::RED));
let style: Style = Style::new().add(Effect::Bold).add(TargetedColor::new(Color::RED, ColorTarget::Foreground));
let style: Style = Style::new().effect(Effect::Bold).color(TargetedColor::new(Color::RED, ColorTarget::Foreground));
let style: Style = Style::new().bold().fg(Color::RED);
let style: Style = Effect::Bold.fg(Color::RED);
let style: Style = Color::RED.bold();
Effects can be used on their own, combined with other elements, or applied to content:
use fluent_ansi::prelude::*;
assert_eq!(format!("{}", Effect::Bold), "\x1b[1m");
assert_eq!(format!("{}", Effect::Bold.applied_to("Some content")), "\x1b[1mSome content\x1b[0m");
In addition to standard effects, specific underline effects are supported (and are mutually exclusive):
use fluent_ansi::prelude::*;
assert_eq!(format!("{}", Effect::CurlyUnderline), "\x1b[4:3m");
assert_eq!(format!("{}", Style::new().dashed_underline()), "\x1b[4:5m");
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 fluent_ansi::prelude::*;
let red_foreground = Color::RED.for_fg();
assert_eq!(format!("{red_foreground}"), "\x1b[31m");
assert_eq!(format!("{}", red_foreground.applied_to("Some content")), "\x1b[31mSome content\x1b[0m");
let blue_background = Color::BLUE.for_bg();
assert_eq!(format!("{blue_background}"), "\x1b[44m");
assert_eq!(format!("{}", blue_background.applied_to("Some content")), "\x1b[44mSome content\x1b[0m");
Execute this to see a demo of ANSI formatting in action:
cargo run --example demo
Its source is in examples/demo if you want to check it.
Foreground as default targetEffect variantsColorKind instead of implementing for each color typeThis project is licensed under the MIT License - see the LICENSE file for details.