Debian Codegraph @

debian/rust-vendor/nu-ansi-term/README.md

raw
1 # nu-ansi-term
2
3 > This is a copy of rust-ansi-term but with Colour change to Color and light foreground colors added (90-97) as well as light background colors added (100-107).
4
5 This is a library for controlling colors and formatting, such as red bold text or blue underlined text, on ANSI terminals.
6
7 ## [View the Rustdoc](https://docs.rs/nu_ansi_term/)
8
9 ## Installation
10
11 This crate works with [Cargo](http://crates.io). Add the following to your `Cargo.toml` dependencies section:
12
13 ```toml
14 [dependencies]
15 nu-ansi-term = "0.49"
16
17 # optional gnu-legacy mode to have two digit instead of one digit styles
18 nu-ansi-term = { version="0.49", features=["gnu_legacy"] }
19 ```
20
21 ## Basic usage
22
23 There are three main types in this crate that you need to be concerned with: `AnsiString`, `Style`, and `Color`.
24
25 A `Style` holds stylistic information: foreground and background colors, whether the text should be bold, or blinking, or other properties.
26 The `Color` enum represents the available colors.
27 And an `AnsiString` is a string paired with a `Style`.
28
29 To format a string, call the `paint` method on a `Style` or a `Color`, passing in the string you want to format as the argument.
30 For example, here’s how to get some red text:
31
32 ```rust
33 use nu_ansi_term::Color::Red;
34
35 println!("This is in red: {}", Red.paint("a red string"));
36 ```
37
38 It’s important to note that the `paint` method does _not_ actually return a string with the ANSI control characters surrounding it.
39 Instead, it returns an `AnsiString` value that has a `Display` implementation that, when formatted, returns the characters.
40 This allows strings to be printed with a minimum of `String` allocations being performed behind the scenes.
41
42 If you _do_ want to get at the escape codes, then you can convert the `AnsiString` to a string as you would any other `Display` value:
43
44 ```rust
45 use nu_ansi_term::Color::Red;
46
47 let red_string = Red.paint("a red string").to_string();
48 ```
49
50 **Note for Windows 10 users:** On Windows 10, the application must enable ANSI support first:
51
52 ```rust,ignore
53 let enabled = nu_ansi_term::enable_ansi_support();
54 ```
55
56 ## Bold, underline, background, and other styles
57
58 For anything more complex than plain foreground color changes, you need to construct `Style` values themselves, rather than beginning with a `Color`.
59 You can do this by chaining methods based on a new `Style`, created with `Style::new()`.
60 Each method creates a new style that has that specific property set.
61 For example:
62
63 ```rust
64 use nu_ansi_term::Style;
65
66 println!("How about some {} and {}?",
67 Style::new().bold().paint("bold"),
68 Style::new().underline().paint("underline"));
69 ```
70
71 For brevity, these methods have also been implemented for `Color` values, so you can give your styles a foreground color without having to begin with an empty `Style` value:
72
73 ```rust
74 use nu_ansi_term::Color::{Blue, Yellow};
75
76 println!("Demonstrating {} and {}!",
77 Blue.bold().paint("blue bold"),
78 Yellow.underline().paint("yellow underline"));
79
80 println!("Yellow on blue: {}", Yellow.on(Blue).paint("wow!"));
81 ```
82
83 The complete list of styles you can use are:
84 `bold`, `dimmed`, `italic`, `underline`, `blink`, `reverse`, `hidden`, and `on` for background colors.
85
86 In some cases, you may find it easier to change the foreground on an existing `Style` rather than starting from the appropriate `Color`.
87 You can do this using the `fg` method:
88
89 ```rust
90 use nu_ansi_term::Style;
91 use nu_ansi_term::Color::{Blue, Cyan, Yellow};
92
93 println!("Yellow on blue: {}", Style::new().on(Blue).fg(Yellow).paint("yow!"));
94 println!("Also yellow on blue: {}", Cyan.on(Blue).fg(Yellow).paint("zow!"));
95 ```
96
97 You can turn a `Color` into a `Style` with the `normal` method.
98 This will produce the exact same `AnsiString` as if you just used the `paint` method on the `Color` directly, but it’s useful in certain cases: for example, you may have a method that returns `Styles`, and need to represent both the “red bold” and “red, but not bold” styles with values of the same type. The `Style` struct also has a `Default` implementation if you want to have a style with _nothing_ set.
99
100 ```rust
101 use nu_ansi_term::Style;
102 use nu_ansi_term::Color::Red;
103
104 Red.normal().paint("yet another red string");
105 Style::default().paint("a completely regular string");
106 ```
107
108 Sometimes it is desirable to hard-reset a style/color just before
109 applying a new one. To reset and apply, the `reset_before_style` method can
110 be used on either `Color` or `Style` structs.
111
112 ```rust
113 use nu_ansi_term::Style;
114
115 println!("\x1b[33mHow about some {} \x1b[33mand {}?\x1b[0m",
116 Style::new().reset_before_style().bold().paint("bold"),
117 Style::new().reset_before_style().underline().paint("underline"));
118 ```
119
120 ## Extended colors
121
122 You can access the extended range of 256 colors by using the `Color::Fixed` variant, which takes an argument of the color number to use.
123 This can be included wherever you would use a `Color`:
124
125 ```rust
126 use nu_ansi_term::Color::Fixed;
127
128 Fixed(134).paint("A sort of light purple");
129 Fixed(221).on(Fixed(124)).paint("Mustard in the ketchup");
130 ```
131
132 The first sixteen of these values are the same as the normal and bold standard color variants.
133 There’s nothing stopping you from using these as `Fixed` colors instead, but there’s nothing to be gained by doing so either.
134
135 You can also access full 24-bit color by using the `Color::RGB` variant, which takes separate `u8` arguments for red, green, and blue:
136
137 ```rust
138 use nu_ansi_term::Color::RGB;
139
140 RGB(70, 130, 180).paint("Steel blue");
141 ```
142
143 ## Combining successive colored strings
144
145 The benefit of writing ANSI escape codes to the terminal is that they _stack_: you do not need to end every colored string with a reset code if the text that follows it is of a similar style.
146 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.
147
148 This crate can optimise the ANSI codes that get printed in situations like this, making life easier for your terminal renderer.
149 The `AnsiStrings` struct takes a slice of several `AnsiString` 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.
150
151 The following code snippet uses this to enclose a binary number displayed in red bold text inside some red, but not bold, brackets:
152
153 ```rust
154 use nu_ansi_term::Color::Red;
155 use nu_ansi_term::{AnsiString, AnsiStrings};
156
157 let some_value = format!("{:b}", 42);
158 let strings: &[AnsiString<'static>] = &[
159 Red.paint("["),
160 Red.bold().paint(some_value),
161 Red.paint("]"),
162 ];
163
164 println!("Value: {}", AnsiStrings(strings));
165 ```
166
167 There are several things to note here.
168 Firstly, the `paint` method can take _either_ an owned `String` or a borrowed `&str`.
169 Internally, an `AnsiString` holds a copy-on-write (`Cow`) string value to deal with both owned and borrowed strings at the same time.
170 This is used here to display a `String`, the result of the `format!` call, using the same mechanism as some statically-available `&str` slices.
171 Secondly, that the `AnsiStrings` value works in the same way as its singular counterpart, with a `Display` implementation that only performs the formatting when required.
172
173 ## Byte strings
174
175 This library also supports formatting `[u8]` byte strings; this supports applications working with text in an unknown encoding.
176 `Style` and `Color` support painting `[u8]` values, resulting in an `AnsiByteString`.
177 This type does not implement `Display`, as it may not contain UTF-8, but it does provide a method `write_to` to write the result to any value that implements `Write`:
178
179 ```rust
180 use nu_ansi_term::Color::Green;
181
182 Green.paint("user data".as_bytes()).write_to(&mut std::io::stdout()).unwrap();
183 ```
184
185 Similarly, the type `AnsiByteStrings` supports writing a list of `AnsiByteString` values with minimal escape sequences:
186
187 ```rust
188 use nu_ansi_term::Color::Green;
189 use nu_ansi_term::AnsiByteStrings;
190
191 AnsiByteStrings(&[
192 Green.paint("user data 1\n".as_bytes()),
193 Green.bold().paint("user data 2\n".as_bytes()),
194 ]).write_to(&mut std::io::stdout()).unwrap();
195 ```