Crate crossterm

source ·
Expand description

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)

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.5.0"

And import the Crossterm modules you want to use.

extern crate crossterm;

// this module is used for styling the terminal
use crossterm::style::*;
// this module is used for cursor related actions
use crossterm::cursor::*;
// this module is used for terminal related actions
use crossterm::terminal::*;
// this module is used for input related actions
use crossterm::input::*;

Features

These are the features from this crate:

  • Cross-platform
  • Everything is multithreaded (Send, Sync)
  • Detailed documentation on every item
  • Very few dependenties.
  • Cursor.
    • Moving n times Up, Down, Left, Right
    • Goto a certain position
    • Get cursor position
    • 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)
  • Styled output
    • Foreground color (16 base colors)
    • Background color (16 base colors)
    • 256 color support (Windows 10 and UNIX only)
    • RGB support (Windows 10 and UNIX only)
    • Text Attributes like: bold, italic, underscore and crossed word ect (unix only)
  • Terminal
    • Clearing (all lines, current line, from cursor down and up, until new line)
    • Scrolling (Up, down)
    • Get the size of the terminal
    • Set the size of the terminal
    • Alternate screen
    • Raw screen
    • Exit the current process
  • Input
    • Read character
    • Read line
    • Read async
    • Read async until
    • Wait for key event (terminal pause)

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.

// screen wheron the `Crossterm` 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.

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

// attributes are only supported for UNIX terminals.
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();

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

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

// custom ansi color value
style("ANSI color value (50) ").with(Color::AnsiValue(50));

Cursor | see more

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

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.move_up(3);
cursor.move_right(3);
cursor.move_down(3);
cursor.move_left(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.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)

Input | see more

This module provides the functionalities to work with terminal input.

use crossterm::input;

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),
 }

Terminal | see more

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

use crossterm::terminal::{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.scroll_down(10);
terminal.scroll_up(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

  • Windows Powershell
    • Windows 10 (pro)
  • Windows CMD
    • Windows 10 (pro)
    • Windows 8.1 (N)
  • Ubuntu Desktop Terminal
    • Ubuntu 17.10
  • (Arch, Manjaro) KDE Konsole
  • Linux Mint

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.

  • Handling mouse events I want to be able to do something based on the clicks the user has done with its mouse.
  • Handling key events I want to be able to read key combination inputs.
  • Tests Find a way to test: color, alternate screen, rawscreen

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

  • Timon Post - Project Owner & creator

License

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

Modules

A module that contains all the actions related to the styling of the terminal. Like applying attributes to font and changing the foreground and background.
A module that contains all the actions related to cursor movement in the terminal. Like: moving the cursor position; saving and resetting the cursor position; hiding showing and control the blinking of the cursor.
A module that contains all the actions related to reading input from the terminal. Like reading a line, reading a character and reading asynchronously.
A module that provides a uniformed way to write to the output no matter if it is in main, alternate or raw mode.
A module that contains all the actions related to the styling of the terminal. Like applying attributes to font and changing the foreground and background.
A module that contains all the actions related to the terminal. like clearing, resizing, pausing and scrolling the terminal.

Macros

This macro will take an ANSI input and combines it with some default ANSI characters and returns the result

Structs

With this type you will be able to switch to alternate screen and back to main screen. Check also the Screen type for swishing to alternate mode.
This is a wrapper for reading from the input asynchronously. This wrapper has a channel receiver that receives the input from the user whenever it typed something. You only need to check whether there are new characters available.
This type could be used to access the cursor, terminal, color, input, styling module more easily. You need to pass a reference to the screen whereon you want to perform the actions to the Crossterm type.
This is a wrapper for a styled object on ‘alternate screen’ so that the styled object could be printed on the ‘alternate screen’ with the standard write functions in rust.
Struct that contains the style properties that can be applied to an displayable object.
This type represents a screen which could be in normal, raw and alternate modes.
Struct that contains both the style and the content wits can be styled.
Struct that stores a platform-specific platform implementation for terminal related actions.
Struct that stores a platform-specific implementation for color related actions.
Struct that stores a platform-specific implementation for cursor related actions.
Struct that stores a platform-specific implementation for input related actions.
Struct that is a handle to a terminal screen. This handle could be used to write to the current screen

Enums

Attributes that could be applied on some text. (*nix values)
Colors that are available for coloring the terminal font.
Color types that can be used to determine if the Color enum is a Fore- or Background Color.
This enum represents key events which could be caused by the user.

Functions

Get a TerminalColor implementation whereon color related actions can be performed.
Get a TerminalCursor instance whereon cursor related actions can be performed.
Get a TerminalInput instance whereon input related actions can be performed.
This could be used to style an Displayable type with colors and attributes.
Get a Terminal instance whereon terminal related actions could performed.