2018-11-26 01:29:27 +11:00
# Crossterm | cross-platform terminal manipulating library.
2018-11-26 00:17:11 +11:00
![Lines of Code][s7] [![Latest Version][s1]][l1] [![MIT][s2]][l2] [![docs][s3]][l3] ![Lines of Code][s6]
2018-07-15 07:37:21 +10:00
2018-11-26 00:17:11 +11:00
[s1]: https://img.shields.io/crates/v/crossterm.svg
[l1]: https://crates.io/crates/crossterm
2018-06-14 05:07:25 +10:00
2018-11-26 00:17:11 +11:00
[s2]: https://img.shields.io/badge/license-MIT-blue.svg
[l2]: ./LICENSE
[s3]: https://docs.rs/crossterm/badge.svg
[l3]: https://docs.rs/crossterm/
[s6]: https://tokei.rs/b1/github/TimonPost/crossterm?category=code
[s7]: https://travis-ci.org/TimonPost/crossterm.svg?branch=master
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.
2018-07-31 05:35:35 +10:00
Crossterm aims to be simple and easy to call in code.
2018-11-26 00:17:11 +11:00
Through the simplicity of Crossterm, you do not have to worry about the platform you are working with.
2018-07-31 05:35:35 +10:00
2018-11-26 00:17:11 +11:00
This crate supports all UNIX and windows terminals down to windows 7 (not all terminals are tested see [Tested Terminals ](#tested-terminals ) for more info)
2018-07-31 05:35:35 +10:00
## Table of contents:
2018-11-26 00:17:11 +11:00
- [Getting started ](#getting-started )
- [Useful links ](#useful-links )
- [Features ](#features )
- [Examples ](#examples )
- [Crossterm Wrapper ](#crossterm-type--see-more )
- [Styling ](#crossterm-type--see-more )
- [Cursor ](#cursor--see-more )
- [Input ](#input--see-more )
- [Terminal ](#terminal--see-more )
- [Tested Terminals ](#tested-terminals )
- [Notice ](#notice )
- [Todo ](#todo )
- [Contributing ](#contributing )
- [Authors ](#authors )
- [License ](#license )
2018-07-31 05:35:35 +10:00
## Getting Started
2019-01-04 03:39:00 +11:00
This documentation is only for Crossterm version `0.5.^` if you have an older version of Crossterm I suggest you check the [Upgrade Manual ](https://github.com/TimonPost/crossterm/blob/master/docs/UpgradeManual.md ). Also, check out the [examples ](https://github.com/TimonPost/crossterm/tree/master/examples ) folders with detailed examples for all functionality of this crate.
2018-07-31 05:35:35 +10:00
Add the Crossterm package to your `Cargo.toml` file.
```
[dependencies]
2019-01-04 06:54:03 +11:00
crossterm = "0.5.3"
2018-07-31 05:35:35 +10:00
```
And import the Crossterm modules you want to use.
```rust
extern crate crossterm;
// this module is used for styling the terminal
2018-11-26 00:17:11 +11:00
use crossterm::style::*;
2018-07-31 05:35:35 +10:00
// this module is used for cursor related actions
2018-11-26 00:17:11 +11:00
use crossterm::cursor::*;
2018-12-12 07:49:50 +11:00
// this module is used for terminal related actions
2018-11-26 00:17:11 +11:00
use crossterm::terminal::*;
2018-12-12 07:49:50 +11:00
// this module is used for input related actions
2018-11-26 00:17:11 +11:00
use crossterm::input::*;
2018-07-31 05:35:35 +10:00
```
2018-11-26 00:17:11 +11:00
### Useful Links
2018-07-31 05:35:35 +10:00
2018-11-26 00:17:11 +11:00
- [Book ](http://atcentra.com/crossterm/ )
- [Documentation ](https://docs.rs/crossterm/ )
- [Crates.io ](https://crates.io/crates/crossterm )
- [Program Examples ](https://github.com/TimonPost/crossterm/tree/master/examples/program_examples )
- [Examples ](https://github.com/TimonPost/crossterm/tree/master/examples )
2018-07-31 05:35:35 +10:00
2018-09-23 06:55:30 +10:00
## Features
2018-07-31 05:35:35 +10:00
These are the features from this crate:
2018-11-26 00:17:11 +11:00
- Cross-platform
- Everything is multithreaded (Send, Sync)
- Detailed documentation on every item
2018-12-11 23:54:15 +11:00
- Very few dependenties.
2018-07-31 05:35:35 +10:00
- Cursor.
2018-11-26 00:17:11 +11:00
- Moving _n_ times Up, Down, Left, Right
- Goto a certain position
2018-07-31 05:35:35 +10:00
- Get cursor position
2018-11-26 00:17:11 +11:00
- Storing the current cursor position and resetting to that stored cursor position later
- Hiding an showing the cursor
- Control over blinking of the terminal cursor (only some terminals are supporting this)
2018-07-31 05:35:35 +10:00
- Styled output
- Foreground color (16 base colors)
- Background color (16 base colors)
2018-11-26 00:46:08 +11:00
- 256 color support (Windows 10 and UNIX only)
- RGB support (Windows 10 and UNIX only)
2019-01-03 02:53:47 +11:00
- Text Attributes like: bold, italic, underscore and crossed word ect (Windows 10 and UNIX only)
2018-07-31 05:35:35 +10:00
- Terminal
- Clearing (all lines, current line, from cursor down and up, until new line)
- Scrolling (Up, down)
- Get the size of the terminal
2018-11-26 00:17:11 +11:00
- Set the size of the terminal
2018-07-31 05:35:35 +10:00
- Alternate screen
2018-11-26 00:17:11 +11:00
- Raw screen
- Exit the current process
2018-08-15 05:40:07 +10:00
- Input
- Read character
- Read line
- Read async
- Read async until
2018-11-26 00:17:11 +11:00
- Wait for key event (terminal pause)
2018-07-31 05:35:35 +10:00
2018-09-23 06:55:30 +10:00
## Examples
These are some basic examples demonstrating how to use this crate. See [examples ](https://github.com/TimonPost/crossterm/blob/master/examples/ ) for more.
2018-08-25 20:00:39 +10:00
### Crossterm Type | [see more](https://github.com/TimonPost/crossterm/blob/master/examples/some_types/mod.rs)
2018-08-15 05:40:07 +10:00
This is a wrapper for all the modules crossterm provides like terminal, cursor, styling and input.
2018-08-15 07:07:41 +10:00
```rust
2018-08-15 05:40:07 +10:00
// screen wheron the `Crossterm` methods will be executed.
2018-11-26 00:17:11 +11:00
let crossterm = Crossterm::new();
2018-07-31 05:35:35 +10:00
// 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();
2018-08-15 05:40:07 +10:00
// styling
2018-11-26 00:17:11 +11:00
println!("{}", crossterm.style("Black font on Green background color").with(Color::Black).on(Color::Green));
2018-08-15 05:40:07 +10:00
2018-07-31 05:35:35 +10:00
```
2018-11-26 00:17:11 +11:00
### Styled Font | [see more](http://atcentra.com/crossterm/styling.html)
This module provides the functionalities to style the terminal.
2018-07-31 05:35:35 +10:00
```rust
2018-08-15 05:40:07 +10:00
use crossterm::style::{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);
2019-01-04 06:54:03 +11:00
// styling font with (Windows 10 and UNIX systems)
2018-08-15 05:40:07 +10:00
let normal = style("Normal text");
let bold = style("Bold text").bold();
let italic = style("Italic text").italic();
let slow_blink = style("Slow blinking text").slow_blink();
let rapid_blink = style("Rapid blinking text").rapid_blink();
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 crossed_out = style("Crossed out font").crossed_out();
2019-01-03 02:53:47 +11:00
// paint styled text to screen (this could also be called inline)
2018-11-26 00:17:11 +11:00
println!("{}", style1);
println!("{}", style2);
println!("{}", bold);
println!("{}", hidden);
...
2018-08-15 05:40:07 +10:00
2019-01-04 06:54:03 +11:00
// custom rgb value (Windows 10 and UNIX systems)
2018-08-15 05:40:07 +10:00
style("RGB color (10,10,10) ").with(Color::Rgb {
r: 10,
g: 10,
b: 10
2018-11-26 00:17:11 +11:00
}));
2018-08-15 05:40:07 +10:00
2019-01-03 02:53:47 +11:00
// custom ansi color value (Windows 10 and UNIX systems)
2018-11-26 00:17:11 +11:00
style("ANSI color value (50) ").with(Color::AnsiValue(50));
2018-08-15 05:40:07 +10:00
2018-07-31 05:35:35 +10:00
```
2018-08-25 20:00:39 +10:00
### Cursor | [see more](https://github.com/TimonPost/crossterm/blob/master/examples/cursor/mod.rs)
2018-07-31 05:35:35 +10:00
This module provides the functionalities to work with the terminal cursor.
```rust
2018-08-22 02:05:53 +10:00
use crossterm::cursor;
2018-07-31 05:35:35 +10:00
2018-11-26 00:17:11 +11:00
let mut cursor = cursor();
2018-07-31 05:35:35 +10:00
2018-08-15 05:40:07 +10:00
/// Moving the cursor
2018-07-31 05:35:35 +10:00
// Set the cursor to position X: 10, Y: 5 in the terminal
cursor.goto(10,5);
2018-08-15 05:40:07 +10:00
// Move the cursor up,right,down,left 3 cells.
2018-07-31 05:35:35 +10:00
cursor.move_up(3);
cursor.move_right(3);
cursor.move_down(3);
cursor.move_left(3);
2018-08-15 05:40:07 +10:00
/// Safe the current cursor position to recall later
2018-07-31 05:35:35 +10:00
// Goto X: 5 Y: 5
cursor.goto(5,5);
// Safe cursor position: X: 5 Y: 5
cursor.save_position();
// 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.reset_position();
// 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)
```
2018-11-26 00:17:11 +11:00
### Input | [see more](http://atcentra.com/crossterm/input.html)
2018-08-22 02:05:53 +10:00
This module provides the functionalities to work with terminal input.
```rust
use crossterm::input;
2018-11-26 00:17:11 +11:00
let mut input = input();
2018-08-22 02:05:53 +10:00
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),
}
```
2018-08-25 20:00:39 +10:00
### Terminal | [see more](https://github.com/TimonPost/crossterm/blob/master/examples/terminal/terminal.rs)
2018-07-31 05:35:35 +10:00
This module provides the functionalities to work with the terminal in general.
```rust
use crossterm::terminal::{terminal,ClearType};
2018-11-26 00:17:11 +11:00
let mut terminal = terminal();
2018-07-31 05:35:35 +10:00
// 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
2018-08-15 05:40:07 +10:00
let (width, height) = terminal.terminal_size();
print!("X: {}, y: {}", width, height);
2018-07-31 05:35:35 +10:00
2018-08-15 05:40:07 +10:00
// Scroll down, up 10 lines.
2018-07-31 05:35:35 +10:00
terminal.scroll_down(10);
terminal.scroll_up(10);
2018-08-15 05:40:07 +10:00
// Set terminal size (width, height)
2018-07-31 05:35:35 +10:00
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");
```
2018-11-26 00:17:11 +11:00
### Alternate and Raw Screen
These concepts are a little more complex, please checkout the [book ](http://atcentra.com/crossterm/screen.html ) topics about these subjects.
2018-07-31 05:35:35 +10:00
## Tested terminals
- Windows Powershell
- Windows 10 (pro)
- Windows CMD
- Windows 10 (pro)
2018-08-22 02:05:53 +10:00
- Windows 8.1 (N)
2018-07-31 05:35:35 +10:00
- Ubuntu Desktop Terminal
- Ubuntu 17.10
2018-11-26 00:17:11 +11:00
- (Arch, Manjaro) KDE Konsole
- Linux Mint
2018-07-31 05:35:35 +10:00
2018-08-22 02:05:53 +10:00
This crate supports all Unix terminals and windows terminals down to Windows 7 but not all of them have been tested.
2018-07-31 05:35:35 +10:00
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
2019-01-04 06:54:03 +11:00
This library is quite stable now, changes could be expected but they will probably be not that big.
2018-11-26 00:17:11 +11:00
If there are any changes that will affect previous versions I will [describe ](https://github.com/TimonPost/crossterm/blob/master/docs/UpgradeManual.md ) what to change to upgrade.
2018-07-31 05:35:35 +10:00
## Todo
I still have some things in mind to implement.
2019-01-04 06:54:03 +11:00
- Handling mouse events:
2018-07-31 05:35:35 +10:00
I want to be able to do something based on the clicks the user has done with its mouse.
2019-01-04 06:54:03 +11:00
- Handling key events:
2018-07-31 05:35:35 +10:00
I want to be able to read key combination inputs.
2019-01-04 06:54:03 +11:00
- Tests:
2018-11-26 01:44:06 +11:00
Find a way to test: color, alternate screen, rawscreen
2018-07-31 05:35:35 +10:00
## Contributing
2018-08-15 05:40:07 +10:00
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.
2018-07-31 05:35:35 +10:00
2018-08-25 20:00:39 +10:00
Check [Contributing ](https://github.com/TimonPost/crossterm/blob/master/docs/Contributing.md ) for more info about branches and code architecture.
2018-07-31 05:35:35 +10:00
## Authors
* **Timon Post** - *Project Owner & creator*
## License
2018-08-30 06:15:22 +10:00
This project is licensed under the MIT License - see the [LICENSE.md ](https://github.com/TimonPost/crossterm/blob/master/LICENSE ) file for details