Have you ever been disappointed when a terminal library for rust was only written for UNIX systems? Crossterm provides clearing, input handling, styling, cursor movement, and terminal actions for both Windows and UNIX systems.
Crossterm aims to be simple and easy to call in code. Through the simplicity of Crossterm, you do not have to worry about the platform you are working with.
This crate supports all UNIX and Windows terminals down to Windows 7 (not all terminals are tested see Tested Terminals for more info).
This crate consists of five modules that are provided behind feature flags so that you can define which features you'd like to have; by default, all features are enabled.
All examples of how crossterm works can be found in the example repository.
Add the Crossterm package to your Cargo.toml
file.
[dependencies]
crossterm = "0.11"
These are some basic examples demonstrating how to use this crate. See the examples repository.
My first recommendation is to use the command API because this might replace some of the existing API in the future. It is more convenient, faster, and easier to use.
This module enables you to style the terminal text.
Good documentation can be found at the following places: docs, book and examples.
style text with attributes
```rust use crossterm::{Colored, Color, Colorize, Styler, Attribute};
// pass any Attribute
value to the formatting braces.
println!("{} Underlined {} No Underline", Attribute::Underlined, Attribute::NoUnderline);
// you could also call different attribute methods on a &str
and keep on chaining if needed.
let styledtext = "Bold Underlined".bold().underlined();
println!("{}", styledtext);
```
style text with colors
```rust println!("{} Red foreground color", Colored::Fg(Color::Red)); println!("{} Blue background color", Colored::Bg(Color::Blue));
// you can also call different coloring methods on a &str
.
let styledtext = "Bold Underlined".red().onblue();
println!("{}", styled_text);
```
style text with RGB and ANSI Value
```rust // custom rgb value (Windows 10 and UNIX systems) println!("{} some colored text", Colored::Fg(Color::Rgb { r: 10, g: 10, b: 10 }));
// custom ansi color value (Windows 10 and UNIX systems) println!("{} some colored text", Colored::Fg(Color::AnsiValue(10))); ```
This module enables you to work with the terminal cursor.
Good documentation could be found on the following places: docs and examples.
```rust use crossterm::cursor;
let mut cursor = cursor();
/// Moving the cursor // Set the cursor to position X: 10, Y: 5 in the terminal cursor.goto(10,5);
// Move the cursor up,right,down,left 3 cells. cursor.moveup(3); cursor.moveright(3); cursor.movedown(3); cursor.moveleft(3);
/// Safe the current cursor position to recall later // Goto X: 5 Y: 5 cursor.goto(5,5); // Safe cursor position: X: 5 Y: 5 cursor.saveposition(); // Goto X: 5 Y: 20 cursor.goto(5,20); // Print at X: 5 Y: 20. print!("Yea!"); // Reset back to X: 5 Y: 5. cursor.restoreposition(); // Print 'Back' at X: 5 Y: 5. print!("Back");
// hide cursor cursor.hide(); // show cursor cursor.show(); // blink or not blinking of the cursor (not widely supported) cursor.blink(true) ```
This module enables you to work with the terminal in general.
Good documentation could be found on the following places: docs and examples.
```rust use crossterm::{terminal,ClearType};
let mut terminal = terminal();
// Clear all lines in terminal; terminal.clear(ClearType::All); // Clear all cells from current cursor position down. terminal.clear(ClearType::FromCursorDown); // Clear all cells from current cursor position down. terminal.clear(ClearType::FromCursorUp); // Clear current line cells. terminal.clear(ClearType::CurrentLine); // Clear all the cells until next line. terminal.clear(ClearType::UntilNewLine);
// Get terminal size let (width, height) = terminal.terminal_size(); print!("X: {}, y: {}", width, height);
// Scroll down, up 10 lines. terminal.scrolldown(10); terminal.scrollup(10);
// Set terminal size (width, height) terminal.set_size(10,10);
// exit the current process. terminal.exit();
// write to the terminal whether you are on the main screen or alternate screen. terminal.write("Some text\n Some text on new line"); ```
This module enables you to read user input events.
Good documentation could be found on the following places: docs, book and examples.
available imports
rust
use crossterm_input::{
input, InputEvent, KeyEvent, MouseButton, MouseEvent, TerminalInput, AsyncReader, SyncReader, Screen
};
Simple Readings
```rust let mut input = input();
match input.read_char() { Ok(s) => println!("char typed: {}", s), Err(e) => println!("char error : {}", e), }
match input.read_line() { Ok(s) => println!("string typed: {}", s), Err(e) => println!("error: {}", e), } ```
Read input events synchronously or asynchronously.
```rust // make sure to enable raw mode, this will make sure key events won't be handled by the terminal it's // self and allows crossterm to read the input and pass it back to you. let screen = RawScreen::intorawmode();
let mut input = input();
// either read the input synchronously let stdin = input.read_sync();
// or asynchronously let stdin = input.read_async();
if let Some(keyevent) = stdin.next() { match keyevent { InputEvent::Keyboard(event: KeyEvent) => match event { /* check key event / } InputEvent::Mouse(event: MouseEvent) => match event { / check mouse event */ } } } ```
Enable mouse input events.
```rust let input = input();
// enable mouse events to be captured. input.enablemousemode().unwrap();
// disable mouse events to be captured. input.disablemousemode().unwrap(); ```
These concepts are a little more complex and would take over the README, please check out the docs, book and examples.
This crate supports all Unix terminals and Windows terminals down to Windows 7; however, not all of the terminals have been tested. If you have used this library for a terminal other than the above list without issues, then feel free to add it to the above list - I really would appreciate it!
I highly appreciate it when you contribute to this crate. Please visit the discord or issue list for more information
Would you like crossterm to be even more gorgeous and beautiful? You can help with this by donating.
This project, crossterm and all it's sub-modules: crosstermscreen, crosstermcursor, crosstermstyle, crossterminput, crosstermterminal, crosstermwinapi, crossterm_utils are licensed under the MIT License - see the LICENSE file for details