Expand description
§Farben
Farben (as in “color” in German) is a zero-dependency terminal coloring library.
It uses a markup-like syntax to apply ANSI styles to your strings: named colors,
RGB, HSL, HSV/HSB, HWB, Lab, LCH, OKLCh, hex, ANSI 256, emphasis styles,
foreground and background targeting, custom named tags, inline resets, and
inline shorthand syntax.
use farben::prelude::*;
cprintln!("[bold green]Done![/] All tests passed.");For a full walkthrough of everything Farben can do, check out the user guide.
§Features
Farben is split into opt-in feature flags so you only pull in what you need:
| Feature | What it adds |
|---|---|
| (default) | Runtime coloring: color, colorb, cformat!, cprint!, cprintln!, cprintb!, cprintbln!, cwrite!, cwriteln!, cwriteb!, cwritebln! |
compile | Compile-time validation of markup strings via proc macros |
format | Named style registry: style!, prefix! |
inline | Inline shorthand syntax (*bold*, /italic/, `code`) inside all c* macros |
lossy | Lenient parsing for unknown tags (default) |
anstyle | Interoperability with anstyle::Style |
markdown | Deprecated. Runtime inline markdown rendering. Use inline instead. |
markdown-compile | Deprecated. Compile-time inline markdown. Use inline + compile instead. |
§Emphasis Styles
Farben supports these emphasis types:
| Tag | Description |
|---|---|
bold | Bold (SGR 1) |
dim | Dimmed (SGR 2) |
italic | Italic (SGR 3) |
underline | Underline (SGR 4) |
double-underline | Double underline (SGR 21) |
blink | Slow blink (SGR 5) |
rapid-blink | Rapid blink (SGR 6) |
reverse | Reverse video (SGR 7) |
invisible | Hidden (SGR 8) |
strikethrough | Strikethrough (SGR 9) |
overline | Overline (SGR 53) |
§Quick Examples
§Named colors and emphasis
use farben::prelude::*;
cprintln!("[red]Error![/] Something went wrong.");
cprintln!("[bold underline]Important.[/]");
cprintln!("[bg:blue fg:white]Inverted.");§RGB, HSL, hex, and ANSI 256
use farben::prelude::*;
cprintln!("[rgb(255,128,0)]Orange.");
cprintln!("[hsl(120,100,50)]Green via HSL.");
cprintln!("[#ff8800]Orange via hex.");
cprintln!("[ansi(93)]Deep purple.");§Custom named tags (format feature)
use farben::prelude::*;
use farben::try_color;
style!("warn", "[bold yellow]");
prefix!("warn", "! ");
println!("{}", try_color("[warn]Watch out.").unwrap());§Inline syntax (inline feature)
use farben::prelude::*;
cprintln!("This is *bold* and /italic/ with `inline code`.");Modules§
- core
- Core Farben internals that are used by certain macros.
- prelude
- The recommended way to bring Farben’s public API into scope.
Macros§
- ansi_
strip Deprecated - Deprecated in favor of
unansi!. - ceprint
- Prints farben-colored markup to stderr without a newline.
- ceprintb
- Prints farben-colored markup to stderr without a newline, without appending a reset.
- ceprintbln
- Prints farben-colored markup to stderr with a trailing newline, without appending a reset.
- ceprintln
- Prints farben-colored markup to stderr with a trailing newline.
- cformat
- Parses and renders a farben markup string with format arguments, appending a final SGR reset.
- cformatb
- Parses and renders a farben markup string with format arguments, without appending a reset.
- color_
fmt Deprecated - Deprecated in favor of
cformat!. - cprint
- Prints farben-colored markup to stdout without a newline.
- cprintb
- Prints farben-colored markup to stdout without a newline, without appending a reset.
- cprintbln
- Prints farben-colored markup to stdout with a trailing newline, without appending a reset.
- cprintln
- Prints farben-colored markup to stdout with a trailing newline.
- cwrite
- Writes farben-colored markup to a writer without a newline.
- cwriteb
- Writes farben-colored markup to a writer without a newline, without appending a reset.
- cwritebln
- Writes farben-colored markup to a writer with a trailing newline, without appending a reset.
- cwriteln
- Writes farben-colored markup to a writer with a trailing newline.
- expand
- Debug macro to expand farben markup and print intermediate representations.
- load_
styles - Includes the build-generated
farben_styles.rsfile fromOUT_DIR. - markup_
strip Deprecated - Deprecated in favor of
unmarkup! - prefix
- Sets a prefix string on a previously defined named style.
- style
- Defines a named style in the global registry.
- unansi
- Removes all CSI ANSI escape sequences from a formatted string.
- unmarkup
- Removes all Farben markup tags from a formatted string.
- untag
- Escapes markup brackets in a formatted string so they render as literal text.
Structs§
- Farben
Str - A compile-time colored string. Stores only the ANSI-styled variant;
plain text is derived at runtime via
strip_ansiwhen color is disabled. - LexError
Display - Diagnostic formatter that renders a
LexErroralongside the original markup input. - Style
- A complete set of visual attributes for a span of text.
Enums§
- LexError
- Errors produced during tokenization of a farben markup string.
- Registry
Error - Errors produced by registry operations (
set_prefix,insert_style).
Functions§
- color
- Parses and renders a farben markup string, appending a final SGR reset.
- color_
enabled - Returns whether ANSI color output is enabled for this process.
- color_
runtime - Parses and renders a farben markup string, appending a final SGR reset.
- colorb
- Parses and renders a farben markup string without appending a trailing reset sequence.
- escape_
tags - Escapes farben markup brackets in
inputso they render as literal text. - insert_
style - Registers a named style in the global registry.
- set_
prefix - Sets the prefix string for an already-registered named style.
- strip_
ansi - Remove all CSI ANSI escape sequences from
inputand return the plain text. - strip_
markup - Strips farben markup tags from a string, returning plain text only.
- try_
color - Parses and renders a farben markup string, appending a final SGR reset.