Skip to main content

Crate farben

Crate farben 

Source
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:

FeatureWhat it adds
(default)Runtime coloring: color, colorb, try_color, cprint!, cprintln!, cprintb!, cprintbln!, cwrite!, cwriteln!, cwriteb!, cwritebln!, color_fmt!
compileCompile-time validation of markup strings via proc macros
formatNamed style registry: style!, prefix!
markdownRuntime inline markdown rendering: [markdown], [md_fmt!], [mdprint!], [mdprintln!]
markdown-compileCompile-time inline markdown rendering

§Emphasis Styles

Farben supports these emphasis types:

TagDescription
boldBold (SGR 1)
dimDimmed (SGR 2)
italicItalic (SGR 3)
underlineUnderline (SGR 4)
double-underlineDouble underline (SGR 21)
blinkSlow blink (SGR 5)
rapid-blinkRapid blink (SGR 6)
reverseReverse video (SGR 7)
invisibleHidden (SGR 8)
strikethroughStrikethrough (SGR 9)
overlineOverline (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.rs file from OUT_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§

FarbenStr
A compile-time colored string with both styled and plain variants. Resolved at runtime based on environment and TTY detection.
LexErrorDisplay
Diagnostic formatter that renders a LexError alongside 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 input and 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.