1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2024-12-12 19:18:24 +02:00
ripgrep/termcolor
Andrew Gallant a7d0e40668 Use basic SGR sequences when possible.
In Emacs, its terminal apparently doesn't support "extended" sets of
foreground/background colors. Unless we need to set an "intense" color,
we should instead use one of the eight basic color codes.

Also, remove the "intense" setting from the default set of colors. It
doesn't do much anyway and enables the default color settings to work
in Emacs out of the box.

Fixes #182 (again)
2017-01-13 19:03:03 -05:00
..
src Use basic SGR sequences when possible. 2017-01-13 19:03:03 -05:00
Cargo.toml termcolor-0.1.1 2016-11-21 20:33:39 -05:00
README.md Completely re-work colored output and tty handling. 2016-11-20 11:14:52 -05:00

termcolor

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.

Windows build status

Linux build status Windows build status

Dual-licensed under MIT or the UNLICENSE.

Documentation

https://docs.rs/termcolor

Usage

Add this to your Cargo.toml:

[dependencies]
termcolor = "0.1"

and this to your crate root:

extern crate termcolor;

Organization

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

Stdout and StdoutLock both satisfy WriteColor and are analogous to std::io::Stdout and std::io::StdoutLock.

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 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 Stdout

The Stdout 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, Stdout, WriteColor};

let mut stdout = Stdout::new(ColorChoice::Always);
try!(stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green))));
try!(writeln!(&mut stdout, "green text!"));

Example: using BufferWriter

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

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

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

let mut bufwtr = BufferWriter::stdout(ColorChoice::Always);
let mut buffer = bufwtr.buffer();
try!(buffer.set_color(ColorSpec::new().set_fg(Some(Color::Green))));
try!(writeln!(&mut buffer, "green text!"));
try!(bufwtr.print(&buffer));