Crate dahlia

Crate dahlia 

Source
Expand description

A simple text formatting package, inspired by the game Minecraft.

Text is formatted in a similar way to in the game. With Dahlia, it is formatted by typing a marker (& by default but can be any single character) followed by a format code and finally the text to be formatted.

§Example

use dahlia::{Dahlia, dprintln};

let dahlia = Dahlia::new().with_auto_depth();

// Print "Hello, world!" in green bold with "world" underlined
let formatted = dahlia.convert("&2&lHello, &nworld!");

// Convenience macro to print text with println! like syntax
let name = "David";
dprintln!(dahlia, "&2Hello, {name}!"); // green "Hello, David!"

// Remove formatting from the text with `clean`
assert_eq!(dahlia.clean("&2Hello, &lworld!"), "Hello, world!");

// Use `_` to escape the marker
assert_eq!(dahlia.convert("&_2Hello!"), "&2Hello!");

§Specification

The library is an implementation of the Dahlia specification. Below is a short summary.

§Color Format Codes

Each digit/letter corresponds to a hex value (dependent on the color depth). The coloring can be applied to the background if a ~ is inserted between the marker and the code.

NameDahliaANSI 3-bitANSI 4-bitANSI 8-bitRGBHEX
Black030300(0, 0, 0)#000000
Blue1343419(0, 0, 170)#0000aa
Green2323234(0, 170, 0)#00aa00
Cyan3363637(0, 170, 170)#00aaaa
Red43131124(170, 0, 0)#aa0000
Purple53535127(170, 0, 170)#aa00aa
Orange63333214(255, 170, 0)#ffaa00
Light gray73737248(170, 170, 170)#aaaaaa
Gray83090240(85, 85, 85)#555555
Light blue93494147(85, 85, 255)#5555ff
Limea329283(85, 255, 85)#55ff55
Turqoiseb349687(85, 255, 255)#55ffff
Light redc3191203(255, 85, 85)#ff5555
Pinkd3595207(255, 85, 255)#ff55ff
Yellowe3393227(255, 255, 85)#ffff55
Whitef379715(255, 255, 255)#ffffff

§Formatting Codes

CodeResult
lBold
mStrikethrough
nUnderline
oItalic
RReset formatting
rfReset foreground
rbReset background
rcReset color
rhReset hidden
riReset inverse
rjReset dim
rkReset blinking
rlReset bold
rmReset strikethrough
rnReset underline
roReset italic

§Custom Colors

For colors by hex code, use square brackets containing the hex code inside of it.

  • Foreground: &#xxx; or &#xxxxxx;
  • Background: &~#xxx; or &~#xxxxxx;

xxx and xxxxxx represents the hex value of the color in 12/24 bits precision respectively.

Macros§

dprint
Wrapper over print!, takes a Dahlia instance as the first argument and uses its convert method for coloring strings.
dprintln
Wrapper over println!, takes a Dahlia instance as the first argument and uses its convert method for coloring strings.

Structs§

Dahlia

Enums§

Depth
Supported color depths

Functions§

clean_ansi
Removes all ANSI codes from a string.