| 1 |
//! This is a library for controlling colors and formatting, such as |
| 2 |
//! red bold text or blue underlined text, on ANSI terminals. |
| 3 |
//! |
| 4 |
//! |
| 5 |
//! ## Basic usage |
| 6 |
//! |
| 7 |
//! There are three main types in this crate that you need to be |
| 8 |
//! concerned with: [`AnsiString`], [`Style`], and [`Color`]. |
| 9 |
//! |
| 10 |
//! A `Style` holds stylistic information: foreground and background colors, |
| 11 |
//! whether the text should be bold, or blinking, or other properties. The |
| 12 |
//! [`Color`] enum represents the available colors. And an [`AnsiString`] is a |
| 13 |
//! string paired with a [`Style`]. |
| 14 |
//! |
| 15 |
//! [`Color`] is also available as an alias to `Color`. |
| 16 |
//! |
| 17 |
//! To format a string, call the `paint` method on a `Style` or a `Color`, |
| 18 |
//! passing in the string you want to format as the argument. For example, |
| 19 |
//! here’s how to get some red text: |
| 20 |
//! |
| 21 |
//! ``` |
| 22 |
//! use nu_ansi_term::Color::Red; |
| 23 |
//! |
| 24 |
//! println!("This is in red: {}", Red.paint("a red string")); |
| 25 |
//! ``` |
| 26 |
//! |
| 27 |
//! It’s important to note that the `paint` method does *not* actually return a |
| 28 |
//! string with the ANSI control characters surrounding it. Instead, it returns |
| 29 |
//! an [`AnsiString`] value that has a [`Display`] implementation that, when |
| 30 |
//! formatted, returns the characters. This allows strings to be printed with a |
| 31 |
//! minimum of [`String`] allocations being performed behind the scenes. |
| 32 |
//! |
| 33 |
//! If you *do* want to get at the escape codes, then you can convert the |
| 34 |
//! [`AnsiString`] to a string as you would any other `Display` value: |
| 35 |
//! |
| 36 |
//! ``` |
| 37 |
//! use nu_ansi_term::Color::Red; |
| 38 |
//! |
| 39 |
//! let red_string = Red.paint("a red string").to_string(); |
| 40 |
//! ``` |
| 41 |
//! |
| 42 |
//! |
| 43 |
//! ## Bold, underline, background, and other styles |
| 44 |
//! |
| 45 |
//! For anything more complex than plain foreground color changes, you need to |
| 46 |
//! construct `Style` values themselves, rather than beginning with a `Color`. |
| 47 |
//! You can do this by chaining methods based on a new `Style`, created with |
| 48 |
//! [`Style::new()`]. Each method creates a new style that has that specific |
| 49 |
//! property set. For example: |
| 50 |
//! |
| 51 |
//! ``` |
| 52 |
//! use nu_ansi_term::Style; |
| 53 |
//! |
| 54 |
//! println!("How about some {} and {}?", |
| 55 |
//! Style::new().bold().paint("bold"), |
| 56 |
//! Style::new().underline().paint("underline")); |
| 57 |
//! ``` |
| 58 |
//! |
| 59 |
//! For brevity, these methods have also been implemented for `Color` values, |
| 60 |
//! so you can give your styles a foreground color without having to begin with |
| 61 |
//! an empty `Style` value: |
| 62 |
//! |
| 63 |
//! ``` |
| 64 |
//! use nu_ansi_term::Color::{Blue, Yellow}; |
| 65 |
//! |
| 66 |
//! println!("Demonstrating {} and {}!", |
| 67 |
//! Blue.bold().paint("blue bold"), |
| 68 |
//! Yellow.underline().paint("yellow underline")); |
| 69 |
//! |
| 70 |
//! println!("Yellow on blue: {}", Yellow.on(Blue).paint("wow!")); |
| 71 |
//! ``` |
| 72 |
//! |
| 73 |
//! The complete list of styles you can use are: [`bold`], [`dimmed`], [`italic`], |
| 74 |
//! [`underline`], [`blink`], [`reverse`], [`hidden`], [`strikethrough`], and [`on`] for |
| 75 |
//! background colors. |
| 76 |
//! |
| 77 |
//! In some cases, you may find it easier to change the foreground on an |
| 78 |
//! existing `Style` rather than starting from the appropriate `Color`. |
| 79 |
//! You can do this using the [`fg`] method: |
| 80 |
//! |
| 81 |
//! ``` |
| 82 |
//! use nu_ansi_term::Style; |
| 83 |
//! use nu_ansi_term::Color::{Blue, Cyan, Yellow}; |
| 84 |
//! |
| 85 |
//! println!("Yellow on blue: {}", Style::new().on(Blue).fg(Yellow).paint("yow!")); |
| 86 |
//! println!("Also yellow on blue: {}", Cyan.on(Blue).fg(Yellow).paint("zow!")); |
| 87 |
//! ``` |
| 88 |
//! |
| 89 |
//! You can turn a `Color` into a `Style` with the [`normal`] method. |
| 90 |
//! This will produce the exact same `AnsiString` as if you just used the |
| 91 |
//! `paint` method on the `Color` directly, but it’s useful in certain cases: |
| 92 |
//! for example, you may have a method that returns `Styles`, and need to |
| 93 |
//! represent both the “red bold” and “red, but not bold” styles with values of |
| 94 |
//! the same type. The `Style` struct also has a [`Default`] implementation if you |
| 95 |
//! want to have a style with *nothing* set. |
| 96 |
//! |
| 97 |
//! ``` |
| 98 |
//! use nu_ansi_term::Style; |
| 99 |
//! use nu_ansi_term::Color::Red; |
| 100 |
//! |
| 101 |
//! Red.normal().paint("yet another red string"); |
| 102 |
//! Style::default().paint("a completely regular string"); |
| 103 |
//! ``` |
| 104 |
//! |
| 105 |
//! |
| 106 |
//! ## Extended colors |
| 107 |
//! |
| 108 |
//! You can access the extended range of 256 colors by using the `Color::Fixed` |
| 109 |
//! variant, which takes an argument of the color number to use. This can be |
| 110 |
//! included wherever you would use a `Color`: |
| 111 |
//! |
| 112 |
//! ``` |
| 113 |
//! use nu_ansi_term::Color::Fixed; |
| 114 |
//! |
| 115 |
//! Fixed(134).paint("A sort of light purple"); |
| 116 |
//! Fixed(221).on(Fixed(124)).paint("Mustard in the ketchup"); |
| 117 |
//! ``` |
| 118 |
//! |
| 119 |
//! The first sixteen of these values are the same as the normal and bold |
| 120 |
//! standard color variants. There’s nothing stopping you from using these as |
| 121 |
//! `Fixed` colors instead, but there’s nothing to be gained by doing so |
| 122 |
//! either. |
| 123 |
//! |
| 124 |
//! You can also access full 24-bit color by using the `Color::Rgb` variant, |
| 125 |
//! which takes separate `u8` arguments for red, green, and blue: |
| 126 |
//! |
| 127 |
//! ``` |
| 128 |
//! use nu_ansi_term::Color::Rgb; |
| 129 |
//! |
| 130 |
//! Rgb(70, 130, 180).paint("Steel blue"); |
| 131 |
//! ``` |
| 132 |
//! |
| 133 |
//! ## Combining successive colored strings |
| 134 |
//! |
| 135 |
//! The benefit of writing ANSI escape codes to the terminal is that they |
| 136 |
//! *stack*: you do not need to end every colored string with a reset code if |
| 137 |
//! the text that follows it is of a similar style. For example, if you want to |
| 138 |
//! have some blue text followed by some blue bold text, it’s possible to send |
| 139 |
//! the ANSI code for blue, followed by the ANSI code for bold, and finishing |
| 140 |
//! with a reset code without having to have an extra one between the two |
| 141 |
//! strings. |
| 142 |
//! |
| 143 |
//! This crate can optimise the ANSI codes that get printed in situations like |
| 144 |
//! this, making life easier for your terminal renderer. The [`AnsiStrings`] |
| 145 |
//! type takes a slice of several [`AnsiString`] values, and will iterate over |
| 146 |
//! each of them, printing only the codes for the styles that need to be updated |
| 147 |
//! as part of its formatting routine. |
| 148 |
//! |
| 149 |
//! The following code snippet uses this to enclose a binary number displayed in |
| 150 |
//! red bold text inside some red, but not bold, brackets: |
| 151 |
//! |
| 152 |
//! ``` |
| 153 |
//! use nu_ansi_term::Color::Red; |
| 154 |
//! use nu_ansi_term::{AnsiString, AnsiStrings}; |
| 155 |
//! |
| 156 |
//! let some_value = format!("{:b}", 42); |
| 157 |
//! let strings: &[AnsiString<'static>] = &[ |
| 158 |
//! Red.paint("["), |
| 159 |
//! Red.bold().paint(some_value), |
| 160 |
//! Red.paint("]"), |
| 161 |
//! ]; |
| 162 |
//! |
| 163 |
//! println!("Value: {}", AnsiStrings(strings)); |
| 164 |
//! ``` |
| 165 |
//! |
| 166 |
//! There are several things to note here. Firstly, the [`paint`] method can take |
| 167 |
//! *either* an owned [`String`] or a borrowed [`&str`]. Internally, an [`AnsiString`] |
| 168 |
//! holds a copy-on-write ([`Cow`]) string value to deal with both owned and |
| 169 |
//! borrowed strings at the same time. This is used here to display a `String`, |
| 170 |
//! the result of the `format!` call, using the same mechanism as some |
| 171 |
//! statically-available `&str` slices. Secondly, that the [`AnsiStrings`] value |
| 172 |
//! works in the same way as its singular counterpart, with a [`Display`] |
| 173 |
//! implementation that only performs the formatting when required. |
| 174 |
//! |
| 175 |
//! ## Byte strings |
| 176 |
//! |
| 177 |
//! This library also supports formatting `\[u8]` byte strings; this supports |
| 178 |
//! applications working with text in an unknown encoding. [`Style`] and |
| 179 |
//! [`Color`] support painting `\[u8]` values, resulting in an [`AnsiByteString`]. |
| 180 |
//! This type does not implement [`Display`], as it may not contain UTF-8, but |
| 181 |
//! it does provide a method [`write_to`] to write the result to any value that |
| 182 |
//! implements [`Write`]: |
| 183 |
//! |
| 184 |
#![cfg_attr(feature = "std", doc = "```")] |
| 185 |
#![cfg_attr(not(feature = "std"), doc = "```ignore")] |
| 186 |
//! use nu_ansi_term::Color::Green; |
| 187 |
//! |
| 188 |
//! Green.paint("user data".as_bytes()).write_to(&mut std::io::stdout()).unwrap(); |
| 189 |
//! ``` |
| 190 |
//! |
| 191 |
//! Similarly, the type [`AnsiByteStrings`] supports writing a list of |
| 192 |
//! [`AnsiByteString`] values with minimal escape sequences: |
| 193 |
//! |
| 194 |
#![cfg_attr(feature = "std", doc = "```")] |
| 195 |
#![cfg_attr(not(feature = "std"), doc = "```ignore")] |
| 196 |
//! use nu_ansi_term::Color::Green; |
| 197 |
//! use nu_ansi_term::AnsiByteStrings; |
| 198 |
//! |
| 199 |
//! AnsiByteStrings(&[ |
| 200 |
//! Green.paint("user data 1\n".as_bytes()), |
| 201 |
//! Green.bold().paint("user data 2\n".as_bytes()), |
| 202 |
//! ]).write_to(&mut std::io::stdout()).unwrap(); |
| 203 |
//! ``` |
| 204 |
//! |
| 205 |
//! [`Cow`]: https://doc.rust-lang.org/std/borrow/enum.Cow.html |
| 206 |
//! [`Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html |
| 207 |
//! [`Default`]: https://doc.rust-lang.org/std/default/trait.Default.html |
| 208 |
//! [`String`]: https://doc.rust-lang.org/std/string/struct.String.html |
| 209 |
//! [`&str`]: https://doc.rust-lang.org/std/primitive.str.html |
| 210 |
//! [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html |
| 211 |
//! [`Style`]: struct.Style.html |
| 212 |
//! [`Style::new()`]: struct.Style.html#method.new |
| 213 |
//! [`Color`]: enum.Color.html |
| 214 |
//! [`Color`]: enum.Color.html |
| 215 |
//! [`AnsiString`]: type.AnsiString.html |
| 216 |
//! [`AnsiStrings`]: type.AnsiStrings.html |
| 217 |
//! [`AnsiByteString`]: type.AnsiByteString.html |
| 218 |
//! [`AnsiByteStrings`]: type.AnsiByteStrings.html |
| 219 |
//! [`write_to`]: type.AnsiByteString.html#method.write_to |
| 220 |
//! [`paint`]: type.AnsiByteString.html#method.write_to |
| 221 |
//! [`normal`]: enum.Color.html#method.normal |
| 222 |
//! |
| 223 |
//! [`bold`]: struct.Style.html#method.bold |
| 224 |
//! [`dimmed`]: struct.Style.html#method.dimmed |
| 225 |
//! [`italic`]: struct.Style.html#method.italic |
| 226 |
//! [`underline`]: struct.Style.html#method.underline |
| 227 |
//! [`blink`]: struct.Style.html#method.blink |
| 228 |
//! [`reverse`]: struct.Style.html#method.reverse |
| 229 |
//! [`hidden`]: struct.Style.html#method.hidden |
| 230 |
//! [`strikethrough`]: struct.Style.html#method.strikethrough |
| 231 |
//! [`fg`]: struct.Style.html#method.fg |
| 232 |
//! [`on`]: struct.Style.html#method.on |
| 233 |
|
| 234 |
#![cfg_attr(not(any(test, feature = "std")), no_std)] |
| 235 |
#![crate_name = "nu_ansi_term"] |
| 236 |
#![crate_type = "rlib"] |
| 237 |
#![warn(missing_copy_implementations)] |
| 238 |
// #![warn(missing_docs)] |
| 239 |
#![warn(trivial_casts, trivial_numeric_casts)] |
| 240 |
// #![warn(unused_extern_crates, unused_qualifications)] |
| 241 |
|
| 242 |
#[cfg(test)] |
| 243 |
doc_comment::doctest!("../README.md"); |
| 244 |
|
| 245 |
extern crate alloc; |
| 246 |
|
| 247 |
pub mod ansi; |
| 248 |
pub use ansi::{Infix, Prefix, Suffix}; |
| 249 |
|
| 250 |
mod style; |
| 251 |
pub use style::{Color, Style}; |
| 252 |
|
| 253 |
mod difference; |
| 254 |
mod display; |
| 255 |
pub use display::*; |
| 256 |
|
| 257 |
mod write; |
| 258 |
|
| 259 |
#[cfg(all(windows, feature = "std"))] |
| 260 |
mod windows; |
| 261 |
#[cfg(all(windows, feature = "std"))] |
| 262 |
pub use crate::windows::*; |
| 263 |
|
| 264 |
mod util; |
| 265 |
pub use util::*; |
| 266 |
|
| 267 |
mod debug; |
| 268 |
|
| 269 |
pub mod gradient; |
| 270 |
pub use gradient::*; |
| 271 |
|
| 272 |
mod rgb; |
| 273 |
pub use rgb::*; |