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 backgroundNote 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. This code prints text in “palette-color #237” (often a shade of grey) with a background color of “palette-color #214” (often a shade of orange):
use flower_pot::*;
println!("{}{}example text{RESET}", color_256(237), color_256_bg(214));
And it provides functions that invoke truecolor functionality for terminals that support it. This code prints text in RGB color [127, 45, 68] with a background color of RGB color [0, 255, 255]:
use flower_pot::*;
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 will be 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.
The named colors covered by ANSI control codes are BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, and WHITE, and these are available as foreground and background colors, along with “bright” versions which are also available as foreground and background colors. If you want shades of gray, you can try 8-bit colors in the range of 232 (darkest grey) to 255 (lightest grey) or use truecolor. Among the text-styling codes, I find BOLD, DIM, ITALIC, UNDERLINE, and RESET to be the most commonly useful.
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_
MAGENTA - Set the foreground color to bright magenta for the following text.
- BRIGHT_
MAGENTA_ BG - Set the background color to bright magenta 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.
- MAGENTA
- Set foreground color to magenta for the following text.
- MAGENTA_
BG - Set background color to magenta for the following text.
- 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_SPACINGcontrol 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.
- 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”.