ANSI Terminal
Recipe | Crates | Categories |
---|---|---|
ANSI Terminal | ||
Printing colored text to the Terminal | ||
Bold text in Terminal | ||
Bold and colored text in terminal | ||
Manipulate the cursor, style the output, handle input events |
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.
There are two main data structures in ansi_term
⮳: ansi_term::ANSIString
⮳ and Style
⮳. A Style
holds stylistic information: colors, whether the text should be bold, or blinking, or whatever. There are also Colour variants that represent simple foreground colour styles. An ansi_term::ANSIString
⮳ is a string paired with a ansi_term::Style
⮳.
Note: British English uses Colour instead of Color.
Printing colored text to the Terminal
use ansi_term::Colour; fn main() { println!( "This is {} in color, {} in color and {} in color", Colour::Red.paint("red"), Colour::Blue.paint("blue"), Colour::Green.paint("green") ); }
Bold text in Terminal
For anything more complex than plain foreground color changes, the code needs to construct ansi_term::Style
⮳ struct. ansi_term::Style::new
⮳ creates the struct, and properties chained.
use ansi_term::Style; fn main() { println!( "{} and this is not", Style::new().bold().paint("This is Bold") ); }
Bold and colored text in terminal
ansi_term::Color
⮳ implements many similar functions as ansi_term::Style
⮳ and can chain methods.
use ansi_term::Colour; use ansi_term::Style; fn main() { println!( "{}, {} and {}", Colour::Yellow.paint("This is colored"), Style::new().bold().paint("this is bold"), Colour::Yellow.bold().paint("this is bold and colored") ); }
Manipulate the cursor, style the output, handle input events
Low-level cross-platform terminal rendering and event handling.
Crossterm is a pure-rust, terminal manipulation library that makes it possible to write cross-platform text-based interfaces. It supports all UNIX and Windows terminals down to Windows 7
- Full control over writing and flushing output buffer
- Is tty
- Cursor manipulation
- Styled output
- Terminal handling
- Events (key inputs, mouse...)
Most popular
- termcolor
- anstyle
- anstream
- nu-ansi-term
Others