Module termcolor
Cross-platform terminal colors.
- Crate
::termcolor. - docs.rs
- crates.io
- GitHub
termcolor provides cross-platform colored output to terminals.
On Unix it uses ANSI escape codes;
on Windows it uses the console API,
so colors work correctly in cmd.exe and PowerShell without extra setup.
In use: create a StandardStream,
set colors with a ColorSpec via the WriteColor trait,
write text, then reset back to defaults.
ColorChoice controls whether colors are actually emitted,
making it easy to respect --color=auto/always/never flags.
For multithreaded programs, BufferWriter lets each thread
write to an independent Buffer that is flushed atomically,
preventing interleaved output.
Examples
Print colored text to stdout:
use Write;
use ;
let mut stdout = stdout;
stdout.set_color.unwrap;
writeln!.unwrap;
stdout.reset.unwrap;
Print with bold red for errors:
use Write;
use ;
let mut stderr = stderr;
stderr.set_color.unwrap;
writeln!.unwrap;
stderr.reset.unwrap;
Structs
-
Ansi
Satisfies
WriteColorusing standard ANSI escape sequences. - Buffer Write colored text to memory.
- BufferWriter Writes colored buffers to stdout or stderr.
-
BufferedStandardStream
Like
StandardStream, but does buffered writing. -
ColorChoiceParseError
An error that occurs when parsing a
ColorChoicefails. - ColorSpec A color specification.
- HyperlinkSpec A hyperlink specification.
-
NoColor
Satisfies
WriteColorbut ignores all color options. - ParseColorError An error from parsing an invalid color specification.
-
StandardStream
Satisfies
io::WriteandWriteColor, and supports optional coloring to either of the standard output streams, stdout and stderr. -
StandardStreamLock
StandardStreamLockis a locked reference to aStandardStream.
Enums
- Color The set of available colors for the terminal foreground/background.
- ColorChoice ColorChoice represents the color preferences of an end user.
Traits
- WriteColor This trait describes the behavior of writers that support colored output.