110 lines
17 KiB
HTML
110 lines
17 KiB
HTML
<!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" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script 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="sidebar"><div class="sidebar-menu" role="button">☰</div><a class="sidebar-logo" href="../termcolor/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.png" alt="logo"></div>
|
||
</a><h2 class="location">Crate termcolor</h2><div class="block version"><div class="narrow-helper"></div><p>Version 1.1.3</p></div><div class="sidebar-elems"><a id="all-types" href="all.html"><p>See all termcolor's items</p></a><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#traits">Traits</a></li></ul></div><div id="sidebar-vars" data-name="termcolor" data-ty="mod" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../termcolor/index.html"><img class="rust-logo" src="../rust-logo.png" alt="logo"></a><nav class="sub"><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../brush.svg"></button><div id="theme-choices" role="menu"></div></div><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../wheel.svg"></a></div></form></nav></div><section id="main-content" class="content"><h1 class="fqn"><span class="in-band">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></span><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span><a class="srclink" href="../src/termcolor/lib.rs.html#1-2260" title="goto source code">[src]</a></span></h1><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" class="section-header"><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" class="section-header"><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> <span class="ident">std::io::Write</span>;
|
||
<span class="kw">use</span> <span class="ident">termcolor</span>::{<span class="ident">Color</span>, <span class="ident">ColorChoice</span>, <span class="ident">ColorSpec</span>, <span class="ident">StandardStream</span>, <span class="ident">WriteColor</span>};
|
||
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">stdout</span> <span class="op">=</span> <span class="ident">StandardStream::stdout</span>(<span class="ident">ColorChoice::Always</span>);
|
||
<span class="ident">stdout</span>.<span class="ident">set_color</span>(<span class="ident">ColorSpec::new</span>().<span class="ident">set_fg</span>(<span class="prelude-val">Some</span>(<span class="ident">Color::Green</span>)))<span class="question-mark">?</span>;
|
||
<span class="macro">writeln!</span>(<span class="kw-2">&mut</span> <span class="ident">stdout</span>, <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" class="section-header"><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> <span class="ident">std::io::Write</span>;
|
||
<span class="kw">use</span> <span class="ident">termcolor</span>::{<span class="ident">BufferWriter</span>, <span class="ident">Color</span>, <span class="ident">ColorChoice</span>, <span class="ident">ColorSpec</span>, <span class="ident">WriteColor</span>};
|
||
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">bufwtr</span> <span class="op">=</span> <span class="ident">BufferWriter::stderr</span>(<span class="ident">ColorChoice::Always</span>);
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">bufwtr</span>.<span class="ident">buffer</span>();
|
||
<span class="ident">buffer</span>.<span class="ident">set_color</span>(<span class="ident">ColorSpec::new</span>().<span class="ident">set_fg</span>(<span class="prelude-val">Some</span>(<span class="ident">Color::Green</span>)))<span class="question-mark">?</span>;
|
||
<span class="macro">writeln!</span>(<span class="kw-2">&mut</span> <span class="ident">buffer</span>, <span class="string">"green text!"</span>)<span class="question-mark">?</span>;
|
||
<span class="ident">bufwtr</span>.<span class="ident">print</span>(<span class="kw-2">&</span><span class="ident">buffer</span>)<span class="question-mark">?</span>;</code></pre></div>
|
||
<h2 id="detecting-presence-of-a-terminal" class="section-header"><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='information'><div class='tooltip ignore'>ⓘ</div></div><div class="example-wrap"><pre class="rust rust-example-rendered ignore"><code><span class="kw">use</span> <span class="ident">atty</span>;
|
||
<span class="kw">use</span> <span class="ident">termcolor</span>::{<span class="ident">ColorChoice</span>, <span class="ident">StandardStream</span>};
|
||
|
||
<span class="kw">let</span> <span class="ident">preference</span> <span class="op">=</span> <span class="ident">argv</span>.<span class="ident">get_flag</span>(<span class="string">"color"</span>).<span class="ident">unwrap_or</span>(<span class="string">"auto"</span>);
|
||
<span class="kw">let</span> <span class="ident">choice</span> <span class="op">=</span> <span class="kw">match</span> <span class="ident">preference</span> {
|
||
<span class="string">"always"</span> => <span class="ident">ColorChoice::Always</span>,
|
||
<span class="string">"ansi"</span> => <span class="ident">ColorChoice::AlwaysAnsi</span>,
|
||
<span class="string">"auto"</span> => {
|
||
<span class="kw">if</span> <span class="ident">atty::is</span>(<span class="ident">atty::Stream::Stdout</span>) {
|
||
<span class="ident">ColorChoice::Auto</span>
|
||
} <span class="kw">else</span> {
|
||
<span class="ident">ColorChoice::Never</span>
|
||
}
|
||
}
|
||
<span class="kw">_</span> => <span class="ident">ColorChoice::Never</span>,
|
||
};
|
||
<span class="kw">let</span> <span class="ident">stdout</span> <span class="op">=</span> <span class="ident">StandardStream::stdout</span>(<span class="ident">choice</span>);
|
||
<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"><p>Satisfies <code>WriteColor</code> using standard ANSI escape sequences.</p>
|
||
</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"><p>Write colored text to memory.</p>
|
||
</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"><p>Writes colored buffers to stdout or stderr.</p>
|
||
</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"><p>Like <code>StandardStream</code>, but does buffered writing.</p>
|
||
</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"><p>A color specification.</p>
|
||
</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"><p>Satisfies <code>WriteColor</code> but ignores all color options.</p>
|
||
</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"><p>An error from parsing an invalid color specification.</p>
|
||
</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"><p>Satisfies <code>io::Write</code> and <code>WriteColor</code>, and supports optional coloring
|
||
to either of the standard output streams, stdout and stderr.</p>
|
||
</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"><p><code>StandardStreamLock</code> is a locked reference to a <code>StandardStream</code>.</p>
|
||
</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"><p>The set of available colors for the terminal foreground/background.</p>
|
||
</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"><p>ColorChoice represents the color preferences of an end user.</p>
|
||
</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"><p>This trait describes the behavior of writers that support colored output.</p>
|
||
</div></div></div></section><section id="search" class="content hidden"></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.59.0 (9d1b2106e 2022-02-23)" ></div>
|
||
</body></html> |