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, ANSI 256, emphasis styles, foreground and background targeting, custom named tags, inline resets, and inline markdown rendering.
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, try_color, cprint!, cprintln!, cprintb!, cprintbln!, cwrite!, cwriteln!, cwriteb!, cwritebln!, color_fmt! |
compile | Compile-time validation of markup strings via proc macros |
format | Named style registry: style!, prefix! |
markdown | Runtime inline markdown rendering: [markdown], [md_fmt!], [mdprint!], [mdprintln!] |
markdown-compile | Compile-time inline markdown rendering |
§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 and ANSI 256
use farben::prelude::*;
cprintln!("[rgb(255,128,0)]Orange.");
cprintln!("[ansi(93)]Deep purple.");§Custom named tags (format feature)
use farben::prelude::*;
style!("warn", "[bold yellow]");
prefix!("warn", "! ");
cprintln!("[warn]Watch out.");§Inline markdown (markdown feature)
use farben::prelude::*;
mdprintln!("**bold**, *italic*, `code`, ~~strikethrough~~");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 - Formats a string and strips all CSI ANSI escape sequences from the result.
- 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.
- color_
fmt - Parses and renders a farben markup string with format arguments, appending a final SGR reset.
- 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 - Formats a string and strips all Farben markup tags from the result.
- prefix
- Sets a prefix string on a previously defined named style.
- style
- Defines a named style in the global registry.
Structs§
- Farben
Str - A compile-time colored string with both styled and plain variants. Resolved at runtime based on environment and TTY detection.
- 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.
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.
- 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.