blob: 3ba45062163e4ef2d10192fbfcc038ee66ea5258 [file] [log] [blame]
<!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 is a library for controlling colours and formatting, such as red bold text or blue underlined text, on ANSI terminals."><meta name="keywords" content="rust, rustlang, rust-lang, ansi_term"><title>ansi_term - 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">&#9776;</button><a class="sidebar-logo" href="../ansi_term/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="../ansi_term/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate ansi_term</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.12.1</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="#functions">Functions</a></li><li><a href="#types">Type Definitions</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="#">ansi_term</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/ansi_term/lib.rs.html#1-271">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>This is a library for controlling colours and formatting, such as
red bold text or blue underlined text, on ANSI terminals.</p>
<h3 id="basic-usage"><a href="#basic-usage">Basic usage</a></h3>
<p>There are three main types in this crate that you need to be
concerned with: <a href="type.ANSIString.html"><code>ANSIString</code></a>, <a href="struct.Style.html"><code>Style</code></a>, and <a href="enum.Colour.html"><code>Colour</code></a>.</p>
<p>A <code>Style</code> holds stylistic information: foreground and background colours,
whether the text should be bold, or blinking, or other properties. The
<a href="enum.Colour.html"><code>Colour</code></a> enum represents the available colours. And an <a href="type.ANSIString.html"><code>ANSIString</code></a> is a
string paired with a <a href="struct.Style.html"><code>Style</code></a>.</p>
<p><a href="enum.Color.html"><code>Color</code></a> is also available as an alias to <code>Colour</code>.</p>
<p>To format a string, call the <code>paint</code> method on a <code>Style</code> or a <code>Colour</code>,
passing in the string you want to format as the argument. For example,
here’s how to get some red text:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ansi_term::Colour::Red;
<span class="macro">println!</span>(<span class="string">&quot;This is in red: {}&quot;</span>, Red.paint(<span class="string">&quot;a red string&quot;</span>));</code></pre></div>
<p>It’s important to note that the <code>paint</code> method does <em>not</em> actually return a
string with the ANSI control characters surrounding it. Instead, it returns
an <a href="type.ANSIString.html"><code>ANSIString</code></a> value that has a <a href="https://doc.rust-lang.org/std/fmt/trait.Display.html"><code>Display</code></a> implementation that, when
formatted, returns the characters. This allows strings to be printed with a
minimum of <a href="https://doc.rust-lang.org/std/string/struct.String.html"><code>String</code></a> allocations being performed behind the scenes.</p>
<p>If you <em>do</em> want to get at the escape codes, then you can convert the
<a href="type.ANSIString.html"><code>ANSIString</code></a> to a string as you would any other <code>Display</code> value:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ansi_term::Colour::Red;
<span class="kw">let </span>red_string = Red.paint(<span class="string">&quot;a red string&quot;</span>).to_string();</code></pre></div>
<h3 id="bold-underline-background-and-other-styles"><a href="#bold-underline-background-and-other-styles">Bold, underline, background, and other styles</a></h3>
<p>For anything more complex than plain foreground colour changes, you need to
construct <code>Style</code> values themselves, rather than beginning with a <code>Colour</code>.
You can do this by chaining methods based on a new <code>Style</code>, created with
<a href="struct.Style.html#method.new"><code>Style::new()</code></a>. Each method creates a new style that has that specific
property set. For example:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ansi_term::Style;
<span class="macro">println!</span>(<span class="string">&quot;How about some {} and {}?&quot;</span>,
Style::new().bold().paint(<span class="string">&quot;bold&quot;</span>),
Style::new().underline().paint(<span class="string">&quot;underline&quot;</span>));</code></pre></div>
<p>For brevity, these methods have also been implemented for <code>Colour</code> values,
so you can give your styles a foreground colour without having to begin with
an empty <code>Style</code> value:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ansi_term::Colour::{Blue, Yellow};
<span class="macro">println!</span>(<span class="string">&quot;Demonstrating {} and {}!&quot;</span>,
Blue.bold().paint(<span class="string">&quot;blue bold&quot;</span>),
Yellow.underline().paint(<span class="string">&quot;yellow underline&quot;</span>));
<span class="macro">println!</span>(<span class="string">&quot;Yellow on blue: {}&quot;</span>, Yellow.on(Blue).paint(<span class="string">&quot;wow!&quot;</span>));</code></pre></div>
<p>The complete list of styles you can use are: <a href="struct.Style.html#method.bold"><code>bold</code></a>, <a href="struct.Style.html#method.dimmed"><code>dimmed</code></a>, <a href="struct.Style.html#method.italic"><code>italic</code></a>,
<a href="struct.Style.html#method.underline"><code>underline</code></a>, <a href="struct.Style.html#method.blink"><code>blink</code></a>, <a href="struct.Style.html#method.reverse"><code>reverse</code></a>, <a href="struct.Style.html#method.hidden"><code>hidden</code></a>, <a href="struct.Style.html#method.strikethrough"><code>strikethrough</code></a>, and <a href="struct.Style.html#method.on"><code>on</code></a> for
background colours.</p>
<p>In some cases, you may find it easier to change the foreground on an
existing <code>Style</code> rather than starting from the appropriate <code>Colour</code>.
You can do this using the <a href="struct.Style.html#method.fg"><code>fg</code></a> method:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ansi_term::Style;
<span class="kw">use </span>ansi_term::Colour::{Blue, Cyan, Yellow};
<span class="macro">println!</span>(<span class="string">&quot;Yellow on blue: {}&quot;</span>, Style::new().on(Blue).fg(Yellow).paint(<span class="string">&quot;yow!&quot;</span>));
<span class="macro">println!</span>(<span class="string">&quot;Also yellow on blue: {}&quot;</span>, Cyan.on(Blue).fg(Yellow).paint(<span class="string">&quot;zow!&quot;</span>));</code></pre></div>
<p>You can turn a <code>Colour</code> into a <code>Style</code> with the <a href="enum.Colour.html#method.normal"><code>normal</code></a> method.
This will produce the exact same <code>ANSIString</code> as if you just used the
<code>paint</code> method on the <code>Colour</code> directly, but it’s useful in certain cases:
for example, you may have a method that returns <code>Styles</code>, and need to
represent both the “red bold” and “red, but not bold” styles with values of
the same type. The <code>Style</code> struct also has a <a href="https://doc.rust-lang.org/std/default/trait.Default.html"><code>Default</code></a> implementation if you
want to have a style with <em>nothing</em> set.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ansi_term::Style;
<span class="kw">use </span>ansi_term::Colour::Red;
Red.normal().paint(<span class="string">&quot;yet another red string&quot;</span>);
Style::default().paint(<span class="string">&quot;a completely regular string&quot;</span>);</code></pre></div>
<h3 id="extended-colours"><a href="#extended-colours">Extended colours</a></h3>
<p>You can access the extended range of 256 colours by using the <code>Colour::Fixed</code>
variant, which takes an argument of the colour number to use. This can be
included wherever you would use a <code>Colour</code>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ansi_term::Colour::Fixed;
Fixed(<span class="number">134</span>).paint(<span class="string">&quot;A sort of light purple&quot;</span>);
Fixed(<span class="number">221</span>).on(Fixed(<span class="number">124</span>)).paint(<span class="string">&quot;Mustard in the ketchup&quot;</span>);</code></pre></div>
<p>The first sixteen of these values are the same as the normal and bold
standard colour variants. There’s nothing stopping you from using these as
<code>Fixed</code> colours instead, but there’s nothing to be gained by doing so
either.</p>
<p>You can also access full 24-bit colour by using the <code>Colour::RGB</code> variant,
which takes separate <code>u8</code> arguments for red, green, and blue:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ansi_term::Colour::RGB;
RGB(<span class="number">70</span>, <span class="number">130</span>, <span class="number">180</span>).paint(<span class="string">&quot;Steel blue&quot;</span>);</code></pre></div>
<h3 id="combining-successive-coloured-strings"><a href="#combining-successive-coloured-strings">Combining successive coloured strings</a></h3>
<p>The benefit of writing ANSI escape codes to the terminal is that they
<em>stack</em>: you do not need to end every coloured string with a reset code if
the text that follows it is of a similar style. For example, if you want to
have some blue text followed by some blue bold text, it’s possible to send
the ANSI code for blue, followed by the ANSI code for bold, and finishing
with a reset code without having to have an extra one between the two
strings.</p>
<p>This crate can optimise the ANSI codes that get printed in situations like
this, making life easier for your terminal renderer. The <a href="type.ANSIStrings.html"><code>ANSIStrings</code></a>
type takes a slice of several <a href="type.ANSIString.html"><code>ANSIString</code></a> values, and will iterate over
each of them, printing only the codes for the styles that need to be updated
as part of its formatting routine.</p>
<p>The following code snippet uses this to enclose a binary number displayed in
red bold text inside some red, but not bold, brackets:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ansi_term::Colour::Red;
<span class="kw">use </span>ansi_term::{ANSIString, ANSIStrings};
<span class="kw">let </span>some_value = <span class="macro">format!</span>(<span class="string">&quot;{:b}&quot;</span>, <span class="number">42</span>);
<span class="kw">let </span>strings: <span class="kw-2">&amp;</span>[ANSIString&lt;<span class="lifetime">&#39;static</span>&gt;] = <span class="kw-2">&amp;</span>[
Red.paint(<span class="string">&quot;[&quot;</span>),
Red.bold().paint(some_value),
Red.paint(<span class="string">&quot;]&quot;</span>),
];
<span class="macro">println!</span>(<span class="string">&quot;Value: {}&quot;</span>, ANSIStrings(strings));</code></pre></div>
<p>There are several things to note here. Firstly, the <a href="type.ANSIByteString.html#method.write_to"><code>paint</code></a> method can take
<em>either</em> an owned <a href="https://doc.rust-lang.org/std/string/struct.String.html"><code>String</code></a> or a borrowed <a href="https://doc.rust-lang.org/std/primitive.str.html"><code>&amp;str</code></a>. Internally, an <a href="type.ANSIString.html"><code>ANSIString</code></a>
holds a copy-on-write (<a href="https://doc.rust-lang.org/std/borrow/enum.Cow.html"><code>Cow</code></a>) string value to deal with both owned and
borrowed strings at the same time. This is used here to display a <code>String</code>,
the result of the <code>format!</code> call, using the same mechanism as some
statically-available <code>&amp;str</code> slices. Secondly, that the <a href="type.ANSIStrings.html"><code>ANSIStrings</code></a> value
works in the same way as its singular counterpart, with a <a href="https://doc.rust-lang.org/std/fmt/trait.Display.html"><code>Display</code></a>
implementation that only performs the formatting when required.</p>
<h3 id="byte-strings"><a href="#byte-strings">Byte strings</a></h3>
<p>This library also supports formatting <code>\[u8]</code> byte strings; this supports
applications working with text in an unknown encoding. <a href="struct.Style.html"><code>Style</code></a> and
<a href="enum.Colour.html"><code>Colour</code></a> support painting <code>\[u8]</code> values, resulting in an <a href="type.ANSIByteString.html"><code>ANSIByteString</code></a>.
This type does not implement <a href="https://doc.rust-lang.org/std/fmt/trait.Display.html"><code>Display</code></a>, as it may not contain UTF-8, but
it does provide a method <a href="type.ANSIByteString.html#method.write_to"><code>write_to</code></a> to write the result to any value that
implements <a href="https://doc.rust-lang.org/std/io/trait.Write.html"><code>Write</code></a>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ansi_term::Colour::Green;
Green.paint(<span class="string">&quot;user data&quot;</span>.as_bytes()).write_to(<span class="kw-2">&amp;mut </span>std::io::stdout()).unwrap();</code></pre></div>
<p>Similarly, the type <a href="type.ANSIByteStrings.html"><code>ANSIByteStrings</code></a> supports writing a list of
<a href="type.ANSIByteString.html"><code>ANSIByteString</code></a> values with minimal escape sequences:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ansi_term::Colour::Green;
<span class="kw">use </span>ansi_term::ANSIByteStrings;
ANSIByteStrings(<span class="kw-2">&amp;</span>[
Green.paint(<span class="string">&quot;user data 1\n&quot;</span>.as_bytes()),
Green.bold().paint(<span class="string">&quot;user data 2\n&quot;</span>.as_bytes()),
]).write_to(<span class="kw-2">&amp;mut </span>std::io::stdout()).unwrap();</code></pre></div>
</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.ANSIGenericString.html" title="ansi_term::ANSIGenericString struct">ANSIGenericString</a></div><div class="item-right docblock-short">An <code>ANSIGenericString</code> includes a generic string type and a <code>Style</code> to
display that string. <code>ANSIString</code> and <code>ANSIByteString</code> are aliases for
this type on <code>str</code> and <code>\[u8]</code>, respectively.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ANSIGenericStrings.html" title="ansi_term::ANSIGenericStrings struct">ANSIGenericStrings</a></div><div class="item-right docblock-short">A set of <code>ANSIGenericString</code>s collected together, in order to be
written with a minimum of control characters.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Infix.html" title="ansi_term::Infix struct">Infix</a></div><div class="item-right docblock-short">Like <code>ANSIString</code>, but only displays the difference between two
styles.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Prefix.html" title="ansi_term::Prefix struct">Prefix</a></div><div class="item-right docblock-short">Like <code>ANSIString</code>, but only displays the style prefix.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Style.html" title="ansi_term::Style struct">Style</a></div><div class="item-right docblock-short">A style is a collection of properties that can format a string
using ANSI escape codes.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Suffix.html" title="ansi_term::Suffix struct">Suffix</a></div><div class="item-right docblock-short">Like <code>ANSIString</code>, but only displays the style suffix.</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="ansi_term::Color enum">Color</a></div><div class="item-right docblock-short">A colour is one specific type of ANSI escape code, and can refer
to either the foreground or background colour.</div></div><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.Colour.html" title="ansi_term::Colour enum">Colour</a></div><div class="item-right docblock-short">A colour is one specific type of ANSI escape code, and can refer
to either the foreground or background colour.</div></div></div><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.ANSIByteStrings.html" title="ansi_term::ANSIByteStrings fn">ANSIByteStrings</a></div><div class="item-right docblock-short">A function to construct an <code>ANSIByteStrings</code> instance.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.ANSIStrings.html" title="ansi_term::ANSIStrings fn">ANSIStrings</a></div><div class="item-right docblock-short">A function to construct an <code>ANSIStrings</code> instance.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.sub_string.html" title="ansi_term::sub_string fn">sub_string</a></div><div class="item-right docblock-short">Return a substring of the given ANSIStrings sequence, while keeping the formatting.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.unstyle.html" title="ansi_term::unstyle fn">unstyle</a></div><div class="item-right docblock-short">Return a concatenated copy of <code>strs</code> without the formatting, as an allocated <code>String</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.unstyled_len.html" title="ansi_term::unstyled_len fn">unstyled_len</a></div><div class="item-right docblock-short">Return the unstyled length of ANSIStrings. This is equaivalent to <code>unstyle(strs).len()</code>.</div></div></div><h2 id="types" class="small-section-header"><a href="#types">Type Definitions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="type" href="type.ANSIByteString.html" title="ansi_term::ANSIByteString type">ANSIByteString</a></div><div class="item-right docblock-short">An <code>ANSIByteString</code> represents a formatted series of bytes. Use
<code>ANSIByteString</code> when styling text with an unknown encoding.</div></div><div class="item-row"><div class="item-left module-item"><a class="type" href="type.ANSIByteStrings.html" title="ansi_term::ANSIByteStrings type">ANSIByteStrings</a></div><div class="item-right docblock-short">A set of <code>ANSIByteString</code>s collected together, in order to be
written with a minimum of control characters.</div></div><div class="item-row"><div class="item-left module-item"><a class="type" href="type.ANSIString.html" title="ansi_term::ANSIString type">ANSIString</a></div><div class="item-right docblock-short">An ANSI String is a string coupled with the <code>Style</code> to display it
in a terminal.</div></div><div class="item-row"><div class="item-left module-item"><a class="type" href="type.ANSIStrings.html" title="ansi_term::ANSIStrings type">ANSIStrings</a></div><div class="item-right docblock-short">A set of <code>ANSIString</code>s collected together, in order to be written with a
minimum of control characters.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="ansi_term" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>