#terminal #clear #console #crossterm #size

crossterm_terminal

A cross-platform library for doing terminal related actions

8 releases

✓ Uses Rust 2018 edition

0.2.6 Aug 2, 2019
0.2.5 Jul 25, 2019
0.2.4 May 20, 2019
0.2.2 Apr 17, 2019
0.1.0 Jan 27, 2019

#27 in Command-line interface

Download history 264/week @ 2019-04-19 318/week @ 2019-04-26 286/week @ 2019-05-03 455/week @ 2019-05-10 442/week @ 2019-05-17 364/week @ 2019-05-24 275/week @ 2019-05-31 498/week @ 2019-06-07 359/week @ 2019-06-14 554/week @ 2019-06-21 780/week @ 2019-06-28 629/week @ 2019-07-05 721/week @ 2019-07-12 635/week @ 2019-07-19 710/week @ 2019-07-26

2,189 downloads per month
Used in 7 crates (5 directly)

MIT license

83KB
1.5K SLoC

Crossterm Terminal | cross-platform terminal actions.

Lines of Code Latest Version MIT docs Join us on Discord

This crate allows you to perform terminal related actions cross-platform e.g clearing, resizing etc. It supports all UNIX and windows terminals down to windows 7 (not all terminals are tested see Tested Terminals for more info)

This crate is a sub-crate of crossterm to perform terminal related actions, and can be use individually.

Other sub-crates are:

When you want to use other modules as well you might want to use crossterm with feature flags.

Table of contents:

Getting Started

This documentation is only for crossterm_terminal version 0.2 check the examples folders with detailed examples for all functionality of this crate.

Add the crossterm_terminal package to your Cargo.toml file.

[dependencies]
crossterm_terminal = "0.2"

And import the crossterm_terminal modules you want to use.

extern crate crossterm_terminal;

pub use crossterm_terminal::{terminal, Terminal, ClearType};

Useful Links

Features

These are the features of this crate:

  • Cross-platform
  • Multithreaded (send, sync)
  • Detailed Documentation
  • Few Dependencies
  • Terminal
    • Clearing (all lines, current line, from cursor down and up, until new line)
    • Scrolling (up, down)
    • Terminal Size (get/set)
    • Exit Current Process

Command API

My first recommendation is to use the command API because this might replace some of the existing API in the future. Because it is more convenient, faster, and easier to use.

Examples

The examples folder has more complete and verbose examples.

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

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.

Authors

  • Timon Post - Project Owner & creator

License

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

Dependencies

~544KB