| 1 |
# `console` |
| 2 |
|
| 3 |
[](https://github.com/console-rs/console/actions?query=workflow%3ACI) |
| 4 |
[](https://crates.io/crates/console) |
| 5 |
[](https://github.com/console-rs/console/blob/main/LICENSE) |
| 6 |
[](https://docs.rs/console) |
| 7 |
|
| 8 |
**console** is a library for Rust that provides access to various terminal |
| 9 |
features so you can build nicer looking command line interfaces. It |
| 10 |
comes with various tools and utilities for working with Terminals and |
| 11 |
formatting text. |
| 12 |
|
| 13 |
Best paired with other libraries in the family: |
| 14 |
|
| 15 |
* [dialoguer](https://docs.rs/dialoguer) |
| 16 |
* [indicatif](https://docs.rs/indicatif) |
| 17 |
|
| 18 |
## Terminal Access |
| 19 |
|
| 20 |
The terminal is abstracted through the `console::Term` type. It can |
| 21 |
either directly provide access to the connected terminal or by buffering |
| 22 |
up commands. A buffered terminal will however not be completely buffered |
| 23 |
on windows where cursor movements are currently directly passed through. |
| 24 |
|
| 25 |
Example usage: |
| 26 |
|
| 27 |
```rust |
| 28 |
use std::thread; |
| 29 |
use std::time::Duration; |
| 30 |
|
| 31 |
use console::Term; |
| 32 |
|
| 33 |
let term = Term::stdout(); |
| 34 |
term.write_line("Hello World!")?; |
| 35 |
thread::sleep(Duration::from_millis(2000)); |
| 36 |
term.clear_line()?; |
| 37 |
``` |
| 38 |
|
| 39 |
## Colors and Styles |
| 40 |
|
| 41 |
`console` automatically detects when to use colors based on the tty flag, following the |
| 42 |
[clicolors standard](https://bixense.com/clicolors/) for color enabling/disabling. It also |
| 43 |
provides higher level wrappers for styling text and other things that can be |
| 44 |
displayed with the `style` function and utility types. |
| 45 |
|
| 46 |
Example usage: |
| 47 |
|
| 48 |
```rust |
| 49 |
use console::style; |
| 50 |
|
| 51 |
println!("This is {} neat", style("quite").cyan()); |
| 52 |
``` |
| 53 |
|
| 54 |
You can also store styles and apply them to text later: |
| 55 |
|
| 56 |
```rust |
| 57 |
use console::Style; |
| 58 |
|
| 59 |
let cyan = Style::new().cyan(); |
| 60 |
println!("This is {} neat", cyan.apply_to("quite")); |
| 61 |
``` |
| 62 |
|
| 63 |
## Working with ANSI Codes |
| 64 |
|
| 65 |
The crate provides the function `strip_ansi_codes` to remove ANSI codes |
| 66 |
from a string as well as `measure_text_width` to calculate the width of a |
| 67 |
string as it would be displayed by the terminal. Both of those together |
| 68 |
are useful for more complex formatting. |
| 69 |
|
| 70 |
## Unicode Width Support |
| 71 |
|
| 72 |
By default this crate depends on the `unicode-width` crate to calculate |
| 73 |
the width of terminal characters. If you do not need this you can disable |
| 74 |
the `unicode-width` feature which will cut down on dependencies. |
| 75 |
|
| 76 |
License: MIT |