Crossterm | cross-platform terminal manipulating library.

Lines of Code Latest Version MIT docs Lines of Code

Ever got disappointed when a terminal library for rust was only written for UNIX systems? Crossterm provides the same core functionalities 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 is exists out of five modules who are behind feature flags so that you can define which features you'd like to have: - Crossterm Style - Crossterm Input - Crossterm Screen - Crossterm Cursor - Crossterm Terminal

Table of contents:

Getting Started

This documentation is only for Crossterm version 0.5 if you have an older version of Crossterm I suggest you check the Upgrade Manual. Also, check out the examples folders with detailed examples for all functionality of this crate.

Add the Crossterm package to your Cargo.toml file.

[dependencies] crossterm = "0.6"

Useful Links

Features

These are the features from this crate:

Examples

These are some basic examples demonstrating how to use this crate. See examples for more.

Crossterm Type | see more

This is a wrapper for all the modules crossterm provides like terminal, cursor, styling and input.

``rust // screen wheron theCrossterm` methods will be executed. let crossterm = Crossterm::new();

// get instance of the modules, whereafter you can use the methods the particulary module provides. let color = crossterm.color(); let cursor = crossterm.cursor(); let terminal = crossterm.terminal();

// styling println!("{}", crossterm.style("Black font on Green background color").with(Color::Black).on(Color::Green));

```

Styled Font | see more

This module provides the functionalities to style the terminal. ```rust
use crossterm::{Color, style};

// store objcets so it could be painted later to the screen.
let style1 = style("Some Blue font on Black background").with(Color::Blue).on(Color::Black); let style2 = style("Some Red font on Yellow background").with(Color::Red).on(Color::Yellow);

// syling font with (Windows 10 and UNIX systems) let normal = style("Normal text"); let bold = style("Bold text").bold(); let italic = style("Italic text").italic(); let slowblink = style("Slow blinking text").slowblink(); let rapidblink = style("Rapid blinking text").rapidblink(); let hidden = style("Hidden text").hidden(); let underlined = style("Underlined text").underlined(); let reversed = style("Reversed text").reverse(); let dimmed = style("Dim text").dim(); let crossedout = style("Crossed out font").crossedout();

// paint styled text to screen (this could also be called inline) println!("{}", style1); println!("{}", style2); println!("{}", bold); println!("{}", hidden); ...

// cursom rgb value (Windows 10 and UNIX systems) style("RGB color (10,10,10) ").with(Color::Rgb { r: 10, g: 10, b: 10 }));

// custom ansi color value (Windows 10 and UNIX systems) style("ANSI color value (50) ").with(Color::AnsiValue(50));

```

Cursor | see more

This module provides the functionalities to work with the terminal cursor.

```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.resetposition(); // 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)

```

Terminal | see more

This module provides the functionalities to work with the terminal in general.

```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"); ```

Alternate and Raw Screen

These concepts are a little more complex, please checkout the book topics about these subjects.

Tested terminals

This crate supports all Unix terminals and windows terminals down to Windows 7 but not all of them have been tested. If you have used this library for a terminal other than the above list without issues feel free to add it to the above list, I really would appreciate it.

Notice

This library is average stable now but I don't expect it to not to change that much. If there are any changes that will affect previous versions I will describe what to change to upgrade.

Todo

I still have some things in mind to implement.

Contributing

I highly appreciate it when you are contributing to this crate. Also Since my native language is not English my grammar and sentence order will not be perfect. So improving this by correcting these mistakes will help both me and the reader of the docs.

Check Contributing for more info about branches and code architecture.

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details