Crate flower_pot

Crate flower_pot 

Source
Expand description

Constants and simple functions for invoking ANSI control codes used for text-styling in terminals (including color codes). No support for cursor movement or any other control codes.

This crate provides constant bindings for text-styling ANSI control codes like BOLD (bound to the string \x1b[1m) and GREEN (bound to \x1b[32m):

use flower_pot::*;

println!("{GREEN}ok{RESET}");           // prints a green "ok"
println!("{BOLD}{RED}error!{RESET}");   // prints a bold, red "error!"
println!("{BLUE_BG}cloud{RESET}");      // prints white text on a blue background

// Note that you must print the RESET code after the end of
// the text you want styled, or else all text printed to the
// terminal after that point will also be styled that way,
// including text outputted by other programs.

It also provides functions to invoke the 8-bit color palette:

use flower_pot::*;

// Prints text in "palette-color #237" (often a shade of grey)
// with a background color of "palette-color #214" (often a
// shade of orange):

println!("{}{}example text{RESET}", color_256(237), color_256_bg(214));

And functions that invoke truecolor functionality for terminals that support it:

use flower_pot::*;

// Prints text in RGB color [127, 45, 68] with a background
// color of RGB color [0, 255, 255]:

println!("{}{}example text{RESET}", truecolor(127, 45, 68), truecolor_bg(0, 255, 255));

The functions color_256, color_256_bg, truecolor, and truecolor_bg all return Strings.

Note that not all terminals support all of the codes defined in this library. The basic workflow of ANSI control codes is that a program outputs sequences of special characters describing what it wants (such as “make the following text bold” or “make the following text green”) to stdout, and then the terminal that the program is running in decides what to do with those characters. The codes themselves are reasonably well-standardized, but not every terminal understands all of them. Some terminals might ignore some codes, or might do weird things when you use them (such as displaying the text following the code incorrectly). This is a feature of the ANSI control code ecosystem, and not something a library can fix.

Once you’ve outputted a control code, all text that follows it is styled in the manner requested. If you want to go back to unstyled text, output the RESET code or one of the more specific style-resetting codes such as NOT_UNDERLINED.

The list of control codes is taken from the Wikipedia page on ANSI control codes. Codes which are not widely supported (as reported by Wikipedia) are marked as such below.

Constants§

ALT_FONT_1
Switch to alternative font #1.
ALT_FONT_2
Switch to alternative font #2.
ALT_FONT_3
Switch to alternative font #3.
ALT_FONT_4
Switch to alternative font #4.
ALT_FONT_5
Switch to alternative font #5.
ALT_FONT_6
Switch to alternative font #6.
ALT_FONT_7
Switch to alternative font #7.
ALT_FONT_8
Switch to alternative font #8.
ALT_FONT_9
Switch to alternative font #9.
BLACK
Set forground color to black for the following text.
BLACK_BG
Set background color to black for the following text.
BLUE
Set foreground color to blue for the following text.
BLUE_BG
Set background color to blue for the following text.
BOLD
Make the following text bold.
BRIGHT_BLACK
Set the foreground color to bright black for the following text.
BRIGHT_BLACK_BG
Set the background color to bright black for the following text.
BRIGHT_BLUE
Set the foreground color to bright blue for the following text.
BRIGHT_BLUE_BG
Set the background color to bright blue for the following text.
BRIGHT_CYAN
Set the foreground color to bright cyan for the following text.
BRIGHT_CYAN_BG
Set the background color to bright cyan for the following text.
BRIGHT_GREEN
Set the foreground color to bright green for the following text.
BRIGHT_GREEN_BG
Set the background color to bright green for the following text.
BRIGHT_PURPLE
Set the foreground color to bright purple for the following text.
BRIGHT_PURPLE_BG
Set the background color to bright purple for the following text.
BRIGHT_RED
Set the foreground color to bright red for the following text.
BRIGHT_RED_BG
Set the background color to bright red for the following text.
BRIGHT_WHITE
Set the foreground color to bright white for the following text.
BRIGHT_WHITE_BG
Set the background color to bright white for the following text.
BRIGHT_YELLOW
Set the foreground color to bright yellow for the following text.
BRIGHT_YELLOW_BG
Set the background color to bright yellow for the following text.
CYAN
Set foreground color to cyan for the following text.
CYAN_BG
Set background color to cyan for the following text.
DEFAULT
Return to the default foreground color for the following text.
DEFAULT_BG
Return to the default background color for the following text.
DEFAULT_FONT
Switch to the default font.
DIM
Make the following text dim.
DOUBLE_UNDERLINE
Double-underline the following text. WARNING: this constant contains the exact same control code as the constant NOT_BOLD, because different terminals interpret the code to mean different things. If you use either constant, be aware that your text may be rendered differently by different terminals.
ENCIRCLED
Encircle the following text.
FRAKTUR
Switch to Fraktur font. Rarely supported according to Wikipedia.
FRAMED
Frame the following text.
GREEN
Set foreground color to green for the following text.
GREEN_BG
Set background color to green for the following text.
HIDDEN
Hide the following text. Not widely supported according to Wikipedia.
INVERTED
Swap the current foreground color and current background color for the following text.
ITALIC
Make the following text italic.
NEITHER_BOLD_NOR_ITALIC
Make the following text neither bold nor italic.
NEITHER_FRAMED_NOR_ENCIRCLED
Make the following text neither framed nor encircled.
NORMAL_INTENSITY
Return to ordinary intensity (neither bold nor dim) for the following text.
NOT_BLINKING
Make the following text not blink.
NOT_BOLD
Make the following text not bold. WARNING: this constant contains the exact same control code as the constant DOUBLE_UNDERLINE, because different terminals interpret the code to mean different things. If you use either constant, be aware that your text may be rendered differently by different terminals.
NOT_HIDDEN
Make the following text not hidden.
NOT_INVERTED
Unswap the foreground and background colors for the following text.
NOT_OVERLINED
Make the following text not overlined.
NOT_STRIKETHROUGH
Make the following text not strikethrough.
NOT_UNDERLINED
Make the following text not underlined.
NO_PROPORTIONAL_SPACING
Return to a non-proportionally spaced font for the following text. Rarely meaningful because the PROPORTIONAL_SPACING control code is rarely supported to begin with.
OVERLINE
Add an overline to the following text.
PROPORTIONAL_SPACING
Use a font with proportional spacing (i.e., a non-monospace font) for the following text. Rarely supported according to Wikipedia.
PURPLE
Set foreground color to purple for the following text.
PURPLE_BG
Set background color to purple for the following text.
RAPID_BLINK
Make the following text blink quickly. Not widely supported according to Wikipedia.
RED
Set foreground color to red for the following text.
RED_BG
Set background color to red for the following text.
RESET
Unset all styles and return to default text formatting.
SLOW_BLINK
Make the following text blink slowly.
STRIKETHROUGH
Make the following text strikethrough. Not supported in Terminal.app according to Wikipedia.
UNDERLINE
Underline the following text.
WHITE
Set foreground color to white for the following text.
WHITE_BG
Set background color to white for the following text.
YELLOW
Set foreground color to yellow for the following text.
YELLOW_BG
Set background color to yellow for the following text.

Functions§

color_256
Set the foreground color for the following text to the nth color in the 256-color palette. Commonly, the set of 256 available colors consists of the 8 named foreground colors, the 8 bright versions of these colors, a 6×6×6 RGB cube (for a total of 216 colors distributed evenly across RGB-space), and then a scale of 24 shades of gray. Different terminals may differ in what colors they provide here.
color_256_bg
Set the background color for the following text to the nth color in the 256-color palette. Commonly, the set of 256 available colors consists of the 8 named background colors, the 8 bright versions of these colors, a 6×6×6 RGB cube (for a total of 216 colors distributed evenly across RGB-space), and then a scale of 24 shades of gray. Different terminals may differ in what colors they provide here.
truecolor
Set the foreground color to the RGB value (r, g, b). Not supported on all terminals. Terminals which do support this feature are called “truecolor terminals”.
truecolor_bg
Set the background color to the RGB value (r, g, b). Not supported on all terminals. Terminals which do support this feature are called “truecolor terminals”.