minicrossterm/README.md

296 lines
10 KiB
Markdown
Raw Normal View History

2018-11-26 01:29:27 +11:00
# Crossterm | cross-platform terminal manipulating library.
![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
[s1]: https://img.shields.io/crates/v/crossterm.svg
[l1]: https://crates.io/crates/crossterm
2018-06-14 05:07:25 +10:00
[s2]: https://img.shields.io/badge/license-MIT-blue.svg
[l2]: crossterm/LICENSE
[s3]: https://docs.rs/crossterm/badge.svg
[l3]: https://docs.rs/crossterm/
[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.
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](#tested-terminals) for more info)
2019-01-28 07:54:50 +11:00
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](https://crates.io/crates/crossterm_style)
- [Crossterm Input](https://crates.io/crates/crossterm_input)
- [Crossterm Screen](https://crates.io/crates/crossterm_screen)
- [Crossterm Cursor](https://crates.io/crates/crossterm_cursor)
- [Crossterm Terminal](https://crates.io/crates/crossterm_terminal)
## Table of contents:
- [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)
## 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](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.
Add the Crossterm package to your `Cargo.toml` file.
```
[dependencies]
crossterm = "0.6"
```
### Useful Links
- [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-09-23 06:55:30 +10:00
## 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 (Windows 10 and 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)
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.
2019-01-28 07:54:50 +11:00
### Crossterm Type | [see more](https://github.com/TimonPost/crossterm/blob/master/examples/crossterm.rs)
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
// 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](http://atcentra.com/crossterm/styling.html)
This module provides the functionalities to style the terminal.
2019-02-23 01:20:24 +11:00
2019-03-22 02:00:30 +11:00
First include those types:
```rust
use crossterm::{Colored, Color, Colorize, Styler, Attribute};
```
_style font with attributes_
```rust
// 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 styled_text = "Bold Underlined".bold().underlined();
println!("{}", styled_text);
// old-way but still usable
let styled_text = style("Bold Underlined").bold().underlined();
```
_style font 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 styled_text = "Bold Underlined".red().on_blue();
println!("{}", styled_text);
// old-way but still usable
let styled_text = style("Bold Underlined").with(Color::Red).on(Color::Blue);
```
_style font 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)
2019-03-22 02:00:30 +11:00
println!("{} some colored text", Colored::Fg(Color::AnsiValue(10)));
```
2019-03-22 02:00:30 +11:00
2019-01-28 07:54:50 +11:00
### Cursor | [see more](https://github.com/TimonPost/crossterm/blob/master/examples/cursor.rs)
This module provides the functionalities to work with the terminal cursor.
```rust
2018-08-22 02:05:53 +10:00
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)
```
2019-01-28 07:54:50 +11:00
### Terminal | [see more](https://github.com/TimonPost/crossterm/blob/master/examples/terminal.rs)
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.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](http://atcentra.com/crossterm/screen.html) topics about these subjects.
## Tested terminals
- Windows Powershell
- Windows 10 (pro)
- Windows CMD
- Windows 10 (pro)
2018-08-22 02:05:53 +10:00
- Windows 8.1 (N)
- Ubuntu Desktop Terminal
- Ubuntu 17.10
- (Arch, Manjaro) KDE Konsole
- Linux Mint
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.
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.
2019-02-23 01:20:24 +11:00
If there are any changes that will affect previous versions I will [describe](https://github.com/TimonPost/crossterm/blob/master/docs/UPGRADE.md) 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
2018-11-26 01:44:06 +11:00
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.
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.
## Authors
* **Timon Post** - *Project Owner & creator*
## License
2019-03-25 00:25:51 +11:00
This project, crossterm and all it's sub-modules: crossterm_screen, crossterm_cursor, crossterm_style, crossterm_input, crossterm_terminal, crossterm_winapi, crossterm_utils are licensed under the MIT License - see the [LICENSE.md](https://github.com/TimonPost/crossterm/blob/master/LICENSE) file for details