diff --git a/README.md b/README.md index 7c68987..30bd7ef 100644 --- a/README.md +++ b/README.md @@ -52,26 +52,19 @@ use self::crossterm::style::*; use self::crossterm::cursor::*; // this mudule is used for terminal related actions 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 -- Code documentation: -version [0.1.0](https://docs.rs/crossterm/0.1.0/crossterm/), -[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) - +- Code [documentation](link). +- Code [Examples]() (see [branches](LINK_TO_BRANCHES) for previous versions) - [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 These are the features from this crate: @@ -96,101 +89,101 @@ These are the features from this crate: - Set the size of the terminal. - Alternate screen - Raw screen +- Input + - Read character + - Read line + - Read async + - Read async until - Exit the current process. - Detailed documentation on every item. +- Crossplatform ## Examples 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) -This is a wrapper for the modules crossterm provides. This is introduced to manage the [`Context`](link_to_context) for the user. +### Crossterm Type | [see more](Link) +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. let color = crossterm.color(); let cursor = crossterm.cursor(); let terminal = crossterm.terminal(); -// write text to console wheter it be the main screen or the alternate screen. -crossterm.write("some text"); -// print some styled font. -println!("{}", crossterm.paint("Red font on blue background").with(Color::Red).on(Color::Blue)); +// styling +let style = crossterm.style("Black font on Green background color").with(Color::Black).on(Color::Green); +style.paint(&screen); + ``` -### 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. ```rust -use crossterm::style::{Color}; -use crossterm::Crossterm; - -// 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. - -// create instance of `Crossterm` -let crossterm = Crossterm::new(); +use crossterm::style::{Color, style}; +use crossterm::Screen; -// store style in styled object and print it -let mut styledobject = crossterm.paint("stored styled font in variable").with(Color::Green).on(Color::Yellow); -println!("{}",styledobject); +// store objcets so it could be painted later to the screen. +let style1 = style("Some Blue font on Black background").with(Color::Blue).on(Color::Black); +let style2 = style("Some Red font on Yellow background").with(Color::Red).on(Color::Yellow); -// Or you can print it directly. -println!("{}", crossterm.paint("Red font on blue background color").with(Color::Red).on(Color::Blue)); -println!("{}", crossterm.paint("Red font on default background color").with(Color::Red)); -println!("{}", crossterm.paint("Default font color on Blue background color").on(Color::Blue)); +let screen = Screen::default(); -/// The following code can only be used for unix systems: +/// ! The following code only works for unix based systems. +// some attributes +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(); -// Set background Color from RGB -println!("RGB (10,10,10): \t {}", crossterm.paint(" ").on(Color::Rgb {r: 10, g: 10, b: 10})); -// Set background Color from RGB -println!("ANSI value (50): \t {}", crossterm.paint(" ").on(Color::AnsiValue(50))); +// paint styled text to screen (this could also be called inline) +style1.paint(&screen); +style2.paint(&screen); +bold.paint(&screen); +hidden.paint(&screen); + +// cursom rgb value +style("RGB color (10,10,10) ").with(Color::Rgb { + r: 10, + g: 10, + b: 10 +}).paint(&screen); + +// custom ansi color value +style("ANSI color value (50) ").with(Color::AnsiValue(50)).paint(&screen); -// 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. ```rust -use crossterm::Context; +use crossterm::Screen; use crossterm::cursor::cursor; -// create context to pass to the `cursor()` function. -let context = Context::new(); -let mut cursor = cursor(&context); +// create Screen to wheron the `cursor()` should function. +let screen = Screen::default(); +let mut cursor = cursor(&screen); -/// Moving the cursor | demo +/// 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 +// Move the cursor up,right,down,left 3 cells. 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 | demo +/// Safe the current cursor position to recall later // Goto X: 5 Y: 5 cursor.goto(5,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. ```rust use crossterm::terminal::{terminal,ClearType}; -use crossterm::Context; +use crossterm::Screen; -let mut context = Context::new(); -let mut terminal = terminal(&context); +// create Screen to wheron the `terminal()` should function. +let screen = Screen::default(); +let mut terminal = terminal(&screen); // Clear all lines in terminal; terminal.clear(ClearType::All); @@ -235,17 +229,14 @@ terminal.clear(ClearType::CurrentLine); terminal.clear(ClearType::UntilNewLine); // Get terminal size -let terminal_size = terminal.terminal_size(); -// Print results -print!("X: {}, y: {}", terminal_size.0, terminal_size.1); +let (width, height) = terminal.terminal_size(); +print!("X: {}, y: {}", width, height); -// Scroll down 10 lines. +// Scroll down, up 10 lines. terminal.scroll_down(10); - -// Scroll up 10 lines. terminal.scroll_up(10); -// Set terminal size +// Set terminal size (width, height) terminal.set_size(10,10); // exit the current process. @@ -253,9 +244,6 @@ terminal.exit(); // write to the terminal whether you are on the main screen or alternate screen. terminal.write("Some text\n Some text on new line"); - -// 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. @@ -272,10 +260,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. 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 This library is not stable yet but I expect it to not to change that much anymore. @@ -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. - Handling mouse events - I want to be able to do something based on the clicks the user has done with its mouse. - Handling key events - I want to be able to read key combination inputs. -- 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 - 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 - -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 +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. - 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. +Check [Contributing](link) for more info about branches and code architecture. ## Authors diff --git a/UPGRADE Manual b/UPGRADE Manual deleted file mode 100644 index e82cce1..0000000 --- a/UPGRADE Manual +++ /dev/null @@ -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(); diff --git a/changes.md b/changes.md deleted file mode 100644 index b55b5d8..0000000 --- a/changes.md +++ /dev/null @@ -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. \ No newline at end of file diff --git a/docs/Contributing.md b/docs/Contributing.md new file mode 100644 index 0000000..cafec3b --- /dev/null +++ b/docs/Contributing.md @@ -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. \ No newline at end of file diff --git a/docs/ReleaseNotesVersion 0.3.0.md b/docs/ReleaseNotes.md similarity index 100% rename from docs/ReleaseNotesVersion 0.3.0.md rename to docs/ReleaseNotes.md diff --git a/docs/UpgradeManual.md b/docs/UpgradeManual.md index c5fa84b..0894a79 100644 --- a/docs/UpgradeManual.md +++ b/docs/UpgradeManual.md @@ -72,7 +72,7 @@ let screen = Screen::default(); // this will create a new screen with raw modes enabled. 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) { let cursor = cursor(&alternate.screen); @@ -81,6 +81,7 @@ if let Ok(alternate) = screen.enable_alternate_modes(false) #### Other - ::crossterm::Crossterm::write() is gone. +- ::crossterm::Crossterm::flush() is gone. - Context type is removed - StateManager is removed - ScreenManager type is renamed to Stdout. diff --git a/examples/color/mod.rs b/examples/color/mod.rs index a50cae7..b05f3a5 100644 --- a/examples/color/mod.rs +++ b/examples/color/mod.rs @@ -8,6 +8,8 @@ use self::crossterm::{terminal, Screen}; /// print some red font | demonstration. pub fn paint_foreground() { + + let screen = Screen::default(); // 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. @@ -66,7 +68,7 @@ pub fn print_all_foreground_colors() { }).paint(&screen); #[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. diff --git a/examples/cursor/mod.rs b/examples/cursor/mod.rs index 504ac7c..685b89e 100644 --- a/examples/cursor/mod.rs +++ b/examples/cursor/mod.rs @@ -9,7 +9,8 @@ use self::crossterm::Screen; /// Set the cursor to position X: 10, Y: 5 in the terminal. pub fn goto() { // 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 cursor.goto(10, 5); } @@ -17,7 +18,8 @@ pub fn goto() { /// get the cursor position pub fn pos() { // Get the cursor - let mut cursor = cursor(&Screen::default()); + let screen = Screen::default(); + let mut cursor = cursor(&screen); // get the cursor position. let (x, y) = cursor.pos(); } @@ -25,7 +27,8 @@ pub fn pos() { /// Move the cursor 3 up | demonstration. pub fn move_up() { // 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 cursor.move_up(10); @@ -33,21 +36,24 @@ pub fn move_up() { /// Move the cursor 3 to the right | demonstration. 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 cursor.move_right(3); } /// Move the cursor 3 down | demonstration. 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 cursor.move_down(3); } /// Move the cursor 3 to the left | demonstration. 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 cursor.move_left(3); @@ -81,7 +87,9 @@ pub fn move_left() { /// Save and reset cursor position | demonstration.. 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 cursor.goto(5, 5); // Safe cursor position: X: 5 Y: 5 @@ -100,19 +108,22 @@ pub fn safe_and_reset_position() { /// Hide cursor display | demonstration. pub fn hide_cursor() { - let mut cursor = cursor(&Screen::default()); + let screen = Screen::default(); + let mut cursor = cursor(&screen); cursor.hide(); } /// Show cursor display | demonstration. pub fn show_cursor() { - let mut cursor = cursor(&Screen::default()); + let screen = Screen::default(); + let mut cursor = cursor(&screen); cursor.show(); } /// Show cursor display, only works on certain terminals.| demonstration 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); } diff --git a/examples/examples.rs b/examples/examples.rs index de7673b..a0b115d 100644 --- a/examples/examples.rs +++ b/examples/examples.rs @@ -9,19 +9,18 @@ extern crate crossterm; // modules that could be test - mod terminal; - mod color; - mod cursor; - mod some_types; - mod input; +mod terminal; +mod color; +mod cursor; +mod some_types; +mod input; +use crossterm::{Screen, Crossterm}; +use std::{time, thread}; +use crossterm::cursor::cursor; fn main() { - // call some test module function -// terminal::terminal::resize_terminal(); - input::keyboard::async_input::read_async_demo(); -// use crossterm::screen::RawScreen; -// RawScreen::into_raw_mode(); -// RawScreen::disable_raw_modes(); + thread::sleep(time::Duration::from_millis(2000)); + } diff --git a/examples/input/keyboard/async_input.rs b/examples/input/keyboard/async_input.rs index ffbe1d1..acbb3a2 100644 --- a/examples/input/keyboard/async_input.rs +++ b/examples/input/keyboard/async_input.rs @@ -11,13 +11,13 @@ use std::time::Duration; /// this will capture the input until the given key. pub fn read_async_until() { // create raw screen - let screen = Screen::new(true); - let crossterm = Crossterm::new(); + let screen = Screen::default(); + let crossterm = Crossterm::new(&screen); // init some modules we use for this demo - let input = crossterm.input(&screen); - let terminal = crossterm.terminal(&screen); - let mut cursor = crossterm.cursor(&screen); + let input = crossterm.input(); + let terminal = crossterm.terminal(); + let mut cursor = crossterm.cursor(); 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. pub fn read_async() { - let input = input(&Screen::default()); + let screen = Screen::default(); + let input = input(&screen); let mut stdin = input.read_async().bytes(); @@ -64,12 +65,12 @@ pub fn read_async() { pub fn read_async_demo() { let screen = Screen::new(true); - let crossterm = Crossterm::new(); + let crossterm = Crossterm::new(&screen); // init some modules we use for this demo - let input = crossterm.input(&screen); - let terminal = crossterm.terminal(&screen); - let mut cursor = crossterm.cursor(&screen); + let input = crossterm.input(); + let terminal = crossterm.terminal(); + let mut cursor = crossterm.cursor(); // this will setup the async reading. let mut stdin = input.read_async().bytes(); @@ -101,15 +102,16 @@ pub fn async_reading_on_alternate_screen() { use crossterm::screen::AlternateScreen; let screen = Screen::new(false); - let crossterm = Crossterm::new(); + // switch to alternate screen if let Ok(alternate) = screen.enable_alternate_modes(true) { + let crossterm = Crossterm::new(&alternate.screen); // init some modules we use for this demo - let input = crossterm.input(&alternate.screen); - let terminal = crossterm.terminal(&alternate.screen); - let mut cursor = crossterm.cursor(&alternate.screen); + let input = crossterm.input(); + let terminal = crossterm.terminal(); + let mut cursor = crossterm.cursor(); // this will setup the async reading. let mut stdin = input.read_async().bytes(); diff --git a/examples/input/keyboard/input.rs b/examples/input/keyboard/input.rs index 979344f..9186d7d 100644 --- a/examples/input/keyboard/input.rs +++ b/examples/input/keyboard/input.rs @@ -4,7 +4,8 @@ use self::crossterm::input::input; use self::crossterm::Screen; pub fn read_char() { - let input = input(&Screen::default()); + let screen = Screen::default(); + let input = input(&screen); match input.read_char() { Ok(s) => println!("char typed: {}", s), @@ -13,7 +14,8 @@ pub fn read_char() { } pub fn read_line() { - let input = input(&Screen::default()); + let screen = Screen::default(); + let input = input(&screen); match input.read_line() { Ok(s) => println!("string typed: {}", s), diff --git a/examples/program_examples/first_depth_search/src/algorithm.rs b/examples/program_examples/first_depth_search/src/algorithm.rs index 9ed7edf..815fa8a 100644 --- a/examples/program_examples/first_depth_search/src/algorithm.rs +++ b/examples/program_examples/first_depth_search/src/algorithm.rs @@ -44,8 +44,8 @@ impl<'screen> FirstDepthSearch<'screen> // push first position on the stack self.stack.push(self.root_pos); - let crossterm = Crossterm::new(); - let mut cursor = crossterm.cursor(&self.screen); + let crossterm = Crossterm::new(&self.screen); + let mut cursor = crossterm.cursor(); cursor.hide(); // loop until there are now items left in the stack. diff --git a/examples/program_examples/first_depth_search/src/main.rs b/examples/program_examples/first_depth_search/src/main.rs index 7ef3a27..90e781d 100644 --- a/examples/program_examples/first_depth_search/src/main.rs +++ b/examples/program_examples/first_depth_search/src/main.rs @@ -26,7 +26,7 @@ fn main() pub fn run() { // 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. terminal(&screen).set_size(60,110); @@ -58,11 +58,12 @@ fn start_algorithm(screen: &Screen) fn print_welcome_screen(screen: &Screen) { + let crossterm = Crossterm::new(&screen); + // create the handle for the cursor and terminal. - let crossterm = Crossterm::new(); - let terminal = crossterm.terminal(&screen); - let cursor = crossterm.cursor(&screen); - let input = crossterm.input(&screen); + let terminal = crossterm.terminal(); + let cursor = crossterm.cursor(); + let input = crossterm.input(); // clear the screen and print the welcome message. 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}` 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 thread::sleep(time::Duration::from_secs(1)); diff --git a/examples/program_examples/first_depth_search/src/map.rs b/examples/program_examples/first_depth_search/src/map.rs index 6827da1..c9bff13 100644 --- a/examples/program_examples/first_depth_search/src/map.rs +++ b/examples/program_examples/first_depth_search/src/map.rs @@ -41,8 +41,8 @@ impl Map // render the map on the screen. pub fn render_map(&mut self, screen: &Screen) { - let crossterm = Crossterm::new(); - let mut cursor = crossterm.cursor(screen); + let crossterm = Crossterm::new(&screen); + let mut cursor = crossterm.cursor(); for row in self.map.iter_mut() { diff --git a/examples/some_types/mod.rs b/examples/some_types/mod.rs index 43aa23a..4bf7b21 100644 --- a/examples/some_types/mod.rs +++ b/examples/some_types/mod.rs @@ -1,20 +1,21 @@ extern crate crossterm; use crossterm::{Crossterm, Screen}; +use crossterm::style::Color; -#[test] /// use the `Crossterm` to get an instance to the cursor module | demonstration. pub fn use_crossterm_cursor() { - let screen = Screen::new(); + let screen = Screen::default(); // Create the crossterm type to access different modules. - let crossterm = Crossterm::new(); + let crossterm = Crossterm::new(&screen); // pass a reference to the current screen. - let cursor = crossterm.cursor(&screen); - let color = crossterm.color(&screen); - let terminal = crossterm.terminal(&screen); + let cursor = crossterm.cursor(); + let color = crossterm.color(); + let terminal = crossterm.terminal(); + let style = crossterm.style("").with(Color::Black).on(Color::Green); // perform some actions with the instances above. } diff --git a/examples/terminal/alternate_screen.rs b/examples/terminal/alternate_screen.rs index fbbb1b1..8dcca37 100644 --- a/examples/terminal/alternate_screen.rs +++ b/examples/terminal/alternate_screen.rs @@ -8,9 +8,9 @@ use std::io::{stdout, Write}; use std::{thread, time}; fn print_wait_screen(screen: &Screen) { - let crossterm = Crossterm::new(); - let terminal = crossterm.terminal(&screen); - let cursor = crossterm.cursor(&screen); + let crossterm = Crossterm::new(screen); + let terminal = crossterm.terminal(); + let cursor = crossterm.cursor(); terminal.clear(ClearType::All); cursor.goto(0, 0); diff --git a/examples/terminal/raw_mode.rs b/examples/terminal/raw_mode.rs index e3db954..895a56e 100644 --- a/examples/terminal/raw_mode.rs +++ b/examples/terminal/raw_mode.rs @@ -8,9 +8,9 @@ use std::io::{stdout, Write}; use std::{thread, time}; fn print_wait_screen(screen: &Screen) { - let crossterm = Crossterm::new(); - let terminal = crossterm.terminal(&screen); - let cursor = crossterm.cursor(&screen); + let crossterm = Crossterm::new(screen); + let terminal = crossterm.terminal(); + let cursor = crossterm.cursor(); terminal.clear(ClearType::All); cursor.goto(0, 0); diff --git a/examples/terminal/terminal.rs b/examples/terminal/terminal.rs index 3ed9eae..4072ab8 100644 --- a/examples/terminal/terminal.rs +++ b/examples/terminal/terminal.rs @@ -15,7 +15,8 @@ fn print_test_data() { /// Clear all lines in terminal | demonstration 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(); @@ -25,14 +26,13 @@ pub fn clear_all_lines() { /// Clear all lines from cursor position X:4, Y:4 down | demonstration pub fn clear_from_cursor_down() { - let screen = Screen::default(); - let crossterm = Crossterm::new(); - let mut terminal = crossterm.terminal(&screen); + let crossterm = Crossterm::new(&Screen::default()); + let mut terminal = crossterm.terminal(); print_test_data(); // 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. 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 pub fn clear_from_cursor_up() { - let screen = Screen::default(); - let crossterm = Crossterm::new(); - let mut terminal = crossterm.terminal(&screen); + let crossterm = Crossterm::new(&Screen::default()); + let mut terminal = crossterm.terminal(); print_test_data(); // 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. 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 pub fn clear_current_line() { - let screen = Screen::default(); - let crossterm = Crossterm::new(); - let mut terminal = crossterm.terminal(&screen); + let crossterm = Crossterm::new(&Screen::default()); + let mut terminal = crossterm.terminal(); print_test_data(); // Set terminal cursor position (see example for more info). - crossterm.cursor(&screen).goto(4, 4); + crossterm.cursor().goto(4, 4); // Clear current line cells. 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 pub fn clear_until_new_line() { - let screen = Screen::default(); - let crossterm = Crossterm::new(); - let mut terminal = crossterm.terminal(&screen); + let crossterm = Crossterm::new(&Screen::default()); + let mut terminal = crossterm.terminal(); print_test_data(); // 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. terminal.clear(ClearType::UntilNewLine); @@ -85,7 +82,8 @@ pub fn clear_until_new_line() { /// Print the the current terminal size | demonstration. pub fn print_terminal_size() { - let mut terminal = terminal(&Screen::default()); + let screen = Screen::default(); + let mut terminal = terminal(&screen); // Get 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. 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); } /// Scroll down 10 lines | demonstration. pub fn scroll_down() { - let mut terminal = terminal(&Screen::default()); + let screen = Screen::default(); + let mut terminal = terminal(&screen); print_test_data(); @@ -113,7 +113,8 @@ pub fn scroll_down() { /// Scroll down 10 lines | demonstration. pub fn scroll_up() { - let mut terminal = terminal(&Screen::default()); + let screen = Screen::default(); + let mut terminal = terminal(&screen); print_test_data(); @@ -123,7 +124,8 @@ pub fn scroll_up() { /// Resize the terminal to X: 10, Y: 10 | demonstration. pub fn resize_terminal() { - let mut terminal = terminal(&Screen::default()); + let screen = Screen::default(); + let mut terminal = terminal(&screen); // Get terminal size terminal.set_size(10, 10); @@ -131,6 +133,7 @@ pub fn resize_terminal() { /// exit the current proccess. pub fn exit() { - let mut terminal = terminal(&Screen::default()); + let screen = Screen::default(); + let mut terminal = terminal(&screen); terminal.exit(); } diff --git a/src/common/crossterm.rs b/src/common/crossterm.rs index ccef03c..079b457 100644 --- a/src/common/crossterm.rs +++ b/src/common/crossterm.rs @@ -54,13 +54,14 @@ use write::Stdout; /// } /// /// ``` -pub struct Crossterm { } +pub struct Crossterm { + stdout: Arc +} impl<'crossterm> Crossterm { - /// Create a new instance of `Crossterm` - pub fn new() -> Crossterm { - Crossterm {} + pub fn new(screen: &Screen) -> Crossterm { + Crossterm { stdout: screen.stdout.clone() } } /// Get an TerminalCursor implementation whereon cursor related actions can be performed. @@ -72,12 +73,12 @@ impl<'crossterm> Crossterm { /// extern crate crossterm; /// use crossterm::{Crossterm, Screen}; /// - /// let crossterm = Crossterm::new(); - /// let cursor = crossterm.cursor(&Screen::default()); + /// let crossterm = Crossterm::new(&Screen::default()); + /// let cursor = crossterm.cursor(); /// /// ``` - pub fn cursor(&self, screen: &'crossterm Screen) -> cursor::TerminalCursor { - cursor::TerminalCursor::new(&screen.stdout.clone()) + pub fn cursor(&self) -> cursor::TerminalCursor { + cursor::TerminalCursor::new(&self.stdout) } /// Get an TerminalInput implementation whereon terminal related actions can be performed. @@ -90,12 +91,12 @@ impl<'crossterm> Crossterm { /// use crossterm::{Crossterm, Screen}; /// use crossterm::terminal; /// - /// let crossterm = Crossterm::new(); - /// let input = crossterm.input(&Screen::default()); + /// let crossterm = Crossterm::new(&Screen::default()); + /// let input = crossterm.input(); /// /// ``` - pub fn input(&self, screen: &'crossterm Screen) -> input::TerminalInput { - return input::TerminalInput::new(&screen.stdout); + pub fn input(&self) -> input::TerminalInput { + return input::TerminalInput::new(&self.stdout); } /// Get an Terminal implementation whereon terminal related actions can be performed. @@ -107,12 +108,12 @@ impl<'crossterm> Crossterm { /// extern crate crossterm; /// use crossterm::{Crossterm, Screen}; /// - /// let crossterm = Crossterm::new(); - /// let mut terminal = crossterm.terminal(&Screen::default()); + /// let crossterm = Crossterm::new(&Screen::default()); + /// let mut terminal = crossterm.terminal(); /// /// ``` - pub fn terminal(&self, screen: &'crossterm Screen) -> terminal::Terminal { - return terminal::Terminal::new(&screen.stdout); + pub fn terminal(&self) -> terminal::Terminal { + return terminal::Terminal::new(&self.stdout); } /// Get an TerminalColor implementation whereon color related actions can be performed. @@ -124,12 +125,12 @@ impl<'crossterm> Crossterm { /// extern crate crossterm; /// use crossterm::{Crossterm, Screen}; /// - /// let crossterm = Crossterm::new(); - /// let mut terminal = crossterm.terminal(&Screen::default()); + /// let crossterm = Crossterm::new(&Screen::default()); + /// let mut terminal = crossterm.terminal(); /// /// ``` - pub fn color(&self, screen: &'crossterm Screen) -> style::TerminalColor { - return style::TerminalColor::new(&screen.stdout); + pub fn color(&self) -> style::TerminalColor { + return style::TerminalColor::new(&self.stdout); } /// This could be used to style an Displayable with colors and attributes. diff --git a/src/modules/cursor/cursor.rs b/src/modules/cursor/cursor.rs index 24e1c23..b043997 100644 --- a/src/modules/cursor/cursor.rs +++ b/src/modules/cursor/cursor.rs @@ -20,8 +20,10 @@ use std::sync::Arc; /// /// extern crate crossterm; /// 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 /// cursor.goto(5,10); @@ -32,14 +34,14 @@ use std::sync::Arc; /// cursor.move_left(2); /// /// ``` -pub struct TerminalCursor { - screen: Arc, +pub struct TerminalCursor<'stdout> { + screen: &'stdout Arc, terminal_cursor: Box, } -impl TerminalCursor { +impl<'stdout> TerminalCursor<'stdout> { /// Create new cursor instance whereon cursor related actions can be performed. - pub fn new(screen: &Arc) -> TerminalCursor { + pub fn new(screen: &'stdout Arc) -> TerminalCursor<'stdout> { #[cfg(target_os = "windows")] let cursor = functions::get_module::>(WinApiCursor::new(), AnsiCursor::new()) @@ -50,7 +52,7 @@ impl TerminalCursor { TerminalCursor { terminal_cursor: cursor, - screen: screen.clone(), + screen: screen, } } @@ -59,8 +61,8 @@ impl TerminalCursor { /// #Example /// /// ```rust - /// - /// let cursor = cursor(&Screen::default()); + /// let screen = Screen::default(); + /// let cursor = cursor(&screen); /// /// // change the cursor to position, x: 4 and y: 5 /// cursor.goto(4,5); @@ -76,7 +78,8 @@ impl TerminalCursor { /// /// ```rust /// - /// let cursor = cursor(&Screen::default()); + /// let screen = Screen::default(); + /// let cursor = cursor(&screen); /// /// // get the current cursor pos /// let (x,y) = cursor.pos(); @@ -91,12 +94,14 @@ impl TerminalCursor { /// /// ```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 /// 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 } @@ -107,13 +112,14 @@ impl TerminalCursor { /// /// ```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 /// 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 } @@ -124,13 +130,14 @@ impl TerminalCursor { /// /// ```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 /// 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 } @@ -141,13 +148,14 @@ impl TerminalCursor { /// /// ```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 /// 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 } @@ -160,7 +168,9 @@ impl TerminalCursor { /// /// ```rust /// - /// let cursor = cursor(&Screen::default()); + /// let screen = Screen::default(); + /// let cursor = cursor(&screen); + /// /// cursor.safe_position(); /// /// ``` @@ -176,7 +186,9 @@ impl TerminalCursor { /// /// ```rust /// - /// let cursor = cursor(&Screen::default()); + /// let screen = Screen::default(); + /// let cursor = cursor(&screen); + /// /// cursor.reset_position(); /// /// ``` @@ -204,7 +216,8 @@ impl TerminalCursor { /// /// ```rust /// - /// let cursor = cursor(&Screen::default()); + /// let screen = Screen::default(); + /// let cursor = cursor(&screen); /// cursor.show(); /// /// ``` @@ -220,7 +233,8 @@ impl TerminalCursor { /// /// ```rust /// - /// let cursor = cursor(&Screen::default()); + /// let screen = Screen::default(); + /// let cursor = cursor(&screen); /// cursor.blink(true); /// cursor.blink(false); /// @@ -232,6 +246,6 @@ impl TerminalCursor { /// 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. -pub fn cursor(screen_manager: &Screen) -> TerminalCursor { +pub fn cursor<'stdout>(screen_manager: &'stdout Screen) -> TerminalCursor<'stdout> { TerminalCursor::new(&screen_manager.stdout) } diff --git a/src/modules/input/input.rs b/src/modules/input/input.rs index 3168669..653eaca 100644 --- a/src/modules/input/input.rs +++ b/src/modules/input/input.rs @@ -14,21 +14,23 @@ use super::*; /// ```rust /// /// extern crate crossterm; +/// use self::crossterm::Screen; /// use self::crossterm::input::input; /// -/// let input = input(&Screen::default()); +/// let screen = Screen::default(); +/// let input = input(&screen); /// let result = input.read_line(); /// let pressed_char = input.read_char(); /// /// ``` -pub struct TerminalInput { +pub struct TerminalInput<'stdout> { terminal_input: Box, - stdout: Arc, + stdout: &'stdout Arc, } -impl TerminalInput{ +impl<'stdout> TerminalInput<'stdout> { /// Create new instance of TerminalInput whereon input related actions could be preformed. - pub fn new(stdout: &Arc) -> TerminalInput { + pub fn new(stdout: &'stdout Arc) -> TerminalInput<'stdout> { #[cfg(target_os = "windows")] let input = Box::from(WindowsInput::new()); @@ -37,7 +39,7 @@ impl TerminalInput{ TerminalInput { terminal_input: input, - stdout: stdout.clone(), + stdout: stdout, } } @@ -47,7 +49,9 @@ impl TerminalInput{ /// /// ```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), /// Err(e) => println!("error: {}", e), /// } @@ -63,7 +67,10 @@ impl TerminalInput{ /// /// ```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), /// 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. /// 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 { /// @@ -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. /// let screen = Screen::new(true); /// - /// let crossterm = Crossterm::new(); - /// let input = crossterm.input(&screen); - /// let terminal = crossterm.terminal(&screen); - /// let mut cursor = crossterm.cursor(&screen); + /// let crossterm = Crossterm::new(&screen); + /// let input = crossterm.input(); + /// let terminal = crossterm.terminal(); + /// let mut cursor = crossterm.cursor(); /// /// /// 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. /// 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); } diff --git a/src/modules/style/color.rs b/src/modules/style/color.rs index 2220eb5..55bdf1c 100644 --- a/src/modules/style/color.rs +++ b/src/modules/style/color.rs @@ -13,8 +13,11 @@ use Screen; /// /// ```rust /// -/// use crossterm::{Screen, Crossterm} -/// let colored_terminal = crossterm.color(&Screen::default()); +/// use crossterm::{Screen} +/// use crossterm::color::color; +/// +/// let screen = Screen::default(); +/// let colored_terminal = color(&screen); /// /// // set foreground color /// colored_terminal.set_fg(Color::Red); @@ -24,14 +27,14 @@ use Screen; /// colored_terminal.reset(); /// /// ``` -pub struct TerminalColor { +pub struct TerminalColor<'stdout> { color: Box, - stdout: Arc, + stdout: &'stdout Arc, } -impl<'terminal> TerminalColor { +impl<'stdout> TerminalColor<'stdout> { /// Create new instance whereon color related actions can be performed. - pub fn new(stdout: &Arc) -> TerminalColor { + pub fn new(stdout: &'stdout Arc) -> TerminalColor<'stdout> { #[cfg(target_os = "windows")] let color = functions::get_module::>( Box::from(WinApiColor::new()), @@ -43,7 +46,7 @@ impl<'terminal> TerminalColor { TerminalColor { color, - stdout: stdout.clone(), + stdout: stdout, } } @@ -52,7 +55,8 @@ impl<'terminal> TerminalColor { /// #Example /// /// ```rust - /// let colored_terminal = crossterm.color(&Screen::default()); + /// let screen = Screen::default(); + /// let colored_terminal = color(&screen); /// /// // Set foreground color of the font /// colored_terminal.set_fg(Color::Red); @@ -70,7 +74,8 @@ impl<'terminal> TerminalColor { /// /// ```rust /// - /// let colored_terminal = crossterm.color(&Screen::default()); + /// let screen = Screen::default(); + /// let colored_terminal = color(&screen); /// /// // Set background color of the font /// colored_terminal.set_bg(Color::Red); @@ -87,7 +92,8 @@ impl<'terminal> TerminalColor { /// /// ```rust /// - /// let colored_terminal = crossterm.color(&Screen::default()); + /// let screen = Screen::default(); + /// let colored_terminal = color(&screen); /// colored_terminal.reset(); /// /// ``` @@ -114,6 +120,6 @@ impl<'terminal> TerminalColor { /// 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. -pub fn color(screen: &Screen) -> TerminalColor { +pub fn color<'stdout>(screen: &'stdout Screen) -> TerminalColor<'stdout> { TerminalColor::new(&screen.stdout) } diff --git a/src/modules/terminal/terminal.rs b/src/modules/terminal/terminal.rs index 68c76e0..91134b8 100644 --- a/src/modules/terminal/terminal.rs +++ b/src/modules/terminal/terminal.rs @@ -16,21 +16,22 @@ use std::io::Write; /// /// use crossterm::terminal::terminal; /// -/// let term = terminal(); +/// let screen = Screen::default(); +/// let term = terminal(&screen); /// /// term.scroll_down(5); /// term.scroll_up(4); /// let (with, height) = term.terminal_size(); /// /// ``` -pub struct Terminal { +pub struct Terminal<'stdout> { terminal: Box, - screen: Arc, + screen: &'stdout Arc, } -impl Terminal { +impl<'stdout> Terminal<'stdout> { /// Create new terminal instance whereon terminal related actions can be performed. - pub fn new(screen: &Arc) -> Terminal { + pub fn new(screen: &'stdout Arc) -> Terminal<'stdout> { #[cfg(target_os = "windows")] let terminal = functions::get_module::>( Box::new(WinApiTerminal::new()), @@ -42,7 +43,7 @@ impl Terminal { Terminal { terminal, - screen: screen.clone(), + screen: screen, } } @@ -52,7 +53,8 @@ impl Terminal { /// /// ```rust /// - /// let term = terminal(); + /// let screen = Screen::default(); + /// let mut term = terminal(&screen); /// /// // clear all cells in terminal. /// term.clear(terminal::ClearType::All); @@ -75,8 +77,8 @@ impl Terminal { /// #Example /// /// ```rust - /// - /// let term = terminal(); + /// let screen = Screen::default(); + /// let mut term = terminal(&screen); /// /// let size = term.terminal_size(); /// println!("{:?}", size); @@ -91,8 +93,8 @@ impl Terminal { /// #Example /// /// ```rust - /// - /// let term = terminal(); + /// let screen = Screen::default(); + /// let mut term = terminal(&screen); /// /// // scroll up by 5 lines /// let size = term.scroll_up(5); @@ -107,8 +109,8 @@ impl Terminal { /// #Example /// /// ```rust - /// - /// let term = terminal(); + /// let screen = Screen::default(); + /// let mut term = terminal(&screen); /// /// // scroll down by 5 lines /// let size = term.scroll_down(5); @@ -123,8 +125,8 @@ impl Terminal { /// #Example /// /// ```rust - /// - /// let term = terminal(); + /// let screen = Screen::default(); + /// let mut term = terminal(&screen); /// /// // Set of the size to X: 10 and Y: 10 /// let size = term.set_size(10,10); @@ -139,8 +141,8 @@ impl Terminal { /// #Example /// /// ```rust - /// - /// let term = terminal(); + /// let screen = Screen::default(); + /// let mut term = terminal(&screen); /// /// let size = term.exit(); /// @@ -154,8 +156,8 @@ impl Terminal { /// #Example /// /// ```rust - /// - /// let term = terminal(); + /// let screen = Screen::default(); + /// let mut term = terminal(&screen); /// /// 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. /// 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) }