| <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="This crate provides a cross platform abstraction for writing colored text to a terminal. Colors are written using either ANSI escape sequences or by communicating with a Windows console. Much of this API was motivated by use inside command line applications, where colors or styles can be configured by the end user and/or the environment."><meta name="keywords" content="rust, rustlang, rust-lang, termcolor"><title>termcolor - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../termcolor/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../termcolor/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate termcolor</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 1.2.0</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#traits">Traits</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Crate <a class="mod" href="#">termcolor</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/termcolor/lib.rs.html#1-2350">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>This crate provides a cross platform abstraction for writing colored text to |
| a terminal. Colors are written using either ANSI escape sequences or by |
| communicating with a Windows console. Much of this API was motivated by use |
| inside command line applications, where colors or styles can be configured |
| by the end user and/or the environment.</p> |
| <p>This crate also provides platform independent support for writing colored text |
| to an in memory buffer. While this is easy to do with ANSI escape sequences |
| (because they are in the buffer themselves), it is trickier to do with the |
| Windows console API, which requires synchronous communication.</p> |
| <h2 id="organization"><a href="#organization">Organization</a></h2> |
| <p>The <code>WriteColor</code> trait extends the <code>io::Write</code> trait with methods for setting |
| colors or resetting them.</p> |
| <p><code>StandardStream</code> and <code>StandardStreamLock</code> both satisfy <code>WriteColor</code> and are |
| analogous to <code>std::io::Stdout</code> and <code>std::io::StdoutLock</code>, or <code>std::io::Stderr</code> |
| and <code>std::io::StderrLock</code>.</p> |
| <p><code>Buffer</code> 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 |
| using a <code>BufferWriter</code>. 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.</p> |
| <p><code>Ansi</code> and <code>NoColor</code> both satisfy <code>WriteColor</code> for arbitrary implementors of |
| <code>io::Write</code>. 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.</p> |
| <h2 id="example-using-standardstream"><a href="#example-using-standardstream">Example: using <code>StandardStream</code></a></h2> |
| <p>The <code>StandardStream</code> type in this crate works similarly to <code>std::io::Stdout</code>, |
| except it is augmented with methods for coloring by the <code>WriteColor</code> trait. |
| For example, to write some green text:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::io::Write; |
| <span class="kw">use </span>termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>stdout = StandardStream::stdout(ColorChoice::Always); |
| stdout.set_color(ColorSpec::new().set_fg(<span class="prelude-val">Some</span>(Color::Green)))<span class="question-mark">?</span>; |
| <span class="macro">writeln!</span>(<span class="kw-2">&mut </span>stdout, <span class="string">"green text!"</span>)<span class="question-mark">?</span>;</code></pre></div> |
| <p>Note that any text written to the terminal now will be colored |
| green when using ANSI escape sequences, even if it is written via |
| stderr, and even if stderr had previously been set to <code>Color::Red</code>. |
| Users will need to manage any color changes themselves by calling |
| <a href="trait.WriteColor.html#tymethod.set_color"><code>WriteColor::set_color</code></a>, and this |
| may include calling <a href="trait.WriteColor.html#tymethod.reset"><code>WriteColor::reset</code></a> |
| before the program exits to a shell.</p> |
| <h2 id="example-using-bufferwriter"><a href="#example-using-bufferwriter">Example: using <code>BufferWriter</code></a></h2> |
| <p>A <code>BufferWriter</code> can create buffers and write buffers to stdout or stderr. It |
| does <em>not</em> implement <code>io::Write</code> or <code>WriteColor</code> itself. Instead, <code>Buffer</code> |
| implements <code>io::Write</code> and <code>io::WriteColor</code>.</p> |
| <p>This example shows how to print some green text to stderr.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::io::Write; |
| <span class="kw">use </span>termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor}; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>bufwtr = BufferWriter::stderr(ColorChoice::Always); |
| <span class="kw">let </span><span class="kw-2">mut </span>buffer = bufwtr.buffer(); |
| buffer.set_color(ColorSpec::new().set_fg(<span class="prelude-val">Some</span>(Color::Green)))<span class="question-mark">?</span>; |
| <span class="macro">writeln!</span>(<span class="kw-2">&mut </span>buffer, <span class="string">"green text!"</span>)<span class="question-mark">?</span>; |
| bufwtr.print(<span class="kw-2">&</span>buffer)<span class="question-mark">?</span>;</code></pre></div> |
| <h2 id="detecting-presence-of-a-terminal"><a href="#detecting-presence-of-a-terminal">Detecting presence of a terminal</a></h2> |
| <p>In many scenarios when using color, one often wants to enable colors |
| automatically when writing to a terminal and disable colors automatically when |
| writing to anything else. The typical way to achieve this in Unix environments |
| is via libc’s |
| <a href="https://man7.org/linux/man-pages/man3/isatty.3.html"><code>isatty</code></a> |
| function. |
| Unfortunately, this notoriously does not work well in Windows environments. To |
| work around that, the currently recommended solution is to use the |
| <a href="https://crates.io/crates/atty"><code>atty</code></a> |
| crate, which goes out of its way to get this as right as possible in Windows |
| environments.</p> |
| <p>For example, in a command line application that exposes a <code>--color</code> flag, |
| your logic for how to enable colors might look like this:</p> |
| |
| <div class="example-wrap ignore"><div class='tooltip'>ⓘ</div><pre class="rust rust-example-rendered"><code><span class="kw">use </span>atty; |
| <span class="kw">use </span>termcolor::{ColorChoice, StandardStream}; |
| |
| <span class="kw">let </span>preference = argv.get_flag(<span class="string">"color"</span>).unwrap_or(<span class="string">"auto"</span>); |
| <span class="kw">let </span><span class="kw-2">mut </span>choice = preference.parse::<ColorChoice>()<span class="question-mark">?</span>; |
| <span class="kw">if </span>choice == ColorChoice::Auto && !atty::is(atty::Stream::Stdout) { |
| choice = ColorChoice::Never; |
| } |
| <span class="kw">let </span>stdout = StandardStream::stdout(choice); |
| <span class="comment">// ... write to stdout</span></code></pre></div> |
| <p>Currently, <code>termcolor</code> does not provide anything to do this for you.</p> |
| </div></details><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Ansi.html" title="termcolor::Ansi struct">Ansi</a></div><div class="item-right docblock-short">Satisfies <code>WriteColor</code> using standard ANSI escape sequences.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Buffer.html" title="termcolor::Buffer struct">Buffer</a></div><div class="item-right docblock-short">Write colored text to memory.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.BufferWriter.html" title="termcolor::BufferWriter struct">BufferWriter</a></div><div class="item-right docblock-short">Writes colored buffers to stdout or stderr.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.BufferedStandardStream.html" title="termcolor::BufferedStandardStream struct">BufferedStandardStream</a></div><div class="item-right docblock-short">Like <code>StandardStream</code>, but does buffered writing.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ColorChoiceParseError.html" title="termcolor::ColorChoiceParseError struct">ColorChoiceParseError</a></div><div class="item-right docblock-short">An error that occurs when parsing a <code>ColorChoice</code> fails.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ColorSpec.html" title="termcolor::ColorSpec struct">ColorSpec</a></div><div class="item-right docblock-short">A color specification.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.NoColor.html" title="termcolor::NoColor struct">NoColor</a></div><div class="item-right docblock-short">Satisfies <code>WriteColor</code> but ignores all color options.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ParseColorError.html" title="termcolor::ParseColorError struct">ParseColorError</a></div><div class="item-right docblock-short">An error from parsing an invalid color specification.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.StandardStream.html" title="termcolor::StandardStream struct">StandardStream</a></div><div class="item-right docblock-short">Satisfies <code>io::Write</code> and <code>WriteColor</code>, and supports optional coloring |
| to either of the standard output streams, stdout and stderr.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.StandardStreamLock.html" title="termcolor::StandardStreamLock struct">StandardStreamLock</a></div><div class="item-right docblock-short"><code>StandardStreamLock</code> is a locked reference to a <code>StandardStream</code>.</div></div></div><h2 id="enums" class="small-section-header"><a href="#enums">Enums</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.Color.html" title="termcolor::Color enum">Color</a></div><div class="item-right docblock-short">The set of available colors for the terminal foreground/background.</div></div><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.ColorChoice.html" title="termcolor::ColorChoice enum">ColorChoice</a></div><div class="item-right docblock-short">ColorChoice represents the color preferences of an end user.</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.WriteColor.html" title="termcolor::WriteColor trait">WriteColor</a></div><div class="item-right docblock-short">This trait describes the behavior of writers that support colored output.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="termcolor" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html> |