ANSI Terminal
| ansiterm
| | |
| anstream
| | |
| anstyle
| | |
| console
| | |
| owo-colors
| | |
| stylish
| | |
| termcolor
| | |
| yansi
| | |
| termion
| | |
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.
Print 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") ); }
Print bold text to the 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") ); }
Print bold and colored text to the 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 used 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...)
use std::io::Write; use std::io::stdout; use crossterm::ExecutableCommand; use crossterm::cursor; use crossterm::event; use crossterm::event::DisableMouseCapture; use crossterm::event::EnableMouseCapture; use crossterm::event::Event; use crossterm::event::KeyCode; use crossterm::execute; use crossterm::queue; use crossterm::style; use crossterm::style::Color; use crossterm::style::Print; use crossterm::style::SetBackgroundColor; use crossterm::style::SetForegroundColor; use crossterm::terminal; use crossterm::terminal::ClearType; fn main() -> anyhow::Result<()> { let mut stdout = stdout(); // Enter raw mode and enable mouse capture terminal::enable_raw_mode()?; // Execute the command immediately execute!(stdout, EnableMouseCapture)?; // Clear the screen and move cursor to the top left // You can queue commands instead of executing them directly // when you call `Write::flush` these commands will be executed. queue!( stdout, terminal::Clear(ClearType::All), cursor::MoveTo(0, 0) )?; // Many other commands... stdout.flush()?; // Display instructions println!("Press 'q' to exit."); execute!( stdout, SetForegroundColor(Color::Blue), SetBackgroundColor(Color::White), Print("Hello, Crossterm!\n") )?; // We can use the `execute` function rather than the macro. // The `execute` function returns itself, // therefore you can queue another command. stdout.execute(style::ResetColor)?; loop { // Listen for key events if let Event::Key(key_event) = event::read()? { match key_event.code { KeyCode::Char('q') => { break; } KeyCode::Up => { println!("Up arrow key pressed."); } KeyCode::Down => { println!("Down arrow key pressed."); } KeyCode::Left => { println!("Left arrow key pressed."); } KeyCode::Right => { println!("Right arrow key pressed."); } _ => {} } } } // Leave raw mode and disable mouse capture terminal::disable_raw_mode()?; execute!(stdout, DisableMouseCapture)?; Ok(()) }
Most popular
termcolor
A simple cross platform library for writing colored text to a terminal
anstyle
ANSI text styling
anstream
A simple cross platform library for writing colored text to a terminal.
Library for ANSI terminal colors and styles (bold, underline)
ansiterm
Library for ANSI terminal colours and styles (bold, underline)
console
A terminal and console abstraction for Rust
owo-colors
Zero-allocation terminal colors that will make people go owo
stylish
Yet another crate implementing colorized text
yansi
A dead simple ANSI terminal color painting library.
termion
Termion is a pure Rust, bindless library for low-level handling, manipulating and reading information about terminals. This provides a full-featured alternative to Termbox.
Termion aims to be simple and yet expressive. It is bindless, meaning that it is not a front-end to some other library (e.g., ncurses or termbox), but a standalone library directly talking to the TTY.