#windows #color #ansi #console #win


A simple cross platform library for writing colored text to a terminal.

12 releases (2 stable)

1.0.1 Jul 21, 2018
0.3.6 Mar 26, 2018
0.3.3 Aug 27, 2017
0.3.2 Mar 15, 2017
0.1.1 Nov 22, 2016

#2 in Command-line interface

Download history 19898/week @ 2018-05-27 24482/week @ 2018-06-03 24709/week @ 2018-06-10 22895/week @ 2018-06-17 24902/week @ 2018-06-24 26182/week @ 2018-07-01 26664/week @ 2018-07-08 26688/week @ 2018-07-15 29634/week @ 2018-07-22 33327/week @ 2018-07-29 42545/week @ 2018-08-05 41937/week @ 2018-08-12 37861/week @ 2018-08-19

108,594 downloads per month


A simple cross platform library for writing colored text to a terminal. This library writes colored text either using standard ANSI escape sequences or by interacting with the Windows console. Several convenient abstractions are provided for use in single-threaded or multi-threaded command line applications.

Linux build status Windows build status

Dual-licensed under MIT or the UNLICENSE.




Add this to your Cargo.toml:

termcolor = "0.3"

and this to your crate root:

extern crate termcolor;


The WriteColor trait extends the io::Write trait with methods for setting colors or resetting them.

StandardStream and StandardStreamLock both satisfy WriteColor and are analogous to std::io::Stdout and std::io::StdoutLock, or std::io::Stderr and std::io::StderrLock.

Buffer is an in memory buffer that supports colored text. In a parallel program, each thread might write to its own buffer. A buffer can be printed to stdout or stderr using a BufferWriter. The advantage of this design is that each thread can work in parallel on a buffer without having to synchronize access to global resources such as the Windows console. Moreover, this design also prevents interleaving of buffer output.

Ansi and NoColor both satisfy WriteColor for arbitrary implementors of io::Write. These types are useful when you know exactly what you need. An analogous type for the Windows console is not provided since it cannot exist.

Example: using StandardStream

The StandardStream type in this crate works similarly to std::io::Stdout, except it is augmented with methods for coloring by the WriteColor trait. For example, to write some green text:

use std::io::Write;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

let mut stdout = StandardStream::stdout(ColorChoice::Always);
writeln!(&mut stdout, "green text!")?;

Example: using BufferWriter

A BufferWriter can create buffers and write buffers to stdout or stderr. It does not implement io::Write or WriteColor itself. Instead, Buffer implements io::Write and termcolor::WriteColor.

This example shows how to print some green text to stderr.

use std::io::Write;
use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};

let mut bufwtr = BufferWriter::stderr(ColorChoice::Always);
let mut buffer = bufwtr.buffer();
writeln!(&mut buffer, "green text!")?;
Unlicense OR MIT license
  • No runtime deps

Reverse deps