Merge branch 'development' of https://github.com/TimonPost/crossterm into development
This commit is contained in:
commit
52c0bbcf36
@ -1,15 +1,15 @@
|
|||||||
Upgrade crossterm 0.2 to 0.2.1
|
Upgrade crossterm 0.2 to 0.2.1
|
||||||
|
|
||||||
Namespaces:
|
Namespaces:
|
||||||
I have changed the namespaces. I found the namsespaces to long so I have shoted them like the followin:
|
I have changed the namespaces. I found the namsespaces to long so I have shortened them like the following:
|
||||||
|
|
||||||
Old: crossterm::crosster_style
|
Old: crossterm::crossterm_style
|
||||||
New: crossterm::style
|
New: crossterm::style
|
||||||
|
|
||||||
Old: crossterm::crosster_terminal
|
Old: crossterm::crossterm_terminal
|
||||||
New: crossterm::terminal
|
New: crossterm::terminal
|
||||||
|
|
||||||
Old: crossterm::crosster_cursor
|
Old: crossterm::crossterm_cursor
|
||||||
New: crossterm::cursor
|
New: crossterm::cursor
|
||||||
|
|
||||||
Method names that changed [Issue 4](https://github.com/TimonPost/crossterm/issues/4):
|
Method names that changed [Issue 4](https://github.com/TimonPost/crossterm/issues/4):
|
||||||
|
174
examples/Crossterm 0.1.0/README.md
Normal file
174
examples/Crossterm 0.1.0/README.md
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
# Crossterm | crossplatform terminal library written in rust.
|
||||||
|
|
||||||
|
Ever got disappointed when a terminal library for rust was only written for unix systems? Crossterm provides the same terminal functionality for both windows and unix systems.
|
||||||
|
|
||||||
|
Crossterm aims to be simple and easy to call in code. True the simplicity of crossterm you do not have to worry about the platform your working with. You can just call some module and unther water it will check what to do based on the current platform.
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
Add the crossterm package to your `Cargo.toml` file.
|
||||||
|
|
||||||
|
```
|
||||||
|
[dependencies]
|
||||||
|
crossterm = "*"
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Add the crate to your solution.
|
||||||
|
And use the crossterm modules withs you want to use.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
// this module is used for styling the terminal
|
||||||
|
use self::crossterm::crossterm_style::*;
|
||||||
|
// this module is used for cursor related actions
|
||||||
|
use self::crossterm::crossterm_cursor::*;
|
||||||
|
// this mudule is used for terminal related actions
|
||||||
|
use self::crossterm::crossterm_terminal::*;
|
||||||
|
|
||||||
|
```
|
||||||
|
## Links
|
||||||
|
|
||||||
|
Documentation for the code can be found [here](https://atcentra.com/crossterm/index.html)
|
||||||
|
|
||||||
|
Documentation for the code can be found [here](https://docs.rs/crossterm/0.1.0/crossterm/)
|
||||||
|
|
||||||
|
The Cargo Page can be found [here](https://crates.io/search?q=crossterm)
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
For detailed examples of all crossterm functionalities check the [examples](https://github.com/TimonPost/crossterm/tree/master/examples) direcory.
|
||||||
|
|
||||||
|
### Styled font
|
||||||
|
```rust
|
||||||
|
use crossterm::crossterm_style::{paint, Color};
|
||||||
|
|
||||||
|
// Crossterm provides method chaining so that you can style the font nicely.
|
||||||
|
// You can either store the styled font.
|
||||||
|
let mut styledobject = paint("Stored styled font").with(Color::Red).on(Color::Blue);
|
||||||
|
println!("{}",styledobject);
|
||||||
|
|
||||||
|
// Or you can print it directly.
|
||||||
|
println!("{}", paint("Red font on blue background color").with(Color::Red).on(Color::Blue));
|
||||||
|
println!("{}", paint("Red font on default background color").with(Color::Red));
|
||||||
|
println!("{}", paint("Default font color on Blue background color").on(Color::Blue));
|
||||||
|
```
|
||||||
|
### Cursor
|
||||||
|
```rust
|
||||||
|
|
||||||
|
use crossterm::crossterm_cursor::get;
|
||||||
|
|
||||||
|
let mut cursor = get();
|
||||||
|
|
||||||
|
// Set the cursor to position X: 10, Y: 5 in the terminal
|
||||||
|
cursor.goto(10,5);
|
||||||
|
|
||||||
|
// Move the cursor to position 3 times to the up in the terminal
|
||||||
|
cursor.move_up(3);
|
||||||
|
|
||||||
|
// Move the cursor to position 3 times to the right in the terminal
|
||||||
|
cursor.move_right(3);
|
||||||
|
|
||||||
|
// Move the cursor to position 3 times to the down in the terminal
|
||||||
|
cursor.move_down(3);
|
||||||
|
|
||||||
|
// Move the cursor to position 3 times to the left in the terminal
|
||||||
|
cursor.move_left(3);
|
||||||
|
|
||||||
|
// Print an character at X: 10, Y: 5 (see examples for more explanation why to use this method).
|
||||||
|
// cursor.goto(10,5).print("@");
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Terminal
|
||||||
|
```rust
|
||||||
|
use crossterm::crossterm_terminal::{get,ClearType};
|
||||||
|
|
||||||
|
let mut cursor = get();
|
||||||
|
|
||||||
|
// 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 terminal_size = terminal.terminal_size().unwrap();
|
||||||
|
// Print results
|
||||||
|
print!("X: {}, y: {}", terminal_size.0, terminal_size.1);
|
||||||
|
|
||||||
|
// Scroll down 10 lines.
|
||||||
|
terminal.scroll_down(10);
|
||||||
|
|
||||||
|
// Scroll up 10 lines.
|
||||||
|
terminal.scroll_up(10);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Features crossterm 0.1
|
||||||
|
|
||||||
|
- Cursor movement.
|
||||||
|
- Up, Down, Left, Right.
|
||||||
|
- Goto an certain position.
|
||||||
|
- Styled output
|
||||||
|
- Foreground color (16 base colors)
|
||||||
|
- Background color (16 base colors)
|
||||||
|
- Terminal
|
||||||
|
- Clearing
|
||||||
|
- Scrolling
|
||||||
|
- Size
|
||||||
|
- Detailed documentation on every item.
|
||||||
|
- Examples for every client callable code.
|
||||||
|
|
||||||
|
## Tested terminals
|
||||||
|
|
||||||
|
- Windows Powershell
|
||||||
|
- Windows 10 (pro)
|
||||||
|
- Windows CMD
|
||||||
|
- Windows 10 (pro)
|
||||||
|
- Ubuntu Desktop Terminal
|
||||||
|
- Ubuntu 17.10
|
||||||
|
|
||||||
|
|
||||||
|
The above terminals have been tested. Crossterm should works also for windows 7, 8 consoles and all ansi supportable consoles.
|
||||||
|
But these are yet to be tested.
|
||||||
|
If you have used this library for an terminal other than the above list without issues feel free to add it to the above list.
|
||||||
|
|
||||||
|
|
||||||
|
## Notice
|
||||||
|
This library is not totally stable **yet**. There will not be changed mutch in the code design so do not worry to mutch. If there are any changes that affect previous versions I will describe what to change when upgrading crossterm to new version.
|
||||||
|
|
||||||
|
## Todo features crossterm 0.2
|
||||||
|
|
||||||
|
- Handling mouse events
|
||||||
|
- Inplementing 256 colors for terminals that support those colors.
|
||||||
|
- Handling key events
|
||||||
|
- Tests
|
||||||
|
- Storing and resetting cursor position.
|
||||||
|
- Text Attributes like: bold,italic, undescore and crossed word.
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
If you would like to contribute to crossterm, than please design the code as it is now. Each module contains the same structures so we can easely extend to multible platforms. As you study the code you will quiqly see what the architecture is. Maybe later there will be an documentation for how crossterm is design.
|
||||||
|
|
||||||
|
## Versioning
|
||||||
|
|
||||||
|
The current version is crossterm 0.1, every commit I merge the version go's up like 0.1.0 -> 0.1.1 -> 0.1.2.
|
||||||
|
|
||||||
|
When new features arrives the packages will go up like 0.1 -> 0.2 -> 0.3
|
||||||
|
|
||||||
|
## Authors
|
||||||
|
|
||||||
|
* **Timon Post** - *Project Owner & creator*
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
|
||||||
|
|
||||||
|
|
||||||
|
|
15
examples/Crossterm 0.1.0/bin.rs
Normal file
15
examples/Crossterm 0.1.0/bin.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
use self::crossterm::crossterm_style::*;
|
||||||
|
use self::crossterm::crossterm_cursor;
|
||||||
|
use self::crossterm::crossterm_terminal::*;
|
||||||
|
|
||||||
|
use std::io::{stdin, stdout, Write};
|
||||||
|
|
||||||
|
pub mod color;
|
||||||
|
pub mod cursor;
|
||||||
|
pub mod terminal;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
|
||||||
|
}
|
100
examples/Crossterm 0.1.0/color/mod.rs
Normal file
100
examples/Crossterm 0.1.0/color/mod.rs
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
//!
|
||||||
|
//! Color Examples
|
||||||
|
//!
|
||||||
|
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
use self::crossterm::crossterm_style::{paint, Color};
|
||||||
|
|
||||||
|
/// print some red font | demonstration.
|
||||||
|
pub fn paint_foreground()
|
||||||
|
{
|
||||||
|
// Pass an string to the `paint()` method with you want to paint.
|
||||||
|
// This will give you an object back wits can be styled and displayed.
|
||||||
|
let mut styledobject = paint("Red font");
|
||||||
|
// Call the method `with()` on the object given by `paint()` and pass in any Color from the Color enum.
|
||||||
|
styledobject = styledobject.with(Color::Red);
|
||||||
|
// Print the object to the console and see the result.
|
||||||
|
println!("{}", styledobject);
|
||||||
|
|
||||||
|
// Crossterm provides method chaining so that the above points can be inlined.
|
||||||
|
println!("{}", paint("Red font").with(Color::Red));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// print some font on red background | demonstration.
|
||||||
|
pub fn paint_background()
|
||||||
|
{
|
||||||
|
// Pass an string to the `paint()` method with you want to paint.
|
||||||
|
// This will give you an object back wits can be styled and displayed.
|
||||||
|
let mut styledobject = paint("Red background color");
|
||||||
|
// Call the method `on()` on the object given by `paint()` and pass in an Color from the Color enum.
|
||||||
|
styledobject = styledobject.on(Color::Blue);
|
||||||
|
// Print the object to the console and check see the result
|
||||||
|
println!("{}", styledobject);
|
||||||
|
|
||||||
|
// Crossterm provides method chaining so that the above points can be inlined.
|
||||||
|
println!("{}", paint("Red background color").on(Color::Red));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// print font with fore- background color | demonstration.
|
||||||
|
pub fn paint_foreground_and_background()
|
||||||
|
{
|
||||||
|
// Pass an string to the `paint()` method with you want to paint.
|
||||||
|
// This will give you an object back wits can be styled and displayed.
|
||||||
|
let mut styledobject = paint("Red font on blue background color");
|
||||||
|
/* Foreground color:
|
||||||
|
Call the method `with()` on the object given by `paint()`
|
||||||
|
Pass in an Color from the Color enum.
|
||||||
|
*/
|
||||||
|
styledobject = styledobject.with(Color::Red);
|
||||||
|
/* Background color:
|
||||||
|
Call the method `on()` on the object given by `paint()`
|
||||||
|
Pass in an Color from the Color enum.
|
||||||
|
*/
|
||||||
|
styledobject = styledobject.on(Color::Blue);
|
||||||
|
// Print the object to the console and see the result.
|
||||||
|
println!("{}", styledobject);
|
||||||
|
|
||||||
|
// Crossterm provides method chaining so that the above points can be inlined.
|
||||||
|
println!("{}", paint("Red font on blue background color").with(Color::Red).on(Color::Blue));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print all available foreground colors | demonstration.
|
||||||
|
pub fn print_all_foreground_colors()
|
||||||
|
{
|
||||||
|
println!("Black : \t {}", paint("■").with(Color::Black));
|
||||||
|
println!("Red : \t\t {}", paint("■").with(Color::Red));
|
||||||
|
println!("Dark Red: \t {}", paint("■").with(Color::DarkRed));
|
||||||
|
println!("Green : \t {}", paint("■").with(Color::Green));
|
||||||
|
println!("Dark Green : \t {}", paint("■").with(Color::DarkGreen));
|
||||||
|
println!("Yellow : \t {}", paint("■").with(Color::Yellow));
|
||||||
|
println!("Dark Yellow : \t {}", paint("■").with(Color::DarkYellow));
|
||||||
|
println!("Blue : \t\t {}", paint("■").with(Color::Blue));
|
||||||
|
println!("Dark Blue : \t {}", paint("■").with(Color::DarkBlue));
|
||||||
|
println!("Magenta : \t {}", paint("■").with(Color::Magenta));
|
||||||
|
println!("Dark Magenta : \t {}", paint("■").with(Color::DarkMagenta));
|
||||||
|
println!("Cyan : \t\t {}", paint("■").with(Color::Cyan));
|
||||||
|
println!("Dark Cyan : \t {}", paint("■").with(Color::DarkCyan));
|
||||||
|
println!("Grey : \t\t {}", paint("■").with(Color::Grey));
|
||||||
|
println!("White : \t {}", paint("■").with(Color::White));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print all available foreground colors | demonstration.
|
||||||
|
pub fn print_all_background_colors()
|
||||||
|
{
|
||||||
|
println!("Black : \t {}", paint(" ").on(Color::Black));
|
||||||
|
println!("Red : \t\t {}", paint(" ").on(Color::Red));
|
||||||
|
println!("Dark Red: \t {}", paint(" ").on(Color::DarkRed));
|
||||||
|
println!("Green : \t {}", paint(" ").on(Color::Green));
|
||||||
|
println!("Dark Green : \t {}", paint(" ").on(Color::DarkGreen));
|
||||||
|
println!("Yellow : \t {}", paint(" ").on(Color::Yellow));
|
||||||
|
println!("Dark Yellow : \t {}", paint(" ").on(Color::DarkYellow));
|
||||||
|
println!("Blue : \t\t {}", paint(" ").on(Color::Blue));
|
||||||
|
println!("Dark Blue : \t {}", paint(" ").on(Color::DarkBlue));
|
||||||
|
println!("Magenta : \t {}", paint(" ").on(Color::Magenta));
|
||||||
|
println!("Dark Magenta : \t {}", paint(" ").on(Color::DarkMagenta));
|
||||||
|
println!("Cyan : \t\t {}", paint(" ").on(Color::Cyan));
|
||||||
|
println!("Dark Cyan : \t {}", paint(" ").on(Color::DarkCyan));
|
||||||
|
println!("Grey : \t\t {}", paint(" ").on(Color::Grey));
|
||||||
|
println!("White : \t {}", paint(" ").on(Color::White));
|
||||||
|
}
|
98
examples/Crossterm 0.1.0/cursor/mod.rs
Normal file
98
examples/Crossterm 0.1.0/cursor/mod.rs
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
//!
|
||||||
|
//! Cursor Examples
|
||||||
|
//!
|
||||||
|
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
use self::crossterm::crossterm_cursor::{get, TerminalCursor};
|
||||||
|
|
||||||
|
/// Set the cursor to position X: 10, Y: 5 in the terminal.
|
||||||
|
pub fn goto()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = get();
|
||||||
|
// Set the cursor to position X: 10, Y: 5 in the terminal
|
||||||
|
cursor.goto(10,5);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move the cursor 3 up | demonstration.
|
||||||
|
pub fn move_up()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = get();
|
||||||
|
// Move the cursor to position 3 times to the up in the terminal
|
||||||
|
cursor.move_up(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move the cursor 3 to the right | demonstration.
|
||||||
|
pub fn move_right()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = get();
|
||||||
|
// Move the cursor to position 3 times to the right in the terminal
|
||||||
|
cursor.move_right(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move the cursor 3 down | demonstration.
|
||||||
|
pub fn move_down()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = get();
|
||||||
|
// Move the cursor to position 3 times to the down in the terminal
|
||||||
|
cursor.move_down(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move the cursor 3 to the left | demonstration.
|
||||||
|
pub fn move_left()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = get();
|
||||||
|
// Move the cursor to position 3 times to the left in the terminal
|
||||||
|
cursor.move_left(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print character at X: 10 Y: 5 | demonstration.
|
||||||
|
pub fn print()
|
||||||
|
{
|
||||||
|
// To print an some displayable content on an certain position.
|
||||||
|
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = get();
|
||||||
|
// Set the cursor to position X: 10, Y: 5 in the terminal
|
||||||
|
cursor.goto(10,5);
|
||||||
|
// Print the @ symbol at position X: 10, Y: 5 in the terminal
|
||||||
|
print!("@");
|
||||||
|
// Rust is line buffered inorder to print at an certain position we need to clear the buffer first.
|
||||||
|
use std;
|
||||||
|
use std::io::Write;
|
||||||
|
std::io::stdout().flush();
|
||||||
|
|
||||||
|
/* Because the above method is a little to mutch code,
|
||||||
|
you can use the `print()` method for printing an value at an certain position in the terminal.
|
||||||
|
|
||||||
|
Crossterm provides method chaining so that the above points can be inlined.
|
||||||
|
*/
|
||||||
|
|
||||||
|
get().goto(10,5).print("@");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
118
examples/Crossterm 0.1.0/terminal/mod.rs
Normal file
118
examples/Crossterm 0.1.0/terminal/mod.rs
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
//!
|
||||||
|
//! Terminal Examples
|
||||||
|
//!
|
||||||
|
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
use crossterm::crossterm_terminal::{get, Terminal, ClearType};
|
||||||
|
use crossterm::crossterm_cursor;
|
||||||
|
|
||||||
|
fn print_test_data()
|
||||||
|
{
|
||||||
|
for i in 0..100 {
|
||||||
|
println!("abcdefghijTest data to test terminal: {}",i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines in terminal | demonstration
|
||||||
|
pub fn clear_all_lines()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Clear all lines in terminal;
|
||||||
|
terminal.clear(ClearType::All);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines from cursor position X:4, Y:4 down | demonstration
|
||||||
|
pub fn clear_from_cursor_down()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Set terminal cursor position (see example for more info).
|
||||||
|
crossterm_cursor::get().goto(4,8);
|
||||||
|
|
||||||
|
// Clear all cells from current cursor position down.
|
||||||
|
terminal.clear(ClearType::FromCursorDown);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
||||||
|
pub fn clear_from_cursor_up()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Set terminal cursor position (see example for more info).
|
||||||
|
crossterm_cursor::get().goto(4,8);
|
||||||
|
|
||||||
|
// Clear all cells from current cursor position down.
|
||||||
|
terminal.clear(ClearType::FromCursorUp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
||||||
|
pub fn clear_current_line()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Set terminal cursor position (see example for more info).
|
||||||
|
crossterm_cursor::get().goto(4,4);
|
||||||
|
|
||||||
|
// Clear current line cells.
|
||||||
|
terminal.clear(ClearType::CurrentLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
||||||
|
pub fn clear_until_new_line()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Set terminal cursor position (see example for more info).
|
||||||
|
crossterm_cursor::get().goto(4,4);
|
||||||
|
|
||||||
|
// Clear all the cells until next line.
|
||||||
|
terminal.clear(ClearType::UntilNewLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_terminal_size()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
// Get terminal size
|
||||||
|
let terminal_size = terminal.terminal_size().unwrap();
|
||||||
|
// Print results
|
||||||
|
print!("X: {}, y: {}", terminal_size.0, terminal_size.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// scroll down 10 lines
|
||||||
|
pub fn scroll_down()
|
||||||
|
{
|
||||||
|
print_test_data();
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
// Scroll down 10 lines.
|
||||||
|
let terminal_size = terminal.scroll_down(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// scroll down 10 lines
|
||||||
|
pub fn scroll_up()
|
||||||
|
{
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
// Scroll up 10 lines.
|
||||||
|
let terminal_size = terminal.scroll_up(10);
|
||||||
|
}
|
217
examples/Crossterm 0.2.0/README.md
Normal file
217
examples/Crossterm 0.2.0/README.md
Normal file
@ -0,0 +1,217 @@
|
|||||||
|
# Crossterm | crossplatform terminal library written in rust.
|
||||||
|
|
||||||
|
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. True the simplicity of crossterm you do not have to worry about the platform your working with. You can just call the action you want to preform and unther water it will check what to do based on the current platform.
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
Add the crossterm package to your `Cargo.toml` file.
|
||||||
|
|
||||||
|
```
|
||||||
|
[dependencies]
|
||||||
|
crossterm = "*"
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Add the crate to your solution.
|
||||||
|
And use the crossterm modules withs you want to use.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
// this module is used for styling the terminal
|
||||||
|
use self::crossterm::crossterm_style::*;
|
||||||
|
// this module is used for cursor related actions
|
||||||
|
use self::crossterm::crossterm_cursor::*;
|
||||||
|
// this mudule is used for terminal related actions
|
||||||
|
use self::crossterm::crossterm_terminal::*;
|
||||||
|
|
||||||
|
```
|
||||||
|
## Links
|
||||||
|
|
||||||
|
Documentation version 0.1.0 can be found [here](https://docs.rs/crossterm/0.1.0/crossterm/)
|
||||||
|
Documentation version 0.2.0 can be found [here](https://docs.rs/crossterm/0.2.0/crossterm/)
|
||||||
|
The Cargo Page can be found [here](https://crates.io/search?q=crossterm)
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
For detailed examples of all crossterm functionalities check the [examples](https://github.com/TimonPost/crossterm/tree/master/examples) direcory.
|
||||||
|
|
||||||
|
### Styled font
|
||||||
|
```rust
|
||||||
|
use crossterm::crossterm_style::{paint, Color};
|
||||||
|
|
||||||
|
// Crossterm provides method chaining so that you can style the font nicely.
|
||||||
|
// the `with()` methods sets the foreground color and the `on()` methods sets the background color
|
||||||
|
// You can either store the styled font.
|
||||||
|
let mut styledobject = paint("Stored styled font").with(Color::Red).on(Color::Blue);
|
||||||
|
println!("{}",styledobject);
|
||||||
|
|
||||||
|
// Or you can print it directly.
|
||||||
|
println!("{}", paint("Red font on blue background color").with(Color::Red).on(Color::Blue));
|
||||||
|
println!("{}", paint("Red font on default background color").with(Color::Red));
|
||||||
|
println!("{}", paint("Default font color on Blue background color").on(Color::Blue));
|
||||||
|
|
||||||
|
/// The following code can only be used for unix systems:
|
||||||
|
// Set background Color from RGB
|
||||||
|
println!("RGB (10,10,10): \t {}", paint(" ").on(Color::Rgb {r: 10, g: 10, b: 10}));
|
||||||
|
// Set background Color from RGB
|
||||||
|
println!("ANSI value (50): \t {}", paint(" ").on(Color::AnsiValue(50)));
|
||||||
|
|
||||||
|
// Use attributes to syle the font.
|
||||||
|
println!("{}", paint("Normal text"));
|
||||||
|
println!("{}", paint("Bold text").bold());
|
||||||
|
println!("{}", paint("Italic text").italic());
|
||||||
|
println!("{}", paint("Slow blinking text").slow_blink());
|
||||||
|
println!("{}", paint("Rapid blinking text").rapid_blink());
|
||||||
|
println!("{}", paint("Hidden text").hidden());
|
||||||
|
println!("{}", paint("Underlined text").underlined());
|
||||||
|
println!("{}", paint("Reversed color").reverse());
|
||||||
|
println!("{}", paint("Dim text color").dim());
|
||||||
|
println!("{}", paint("Crossed out font").crossed_out());
|
||||||
|
|
||||||
|
```
|
||||||
|
### Cursor
|
||||||
|
```rust
|
||||||
|
|
||||||
|
use crossterm::crossterm_cursor::get;
|
||||||
|
|
||||||
|
let mut cursor = get();
|
||||||
|
|
||||||
|
/// Moving the cursor
|
||||||
|
// Set the cursor to position X: 10, Y: 5 in the terminal
|
||||||
|
cursor.goto(10,5);
|
||||||
|
|
||||||
|
// Move the cursor to position 3 times to the up in the terminal
|
||||||
|
cursor.move_up(3);
|
||||||
|
|
||||||
|
// Move the cursor to position 3 times to the right in the terminal
|
||||||
|
cursor.move_right(3);
|
||||||
|
|
||||||
|
// Move the cursor to position 3 times to the down in the terminal
|
||||||
|
cursor.move_down(3);
|
||||||
|
|
||||||
|
// Move the cursor to position 3 times to the left in the terminal
|
||||||
|
cursor.move_left(3);
|
||||||
|
|
||||||
|
// Print an character at X: 10, Y: 5 (see examples for more explanation why to use this method).
|
||||||
|
// cursor.goto(10,5).print("@");
|
||||||
|
|
||||||
|
/// 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.safe_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");
|
||||||
|
```
|
||||||
|
|
||||||
|
### Terminal
|
||||||
|
```rust
|
||||||
|
use crossterm::crossterm_terminal::{get,ClearType};
|
||||||
|
|
||||||
|
let mut terminal = get();
|
||||||
|
|
||||||
|
// 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 terminal_size = terminal.terminal_size().unwrap();
|
||||||
|
// Print results
|
||||||
|
print!("X: {}, y: {}", terminal_size.0, terminal_size.1);
|
||||||
|
|
||||||
|
// Scroll down 10 lines.
|
||||||
|
terminal.scroll_down(10);
|
||||||
|
|
||||||
|
// Scroll up 10 lines.
|
||||||
|
terminal.scroll_up(10);
|
||||||
|
|
||||||
|
// Set terminal size
|
||||||
|
terminal.set_size(10,10);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Features crossterm 0.1
|
||||||
|
|
||||||
|
- Cursor movement.
|
||||||
|
- Up, Down, Left, Right.
|
||||||
|
- Goto an certain position.
|
||||||
|
- Styled output
|
||||||
|
- Foreground color (16 base colors)
|
||||||
|
- Background color (16 base colors)
|
||||||
|
- Terminal
|
||||||
|
- Clearing
|
||||||
|
- Scrolling
|
||||||
|
- Size
|
||||||
|
- Detailed documentation on every item.
|
||||||
|
- Examples for every client callable code.
|
||||||
|
|
||||||
|
## Features crossterm 0.2
|
||||||
|
|
||||||
|
- 256 color support.
|
||||||
|
- Text Attributes like: bold, italic, underscore and crossed word ect.
|
||||||
|
- Custom ANSI color code input to set fore- and background color for unix.
|
||||||
|
- Storing the current cursor position and resetting to that stored cursor position later.
|
||||||
|
- Resizing the terminal.
|
||||||
|
|
||||||
|
## Tested terminals
|
||||||
|
|
||||||
|
- Windows Powershell
|
||||||
|
- Windows 10 (pro)
|
||||||
|
- Windows CMD
|
||||||
|
- Windows 10 (pro)
|
||||||
|
- Ubuntu Desktop Terminal
|
||||||
|
- Ubuntu 17.10
|
||||||
|
|
||||||
|
|
||||||
|
The above terminals have been tested. Crossterm should works also for windows 7, 8 consoles and all ansi supportable consoles.
|
||||||
|
But these are yet to be tested.
|
||||||
|
If you have used this library for an terminal other than the above list without issues feel free to add it to the above list.
|
||||||
|
|
||||||
|
## How it works
|
||||||
|
|
||||||
|
Crossterm is using `WINAPI` for windows systems and `ANSI escape codes` for unix systems. Crossterm provides one base trait with can be implemented for a platform specific instance. For example, there is an implementation for windows (`WINAPI`) and unix(`ANSI`) for the `cursor module`. To call the platform specific implementation there is one module that rules them all. Thrue this module the client calls some action and the module will deside what to do based on the current platform. And it will execute that action.
|
||||||
|
|
||||||
|
## Notice
|
||||||
|
This library is library is stable. There will not be changed mutch in the code design so do not worry to mutch. If there are any changes that affect previous versions I will describe what to change when upgrading crossterm to an newer version.
|
||||||
|
|
||||||
|
## Todo
|
||||||
|
|
||||||
|
- Handling mouse events
|
||||||
|
- Handling key events
|
||||||
|
- Tests
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
If you would like to contribute to crossterm, than please design the code as it is now. Each module contains the same structures so we can easely extend to multible platforms. As you study the code you will quiqly see what the architecture is. Maybe later there will be an documentation for how crossterm is design.
|
||||||
|
|
||||||
|
## Versioning
|
||||||
|
|
||||||
|
The current version is crossterm 0.1, every commit I merge the version go's up like 0.1.0 -> 0.1.1 -> 0.1.2.
|
||||||
|
|
||||||
|
When new features arrives the packages will go up like 0.1 -> 0.2 -> 0.3
|
||||||
|
|
||||||
|
## Authors
|
||||||
|
|
||||||
|
* **Timon Post** - *Project Owner & creator*
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
|
||||||
|
|
||||||
|
|
||||||
|
|
29
examples/Crossterm 0.2.0/bin.rs
Normal file
29
examples/Crossterm 0.2.0/bin.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
//! This bin folder can be used to try the examples out located in the examples directory.
|
||||||
|
//!
|
||||||
|
//! All you need to do is:
|
||||||
|
//!
|
||||||
|
//! - Download the crossterm source code.
|
||||||
|
//! - Add this in the Cargo.toml file:
|
||||||
|
//! ``` [[bin]]
|
||||||
|
//! name = "example_bin"
|
||||||
|
//! path = "./examples/bin.rs"
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! - Run program with: `cargo run`
|
||||||
|
|
||||||
|
// Import crossterm crate.
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
// Add the usings for the crossterms modules to play with crossterm
|
||||||
|
use self::crossterm::crossterm_style;
|
||||||
|
use self::crossterm::crossterm_cursor;
|
||||||
|
use self::crossterm::crossterm_terminal;
|
||||||
|
|
||||||
|
// Import the example modules.
|
||||||
|
pub mod color;
|
||||||
|
pub mod cursor;
|
||||||
|
pub mod terminal;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
|
||||||
|
}
|
133
examples/Crossterm 0.2.0/color/mod.rs
Normal file
133
examples/Crossterm 0.2.0/color/mod.rs
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
//!
|
||||||
|
//! Color Examples
|
||||||
|
//!
|
||||||
|
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
use self::crossterm::crossterm_style::{paint, Color};
|
||||||
|
|
||||||
|
/// print some red font | demonstration.
|
||||||
|
pub fn paint_foreground()
|
||||||
|
{
|
||||||
|
// Pass an string to the `paint()` method with you want to paint.
|
||||||
|
// This will give you an object back wits can be styled and displayed.
|
||||||
|
let mut styledobject = paint("Red font");
|
||||||
|
// Call the method `with()` on the object given by `paint()` and pass in any Color from the Color enum.
|
||||||
|
styledobject = styledobject.with(Color::Red);
|
||||||
|
// Print the object to the console and see the result.
|
||||||
|
println!("{}", styledobject);
|
||||||
|
|
||||||
|
// Crossterm provides method chaining so that the above points can be inlined.
|
||||||
|
println!("{}", paint("Red font").with(Color::Red));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// print some font on red background | demonstration.
|
||||||
|
pub fn paint_background()
|
||||||
|
{
|
||||||
|
// Pass an string to the `paint()` method with you want to paint.
|
||||||
|
// This will give you an object back wits can be styled and displayed.
|
||||||
|
let mut styledobject = paint("Red background color");
|
||||||
|
// Call the method `on()` on the object given by `paint()` and pass in an Color from the Color enum.
|
||||||
|
styledobject = styledobject.on(Color::Red);
|
||||||
|
// Print the object to the console and check see the result
|
||||||
|
println!("{}", styledobject);
|
||||||
|
|
||||||
|
// Crossterm provides method chaining so that the above points can be inlined.
|
||||||
|
println!("{}", paint("Red background color").on(Color::Red));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// print font with fore- background color | demonstration.
|
||||||
|
pub fn paint_foreground_and_background()
|
||||||
|
{
|
||||||
|
// Pass an string to the `paint()` method with you want to paint.
|
||||||
|
// This will give you an object back wits can be styled and displayed.
|
||||||
|
let mut styledobject = paint("Red font on blue background color");
|
||||||
|
/* Foreground color:
|
||||||
|
Call the method `with()` on the object given by `paint()`
|
||||||
|
Pass in an Color from the Color enum.
|
||||||
|
*/
|
||||||
|
styledobject = styledobject.with(Color::Red);
|
||||||
|
/* Background color:
|
||||||
|
Call the method `on()` on the object given by `paint()`
|
||||||
|
Pass in an Color from the Color enum.
|
||||||
|
*/
|
||||||
|
styledobject = styledobject.on(Color::Blue);
|
||||||
|
// Print the object to the console and see the result.
|
||||||
|
println!("{}", styledobject);
|
||||||
|
|
||||||
|
// Crossterm provides method chaining so that the above points can be inlined.
|
||||||
|
println!("{}", paint("Red font on blue background color").with(Color::Red).on(Color::Blue));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print all available foreground colors | demonstration.
|
||||||
|
pub fn print_all_foreground_colors()
|
||||||
|
{
|
||||||
|
println!("Black : \t {}", paint("■").with(Color::Black));
|
||||||
|
println!("Red : \t\t {}", paint("■").with(Color::Red));
|
||||||
|
println!("Dark Red: \t {}", paint("■").with(Color::DarkRed));
|
||||||
|
println!("Green : \t {}", paint("■").with(Color::Green));
|
||||||
|
println!("Dark Green : \t {}", paint("■").with(Color::DarkGreen));
|
||||||
|
println!("Yellow : \t {}", paint("■").with(Color::Yellow));
|
||||||
|
println!("Dark Yellow : \t {}", paint("■").with(Color::DarkYellow));
|
||||||
|
println!("Blue : \t\t {}", paint("■").with(Color::Blue));
|
||||||
|
println!("Dark Blue : \t {}", paint("■").with(Color::DarkBlue));
|
||||||
|
println!("Magenta : \t {}", paint("■").with(Color::Magenta));
|
||||||
|
println!("Dark Magenta : \t {}", paint("■").with(Color::DarkMagenta));
|
||||||
|
println!("Cyan : \t\t {}", paint("■").with(Color::Cyan));
|
||||||
|
println!("Dark Cyan : \t {}", paint("■").with(Color::DarkCyan));
|
||||||
|
println!("Grey : \t\t {}", paint("■").with(Color::Grey));
|
||||||
|
println!("White : \t {}", paint("■").with(Color::White));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print all available foreground colors | demonstration.
|
||||||
|
pub fn print_all_background_colors()
|
||||||
|
{
|
||||||
|
println!("Black : \t {}", paint(" ").on(Color::Black));
|
||||||
|
println!("Red : \t\t {}", paint(" ").on(Color::Red));
|
||||||
|
println!("Dark Red: \t {}", paint(" ").on(Color::DarkRed));
|
||||||
|
println!("Green : \t {}", paint(" ").on(Color::Green));
|
||||||
|
println!("Dark Green : \t {}", paint(" ").on(Color::DarkGreen));
|
||||||
|
println!("Yellow : \t {}", paint(" ").on(Color::Yellow));
|
||||||
|
println!("Dark Yellow : \t {}", paint(" ").on(Color::DarkYellow));
|
||||||
|
println!("Blue : \t\t {}", paint(" ").on(Color::Blue));
|
||||||
|
println!("Dark Blue : \t {}", paint(" ").on(Color::DarkBlue));
|
||||||
|
println!("Magenta : \t {}", paint(" ").on(Color::Magenta));
|
||||||
|
println!("Dark Magenta : \t {}", paint(" ").on(Color::DarkMagenta));
|
||||||
|
println!("Cyan : \t\t {}", paint(" ").on(Color::Cyan));
|
||||||
|
println!("Dark Cyan : \t {}", paint(" ").on(Color::DarkCyan));
|
||||||
|
println!("Grey : \t\t {}", paint(" ").on(Color::Grey));
|
||||||
|
println!("White : \t {}", paint(" ").on(Color::White));
|
||||||
|
#[cfg(unix)]
|
||||||
|
println!("RGB (10,10,10): \t {}", paint(" ").on(Color::Rgb {r: 10, g: 10, b: 10}));
|
||||||
|
#[cfg(unix)]
|
||||||
|
println!("RGB (10,10,10): \t {}", paint(" ").on(Color::AnsiValue(50)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print font with all available attributes. Note that this can only be used at unix systems and that some are not supported widely.
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub fn print_font_with_attributes()
|
||||||
|
{
|
||||||
|
println!("{}", paint("Normal text"));
|
||||||
|
println!("{}", paint("Bold text").bold());
|
||||||
|
println!("{}", paint("Italic text").italic());
|
||||||
|
println!("{}", paint("Slow blinking text").slow_blink());
|
||||||
|
println!("{}", paint("Rapid blinking text").rapid_blink());
|
||||||
|
println!("{}", paint("Hidden text").hidden());
|
||||||
|
println!("{}", paint("Underlined text").underlined());
|
||||||
|
println!("{}", paint("Reversed color").reverse());
|
||||||
|
println!("{}", paint("Dim text color").dim());
|
||||||
|
println!("{}", paint("Crossed out font").crossed_out());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print all supported rgb colors
|
||||||
|
#[cfg(unix)]#[cfg(unix)]
|
||||||
|
pub fn print_supported_colors()
|
||||||
|
{
|
||||||
|
let count = crossterm::crossterm_style::get().get_available_color_count().unwrap();
|
||||||
|
|
||||||
|
for i in 0..count
|
||||||
|
{
|
||||||
|
println!("{}", paint(format!("Color: {}",i)).with(Color::AnsiValue(i as u8)));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
119
examples/Crossterm 0.2.0/cursor/mod.rs
Normal file
119
examples/Crossterm 0.2.0/cursor/mod.rs
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
//!
|
||||||
|
//! Cursor Examples
|
||||||
|
//!
|
||||||
|
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
use self::crossterm::crossterm_cursor::{get, TerminalCursor};
|
||||||
|
|
||||||
|
/// Set the cursor to position X: 10, Y: 5 in the terminal.
|
||||||
|
pub fn goto()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = get();
|
||||||
|
// Set the cursor to position X: 10, Y: 5 in the terminal
|
||||||
|
cursor.goto(10,5);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move the cursor 3 up | demonstration.
|
||||||
|
pub fn move_up()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = get();
|
||||||
|
// Move the cursor to position 3 times to the up in the terminal
|
||||||
|
cursor.move_up(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move the cursor 3 to the right | demonstration.
|
||||||
|
pub fn move_right()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = get();
|
||||||
|
// Move the cursor to position 3 times to the right in the terminal
|
||||||
|
cursor.move_right(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move the cursor 3 down | demonstration.
|
||||||
|
pub fn move_down()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = get();
|
||||||
|
// Move the cursor to position 3 times to the down in the terminal
|
||||||
|
cursor.move_down(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move the cursor 3 to the left | demonstration.
|
||||||
|
pub fn move_left()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = get();
|
||||||
|
// Move the cursor to position 3 times to the left in the terminal
|
||||||
|
cursor.move_left(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print character at X: 10 Y: 5 | demonstration.
|
||||||
|
pub fn print()
|
||||||
|
{
|
||||||
|
// To print an some displayable content on an certain position.
|
||||||
|
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = get();
|
||||||
|
// Set the cursor to position X: 10, Y: 5 in the terminal
|
||||||
|
cursor.goto(10,5);
|
||||||
|
// Print the @ symbol at position X: 10, Y: 5 in the terminal
|
||||||
|
print!("@");
|
||||||
|
// Rust is line buffered inorder to print at an certain position we need to clear the buffer first.
|
||||||
|
use std;
|
||||||
|
use std::io::Write;
|
||||||
|
std::io::stdout().flush();
|
||||||
|
|
||||||
|
/* Because the above method is a little to mutch code,
|
||||||
|
you can use the `print()` method for printing an value at an certain position in the terminal.
|
||||||
|
|
||||||
|
Crossterm provides method chaining so that the above points can be inlined.
|
||||||
|
*/
|
||||||
|
|
||||||
|
get().goto(10,5).print("@");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Save and reset cursor position.
|
||||||
|
pub fn safe_and_reset_position()
|
||||||
|
{
|
||||||
|
let mut cursor = get();
|
||||||
|
|
||||||
|
// Goto X: 5 Y: 5
|
||||||
|
cursor.goto(5,5);
|
||||||
|
// Safe cursor position: X: 5 Y: 5
|
||||||
|
cursor.safe_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");
|
||||||
|
|
||||||
|
println!()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
135
examples/Crossterm 0.2.0/terminal/mod.rs
Normal file
135
examples/Crossterm 0.2.0/terminal/mod.rs
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
//!
|
||||||
|
//! Terminal Examples
|
||||||
|
//!
|
||||||
|
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
use crossterm::crossterm_terminal::{get, Terminal, ClearType};
|
||||||
|
use crossterm::crossterm_cursor;
|
||||||
|
|
||||||
|
fn print_test_data()
|
||||||
|
{
|
||||||
|
for i in 0..100 {
|
||||||
|
println!("abcdefghijTest data to test terminal: {}",i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines in terminal | demonstration
|
||||||
|
pub fn clear_all_lines()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Clear all lines in terminal;
|
||||||
|
terminal.clear(ClearType::All);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines from cursor position X:4, Y:4 down | demonstration
|
||||||
|
pub fn clear_from_cursor_down()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Set terminal cursor position (see example for more info).
|
||||||
|
crossterm_cursor::get().goto(4,8);
|
||||||
|
|
||||||
|
// Clear all cells from current cursor position down.
|
||||||
|
terminal.clear(ClearType::FromCursorDown);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
||||||
|
pub fn clear_from_cursor_up()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Set terminal cursor position (see example for more info).
|
||||||
|
crossterm_cursor::get().goto(4,8);
|
||||||
|
|
||||||
|
// Clear all cells from current cursor position down.
|
||||||
|
terminal.clear(ClearType::FromCursorUp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
||||||
|
pub fn clear_current_line()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Set terminal cursor position (see example for more info).
|
||||||
|
crossterm_cursor::get().goto(4,4);
|
||||||
|
|
||||||
|
// Clear current line cells.
|
||||||
|
terminal.clear(ClearType::CurrentLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
||||||
|
pub fn clear_until_new_line()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Set terminal cursor position (see example for more info).
|
||||||
|
crossterm_cursor::get().goto(4,7);
|
||||||
|
|
||||||
|
// Clear all the cells until next line.
|
||||||
|
terminal.clear(ClearType::UntilNewLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_terminal_size()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
// Get terminal size
|
||||||
|
let terminal_size = terminal.terminal_size().unwrap();
|
||||||
|
// Print results
|
||||||
|
print!("X: {}, y: {}", terminal_size.0, terminal_size.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the terminal size to width 10, height: 10.
|
||||||
|
pub fn set_terminal_size()
|
||||||
|
{
|
||||||
|
let mut terminal = get();
|
||||||
|
|
||||||
|
terminal.set_size(10,10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// scroll down 10 lines
|
||||||
|
pub fn scroll_down()
|
||||||
|
{
|
||||||
|
print_test_data();
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
// Scroll down 10 lines.
|
||||||
|
terminal.scroll_down(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// scroll down 10 lines
|
||||||
|
pub fn scroll_up()
|
||||||
|
{
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
// Scroll up 10 lines.
|
||||||
|
terminal.scroll_up(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resize the terminal to X: 10, Y: 10
|
||||||
|
pub fn resize_terminal()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = get();
|
||||||
|
// Get terminal size
|
||||||
|
terminal.set_size(1,1);
|
||||||
|
}
|
240
examples/Crossterm 0.2.1/README.md
Normal file
240
examples/Crossterm 0.2.1/README.md
Normal file
@ -0,0 +1,240 @@
|
|||||||
|
# Crossterm | crossplatform terminal library written in rust.
|
||||||
|
|
||||||
|
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. True the simplicity of crossterm you do not have to worry about the platform your working with. You can just call the action you want to preform and unther water it will check what to do based on the current platform.
|
||||||
|
|
||||||
|
Currently working on the alternatescreen and raw terminal features.
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
This documentation is only for the newest version of crossterm. See the [Upgrade manual for more info](https://github.com/TimonPost/crossterm/blob/development/UPGRADE%20Manual)
|
||||||
|
|
||||||
|
Add the crossterm package to your `Cargo.toml` file.
|
||||||
|
|
||||||
|
```
|
||||||
|
[dependencies]
|
||||||
|
crossterm = "*"
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Add the crate to your solution.
|
||||||
|
And use the crossterm modules withs you want to use.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
// this module is used for styling the terminal
|
||||||
|
use self::crossterm::style::*;
|
||||||
|
// this module is used for cursor related actions
|
||||||
|
use self::crossterm::cursor::*;
|
||||||
|
// this mudule is used for terminal related actions
|
||||||
|
use self::crossterm::terminal::*;
|
||||||
|
|
||||||
|
```
|
||||||
|
## Links
|
||||||
|
|
||||||
|
Documentation for the code version 0.1 can be found [here](https://docs.rs/crossterm/0.1.0/crossterm/)
|
||||||
|
|
||||||
|
Documentation for the code version 0.2 can be found [here](https://docs.rs/crossterm/0.2.0/crossterm/)
|
||||||
|
|
||||||
|
The Cargo Page can be found [here](https://crates.io/search?q=crossterm)
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
For detailed examples of all crossterm functionalities check the [examples](https://github.com/TimonPost/crossterm/tree/master/examples) direcory.
|
||||||
|
|
||||||
|
### Styled font
|
||||||
|
```rust
|
||||||
|
use crossterm::style::{paint, Color};
|
||||||
|
|
||||||
|
// Crossterm provides method chaining so that you can style the font nicely.
|
||||||
|
// the `with()` methods sets the foreground color and the `on()` methods sets the background color
|
||||||
|
// You can either store the styled font.
|
||||||
|
let mut styledobject = paint("Stored styled font").with(Color::Red).on(Color::Blue);
|
||||||
|
println!("{}",styledobject);
|
||||||
|
|
||||||
|
// Or you can print it directly.
|
||||||
|
println!("{}", paint("Red font on blue background color").with(Color::Red).on(Color::Blue));
|
||||||
|
println!("{}", paint("Red font on default background color").with(Color::Red));
|
||||||
|
println!("{}", paint("Default font color on Blue background color").on(Color::Blue));
|
||||||
|
|
||||||
|
/// The following code can only be used for unix systems:
|
||||||
|
// Set background Color from RGB
|
||||||
|
println!("RGB (10,10,10): \t {}", paint(" ").on(Color::Rgb {r: 10, g: 10, b: 10}));
|
||||||
|
// Set background Color from RGB
|
||||||
|
println!("ANSI value (50): \t {}", paint(" ").on(Color::AnsiValue(50)));
|
||||||
|
|
||||||
|
// Use attributes to syle the font.
|
||||||
|
println!("{}", paint("Normal text"));
|
||||||
|
println!("{}", paint("Bold text").bold());
|
||||||
|
println!("{}", paint("Italic text").italic());
|
||||||
|
println!("{}", paint("Slow blinking text").slow_blink());
|
||||||
|
println!("{}", paint("Rapid blinking text").rapid_blink());
|
||||||
|
println!("{}", paint("Hidden text").hidden());
|
||||||
|
println!("{}", paint("Underlined text").underlined());
|
||||||
|
println!("{}", paint("Reversed color").reverse());
|
||||||
|
println!("{}", paint("Dim text color").dim());
|
||||||
|
println!("{}", paint("Crossed out font").crossed_out());
|
||||||
|
|
||||||
|
```
|
||||||
|
### Cursor
|
||||||
|
```rust
|
||||||
|
|
||||||
|
use crossterm::cursor::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 to position 3 times to the up in the terminal
|
||||||
|
cursor.move_up(3);
|
||||||
|
|
||||||
|
// Move the cursor to position 3 times to the right in the terminal
|
||||||
|
cursor.move_right(3);
|
||||||
|
|
||||||
|
// Move the cursor to position 3 times to the down in the terminal
|
||||||
|
cursor.move_down(3);
|
||||||
|
|
||||||
|
// Move the cursor to position 3 times to the left in the terminal
|
||||||
|
cursor.move_left(3);
|
||||||
|
|
||||||
|
// Print an character at X: 10, Y: 5 (see examples for more explanation why to use this method).
|
||||||
|
// cursor.goto(10,5).print("@");
|
||||||
|
|
||||||
|
/// 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.safe_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");
|
||||||
|
```
|
||||||
|
|
||||||
|
### Terminal
|
||||||
|
```rust
|
||||||
|
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 terminal_size = terminal.terminal_size().unwrap();
|
||||||
|
// Print results
|
||||||
|
print!("X: {}, y: {}", terminal_size.0, terminal_size.1);
|
||||||
|
|
||||||
|
// Scroll down 10 lines.
|
||||||
|
terminal.scroll_down(10);
|
||||||
|
|
||||||
|
// Scroll up 10 lines.
|
||||||
|
terminal.scroll_up(10);
|
||||||
|
|
||||||
|
// Set terminal size
|
||||||
|
terminal.set_size(10,10);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Features crossterm 0.1
|
||||||
|
|
||||||
|
- Cursor movement.
|
||||||
|
- Up, Down, Left, Right.
|
||||||
|
- Goto an certain position.
|
||||||
|
- Styled output
|
||||||
|
- Foreground color (16 base colors)
|
||||||
|
- Background color (16 base colors)
|
||||||
|
- Terminal
|
||||||
|
- Clearing
|
||||||
|
- Scrolling
|
||||||
|
- Size
|
||||||
|
- Detailed documentation on every item.
|
||||||
|
- Examples for every client callable code.
|
||||||
|
|
||||||
|
## Features crossterm 0.2
|
||||||
|
|
||||||
|
- 256 color support.
|
||||||
|
- Text Attributes like: bold, italic, underscore and crossed word ect.
|
||||||
|
- Custom ANSI color code input to set fore- and background color for unix.
|
||||||
|
- Storing the current cursor position and resetting to that stored cursor position later.
|
||||||
|
- Resizing the terminal.
|
||||||
|
|
||||||
|
### fixes in crossterm 0.2.1
|
||||||
|
|
||||||
|
- Default ANSI escape codes for windows machines, if windows does not support ANSI switsh back to WINAPI.
|
||||||
|
- method grammer mistake fixed [Issue 3](https://github.com/TimonPost/crossterm/issues/3)
|
||||||
|
- Some Refactorings in method names see [issue 4](https://github.com/TimonPost/crossterm/issues/4)
|
||||||
|
- Removed bin refrence from crate [Issue 6](https://github.com/TimonPost/crossterm/issues/6)
|
||||||
|
- The terminal state will be set to its original state when process ends [issue7](https://github.com/TimonPost/crossterm/issues/7).
|
||||||
|
- Get position unix fixed [issue 8](https://github.com/TimonPost/crossterm/issues/8)
|
||||||
|
|
||||||
|
|
||||||
|
## TODO Features crossterm 0.3
|
||||||
|
- Raw state implementation [Issue 5](https://github.com/TimonPost/crossterm/issues/5).
|
||||||
|
- Alternate screen implementation.
|
||||||
|
- Tests
|
||||||
|
|
||||||
|
## Tested terminals
|
||||||
|
|
||||||
|
- Windows Powershell
|
||||||
|
- Windows 10 (pro)
|
||||||
|
- Windows CMD
|
||||||
|
- Windows 10 (pro)
|
||||||
|
- Ubuntu Desktop Terminal
|
||||||
|
- Ubuntu 17.10
|
||||||
|
|
||||||
|
|
||||||
|
The above terminals have been tested. Crossterm should works also for windows 7, 8 consoles and all ansi supportable consoles.
|
||||||
|
But these are yet to be tested.
|
||||||
|
If you have used this library for an terminal other than the above list without issues feel free to add it to the above list.
|
||||||
|
|
||||||
|
## How it works
|
||||||
|
|
||||||
|
Crossterm is using `WINAPI` for windows systems and `ANSI escape codes` for unix systems. Crossterm provides one base trait with can be implemented for a platform specific instance. For example, there is an implementation for windows (`WINAPI`) and unix(`ANSI`) for the `cursor module`. To call the platform specific implementation there is one module that rules them all. Thrue this module the client calls some action and the module will deside what to do based on the current platform. And it will execute that action.
|
||||||
|
|
||||||
|
## Notice
|
||||||
|
This library is library is stable. There will not be changed mutch in the code design so do not worry to mutch. If there are any changes that affect previous versions I will describe what to change when upgrading crossterm to an newer version.
|
||||||
|
|
||||||
|
## Todo
|
||||||
|
|
||||||
|
- This library does not support any kind of raw terminal. When an terminal changes some core state of the terminal this state should be revered when the process ends from this library. Currently there are not made any changed to the core state of the terminal with this library. But when some fearures in the furure will be inplemented this will be the case. So there should come an kind of raw state for the terminal and reversable options to redo all the changes made to the core state when the process ends. More information can be found at this [thread](https://www.reddit.com/r/rust/comments/7tg6n2/looking_for_feedback_onmy_cross_platform_terminal/dtf4ilo/)
|
||||||
|
|
||||||
|
- Handling mouse events
|
||||||
|
- Handling key events
|
||||||
|
- Tests
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
If you would like to contribute to crossterm, than please design the code as it is now. Each module contains the same structures so we can easely extend to multible platforms. As you study the code you will quiqly see what the architecture is. Maybe later there will be an documentation for how crossterm is design.
|
||||||
|
|
||||||
|
## Versioning
|
||||||
|
|
||||||
|
The current version is crossterm 0.1, every commit I merge the version go's up like 0.1.0 -> 0.1.1 -> 0.1.2.
|
||||||
|
|
||||||
|
When new features arrives the packages will go up like 0.1 -> 0.2 -> 0.3
|
||||||
|
|
||||||
|
## Authors
|
||||||
|
|
||||||
|
* **Timon Post** - *Project Owner & creator*
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
|
||||||
|
|
||||||
|
|
||||||
|
|
19
examples/Crossterm 0.2.2 - New Version (Not finished)/bin.rs
Normal file
19
examples/Crossterm 0.2.2 - New Version (Not finished)/bin.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
//! This bin folder can be used to try the examples out located in the examples directory.
|
||||||
|
//!
|
||||||
|
//! All you need to do is:
|
||||||
|
//!
|
||||||
|
//! - Download the crossterm source code.
|
||||||
|
//! - Add this in the Cargo.toml file:
|
||||||
|
//! ``` [[bin]]
|
||||||
|
//! name = "example_bin"
|
||||||
|
//! path = "./examples/bin.rs"
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! - Run program with: `cargo run`
|
||||||
|
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
use crossterm::cursor;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
}
|
@ -0,0 +1,132 @@
|
|||||||
|
//!
|
||||||
|
//! Color Examples
|
||||||
|
//!
|
||||||
|
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
use self::crossterm::style::{paint, Color};
|
||||||
|
|
||||||
|
/// print some red font | demonstration.
|
||||||
|
pub fn paint_foreground()
|
||||||
|
{
|
||||||
|
// Pass an string to the `paint()` method with you want to paint.
|
||||||
|
// This will give you an object back wits can be styled and displayed.
|
||||||
|
let mut styledobject = paint("Red font");
|
||||||
|
// Call the method `with()` on the object given by `paint()` and pass in any Color from the Color enum.
|
||||||
|
styledobject = styledobject.with(Color::Red);
|
||||||
|
// Print the object to the console and see the result.
|
||||||
|
println!("{}", styledobject);
|
||||||
|
|
||||||
|
// Crossterm provides method chaining so that the above points can be inlined.
|
||||||
|
println!("{}", paint("Red font").with(Color::Red));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// print some font on red background | demonstration.
|
||||||
|
pub fn paint_background()
|
||||||
|
{
|
||||||
|
// Pass an string to the `paint()` method with you want to paint.
|
||||||
|
// This will give you an object back wits can be styled and displayed.
|
||||||
|
let mut styledobject = paint("Red background color");
|
||||||
|
// Call the method `on()` on the object given by `paint()` and pass in an Color from the Color enum.
|
||||||
|
styledobject = styledobject.on(Color::Red);
|
||||||
|
// Print the object to the console and check see the result
|
||||||
|
println!("{}", styledobject);
|
||||||
|
|
||||||
|
// Crossterm provides method chaining so that the above points can be inlined.
|
||||||
|
println!("{}", paint("Red background color").on(Color::Red));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// print font with fore- background color | demonstration.
|
||||||
|
pub fn paint_foreground_and_background()
|
||||||
|
{
|
||||||
|
// Pass an string to the `paint()` method with you want to paint.
|
||||||
|
// This will give you an object back wits can be styled and displayed.
|
||||||
|
let mut styledobject = paint("Red font on blue background color");
|
||||||
|
/* Foreground color:
|
||||||
|
Call the method `with()` on the object given by `paint()`
|
||||||
|
Pass in an Color from the Color enum.
|
||||||
|
*/
|
||||||
|
styledobject = styledobject.with(Color::Red);
|
||||||
|
/* Background color:
|
||||||
|
Call the method `on()` on the object given by `paint()`
|
||||||
|
Pass in an Color from the Color enum.
|
||||||
|
*/
|
||||||
|
styledobject = styledobject.on(Color::Blue);
|
||||||
|
// Print the object to the console and see the result.
|
||||||
|
println!("{}", styledobject);
|
||||||
|
|
||||||
|
// Crossterm provides method chaining so that the above points can be inlined.
|
||||||
|
println!("{}", paint("Red font on blue background color").with(Color::Red).on(Color::Blue));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print all available foreground colors | demonstration.
|
||||||
|
pub fn print_all_foreground_colors()
|
||||||
|
{
|
||||||
|
println!("Black : \t {}", paint("■").with(Color::Black));
|
||||||
|
println!("Red : \t\t {}", paint("■").with(Color::Red));
|
||||||
|
println!("Dark Red: \t {}", paint("■").with(Color::DarkRed));
|
||||||
|
println!("Green : \t {}", paint("■").with(Color::Green));
|
||||||
|
println!("Dark Green : \t {}", paint("■").with(Color::DarkGreen));
|
||||||
|
println!("Yellow : \t {}", paint("■").with(Color::Yellow));
|
||||||
|
println!("Dark Yellow : \t {}", paint("■").with(Color::DarkYellow));
|
||||||
|
println!("Blue : \t\t {}", paint("■").with(Color::Blue));
|
||||||
|
println!("Dark Blue : \t {}", paint("■").with(Color::DarkBlue));
|
||||||
|
println!("Magenta : \t {}", paint("■").with(Color::Magenta));
|
||||||
|
println!("Dark Magenta : \t {}", paint("■").with(Color::DarkMagenta));
|
||||||
|
println!("Cyan : \t\t {}", paint("■").with(Color::Cyan));
|
||||||
|
println!("Dark Cyan : \t {}", paint("■").with(Color::DarkCyan));
|
||||||
|
println!("Grey : \t\t {}", paint("■").with(Color::Grey));
|
||||||
|
println!("White : \t {}", paint("■").with(Color::White));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print all available foreground colors | demonstration.
|
||||||
|
pub fn print_all_background_colors()
|
||||||
|
{
|
||||||
|
println!("Black : \t {}", paint(" ").on(Color::Black));
|
||||||
|
println!("Red : \t\t {}", paint(" ").on(Color::Red));
|
||||||
|
println!("Dark Red: \t {}", paint(" ").on(Color::DarkRed));
|
||||||
|
println!("Green : \t {}", paint(" ").on(Color::Green));
|
||||||
|
println!("Dark Green : \t {}", paint(" ").on(Color::DarkGreen));
|
||||||
|
println!("Yellow : \t {}", paint(" ").on(Color::Yellow));
|
||||||
|
println!("Dark Yellow : \t {}", paint(" ").on(Color::DarkYellow));
|
||||||
|
println!("Blue : \t\t {}", paint(" ").on(Color::Blue));
|
||||||
|
println!("Dark Blue : \t {}", paint(" ").on(Color::DarkBlue));
|
||||||
|
println!("Magenta : \t {}", paint(" ").on(Color::Magenta));
|
||||||
|
println!("Dark Magenta : \t {}", paint(" ").on(Color::DarkMagenta));
|
||||||
|
println!("Cyan : \t\t {}", paint(" ").on(Color::Cyan));
|
||||||
|
println!("Dark Cyan : \t {}", paint(" ").on(Color::DarkCyan));
|
||||||
|
println!("Grey : \t\t {}", paint(" ").on(Color::Grey));
|
||||||
|
println!("White : \t {}", paint(" ").on(Color::White));
|
||||||
|
#[cfg(unix)]
|
||||||
|
println!("RGB (10,10,10): \t {}", paint(" ").on(Color::Rgb {r: 10, g: 10, b: 10}));
|
||||||
|
#[cfg(unix)]
|
||||||
|
println!("RGB (10,10,10): \t {}", paint(" ").on(Color::AnsiValue(50)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print font with all available attributes. Note that this can only be used at unix systems and that some are not supported widely | demonstration..
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub fn print_font_with_attributes()
|
||||||
|
{
|
||||||
|
println!("{}", paint("Normal text"));
|
||||||
|
println!("{}", paint("Bold text").bold());
|
||||||
|
println!("{}", paint("Italic text").italic());
|
||||||
|
println!("{}", paint("Slow blinking text").slow_blink());
|
||||||
|
println!("{}", paint("Rapid blinking text").rapid_blink());
|
||||||
|
println!("{}", paint("Hidden text").hidden());
|
||||||
|
println!("{}", paint("Underlined text").underlined());
|
||||||
|
println!("{}", paint("Reversed color").reverse());
|
||||||
|
println!("{}", paint("Dim text color").dim());
|
||||||
|
println!("{}", paint("Crossed out font").crossed_out());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print all supported rgb colors | demonstration.
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub fn print_supported_colors()
|
||||||
|
{
|
||||||
|
let count = crossterm::style::color().get_available_color_count().unwrap();
|
||||||
|
|
||||||
|
for i in 0..count
|
||||||
|
{
|
||||||
|
println!("{}", paint(format!("Color: {}",i)).with(Color::AnsiValue(i as u8)));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,119 @@
|
|||||||
|
//!
|
||||||
|
//! Cursor Examples
|
||||||
|
//!
|
||||||
|
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
use self::crossterm::cursor::{cursor, TerminalCursor};
|
||||||
|
|
||||||
|
/// Set the cursor to position X: 10, Y: 5 in the terminal.
|
||||||
|
pub fn goto()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = cursor();
|
||||||
|
// Set the cursor to position X: 10, Y: 5 in the terminal
|
||||||
|
cursor.goto(10,5);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move the cursor 3 up | demonstration.
|
||||||
|
pub fn move_up()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = cursor();
|
||||||
|
// Move the cursor to position 3 times to the up in the terminal
|
||||||
|
cursor.move_up(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move the cursor 3 to the right | demonstration.
|
||||||
|
pub fn move_right()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = cursor();
|
||||||
|
// Move the cursor to position 3 times to the right in the terminal
|
||||||
|
cursor.move_right(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move the cursor 3 down | demonstration.
|
||||||
|
pub fn move_down()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = cursor();
|
||||||
|
// Move the cursor to position 3 times to the down in the terminal
|
||||||
|
cursor.move_down(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move the cursor 3 to the left | demonstration.
|
||||||
|
pub fn move_left()
|
||||||
|
{
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = cursor();
|
||||||
|
// Move the cursor to position 3 times to the left in the terminal
|
||||||
|
cursor.move_left(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print character at X: 10 Y: 5 | demonstration.
|
||||||
|
pub fn print()
|
||||||
|
{
|
||||||
|
// To print an some displayable content on an certain position.
|
||||||
|
|
||||||
|
// Get the cursor
|
||||||
|
let mut cursor = cursor();
|
||||||
|
// Set the cursor to position X: 10, Y: 5 in the terminal
|
||||||
|
cursor.goto(10,5);
|
||||||
|
// Print the @ symbol at position X: 10, Y: 5 in the terminal
|
||||||
|
print!("@");
|
||||||
|
// Rust is line buffered inorder to print at an certain position we need to clear the buffer first.
|
||||||
|
use std;
|
||||||
|
use std::io::Write;
|
||||||
|
std::io::stdout().flush();
|
||||||
|
|
||||||
|
/* Because the above method is a little to much code,
|
||||||
|
you can use the `print()` method for printing an value at an certain position in the terminal.
|
||||||
|
|
||||||
|
Crossterm provides method chaining so that the above points can be inlined.
|
||||||
|
*/
|
||||||
|
|
||||||
|
cursor.goto(10,5).print("@");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Save and reset cursor position | demonstration..
|
||||||
|
pub fn safe_and_reset_position()
|
||||||
|
{
|
||||||
|
let mut cursor = cursor();
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
println!("Yea!");
|
||||||
|
// Reset back to X: 5 Y: 5.
|
||||||
|
cursor.reset_position();
|
||||||
|
// Print Back at X: 5 Y: 5.
|
||||||
|
println!("Back");
|
||||||
|
|
||||||
|
println!()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,73 @@
|
|||||||
|
// alternate screen is not working correctly currently
|
||||||
|
|
||||||
|
|
||||||
|
//extern crate crossterm;
|
||||||
|
//
|
||||||
|
//use crossterm::terminal::screen::{AlternateScreen, ToAlternateScreen, ToMainScreen};
|
||||||
|
//use crossterm::cursor::cursor;
|
||||||
|
//use crossterm::terminal::{self, ClearType};
|
||||||
|
//
|
||||||
|
//use std::io::{Write, stdout};
|
||||||
|
//use std::{time, thread};
|
||||||
|
//
|
||||||
|
//fn print_wait_screen(screen: &mut Write)
|
||||||
|
//{
|
||||||
|
// terminal::terminal().clear(ClearType::All);
|
||||||
|
// write!(screen,
|
||||||
|
// "Welcome to the wait screen.\n\
|
||||||
|
// Please wait a few seconds until we arrive back at the main screen.\n\
|
||||||
|
// Seconds to Go: "
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// let mut counter = 5;
|
||||||
|
// // get cursor instance
|
||||||
|
// let mut cursor = cursor();
|
||||||
|
//
|
||||||
|
// // loop until the counter hits 0
|
||||||
|
// loop
|
||||||
|
// {
|
||||||
|
// // 1 second delay
|
||||||
|
// thread::sleep(time::Duration::from_secs(1));
|
||||||
|
// // decrement counter
|
||||||
|
// counter -= 1;
|
||||||
|
//
|
||||||
|
// // print the current counter at the line of `Seconds to Go: {counter}`
|
||||||
|
// cursor.goto(15,2).print(counter);
|
||||||
|
//
|
||||||
|
// if counter <= 0
|
||||||
|
// {
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//pub fn with_alternate_screen_instance()
|
||||||
|
//{
|
||||||
|
// // create scope. If this scope ends the screen will be switched back to mainscreen.
|
||||||
|
// // becouse `AlternateScreen` switches back to main screen when switching back.
|
||||||
|
// {
|
||||||
|
// // create new alternate screen instance and switch to the alternate screen.
|
||||||
|
// let mut screen = AlternateScreen::from(stdout());
|
||||||
|
//
|
||||||
|
// // Print the wait screen.
|
||||||
|
// print_wait_screen(&mut screen);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// println!("Whe are back at the main screen");
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//pub fn manually_switch_to_alternate_screen()
|
||||||
|
//{
|
||||||
|
// // You can switch to alternate screen manually but if you forget to switch back your terminal may cause some undefined behavior.
|
||||||
|
//
|
||||||
|
// let mut screen = stdout();
|
||||||
|
//
|
||||||
|
// // switch to alternate screeen
|
||||||
|
// write!(screen, "{}", ToAlternateScreen);
|
||||||
|
// // load wait screen
|
||||||
|
// print_wait_screen(&mut screen);
|
||||||
|
// // switch back
|
||||||
|
// write!(screen,"{}", ToMainScreen);
|
||||||
|
// println!("Whe are back at the main screen");
|
||||||
|
//
|
||||||
|
//}
|
@ -0,0 +1,5 @@
|
|||||||
|
mod alternate_screen;
|
||||||
|
mod raw_mode;
|
||||||
|
|
||||||
|
|
||||||
|
pub mod terminal;
|
@ -0,0 +1 @@
|
|||||||
|
// raw screen is not working correctly currently
|
@ -0,0 +1,136 @@
|
|||||||
|
//!
|
||||||
|
//! Terminal Examples
|
||||||
|
//!
|
||||||
|
|
||||||
|
extern crate crossterm;
|
||||||
|
|
||||||
|
use crossterm::terminal::{ Terminal, ClearType, terminal};
|
||||||
|
use crossterm::cursor;
|
||||||
|
|
||||||
|
fn print_test_data()
|
||||||
|
{
|
||||||
|
for i in 0..100 {
|
||||||
|
println!("Test data to test terminal: {}",i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines in terminal | demonstration
|
||||||
|
pub fn clear_all_lines()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = terminal();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Clear all lines in terminal;
|
||||||
|
terminal.clear(ClearType::All);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines from cursor position X:4, Y:4 down | demonstration
|
||||||
|
pub fn clear_from_cursor_down()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = terminal();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Set terminal cursor position (see example for more info).
|
||||||
|
cursor::cursor().goto(4,8);
|
||||||
|
|
||||||
|
// Clear all cells from current cursor position down.
|
||||||
|
terminal.clear(ClearType::FromCursorDown);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
||||||
|
pub fn clear_from_cursor_up()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = terminal();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Set terminal cursor position (see example for more info).
|
||||||
|
cursor::cursor().goto(4,4);
|
||||||
|
|
||||||
|
// Clear all cells from current cursor position down.
|
||||||
|
terminal.clear(ClearType::FromCursorUp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
||||||
|
pub fn clear_current_line()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = terminal();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Set terminal cursor position (see example for more info).
|
||||||
|
cursor::cursor().goto(4,4);
|
||||||
|
|
||||||
|
// Clear current line cells.
|
||||||
|
terminal.clear(ClearType::CurrentLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all lines from cursor position X:4, Y:7 up | demonstration
|
||||||
|
pub fn clear_until_new_line()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = terminal();
|
||||||
|
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Set terminal cursor position (see example for more info).
|
||||||
|
cursor::cursor().goto(4,20);
|
||||||
|
|
||||||
|
// Clear all the cells until next line.
|
||||||
|
terminal.clear(ClearType::UntilNewLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print the the current terminal size | demonstration.
|
||||||
|
pub fn print_terminal_size()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = terminal();
|
||||||
|
// Get terminal size
|
||||||
|
let terminal_size = terminal.terminal_size();
|
||||||
|
// Print results
|
||||||
|
print!("X: {}, y: {}", terminal_size.0, terminal_size.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the terminal size to width 10, height: 10 | demonstration.
|
||||||
|
pub fn set_terminal_size()
|
||||||
|
{
|
||||||
|
let mut terminal = terminal();
|
||||||
|
|
||||||
|
terminal.set_size(10,10);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Scroll down 10 lines | demonstration.
|
||||||
|
pub fn scroll_down()
|
||||||
|
{
|
||||||
|
print_test_data();
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = terminal();
|
||||||
|
// Scroll down 10 lines.
|
||||||
|
terminal.scroll_down(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Scroll down 10 lines | demonstration.
|
||||||
|
pub fn scroll_up()
|
||||||
|
{
|
||||||
|
print_test_data();
|
||||||
|
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = terminal();
|
||||||
|
// Scroll up 10 lines.
|
||||||
|
terminal.scroll_up(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resize the terminal to X: 10, Y: 10 | demonstration.
|
||||||
|
pub fn resize_terminal()
|
||||||
|
{
|
||||||
|
// Get terminal
|
||||||
|
let mut terminal = terminal();
|
||||||
|
// Get terminal size
|
||||||
|
terminal.set_size(10,10);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user