Skip to content

Commit d4e4463

Browse files
sn99AndyGauge
authored andcommitted
added ansi_term example (#457)
* added ansi_term example * added ansi and ansiterm to dictionary * Delete ansi_terminal.md * Update arguments.md * link shows but does not work * bug fix [link now shows and works perfectly] * highlighted `` ansi-term name * resolved flaws * fixed issues with travis CI
1 parent bb73dd9 commit d4e4463

File tree

8 files changed

+89
-1
lines changed

8 files changed

+89
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ threadpool = "1.6"
4747
toml = "0.4"
4848
url = "1.6"
4949
walkdir = "2.0"
50+
ansi_term = "0.11.0"
5051

5152
[target.'cfg(target_os = "linux")'.dependencies]
5253
syslog = "4.0"

ci/dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Akshat
1111
akshat
1212
alisha
1313
AlphaNumeric
14+
ansi
15+
ansiterm
1416
ApiResponse
1517
apis
1618
APIs

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Sort a Vector](algorithms/sorting.md)
88
- [Command Line](cli.md)
99
- [Argument Parsing](cli/arguments.md)
10+
- [ANSI Terminal](cli/ansi_terminal.md)
1011
- [Compression](compression.md)
1112
- [Working with Tarballs](compression/tar.md)
1213
- [Concurrency](concurrency.md)

src/cli.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
| Recipe | Crates | Categories |
44
|--------|--------|------------|
55
| [Parse command line arguments][ex-clap-basic] | [![clap-badge]][clap] | [![cat-command-line-badge]][cat-command-line] |
6+
| [ANSI Terminal][ex-ansi_term-basic] | [![ansi_term-badge]][ansi_term]| [![cat-command-line-badge]][cat-command-line] |
67

78
[ex-clap-basic]: cli/arguments.html#parse-command-line-arguments
8-
{{#include links.md}}
9+
[ex-ansi_term-basic]: cli/ansi_terminal.html#ansi-terminal
10+
11+
{{#include links.md}}

src/cli/ansi_terminal.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ANSI Terminal
2+
3+
{{#include ansi_terminal/ansi_term-basic.md}}
4+
5+
{{#include ../links.md}}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
## ANSI Terminal
2+
3+
[![ansi_term-badge]][ansi_term] [![cat-command-line-badge]][cat-command-line]
4+
5+
This program depicts the use of [`ansi_term` crate] and how it is used for controlling colours and formatting, such as blue bold text or yellow underlined text, on ANSI terminals.
6+
7+
There are two main data structures in this crate that our code needs to be concerned with: [`ANSIString`] and [`Style`]. A [`Style`] holds stylistic information: colours, whether the text should be bold, or blinking, or whatever. There are also Colour variants that represent simple foreground colour styles. An [`ANSIString`] is a string paired with a [`Style`].
8+
9+
**Note:** British english uses *Colour* instead of *Color*, don't get confused
10+
11+
### Printing colored text to the Terminal
12+
13+
```rust
14+
extern crate ansi_term;
15+
16+
use ansi_term::Colour;
17+
18+
fn main() {
19+
println!("This is {} in color, {} in color and {} in color",
20+
Colour::Red.paint("red"),
21+
Colour::Blue.paint("blue"),
22+
Colour::Green.paint("green"));
23+
}
24+
```
25+
26+
#### Another way to print colored text to Terminal
27+
The code can be reformatted such that it converts the [`ANSIString`] to a string as so any other `Display` value
28+
```rust
29+
extern crate ansi_term;
30+
31+
use ansi_term::Colour;
32+
33+
fn main() {
34+
let blue_string = Colour::Blue.paint("Blue").to_string();
35+
println!("This is {} color", blue_string);
36+
}
37+
```
38+
39+
### Bold text in Terminal
40+
For anything more complex than plain foreground colour changes, the code needs to construct `Style` objects themselves, rather than beginning with a `Colour`. It can be achieved by chaining methods based on a new `Style`, created with [`Style::new()`]
41+
```rust
42+
extern crate ansi_term;
43+
44+
use ansi_term::Style;
45+
46+
fn main() {
47+
println!("{} and this is not",
48+
Style::new().bold().paint("This is Bold"));
49+
}
50+
```
51+
### Bold and colored text in terminal
52+
`Bold` methods can also been implemented for `Colour` values, and can be done by giving `styles` a foreground colour without having to begin with an empty Style value
53+
54+
```rust
55+
extern crate ansi_term;
56+
57+
use ansi_term::Colour;
58+
use ansi_term::Style;
59+
60+
fn main(){
61+
println!("{}, {} and {}",
62+
Colour::Yellow.paint("This is colored"),
63+
Style::new().bold().paint("this is bold"),
64+
Colour::Yellow.bold().paint("this is bold and colored"));
65+
}
66+
```
67+
68+
[documentation]: https://docs.rs/ansi_term/
69+
[`ansi_term` crate]: https://crates.io/crates/ansi_term
70+
[`ANSIString`]: https://docs.rs/ansi_term/*/ansi_term/type.ANSIString.html
71+
[`Style`]: https://docs.rs/ansi_term/*/ansi_term/struct.Style.html
72+
[`Style::new()`]: https://docs.rs/ansi_term/0.11.0/ansi_term/struct.Style.html#method.new

src/cli/arguments.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Clap basic
2+
13
{{#include arguments/clap-basic.md}}
24

35
{{#include ../links.md}}

src/links.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Keep lines sorted.
4848

4949
<!-- Crates -->
5050

51+
[ansi_term-badge]: https://badge-cache.kominick.com/crates/v/base64.svg?label=ansi_term
52+
[ansi_term]: https://docs.rs/ansi_term/
5153
[base64-badge]: https://badge-cache.kominick.com/crates/v/base64.svg?label=base64
5254
[base64]: https://docs.rs/base64/
5355
[bitflags-badge]: https://badge-cache.kominick.com/crates/v/bitflags.svg?label=bitflags

0 commit comments

Comments
 (0)