Changed Crossterm type so that the user does not have to enter a reference to a Screen every function call and also updated the readme, docs,comments,examples

This commit is contained in:
TimonPost 2018-08-14 21:40:07 +02:00
parent df068b823e
commit f7c6f36a46
23 changed files with 347 additions and 327 deletions

194
README.md
View File

@ -52,26 +52,19 @@ use self::crossterm::style::*;
use self::crossterm::cursor::*; use self::crossterm::cursor::*;
// this mudule is used for terminal related actions // this mudule is used for terminal related actions
use self::crossterm::terminal::*; use self::crossterm::terminal::*;
// this mudule is used for input related actions
use self::crossterm::terminal::*;
// this type could be used to access the above modules.
use self::crossterm::Crossterm;
``` ```
## Useful Links ## Useful Links
- Code documentation: - Code [documentation](link).
version [0.1.0](https://docs.rs/crossterm/0.1.0/crossterm/), - Code [Examples]() (see [branches](LINK_TO_BRANCHES) for previous versions)
[0.2.0](https://docs.rs/crossterm/0.2.0/crossterm/),
[0.2.1](https://docs.rs/crossterm/0.2.1/crossterm/)
[0.3.0](https://docs.rs/crossterm/0.3.0/crossterm/)
[0.4.0](https://docs.rs/crossterm/0.4.0/crossterm/)
- Code Examples:
# TO UPDATE
version [0.1.0](https://github.com/TimonPost/crossterm/tree/master/examples/Crossterm%200.1.0),
[0.2.0](https://github.com/TimonPost/crossterm/tree/master/examples/Crossterm%200.2.0),
[0.2.1](https://github.com/TimonPost/crossterm/tree/master/examples/Crossterm%200.2.1)
and [0.3.0](https://github.com/TimonPost/crossterm/tree/master/examples/Crossterm%200.3.0)
- [Cargo Page](https://crates.io/crates/crossterm) - [Cargo Page](https://crates.io/crates/crossterm)
- [Real life examples](https://github.com/TimonPost/crossterm/tree/master/examples/Crossterm%200.3.0/program_examples) - [Program Examples](https://github.com/TimonPost/crossterm/tree/master/examples/program_examples)
# Features # Features
These are the features from this crate: These are the features from this crate:
@ -96,101 +89,101 @@ These are the features from this crate:
- Set the size of the terminal. - Set the size of the terminal.
- Alternate screen - Alternate screen
- Raw screen - Raw screen
- Input
- Read character
- Read line
- Read async
- Read async until
- Exit the current process. - Exit the current process.
- Detailed documentation on every item. - Detailed documentation on every item.
- Crossplatform
## Examples ## Examples
For detailed examples of all Crossterm functionalities check the [examples](https://github.com/TimonPost/crossterm/tree/master/examples) directory. For detailed examples of all Crossterm functionalities check the [examples](https://github.com/TimonPost/crossterm/tree/master/examples) directory.
### Crossterm wrapper | [see more](https://github.com/TimonPost/crossterm/blob/master/examples/Crossterm%200.3.0/crossterm_type/mod.rs) ### Crossterm Type | [see more](Link)
This is a wrapper for the modules crossterm provides. This is introduced to manage the [`Context`](link_to_context) for the user. This is a wrapper for all the modules crossterm provides like terminal, cursor, styling and input.
``` ```
let crossterm = Crossterm::new(); // screen wheron the `Crossterm` methods will be executed.
let screen = Screen::default();
let crossterm = Crossterm::new(&screen);
// get instance of the modules, whereafter you can use the methods the particulary module provides. // get instance of the modules, whereafter you can use the methods the particulary module provides.
let color = crossterm.color(); let color = crossterm.color();
let cursor = crossterm.cursor(); let cursor = crossterm.cursor();
let terminal = crossterm.terminal(); let terminal = crossterm.terminal();
// write text to console wheter it be the main screen or the alternate screen. // styling
crossterm.write("some text"); let style = crossterm.style("Black font on Green background color").with(Color::Black).on(Color::Green);
// print some styled font. style.paint(&screen);
println!("{}", crossterm.paint("Red font on blue background").with(Color::Red).on(Color::Blue));
``` ```
### Styled font | [see more](https://github.com/TimonPost/crossterm/blob/master/examples/Crossterm%200.3.0/color/mod.rs) ### Styled Font | [see more](Link)
This module provides the functionalities to style the terminal cursor. This module provides the functionalities to style the terminal cursor.
```rust ```rust
use crossterm::style::{Color}; use crossterm::style::{Color, style};
use crossterm::Crossterm; use crossterm::Screen;
// Crossterm provides method chaining so that you can style the font nicely. // store objcets so it could be painted later to the screen.
// the `with()` methods sets the foreground color and the `on()` methods sets the background color let style1 = style("Some Blue font on Black background").with(Color::Blue).on(Color::Black);
// You can either store the styled font. let style2 = style("Some Red font on Yellow background").with(Color::Red).on(Color::Yellow);
// create instance of `Crossterm` let screen = Screen::default();
let crossterm = Crossterm::new();
// store style in styled object and print it /// ! The following code only works for unix based systems.
let mut styledobject = crossterm.paint("stored styled font in variable").with(Color::Green).on(Color::Yellow); // some attributes
println!("{}",styledobject); let normal = style("Normal text");
let bold = style("Bold text").bold();
let italic = style("Italic text").italic();
let slow_blink = style("Slow blinking text").slow_blink();
let rapid_blink = style("Rapid blinking text").rapid_blink();
let hidden = style("Hidden text").hidden();
let underlined = style("Underlined text").underlined();
let reversed = style("Reversed text").reverse();
let dimmed = style("Dim text").dim();
let crossed_out = style("Crossed out font").crossed_out();
// Or you can print it directly. // paint styled text to screen (this could also be called inline)
println!("{}", crossterm.paint("Red font on blue background color").with(Color::Red).on(Color::Blue)); style1.paint(&screen);
println!("{}", crossterm.paint("Red font on default background color").with(Color::Red)); style2.paint(&screen);
println!("{}", crossterm.paint("Default font color on Blue background color").on(Color::Blue)); bold.paint(&screen);
hidden.paint(&screen);
/// The following code can only be used for unix systems: // cursom rgb value
style("RGB color (10,10,10) ").with(Color::Rgb {
r: 10,
g: 10,
b: 10
}).paint(&screen);
// Set background Color from RGB // custom ansi color value
println!("RGB (10,10,10): \t {}", crossterm.paint(" ").on(Color::Rgb {r: 10, g: 10, b: 10})); style("ANSI color value (50) ").with(Color::AnsiValue(50)).paint(&screen);
// Set background Color from RGB
println!("ANSI value (50): \t {}", crossterm.paint(" ").on(Color::AnsiValue(50)));
// Use attributes to syle the font.
println!("{}", crossterm.paint("Normal text"));
println!("{}", crossterm.paint("Bold text").bold());
println!("{}", crossterm.paint("Italic text").italic());
println!("{}", crossterm.paint("Slow blinking text").slow_blink());
println!("{}", crossterm.paint("Rapid blinking text").rapid_blink());
println!("{}", crossterm.paint("Hidden text").hidden());
println!("{}", crossterm.paint("Underlined text").underlined());
println!("{}", crossterm.paint("Reversed color").reverse());
println!("{}", crossterm.paint("Dim text color").dim());
println!("{}", crossterm.paint("Crossed out font").crossed_out());
``` ```
### Cursor | [see more](https://github.com/TimonPost/crossterm/blob/master/examples/Crossterm%200.3.0/cursor/mod.rs) ### Cursor | [see more](LINK)
This module provides the functionalities to work with the terminal cursor. This module provides the functionalities to work with the terminal cursor.
```rust ```rust
use crossterm::Context; use crossterm::Screen;
use crossterm::cursor::cursor; use crossterm::cursor::cursor;
// create context to pass to the `cursor()` function. // create Screen to wheron the `cursor()` should function.
let context = Context::new(); let screen = Screen::default();
let mut cursor = cursor(&context); let mut cursor = cursor(&screen);
/// Moving the cursor | demo /// Moving the cursor
// Set the cursor to position X: 10, Y: 5 in the terminal // Set the cursor to position X: 10, Y: 5 in the terminal
cursor.goto(10,5); cursor.goto(10,5);
// Move the cursor to position 3 times to the up in the terminal // Move the cursor up,right,down,left 3 cells.
cursor.move_up(3); cursor.move_up(3);
// Move the cursor to position 3 times to the right in the terminal
cursor.move_right(3); cursor.move_right(3);
// Move the cursor to position 3 times to the down in the terminal
cursor.move_down(3); cursor.move_down(3);
// Move the cursor to position 3 times to the left in the terminal
cursor.move_left(3); cursor.move_left(3);
// Print an character at X: 10, Y: 5 (see examples for more explanation why to use this method). /// Safe the current cursor position to recall later
// cursor.goto(10,5).print("@");
/// Safe the current cursor position to recall later | demo
// Goto X: 5 Y: 5 // Goto X: 5 Y: 5
cursor.goto(5,5); cursor.goto(5,5);
// Safe cursor position: X: 5 Y: 5 // Safe cursor position: X: 5 Y: 5
@ -213,15 +206,16 @@ cursor.blink(true)
``` ```
### Terminal | [see more](https://github.com/TimonPost/crossterm/blob/master/examples/Crossterm%200.3.0/terminal/terminal.rs) ### Terminal | [see more](LINK)
This module provides the functionalities to work with the terminal in general. This module provides the functionalities to work with the terminal in general.
```rust ```rust
use crossterm::terminal::{terminal,ClearType}; use crossterm::terminal::{terminal,ClearType};
use crossterm::Context; use crossterm::Screen;
let mut context = Context::new(); // create Screen to wheron the `terminal()` should function.
let mut terminal = terminal(&context); let screen = Screen::default();
let mut terminal = terminal(&screen);
// Clear all lines in terminal; // Clear all lines in terminal;
terminal.clear(ClearType::All); terminal.clear(ClearType::All);
@ -235,17 +229,14 @@ terminal.clear(ClearType::CurrentLine);
terminal.clear(ClearType::UntilNewLine); terminal.clear(ClearType::UntilNewLine);
// Get terminal size // Get terminal size
let terminal_size = terminal.terminal_size(); let (width, height) = terminal.terminal_size();
// Print results print!("X: {}, y: {}", width, height);
print!("X: {}, y: {}", terminal_size.0, terminal_size.1);
// Scroll down 10 lines. // Scroll down, up 10 lines.
terminal.scroll_down(10); terminal.scroll_down(10);
// Scroll up 10 lines.
terminal.scroll_up(10); terminal.scroll_up(10);
// Set terminal size // Set terminal size (width, height)
terminal.set_size(10,10); terminal.set_size(10,10);
// exit the current process. // exit the current process.
@ -253,9 +244,6 @@ terminal.exit();
// write to the terminal whether you are on the main screen or alternate screen. // write to the terminal whether you are on the main screen or alternate screen.
terminal.write("Some text\n Some text on new line"); terminal.write("Some text\n Some text on new line");
// use the `paint()` for styling font
println!("{}", terminal.paint("x").with(Color::Red).on(Color::Blue));
``` ```
Check these links: [AlternateScreen](https://github.com/TimonPost/crossterm/blob/master/examples/Crossterm%200.3.0/terminal/alternate_screen.rs) and [RawScreen](https://github.com/TimonPost/crossterm/blob/master/examples/Crossterm%200.3.0/terminal/raw_mode.rs) for information about how to work with these features. Check these links: [AlternateScreen](https://github.com/TimonPost/crossterm/blob/master/examples/Crossterm%200.3.0/terminal/alternate_screen.rs) and [RawScreen](https://github.com/TimonPost/crossterm/blob/master/examples/Crossterm%200.3.0/terminal/raw_mode.rs) for information about how to work with these features.
@ -273,10 +261,6 @@ Check these links: [AlternateScreen](https://github.com/TimonPost/crossterm/blob
This crate supports all Unix terminals and windows terminals down to Windows XP but not all of them have been tested. This crate supports all Unix terminals and windows terminals down to Windows XP 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. 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.
## How it works
Crossterm is using ANSI escape codes by default for both Unix and Windows systems.
But for Windows, it is a bit more complicated since Windows versions 8 or lower are not supporting ANSI escape codes. This is why we use WinApi for those machines. For Windows 10 ANSI codes will be the default.
## Notice ## Notice
This library is not stable yet but I expect it to not to change that much anymore. This library is not stable yet but I expect it to not to change that much anymore.
And if there are any changes that affect previous versions I will [describe](https://github.com/TimonPost/crossterm/blob/master/docs/UpgradeManual.md) what to change when upgrading Crossterm to a newer version. And if there are any changes that affect previous versions I will [describe](https://github.com/TimonPost/crossterm/blob/master/docs/UpgradeManual.md) what to change when upgrading Crossterm to a newer version.
@ -285,47 +269,19 @@ And if there are any changes that affect previous versions I will [describe](htt
I still have some things in mind to implement. I still have some things in mind to implement.
- Handling mouse events - Handling mouse events
I want to be able to do something based on the clicks the user has done with its mouse. I want to be able to do something based on the clicks the user has done with its mouse.
- Handling key events - Handling key events
I want to be able to read key combination inputs. I want to be able to read key combination inputs.
- reading from the console.
I want to be able to read the input of the console.
- Error handling
Currently, I am not doing that much with returned errors. This is bad since I suspect that everything is working. I want to manage this better. When you build this crate you will see the warnings about not used return values. This is what needs to be improved.
- Tests - Tests
Also, I want to have tests for this crate, and yes maybe a little late :). But I find it difficult to test some functionalities because how would you ever test if the screen is indeed int alternate, raw modes or how would you ever test if the terminal cursor is moved certainly. Also, I want to have tests for this crate, and yes maybe a little late :). But I find it difficult to test some functionalities because how would you ever test if the screen is indeed int alternate, raw modes or how would you ever test if the terminal cursor is moved certainly.
## Contributing ## Contributing
If you would like to contribute to Crossterm, than please design the code as it is now. I highly appreciate it when you are contributing to this crate.
For example, a module like cursor has the following file structure: Also Since my native language is not English my grammar and sentence order will not be perfect.
- mod.rs So improving this by correcting these mistakes will help both me and the reader of the docs.
This file contains some trait, in this case, `ITerminalCursor`, for other modules to implement. So that it can work at a specific platform. Check [Contributing](link) for more info about branches and code architecture.
- cursor.rs
The end user will call this module to access the cursor functionalities. This module will decide which implementation to use based on the current platform.
- winapi_cursor
This is the cursor trait (located in mod.rs) implementation with WinApi.
- ansi_cursor
This is the cursor trait (located in mod.rs) implementation with ANSI escape codes.
The above structure is the same for the terminal, color, manager modules.
Why I have chosen for this design:
- Because you can easily extend to multiple platforms by implementing the trait int the mod.rs.
- You keep the functionalities for different platforms separated in different files.
- Also, you have one API the user can call like in the `cursor.rs` above. This file should be avoided to change that much. All the other code could change a lot because it has no impact on the user side.
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.
## Authors ## Authors

View File

@ -1,24 +0,0 @@
Upgrade crossterm 0.2 to 0.2.1
Namespaces:
I have changed the namespaces. I found the namsespaces to long so I have shortened them like the following:
Old: crossterm::crossterm_style
New: crossterm::style
Old: crossterm::crossterm_terminal
New: crossterm::terminal
Old: crossterm::crossterm_cursor
New: crossterm::cursor
Method names that changed [Issue 4](https://github.com/TimonPost/crossterm/issues/4):
Old: crossterm::crossterm_cursor::get();
New: use crossterm::cursor::cursor();
Old: crossterm::crossterm_terminal::get();
New: use crossterm::terminal::terminal();
Old: crossterm::crossterm_style::color::get();
New: use crossterm::style::color::color();

View File

@ -1,10 +0,0 @@
1. improved performance: removed mutexes, removed state manager, removed unceasingly RC types.
2. This create supports multithreading by implementing `Send`
2. nicer looking coloring module.
4. Added input functionality.
4. Able to have different screens to write to.
2. cursor, color, terminal functions have other parameters.
3. bug fix resetting console color
6. Added Screen type
8. removed paint from Crossterm type.

46
docs/Contributing.md Normal file
View File

@ -0,0 +1,46 @@
I would really appreciate any contributing to this crate. But there are some things that are handy to know.
## Branch
- If you have small commits (e.g. bugfixes, grammar improvements, examples, comments) please create a pull request to the development branch.
- If you have a large feature you could better create a separate branch for that and pull request this one into development.
## How it works
Crossterm is using ANSI escape codes by default for both Unix and Windows systems.
But for Windows, it is a bit more complicated since Windows versions 8 or lower are not supporting ANSI escape codes.
This is why we use WinApi for those machines.
## Architecture
Here I will discuss the architecture of crossterm. At first we will discuss the five modules crossterm has like: cursor, input, style, terminal, write.
### The different modules
If you would like to contribute to Crossterm, than please design the code as it is now.
For example, a module like cursor has the following file structure:
- mod.rs
This file contains some trait, in this case, `ITerminalCursor`, for other modules to implement. So that it can work at a specific platform.
- cursor.rs
The end user will call this module to access the cursor functionalities. This module will decide which implementation to use based on the current platform.
- winapi_cursor
This is the cursor trait (located in mod.rs) implementation with WinApi.
- ansi_cursor
This is the cursor trait (located in mod.rs) implementation with ANSI escape codes.
The above structure is the same for the terminal, color, manager modules.
Why I have chosen for this design:
- Because you can easily extend to multiple platforms by implementing the trait int the mod.rs.
- You keep the functionalities for different platforms separated in different files.
- Also, you have one API the user can call like in the `cursor.rs` above. This file should be avoided to change that much. All the other code could change a lot because it has no impact on the user side.
### Kernel
The kernel is divided into to modules one containing all the windows specific logic and the other containing all the unix specific code.
Here we will do all the unsafe system/C calls.
### Common
Here is the code located that could be used everywhere. An example is the `Screen` type.
The user can call this but also the different modules and the kernel are using this type.

View File

@ -72,7 +72,7 @@ let screen = Screen::default();
// this will create a new screen with raw modes enabled. // this will create a new screen with raw modes enabled.
let screen = Screen::new(true); let screen = Screen::new(true);
// false specifies whether the alternate screen should be in raw modes. // `false` specifies whether the alternate screen should be in raw modes.
if let Ok(alternate) = screen.enable_alternate_modes(false) if let Ok(alternate) = screen.enable_alternate_modes(false)
{ {
let cursor = cursor(&alternate.screen); let cursor = cursor(&alternate.screen);
@ -81,6 +81,7 @@ if let Ok(alternate) = screen.enable_alternate_modes(false)
#### Other #### Other
- ::crossterm::Crossterm::write() is gone. - ::crossterm::Crossterm::write() is gone.
- ::crossterm::Crossterm::flush() is gone.
- Context type is removed - Context type is removed
- StateManager is removed - StateManager is removed
- ScreenManager type is renamed to Stdout. - ScreenManager type is renamed to Stdout.

View File

@ -8,6 +8,8 @@ use self::crossterm::{terminal, Screen};
/// print some red font | demonstration. /// print some red font | demonstration.
pub fn paint_foreground() { pub fn paint_foreground() {
let screen = Screen::default(); let screen = Screen::default();
// Pass an string to the `paint()` method with you want to paint. // 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. // This will give you an object back wits can be styled and displayed.
@ -66,7 +68,7 @@ pub fn print_all_foreground_colors() {
}).paint(&screen); }).paint(&screen);
#[cfg(unix)] #[cfg(unix)]
style("RGB color (10,10,10) ").with(Color::AnsiValue(50)).paint(&screen); style("RGB color (10,10,10) ").with(Color::AnsiValue(50)).paint(&screen);
} }
/// Print all available foreground colors | demonstration. /// Print all available foreground colors | demonstration.

View File

@ -9,7 +9,8 @@ use self::crossterm::Screen;
/// Set the cursor to position X: 10, Y: 5 in the terminal. /// Set the cursor to position X: 10, Y: 5 in the terminal.
pub fn goto() { pub fn goto() {
// Get the cursor // Get the cursor
let mut cursor = cursor(&Screen::default()); let screen = Screen::default();
let mut cursor = cursor(&screen);
// Set the cursor to position X: 10, Y: 5 in the terminal // Set the cursor to position X: 10, Y: 5 in the terminal
cursor.goto(10, 5); cursor.goto(10, 5);
} }
@ -17,7 +18,8 @@ pub fn goto() {
/// get the cursor position /// get the cursor position
pub fn pos() { pub fn pos() {
// Get the cursor // Get the cursor
let mut cursor = cursor(&Screen::default()); let screen = Screen::default();
let mut cursor = cursor(&screen);
// get the cursor position. // get the cursor position.
let (x, y) = cursor.pos(); let (x, y) = cursor.pos();
} }
@ -25,7 +27,8 @@ pub fn pos() {
/// Move the cursor 3 up | demonstration. /// Move the cursor 3 up | demonstration.
pub fn move_up() { pub fn move_up() {
// Get the cursor // Get the cursor
let mut cursor = cursor(&Screen::default()); let screen = Screen::default();
let mut cursor = cursor(&screen);
// Move the cursor to position 3 times to the up in the terminal // Move the cursor to position 3 times to the up in the terminal
cursor.move_up(10); cursor.move_up(10);
@ -33,21 +36,24 @@ pub fn move_up() {
/// Move the cursor 3 to the right | demonstration. /// Move the cursor 3 to the right | demonstration.
pub fn move_right() { pub fn move_right() {
let mut cursor = cursor(&Screen::default()); let screen = Screen::default();
let mut cursor = cursor(&screen);
// Move the cursor to position 3 times to the right in the terminal // Move the cursor to position 3 times to the right in the terminal
cursor.move_right(3); cursor.move_right(3);
} }
/// Move the cursor 3 down | demonstration. /// Move the cursor 3 down | demonstration.
pub fn move_down() { pub fn move_down() {
let mut cursor = cursor(&Screen::default()); let screen = Screen::default();
let mut cursor = cursor(&screen);
// Move the cursor to position 3 times to the down in the terminal // Move the cursor to position 3 times to the down in the terminal
cursor.move_down(3); cursor.move_down(3);
} }
/// Move the cursor 3 to the left | demonstration. /// Move the cursor 3 to the left | demonstration.
pub fn move_left() { pub fn move_left() {
let mut cursor = cursor(&Screen::default()); let screen = Screen::default();
let mut cursor = cursor(&screen);
// Move the cursor to position 3 times to the left in the terminal // Move the cursor to position 3 times to the left in the terminal
cursor.move_left(3); cursor.move_left(3);
@ -81,7 +87,9 @@ pub fn move_left() {
/// Save and reset cursor position | demonstration.. /// Save and reset cursor position | demonstration..
pub fn safe_and_reset_position() { pub fn safe_and_reset_position() {
let mut cursor = cursor(&Screen::default()); let screen = Screen::default();
let mut cursor = cursor(&screen);
// Goto X: 5 Y: 5 // Goto X: 5 Y: 5
cursor.goto(5, 5); cursor.goto(5, 5);
// Safe cursor position: X: 5 Y: 5 // Safe cursor position: X: 5 Y: 5
@ -100,19 +108,22 @@ pub fn safe_and_reset_position() {
/// Hide cursor display | demonstration. /// Hide cursor display | demonstration.
pub fn hide_cursor() { pub fn hide_cursor() {
let mut cursor = cursor(&Screen::default()); let screen = Screen::default();
let mut cursor = cursor(&screen);
cursor.hide(); cursor.hide();
} }
/// Show cursor display | demonstration. /// Show cursor display | demonstration.
pub fn show_cursor() { pub fn show_cursor() {
let mut cursor = cursor(&Screen::default()); let screen = Screen::default();
let mut cursor = cursor(&screen);
cursor.show(); cursor.show();
} }
/// Show cursor display, only works on certain terminals.| demonstration /// Show cursor display, only works on certain terminals.| demonstration
pub fn blink_cursor() { pub fn blink_cursor() {
let mut cursor = cursor(&Screen::default()); let screen = Screen::default();
let mut cursor = cursor(&screen);
cursor.blink(false); cursor.blink(false);
cursor.blink(false); cursor.blink(false);
} }

View File

@ -9,19 +9,18 @@
extern crate crossterm; extern crate crossterm;
// modules that could be test // modules that could be test
mod terminal; mod terminal;
mod color; mod color;
mod cursor; mod cursor;
mod some_types; mod some_types;
mod input; mod input;
use crossterm::{Screen, Crossterm};
use std::{time, thread};
use crossterm::cursor::cursor;
fn main() { fn main() {
// call some test module function
// terminal::terminal::resize_terminal(); thread::sleep(time::Duration::from_millis(2000));
input::keyboard::async_input::read_async_demo();
// use crossterm::screen::RawScreen;
// RawScreen::into_raw_mode();
// RawScreen::disable_raw_modes();
} }

View File

@ -11,13 +11,13 @@ use std::time::Duration;
/// this will capture the input until the given key. /// this will capture the input until the given key.
pub fn read_async_until() { pub fn read_async_until() {
// create raw screen // create raw screen
let screen = Screen::new(true); let screen = Screen::default();
let crossterm = Crossterm::new(); let crossterm = Crossterm::new(&screen);
// init some modules we use for this demo // init some modules we use for this demo
let input = crossterm.input(&screen); let input = crossterm.input();
let terminal = crossterm.terminal(&screen); let terminal = crossterm.terminal();
let mut cursor = crossterm.cursor(&screen); let mut cursor = crossterm.cursor();
let mut stdin = input.read_until_async(b'\r').bytes(); let mut stdin = input.read_until_async(b'\r').bytes();
@ -44,7 +44,8 @@ pub fn read_async_until() {
/// this will read pressed characters async until `x` is typed. /// this will read pressed characters async until `x` is typed.
pub fn read_async() { pub fn read_async() {
let input = input(&Screen::default()); let screen = Screen::default();
let input = input(&screen);
let mut stdin = input.read_async().bytes(); let mut stdin = input.read_async().bytes();
@ -64,12 +65,12 @@ pub fn read_async() {
pub fn read_async_demo() { pub fn read_async_demo() {
let screen = Screen::new(true); let screen = Screen::new(true);
let crossterm = Crossterm::new(); let crossterm = Crossterm::new(&screen);
// init some modules we use for this demo // init some modules we use for this demo
let input = crossterm.input(&screen); let input = crossterm.input();
let terminal = crossterm.terminal(&screen); let terminal = crossterm.terminal();
let mut cursor = crossterm.cursor(&screen); let mut cursor = crossterm.cursor();
// this will setup the async reading. // this will setup the async reading.
let mut stdin = input.read_async().bytes(); let mut stdin = input.read_async().bytes();
@ -101,15 +102,16 @@ pub fn async_reading_on_alternate_screen() {
use crossterm::screen::AlternateScreen; use crossterm::screen::AlternateScreen;
let screen = Screen::new(false); let screen = Screen::new(false);
let crossterm = Crossterm::new();
// switch to alternate screen // switch to alternate screen
if let Ok(alternate) = screen.enable_alternate_modes(true) if let Ok(alternate) = screen.enable_alternate_modes(true)
{ {
let crossterm = Crossterm::new(&alternate.screen);
// init some modules we use for this demo // init some modules we use for this demo
let input = crossterm.input(&alternate.screen); let input = crossterm.input();
let terminal = crossterm.terminal(&alternate.screen); let terminal = crossterm.terminal();
let mut cursor = crossterm.cursor(&alternate.screen); let mut cursor = crossterm.cursor();
// this will setup the async reading. // this will setup the async reading.
let mut stdin = input.read_async().bytes(); let mut stdin = input.read_async().bytes();

View File

@ -4,7 +4,8 @@ use self::crossterm::input::input;
use self::crossterm::Screen; use self::crossterm::Screen;
pub fn read_char() { pub fn read_char() {
let input = input(&Screen::default()); let screen = Screen::default();
let input = input(&screen);
match input.read_char() { match input.read_char() {
Ok(s) => println!("char typed: {}", s), Ok(s) => println!("char typed: {}", s),
@ -13,7 +14,8 @@ pub fn read_char() {
} }
pub fn read_line() { pub fn read_line() {
let input = input(&Screen::default()); let screen = Screen::default();
let input = input(&screen);
match input.read_line() { match input.read_line() {
Ok(s) => println!("string typed: {}", s), Ok(s) => println!("string typed: {}", s),

View File

@ -44,8 +44,8 @@ impl<'screen> FirstDepthSearch<'screen>
// push first position on the stack // push first position on the stack
self.stack.push(self.root_pos); self.stack.push(self.root_pos);
let crossterm = Crossterm::new(); let crossterm = Crossterm::new(&self.screen);
let mut cursor = crossterm.cursor(&self.screen); let mut cursor = crossterm.cursor();
cursor.hide(); cursor.hide();
// loop until there are now items left in the stack. // loop until there are now items left in the stack.

View File

@ -26,7 +26,7 @@ fn main()
pub fn run() pub fn run()
{ {
// This is represents the current screen. // This is represents the current screen.
let screen = Screen::new(true); let screen = Screen::default();
// set size of terminal so the map we are going to draw is fitting the screen. // set size of terminal so the map we are going to draw is fitting the screen.
terminal(&screen).set_size(60,110); terminal(&screen).set_size(60,110);
@ -58,11 +58,12 @@ fn start_algorithm(screen: &Screen)
fn print_welcome_screen(screen: &Screen) fn print_welcome_screen(screen: &Screen)
{ {
let crossterm = Crossterm::new(&screen);
// create the handle for the cursor and terminal. // create the handle for the cursor and terminal.
let crossterm = Crossterm::new(); let terminal = crossterm.terminal();
let terminal = crossterm.terminal(&screen); let cursor = crossterm.cursor();
let cursor = crossterm.cursor(&screen); let input = crossterm.input();
let input = crossterm.input(&screen);
// clear the screen and print the welcome message. // clear the screen and print the welcome message.
terminal.clear(ClearType::All); terminal.clear(ClearType::All);
@ -88,7 +89,7 @@ fn print_welcome_screen(screen: &Screen)
// print the current counter at the line of `Seconds to Go: {counter}` // print the current counter at the line of `Seconds to Go: {counter}`
cursor.goto(48, 10); cursor.goto(48, 10);
crossterm.style(format!("{}", i)).with(Color::Red).on(Color::Blue).paint(&screen); crossterm.style(format!("{}", i)).with(Color::Red).on(Color::Blue).paint(&crossterm.get_screen());
// 1 second delay // 1 second delay
thread::sleep(time::Duration::from_secs(1)); thread::sleep(time::Duration::from_secs(1));

View File

@ -41,8 +41,8 @@ impl Map
// render the map on the screen. // render the map on the screen.
pub fn render_map(&mut self, screen: &Screen) pub fn render_map(&mut self, screen: &Screen)
{ {
let crossterm = Crossterm::new(); let crossterm = Crossterm::new(&screen);
let mut cursor = crossterm.cursor(screen); let mut cursor = crossterm.cursor();
for row in self.map.iter_mut() for row in self.map.iter_mut()
{ {

View File

@ -1,20 +1,21 @@
extern crate crossterm; extern crate crossterm;
use crossterm::{Crossterm, Screen}; use crossterm::{Crossterm, Screen};
use crossterm::style::Color;
#[test]
/// use the `Crossterm` to get an instance to the cursor module | demonstration. /// use the `Crossterm` to get an instance to the cursor module | demonstration.
pub fn use_crossterm_cursor() pub fn use_crossterm_cursor()
{ {
let screen = Screen::new(); let screen = Screen::default();
// Create the crossterm type to access different modules. // Create the crossterm type to access different modules.
let crossterm = Crossterm::new(); let crossterm = Crossterm::new(&screen);
// pass a reference to the current screen. // pass a reference to the current screen.
let cursor = crossterm.cursor(&screen); let cursor = crossterm.cursor();
let color = crossterm.color(&screen); let color = crossterm.color();
let terminal = crossterm.terminal(&screen); let terminal = crossterm.terminal();
let style = crossterm.style("").with(Color::Black).on(Color::Green);
// perform some actions with the instances above. // perform some actions with the instances above.
} }

View File

@ -8,9 +8,9 @@ use std::io::{stdout, Write};
use std::{thread, time}; use std::{thread, time};
fn print_wait_screen(screen: &Screen) { fn print_wait_screen(screen: &Screen) {
let crossterm = Crossterm::new(); let crossterm = Crossterm::new(screen);
let terminal = crossterm.terminal(&screen); let terminal = crossterm.terminal();
let cursor = crossterm.cursor(&screen); let cursor = crossterm.cursor();
terminal.clear(ClearType::All); terminal.clear(ClearType::All);
cursor.goto(0, 0); cursor.goto(0, 0);

View File

@ -8,9 +8,9 @@ use std::io::{stdout, Write};
use std::{thread, time}; use std::{thread, time};
fn print_wait_screen(screen: &Screen) { fn print_wait_screen(screen: &Screen) {
let crossterm = Crossterm::new(); let crossterm = Crossterm::new(screen);
let terminal = crossterm.terminal(&screen); let terminal = crossterm.terminal();
let cursor = crossterm.cursor(&screen); let cursor = crossterm.cursor();
terminal.clear(ClearType::All); terminal.clear(ClearType::All);
cursor.goto(0, 0); cursor.goto(0, 0);

View File

@ -15,7 +15,8 @@ fn print_test_data() {
/// Clear all lines in terminal | demonstration /// Clear all lines in terminal | demonstration
pub fn clear_all_lines() { pub fn clear_all_lines() {
let mut terminal = terminal(&Screen::default()); let crossterm = Crossterm::new(&Screen::default());
let mut terminal = crossterm.terminal();
print_test_data(); print_test_data();
@ -25,14 +26,13 @@ pub fn clear_all_lines() {
/// Clear all lines from cursor position X:4, Y:4 down | demonstration /// Clear all lines from cursor position X:4, Y:4 down | demonstration
pub fn clear_from_cursor_down() { pub fn clear_from_cursor_down() {
let screen = Screen::default(); let crossterm = Crossterm::new(&Screen::default());
let crossterm = Crossterm::new(); let mut terminal = crossterm.terminal();
let mut terminal = crossterm.terminal(&screen);
print_test_data(); print_test_data();
// Set terminal cursor position (see example for more info). // Set terminal cursor position (see example for more info).
crossterm.cursor(&screen).goto(4, 8); crossterm.cursor().goto(4, 8);
// Clear all cells from current cursor position down. // Clear all cells from current cursor position down.
terminal.clear(ClearType::FromCursorDown); terminal.clear(ClearType::FromCursorDown);
@ -40,14 +40,13 @@ pub fn clear_from_cursor_down() {
/// Clear all lines from cursor position X:4, Y:4 up | demonstration /// Clear all lines from cursor position X:4, Y:4 up | demonstration
pub fn clear_from_cursor_up() { pub fn clear_from_cursor_up() {
let screen = Screen::default(); let crossterm = Crossterm::new(&Screen::default());
let crossterm = Crossterm::new(); let mut terminal = crossterm.terminal();
let mut terminal = crossterm.terminal(&screen);
print_test_data(); print_test_data();
// Set terminal cursor position (see example for more info). // Set terminal cursor position (see example for more info).
crossterm.cursor(&screen).goto(4, 4); crossterm.cursor().goto(4, 4);
// Clear all cells from current cursor position down. // Clear all cells from current cursor position down.
terminal.clear(ClearType::FromCursorUp); terminal.clear(ClearType::FromCursorUp);
@ -55,14 +54,13 @@ pub fn clear_from_cursor_up() {
/// Clear all lines from cursor position X:4, Y:4 up | demonstration /// Clear all lines from cursor position X:4, Y:4 up | demonstration
pub fn clear_current_line() { pub fn clear_current_line() {
let screen = Screen::default(); let crossterm = Crossterm::new(&Screen::default());
let crossterm = Crossterm::new(); let mut terminal = crossterm.terminal();
let mut terminal = crossterm.terminal(&screen);
print_test_data(); print_test_data();
// Set terminal cursor position (see example for more info). // Set terminal cursor position (see example for more info).
crossterm.cursor(&screen).goto(4, 4); crossterm.cursor().goto(4, 4);
// Clear current line cells. // Clear current line cells.
terminal.clear(ClearType::CurrentLine); terminal.clear(ClearType::CurrentLine);
@ -70,14 +68,13 @@ pub fn clear_current_line() {
/// Clear all lines from cursor position X:4, Y:7 up | demonstration /// Clear all lines from cursor position X:4, Y:7 up | demonstration
pub fn clear_until_new_line() { pub fn clear_until_new_line() {
let screen = Screen::default(); let crossterm = Crossterm::new(&Screen::default());
let crossterm = Crossterm::new(); let mut terminal = crossterm.terminal();
let mut terminal = crossterm.terminal(&screen);
print_test_data(); print_test_data();
// Set terminal cursor position (see example for more info). // Set terminal cursor position (see example for more info).
crossterm.cursor(&screen).goto(4, 20); crossterm.cursor().goto(4, 20);
// Clear all the cells until next line. // Clear all the cells until next line.
terminal.clear(ClearType::UntilNewLine); terminal.clear(ClearType::UntilNewLine);
@ -85,7 +82,8 @@ pub fn clear_until_new_line() {
/// Print the the current terminal size | demonstration. /// Print the the current terminal size | demonstration.
pub fn print_terminal_size() { pub fn print_terminal_size() {
let mut terminal = terminal(&Screen::default()); let screen = Screen::default();
let mut terminal = terminal(&screen);
// Get terminal size // Get terminal size
let (width, height) = terminal.terminal_size(); let (width, height) = terminal.terminal_size();
@ -96,14 +94,16 @@ pub fn print_terminal_size() {
/// Set the terminal size to width 10, height: 10 | demonstration. /// Set the terminal size to width 10, height: 10 | demonstration.
pub fn set_terminal_size() { pub fn set_terminal_size() {
let mut terminal = terminal(&Screen::default()); let screen = Screen::default();
let mut terminal = terminal(&screen);
terminal.set_size(10, 10); terminal.set_size(10, 10);
} }
/// Scroll down 10 lines | demonstration. /// Scroll down 10 lines | demonstration.
pub fn scroll_down() { pub fn scroll_down() {
let mut terminal = terminal(&Screen::default()); let screen = Screen::default();
let mut terminal = terminal(&screen);
print_test_data(); print_test_data();
@ -113,7 +113,8 @@ pub fn scroll_down() {
/// Scroll down 10 lines | demonstration. /// Scroll down 10 lines | demonstration.
pub fn scroll_up() { pub fn scroll_up() {
let mut terminal = terminal(&Screen::default()); let screen = Screen::default();
let mut terminal = terminal(&screen);
print_test_data(); print_test_data();
@ -123,7 +124,8 @@ pub fn scroll_up() {
/// Resize the terminal to X: 10, Y: 10 | demonstration. /// Resize the terminal to X: 10, Y: 10 | demonstration.
pub fn resize_terminal() { pub fn resize_terminal() {
let mut terminal = terminal(&Screen::default()); let screen = Screen::default();
let mut terminal = terminal(&screen);
// Get terminal size // Get terminal size
terminal.set_size(10, 10); terminal.set_size(10, 10);
@ -131,6 +133,7 @@ pub fn resize_terminal() {
/// exit the current proccess. /// exit the current proccess.
pub fn exit() { pub fn exit() {
let mut terminal = terminal(&Screen::default()); let screen = Screen::default();
let mut terminal = terminal(&screen);
terminal.exit(); terminal.exit();
} }

View File

@ -54,13 +54,14 @@ use write::Stdout;
/// } /// }
/// ///
/// ``` /// ```
pub struct Crossterm { } pub struct Crossterm {
stdout: Arc<Stdout>
}
impl<'crossterm> Crossterm { impl<'crossterm> Crossterm {
/// Create a new instance of `Crossterm` /// Create a new instance of `Crossterm`
pub fn new() -> Crossterm { pub fn new(screen: &Screen) -> Crossterm {
Crossterm {} Crossterm { stdout: screen.stdout.clone() }
} }
/// Get an TerminalCursor implementation whereon cursor related actions can be performed. /// Get an TerminalCursor implementation whereon cursor related actions can be performed.
@ -72,12 +73,12 @@ impl<'crossterm> Crossterm {
/// extern crate crossterm; /// extern crate crossterm;
/// use crossterm::{Crossterm, Screen}; /// use crossterm::{Crossterm, Screen};
/// ///
/// let crossterm = Crossterm::new(); /// let crossterm = Crossterm::new(&Screen::default());
/// let cursor = crossterm.cursor(&Screen::default()); /// let cursor = crossterm.cursor();
/// ///
/// ``` /// ```
pub fn cursor(&self, screen: &'crossterm Screen) -> cursor::TerminalCursor { pub fn cursor(&self) -> cursor::TerminalCursor {
cursor::TerminalCursor::new(&screen.stdout.clone()) cursor::TerminalCursor::new(&self.stdout)
} }
/// Get an TerminalInput implementation whereon terminal related actions can be performed. /// Get an TerminalInput implementation whereon terminal related actions can be performed.
@ -90,12 +91,12 @@ impl<'crossterm> Crossterm {
/// use crossterm::{Crossterm, Screen}; /// use crossterm::{Crossterm, Screen};
/// use crossterm::terminal; /// use crossterm::terminal;
/// ///
/// let crossterm = Crossterm::new(); /// let crossterm = Crossterm::new(&Screen::default());
/// let input = crossterm.input(&Screen::default()); /// let input = crossterm.input();
/// ///
/// ``` /// ```
pub fn input(&self, screen: &'crossterm Screen) -> input::TerminalInput { pub fn input(&self) -> input::TerminalInput {
return input::TerminalInput::new(&screen.stdout); return input::TerminalInput::new(&self.stdout);
} }
/// Get an Terminal implementation whereon terminal related actions can be performed. /// Get an Terminal implementation whereon terminal related actions can be performed.
@ -107,12 +108,12 @@ impl<'crossterm> Crossterm {
/// extern crate crossterm; /// extern crate crossterm;
/// use crossterm::{Crossterm, Screen}; /// use crossterm::{Crossterm, Screen};
/// ///
/// let crossterm = Crossterm::new(); /// let crossterm = Crossterm::new(&Screen::default());
/// let mut terminal = crossterm.terminal(&Screen::default()); /// let mut terminal = crossterm.terminal();
/// ///
/// ``` /// ```
pub fn terminal(&self, screen: &'crossterm Screen) -> terminal::Terminal { pub fn terminal(&self) -> terminal::Terminal {
return terminal::Terminal::new(&screen.stdout); return terminal::Terminal::new(&self.stdout);
} }
/// Get an TerminalColor implementation whereon color related actions can be performed. /// Get an TerminalColor implementation whereon color related actions can be performed.
@ -124,12 +125,12 @@ impl<'crossterm> Crossterm {
/// extern crate crossterm; /// extern crate crossterm;
/// use crossterm::{Crossterm, Screen}; /// use crossterm::{Crossterm, Screen};
/// ///
/// let crossterm = Crossterm::new(); /// let crossterm = Crossterm::new(&Screen::default());
/// let mut terminal = crossterm.terminal(&Screen::default()); /// let mut terminal = crossterm.terminal();
/// ///
/// ``` /// ```
pub fn color(&self, screen: &'crossterm Screen) -> style::TerminalColor { pub fn color(&self) -> style::TerminalColor {
return style::TerminalColor::new(&screen.stdout); return style::TerminalColor::new(&self.stdout);
} }
/// This could be used to style an Displayable with colors and attributes. /// This could be used to style an Displayable with colors and attributes.

View File

@ -20,8 +20,10 @@ use std::sync::Arc;
/// ///
/// extern crate crossterm; /// extern crate crossterm;
/// use self::crossterm::cursor::cursor; /// use self::crossterm::cursor::cursor;
/// use self::crossterm::Screen;
/// ///
/// let mut cursor = cursor(); /// let screen = Screen::default();
/// let mut cursor = cursor(&screen);
/// ///
/// // Get cursor and goto pos X: 5, Y: 10 /// // Get cursor and goto pos X: 5, Y: 10
/// cursor.goto(5,10); /// cursor.goto(5,10);
@ -32,14 +34,14 @@ use std::sync::Arc;
/// cursor.move_left(2); /// cursor.move_left(2);
/// ///
/// ``` /// ```
pub struct TerminalCursor { pub struct TerminalCursor<'stdout> {
screen: Arc<Stdout>, screen: &'stdout Arc<Stdout>,
terminal_cursor: Box<ITerminalCursor>, terminal_cursor: Box<ITerminalCursor>,
} }
impl TerminalCursor { impl<'stdout> TerminalCursor<'stdout> {
/// Create new cursor instance whereon cursor related actions can be performed. /// Create new cursor instance whereon cursor related actions can be performed.
pub fn new(screen: &Arc<Stdout>) -> TerminalCursor { pub fn new(screen: &'stdout Arc<Stdout>) -> TerminalCursor<'stdout> {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let cursor = let cursor =
functions::get_module::<Box<ITerminalCursor>>(WinApiCursor::new(), AnsiCursor::new()) functions::get_module::<Box<ITerminalCursor>>(WinApiCursor::new(), AnsiCursor::new())
@ -50,7 +52,7 @@ impl TerminalCursor {
TerminalCursor { TerminalCursor {
terminal_cursor: cursor, terminal_cursor: cursor,
screen: screen.clone(), screen: screen,
} }
} }
@ -59,8 +61,8 @@ impl TerminalCursor {
/// #Example /// #Example
/// ///
/// ```rust /// ```rust
/// /// let screen = Screen::default();
/// let cursor = cursor(&Screen::default()); /// let cursor = cursor(&screen);
/// ///
/// // change the cursor to position, x: 4 and y: 5 /// // change the cursor to position, x: 4 and y: 5
/// cursor.goto(4,5); /// cursor.goto(4,5);
@ -76,7 +78,8 @@ impl TerminalCursor {
/// ///
/// ```rust /// ```rust
/// ///
/// let cursor = cursor(&Screen::default()); /// let screen = Screen::default();
/// let cursor = cursor(&screen);
/// ///
/// // get the current cursor pos /// // get the current cursor pos
/// let (x,y) = cursor.pos(); /// let (x,y) = cursor.pos();
@ -91,12 +94,14 @@ impl TerminalCursor {
/// ///
/// ```rust /// ```rust
/// ///
/// let cursor = cursor(&Screen::default()); /// let screen = Screen::default();
/// let cursor = cursor(&screen);
///
/// // Move the cursor to position 3 times to the up in the terminal /// // Move the cursor to position 3 times to the up in the terminal
/// cursor.move_up(3); /// cursor.move_up(3);
/// ///
/// ``` /// ```
pub fn move_up(&mut self, count: u16) -> &mut TerminalCursor { pub fn move_up(&mut self, count: u16) -> &mut TerminalCursor<'stdout> {
self.terminal_cursor.move_up(count, &self.screen); self.terminal_cursor.move_up(count, &self.screen);
self self
} }
@ -107,13 +112,14 @@ impl TerminalCursor {
/// ///
/// ```rust /// ```rust
/// ///
/// let cursor = cursor(&Screen::default()); /// let screen = Screen::default();
/// let cursor = cursor(&screen);
/// ///
/// // Move the cursor to position 3 times to the right in the terminal /// // Move the cursor to position 3 times to the right in the terminal
/// cursor.move_right(3); /// cursor.move_right(3);
/// ///
/// ``` /// ```
pub fn move_right(&mut self, count: u16) -> &mut TerminalCursor { pub fn move_right(&mut self, count: u16) -> &mut TerminalCursor<'stdout> {
self.terminal_cursor.move_right(count, &self.screen); self.terminal_cursor.move_right(count, &self.screen);
self self
} }
@ -124,13 +130,14 @@ impl TerminalCursor {
/// ///
/// ```rust /// ```rust
/// ///
/// let cursor = cursor(&Screen::default()); /// let screen = Screen::default();
/// let cursor = cursor(&screen);
/// ///
/// // Move the cursor to position 3 times to the down in the terminal /// // Move the cursor to position 3 times to the down in the terminal
/// cursor.move_down(3); /// cursor.move_down(3);
/// ///
/// ``` /// ```
pub fn move_down(&mut self, count: u16) -> &mut TerminalCursor { pub fn move_down(&mut self, count: u16) -> &mut TerminalCursor<'stdout> {
self.terminal_cursor.move_down(count, &self.screen); self.terminal_cursor.move_down(count, &self.screen);
self self
} }
@ -141,13 +148,14 @@ impl TerminalCursor {
/// ///
/// ```rust /// ```rust
/// ///
/// let cursor = cursor(&Screen::default()); /// let screen = Screen::default();
/// let cursor = cursor(&screen);
/// ///
/// // Move the cursor to position 3 times to the left in the terminal /// // Move the cursor to position 3 times to the left in the terminal
/// cursor.move_left(3); /// cursor.move_left(3);
/// ///
/// ``` /// ```
pub fn move_left(&mut self, count: u16) -> &mut TerminalCursor { pub fn move_left(&mut self, count: u16) -> &mut TerminalCursor<'stdout> {
self.terminal_cursor.move_left(count, &self.screen); self.terminal_cursor.move_left(count, &self.screen);
self self
} }
@ -160,7 +168,9 @@ impl TerminalCursor {
/// ///
/// ```rust /// ```rust
/// ///
/// let cursor = cursor(&Screen::default()); /// let screen = Screen::default();
/// let cursor = cursor(&screen);
///
/// cursor.safe_position(); /// cursor.safe_position();
/// ///
/// ``` /// ```
@ -176,7 +186,9 @@ impl TerminalCursor {
/// ///
/// ```rust /// ```rust
/// ///
/// let cursor = cursor(&Screen::default()); /// let screen = Screen::default();
/// let cursor = cursor(&screen);
///
/// cursor.reset_position(); /// cursor.reset_position();
/// ///
/// ``` /// ```
@ -204,7 +216,8 @@ impl TerminalCursor {
/// ///
/// ```rust /// ```rust
/// ///
/// let cursor = cursor(&Screen::default()); /// let screen = Screen::default();
/// let cursor = cursor(&screen);
/// cursor.show(); /// cursor.show();
/// ///
/// ``` /// ```
@ -220,7 +233,8 @@ impl TerminalCursor {
/// ///
/// ```rust /// ```rust
/// ///
/// let cursor = cursor(&Screen::default()); /// let screen = Screen::default();
/// let cursor = cursor(&screen);
/// cursor.blink(true); /// cursor.blink(true);
/// cursor.blink(false); /// cursor.blink(false);
/// ///
@ -232,6 +246,6 @@ impl TerminalCursor {
/// Get an TerminalCursor implementation whereon cursor related actions can be performed. /// Get an TerminalCursor implementation whereon cursor related actions can be performed.
/// Pass the reference to any screen you want this type to perform actions on. /// Pass the reference to any screen you want this type to perform actions on.
pub fn cursor(screen_manager: &Screen) -> TerminalCursor { pub fn cursor<'stdout>(screen_manager: &'stdout Screen) -> TerminalCursor<'stdout> {
TerminalCursor::new(&screen_manager.stdout) TerminalCursor::new(&screen_manager.stdout)
} }

View File

@ -14,21 +14,23 @@ use super::*;
/// ```rust /// ```rust
/// ///
/// extern crate crossterm; /// extern crate crossterm;
/// use self::crossterm::Screen;
/// use self::crossterm::input::input; /// use self::crossterm::input::input;
/// ///
/// let input = input(&Screen::default()); /// let screen = Screen::default();
/// let input = input(&screen);
/// let result = input.read_line(); /// let result = input.read_line();
/// let pressed_char = input.read_char(); /// let pressed_char = input.read_char();
/// ///
/// ``` /// ```
pub struct TerminalInput { pub struct TerminalInput<'stdout> {
terminal_input: Box<ITerminalInput>, terminal_input: Box<ITerminalInput>,
stdout: Arc<Stdout>, stdout: &'stdout Arc<Stdout>,
} }
impl TerminalInput{ impl<'stdout> TerminalInput<'stdout> {
/// Create new instance of TerminalInput whereon input related actions could be preformed. /// Create new instance of TerminalInput whereon input related actions could be preformed.
pub fn new(stdout: &Arc<Stdout>) -> TerminalInput { pub fn new(stdout: &'stdout Arc<Stdout>) -> TerminalInput<'stdout> {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let input = Box::from(WindowsInput::new()); let input = Box::from(WindowsInput::new());
@ -37,7 +39,7 @@ impl TerminalInput{
TerminalInput { TerminalInput {
terminal_input: input, terminal_input: input,
stdout: stdout.clone(), stdout: stdout,
} }
} }
@ -47,7 +49,9 @@ impl TerminalInput{
/// ///
/// ```rust /// ```rust
/// ///
/// match input(&Screen::default()).read_line() { /// let screen = Screen::default();
/// let input = input(&screen);
/// match input.read_line() {
/// Ok(s) => println!("string typed: {}", s), /// Ok(s) => println!("string typed: {}", s),
/// Err(e) => println!("error: {}", e), /// Err(e) => println!("error: {}", e),
/// } /// }
@ -63,7 +67,10 @@ impl TerminalInput{
/// ///
/// ```rust /// ```rust
/// ///
/// match crossterm.input(&Screen::default()).read_char() { /// let screen = Screen::default();
/// let input = input(&screen);
///
/// match input.read_char() {
/// Ok(c) => println!("character pressed: {}", c), /// Ok(c) => println!("character pressed: {}", c),
/// Err(e) => println!("error: {}", e), /// Err(e) => println!("error: {}", e),
/// } /// }
@ -83,9 +90,9 @@ impl TerminalInput{
/// ///
/// // we need to enable raw mode otherwise the characters will be outputted by default before we are able to read them. /// // we need to enable raw mode otherwise the characters will be outputted by default before we are able to read them.
/// let screen = Screen::new(true); /// let screen = Screen::new(true);
/// let crossterm = Crossterm::new(); /// let input = input(&screen);
/// ///
/// let mut stdin = crossterm.input(&screen).read_async().bytes(); /// let mut stdin = input.read_async().bytes();
/// ///
/// for i in 0..100 { /// for i in 0..100 {
/// ///
@ -115,10 +122,10 @@ impl TerminalInput{
/// // we need to enable raw mode otherwise the characters will be outputted by default before we are able to read them. /// // we need to enable raw mode otherwise the characters will be outputted by default before we are able to read them.
/// let screen = Screen::new(true); /// let screen = Screen::new(true);
/// ///
/// let crossterm = Crossterm::new(); /// let crossterm = Crossterm::new(&screen);
/// let input = crossterm.input(&screen); /// let input = crossterm.input();
/// let terminal = crossterm.terminal(&screen); /// let terminal = crossterm.terminal();
/// let mut cursor = crossterm.cursor(&screen); /// let mut cursor = crossterm.cursor();
/// ///
/// ///
/// let mut stdin = input.read_until_async(b'\r').bytes(); /// let mut stdin = input.read_until_async(b'\r').bytes();
@ -151,6 +158,6 @@ impl TerminalInput{
/// Get an Terminal Input implementation whereon input related actions can be performed. /// Get an Terminal Input implementation whereon input related actions can be performed.
/// Pass the reference to any screen you want this type to perform actions on. /// Pass the reference to any screen you want this type to perform actions on.
pub fn input(stdout: &Screen) -> TerminalInput { pub fn input<'stdout>(stdout: &'stdout Screen) -> TerminalInput<'stdout> {
return TerminalInput::new(&stdout.stdout); return TerminalInput::new(&stdout.stdout);
} }

View File

@ -13,8 +13,11 @@ use Screen;
/// ///
/// ```rust /// ```rust
/// ///
/// use crossterm::{Screen, Crossterm} /// use crossterm::{Screen}
/// let colored_terminal = crossterm.color(&Screen::default()); /// use crossterm::color::color;
///
/// let screen = Screen::default();
/// let colored_terminal = color(&screen);
/// ///
/// // set foreground color /// // set foreground color
/// colored_terminal.set_fg(Color::Red); /// colored_terminal.set_fg(Color::Red);
@ -24,14 +27,14 @@ use Screen;
/// colored_terminal.reset(); /// colored_terminal.reset();
/// ///
/// ``` /// ```
pub struct TerminalColor { pub struct TerminalColor<'stdout> {
color: Box<ITerminalColor>, color: Box<ITerminalColor>,
stdout: Arc<Stdout>, stdout: &'stdout Arc<Stdout>,
} }
impl<'terminal> TerminalColor { impl<'stdout> TerminalColor<'stdout> {
/// Create new instance whereon color related actions can be performed. /// Create new instance whereon color related actions can be performed.
pub fn new(stdout: &Arc<Stdout>) -> TerminalColor { pub fn new(stdout: &'stdout Arc<Stdout>) -> TerminalColor<'stdout> {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let color = functions::get_module::<Box<ITerminalColor>>( let color = functions::get_module::<Box<ITerminalColor>>(
Box::from(WinApiColor::new()), Box::from(WinApiColor::new()),
@ -43,7 +46,7 @@ impl<'terminal> TerminalColor {
TerminalColor { TerminalColor {
color, color,
stdout: stdout.clone(), stdout: stdout,
} }
} }
@ -52,7 +55,8 @@ impl<'terminal> TerminalColor {
/// #Example /// #Example
/// ///
/// ```rust /// ```rust
/// let colored_terminal = crossterm.color(&Screen::default()); /// let screen = Screen::default();
/// let colored_terminal = color(&screen);
/// ///
/// // Set foreground color of the font /// // Set foreground color of the font
/// colored_terminal.set_fg(Color::Red); /// colored_terminal.set_fg(Color::Red);
@ -70,7 +74,8 @@ impl<'terminal> TerminalColor {
/// ///
/// ```rust /// ```rust
/// ///
/// let colored_terminal = crossterm.color(&Screen::default()); /// let screen = Screen::default();
/// let colored_terminal = color(&screen);
/// ///
/// // Set background color of the font /// // Set background color of the font
/// colored_terminal.set_bg(Color::Red); /// colored_terminal.set_bg(Color::Red);
@ -87,7 +92,8 @@ impl<'terminal> TerminalColor {
/// ///
/// ```rust /// ```rust
/// ///
/// let colored_terminal = crossterm.color(&Screen::default()); /// let screen = Screen::default();
/// let colored_terminal = color(&screen);
/// colored_terminal.reset(); /// colored_terminal.reset();
/// ///
/// ``` /// ```
@ -114,6 +120,6 @@ impl<'terminal> TerminalColor {
/// Get an Terminal Color implementation whereon color related actions can be performed. /// Get an Terminal Color implementation whereon color related actions can be performed.
/// Pass the reference to any screen you want this type to perform actions on. /// Pass the reference to any screen you want this type to perform actions on.
pub fn color(screen: &Screen) -> TerminalColor { pub fn color<'stdout>(screen: &'stdout Screen) -> TerminalColor<'stdout> {
TerminalColor::new(&screen.stdout) TerminalColor::new(&screen.stdout)
} }

View File

@ -16,21 +16,22 @@ use std::io::Write;
/// ///
/// use crossterm::terminal::terminal; /// use crossterm::terminal::terminal;
/// ///
/// let term = terminal(); /// let screen = Screen::default();
/// let term = terminal(&screen);
/// ///
/// term.scroll_down(5); /// term.scroll_down(5);
/// term.scroll_up(4); /// term.scroll_up(4);
/// let (with, height) = term.terminal_size(); /// let (with, height) = term.terminal_size();
/// ///
/// ``` /// ```
pub struct Terminal { pub struct Terminal<'stdout> {
terminal: Box<ITerminal>, terminal: Box<ITerminal>,
screen: Arc<Stdout>, screen: &'stdout Arc<Stdout>,
} }
impl Terminal { impl<'stdout> Terminal<'stdout> {
/// Create new terminal instance whereon terminal related actions can be performed. /// Create new terminal instance whereon terminal related actions can be performed.
pub fn new(screen: &Arc<Stdout>) -> Terminal { pub fn new(screen: &'stdout Arc<Stdout>) -> Terminal<'stdout> {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let terminal = functions::get_module::<Box<ITerminal>>( let terminal = functions::get_module::<Box<ITerminal>>(
Box::new(WinApiTerminal::new()), Box::new(WinApiTerminal::new()),
@ -42,7 +43,7 @@ impl Terminal {
Terminal { Terminal {
terminal, terminal,
screen: screen.clone(), screen: screen,
} }
} }
@ -52,7 +53,8 @@ impl Terminal {
/// ///
/// ```rust /// ```rust
/// ///
/// let term = terminal(); /// let screen = Screen::default();
/// let mut term = terminal(&screen);
/// ///
/// // clear all cells in terminal. /// // clear all cells in terminal.
/// term.clear(terminal::ClearType::All); /// term.clear(terminal::ClearType::All);
@ -75,8 +77,8 @@ impl Terminal {
/// #Example /// #Example
/// ///
/// ```rust /// ```rust
/// /// let screen = Screen::default();
/// let term = terminal(); /// let mut term = terminal(&screen);
/// ///
/// let size = term.terminal_size(); /// let size = term.terminal_size();
/// println!("{:?}", size); /// println!("{:?}", size);
@ -91,8 +93,8 @@ impl Terminal {
/// #Example /// #Example
/// ///
/// ```rust /// ```rust
/// /// let screen = Screen::default();
/// let term = terminal(); /// let mut term = terminal(&screen);
/// ///
/// // scroll up by 5 lines /// // scroll up by 5 lines
/// let size = term.scroll_up(5); /// let size = term.scroll_up(5);
@ -107,8 +109,8 @@ impl Terminal {
/// #Example /// #Example
/// ///
/// ```rust /// ```rust
/// /// let screen = Screen::default();
/// let term = terminal(); /// let mut term = terminal(&screen);
/// ///
/// // scroll down by 5 lines /// // scroll down by 5 lines
/// let size = term.scroll_down(5); /// let size = term.scroll_down(5);
@ -123,8 +125,8 @@ impl Terminal {
/// #Example /// #Example
/// ///
/// ```rust /// ```rust
/// /// let screen = Screen::default();
/// let term = terminal(); /// let mut term = terminal(&screen);
/// ///
/// // Set of the size to X: 10 and Y: 10 /// // Set of the size to X: 10 and Y: 10
/// let size = term.set_size(10,10); /// let size = term.set_size(10,10);
@ -139,8 +141,8 @@ impl Terminal {
/// #Example /// #Example
/// ///
/// ```rust /// ```rust
/// /// let screen = Screen::default();
/// let term = terminal(); /// let mut term = terminal(&screen);
/// ///
/// let size = term.exit(); /// let size = term.exit();
/// ///
@ -154,8 +156,8 @@ impl Terminal {
/// #Example /// #Example
/// ///
/// ```rust /// ```rust
/// /// let screen = Screen::default();
/// let term = terminal(); /// let mut term = terminal(&screen);
/// ///
/// let size = term.write("Some text \n Some text on new line"); /// let size = term.write("Some text \n Some text on new line");
/// ///
@ -170,6 +172,6 @@ impl Terminal {
/// Get an terminal implementation whereon terminal related actions could performed. /// Get an terminal implementation whereon terminal related actions could performed.
/// Pass the reference to any screen you want this type to perform actions on. /// Pass the reference to any screen you want this type to perform actions on.
pub fn terminal(screen: &Screen) -> Terminal { pub fn terminal<'stdout>(screen: &'stdout Screen) -> Terminal<'stdout> {
Terminal::new(&screen.stdout) Terminal::new(&screen.stdout)
} }