From f6b1955cae904c4576723c4ba3814478fd5f74e5 Mon Sep 17 00:00:00 2001 From: TimonPost Date: Fri, 3 Aug 2018 12:01:04 +0200 Subject: [PATCH] worked on some examples --- .../terminal/alternate_screen.rs | 72 --------- examples/Crossterm 0.3.1/terminal/raw_mode.rs | 54 ------- examples/Crossterm 0.3.1/terminal/terminal.rs | 139 ------------------ examples/README.md | 4 + examples/program_examples/README.md | 3 +- examples/{ => program_examples}/duplex.rs | 0 .../first_depth_search/Cargo.toml | 2 +- .../first_depth_search/src/main.rs | 39 +++-- .../first_depth_search/src/map.rs | 4 - .../first_depth_search/src/variables.rs | 1 - examples/simple.rs | 20 ++- examples/terminal/alternate_screen.rs | 56 +++---- examples/terminal/raw_mode.rs | 26 ++-- examples/terminal/terminal.rs | 83 ++++------- 14 files changed, 118 insertions(+), 385 deletions(-) delete mode 100644 examples/Crossterm 0.3.1/terminal/alternate_screen.rs delete mode 100644 examples/Crossterm 0.3.1/terminal/raw_mode.rs delete mode 100644 examples/Crossterm 0.3.1/terminal/terminal.rs rename examples/{ => program_examples}/duplex.rs (100%) diff --git a/examples/Crossterm 0.3.1/terminal/alternate_screen.rs b/examples/Crossterm 0.3.1/terminal/alternate_screen.rs deleted file mode 100644 index f0cd764..0000000 --- a/examples/Crossterm 0.3.1/terminal/alternate_screen.rs +++ /dev/null @@ -1,72 +0,0 @@ -extern crate crossterm; - -use crossterm::style::Color; -use crossterm::terminal::{self, ClearType}; -use crossterm::Crossterm; - -use std::io::{stdout, Write}; -use std::{thread, time}; - -fn print_wait_screen(crossterm: &mut Crossterm) { - let mut terminal = crossterm.terminal(); - let mut cursor = crossterm.cursor(); - - terminal.clear(ClearType::All); - - cursor.goto(0, 0); - cursor.hide(); - - terminal.write( - "Welcome to the wait screen.\n\ - Please wait a few seconds until we arrive back at the main screen.\n\ - Progress: ", - ); - - // print some progress example. - for i in 1..5 { - // print the current counter at the line of `Seconds to Go: {counter}` - cursor - .goto(10, 2) - .print(crossterm.paint(format!("{} of the 5 items processed", i)).with(Color::Red).on(Color::Blue)); - - // 1 second delay - thread::sleep(time::Duration::from_secs(1)); - } - - stdout().flush(); -} - -/// print wait screen on alternate screen, then swich back. -pub fn print_wait_screen_on_alternate_window() { - - let mut term = Crossterm::new(); - term.to_alternate_screen(); - - term.write(b"test"); - print_wait_screen(&mut term); -} - -/// some stress test switch from and to alternate screen. -pub fn switch_between_main_and_alternate_screen() { - - { - let mut term = Crossterm::new(); - let cursor = term.cursor(); - - // create new alternate screen instance and switch to the alternate screen. - term.to_alternate_screen(); - cursor.goto(0, 0); - write!(term, "we are at the alternate screen!"); - thread::sleep(time::Duration::from_secs(3)); - - term.to_main_screen(); - write!(term, "we are at the alternate screen!"); - thread::sleep(time::Duration::from_secs(3)); - - term.to_alternate_screen(); - write!(term, "we are at the alternate screen!"); - thread::sleep(time::Duration::from_secs(3)); - } // <- Crossterm goes out of scope. - - println!("Whe are back at the main screen"); -} diff --git a/examples/Crossterm 0.3.1/terminal/raw_mode.rs b/examples/Crossterm 0.3.1/terminal/raw_mode.rs deleted file mode 100644 index 5bc1ce2..0000000 --- a/examples/Crossterm 0.3.1/terminal/raw_mode.rs +++ /dev/null @@ -1,54 +0,0 @@ -extern crate crossterm; - -use crossterm::Crossterm; - -use crossterm::terminal::{self, ClearType}; - -use std::io::{stdout, Write}; -use std::{thread, time}; - -// raw screen is not working correctly currently -fn print_wait_screen(crossterm: &mut Crossterm) { - let terminal = crossterm.terminal(); - let mut cursor = crossterm.cursor(); - - terminal.clear(ClearType::All); - - cursor.goto(0, 0).print("Welcome to the wait screen."); - cursor - .goto(0, 1) - .print("Please wait a few seconds until we arrive back at the main screen."); - cursor.goto(0, 2).print("Progress: "); - - // print some progress example. - for i in 1..5 { - // print the current counter at the line of `Seconds to Go: {counter}` - cursor - .goto(10, 2) - .print(format!("{} of the 5 items processed", i)); - - // 1 second delay - thread::sleep(time::Duration::from_secs(1)); - } -} - -pub fn print_wait_screen_on_alternate_window() { - let mut term = Crossterm::new(); - - // create scope. If this scope ends the screen will be switched back to mainscreen. - // because `AlternateScreen` switches back to main screen when going out of scope. - { - // create new alternate screen instance this call is also switching the screen to alternate screen. - // then convert the output of the program to raw mode. - // then print the wait screen on the alternate screen in raw mode. - term.to_alternate_screen(); - term.enable_raw_mode(); - - // Print the wait screen. - print_wait_screen(&mut term); - - term.flush(); - } - - println!("Whe are back at the main screen"); -} diff --git a/examples/Crossterm 0.3.1/terminal/terminal.rs b/examples/Crossterm 0.3.1/terminal/terminal.rs deleted file mode 100644 index 5ca260f..0000000 --- a/examples/Crossterm 0.3.1/terminal/terminal.rs +++ /dev/null @@ -1,139 +0,0 @@ -//! -//! Terminal Examples -//! - -extern crate crossterm; - -use crossterm::terminal::ClearType; -use crossterm::Crossterm; - -fn print_test_data() { - for i in 0..100 { - println!("Test data to test terminal: {}", i); - } -} - -/// Clear all lines in terminal | demonstration -pub fn clear_all_lines() { - let term = Crossterm::new(); - let mut terminal = term.terminal(); - - print_test_data(); - - // Clear all lines in terminal; - terminal.clear(ClearType::All); -} - -/// Clear all lines from cursor position X:4, Y:4 down | demonstration -pub fn clear_from_cursor_down() { - let term = Crossterm::new(); - let mut terminal = term.terminal(); - - print_test_data(); - - // Set terminal cursor position (see example for more info). - term.cursor().goto(4, 8); - - // Clear all cells from current cursor position down. - terminal.clear(ClearType::FromCursorDown); -} - -/// Clear all lines from cursor position X:4, Y:4 up | demonstration -pub fn clear_from_cursor_up() { - let term = Crossterm::new(); - let mut terminal = term.terminal(); - - print_test_data(); - - // Set terminal cursor position (see example for more info). - term.cursor().goto(4, 4); - - // Clear all cells from current cursor position down. - terminal.clear(ClearType::FromCursorUp); -} - -/// Clear all lines from cursor position X:4, Y:4 up | demonstration -pub fn clear_current_line() { - let term = Crossterm::new(); - let mut terminal = term.terminal(); - - print_test_data(); - - // Set terminal cursor position (see example for more info). - term.cursor().goto(4, 4); - - // Clear current line cells. - terminal.clear(ClearType::CurrentLine); -} - -/// Clear all lines from cursor position X:4, Y:7 up | demonstration -pub fn clear_until_new_line() { - let term = Crossterm::new(); - let mut terminal = term.terminal(); - - print_test_data(); - - // Set terminal cursor position (see example for more info). - term.cursor().goto(4, 20); - - // Clear all the cells until next line. - terminal.clear(ClearType::UntilNewLine); -} - -/// Print the the current terminal size | demonstration. -pub fn print_terminal_size() { - let term = Crossterm::new(); - let mut terminal = term.terminal(); - - // Get terminal size - let (width, height) = terminal.terminal_size(); - - // Print results - print!("X: {}, y: {}", width, height); -} - -/// Set the terminal size to width 10, height: 10 | demonstration. -pub fn set_terminal_size() { - let term = Crossterm::new(); - let mut terminal = term.terminal(); - - terminal.set_size(10, 10); -} - -/// Scroll down 10 lines | demonstration. -pub fn scroll_down() { - let term = Crossterm::new(); - let mut terminal = term.terminal(); - - print_test_data(); - - // Scroll down 10 lines. - terminal.scroll_down(10); -} - -/// Scroll down 10 lines | demonstration. -pub fn scroll_up() { - let term = Crossterm::new(); - let mut terminal = term.terminal(); - - print_test_data(); - - // Scroll up 10 lines. - terminal.scroll_up(5); -} - -/// Resize the terminal to X: 10, Y: 10 | demonstration. -pub fn resize_terminal() { - let term = Crossterm::new(); - let mut terminal = term.terminal(); - - // Get terminal size - terminal.set_size(10, 10); -} - -/// exit the current proccess. -pub fn exit() { - let term = Crossterm::new(); - let mut terminal = term.terminal(); - terminal.exit(); -} diff --git a/examples/README.md b/examples/README.md index 2e1d360..91151bd 100644 --- a/examples/README.md +++ b/examples/README.md @@ -4,4 +4,8 @@ It has 4 modules: - color (this is about all the styling of the terminal) - cursor (this is about all the actions you can perform with the cursor) - terminal (this is about all the actions you can perform on the terminal) +- input (this is about all input actions you can perform on with terminal) +- crossterm_type (this is about the struct `Crossterm`) - program examples (this folder will contain some real life examples) + +To run any example where there is a `main()` you could run `cargo run --example simple`. \ No newline at end of file diff --git a/examples/program_examples/README.md b/examples/program_examples/README.md index 65103a4..d8e0a7a 100644 --- a/examples/program_examples/README.md +++ b/examples/program_examples/README.md @@ -5,4 +5,5 @@ If you have created a game or something feel free to upload it, would be a great The programs are: - First depth search: - This is an search algorithm implemented visually. This program uses the following functionalities: cursor movement, coloring, alternate screen and terminal clearing. \ No newline at end of file + This is an search algorithm implemented visually. This program uses the following functionalities: cursor movement, coloring, alternate screen and terminal clearing. +- Duplex: This is a terminal application where there is some kind of conterminous output and with async input. So you could type an command while text is being outputted. \ No newline at end of file diff --git a/examples/duplex.rs b/examples/program_examples/duplex.rs similarity index 100% rename from examples/duplex.rs rename to examples/program_examples/duplex.rs diff --git a/examples/program_examples/first_depth_search/Cargo.toml b/examples/program_examples/first_depth_search/Cargo.toml index 8f6fe91..b03910c 100644 --- a/examples/program_examples/first_depth_search/Cargo.toml +++ b/examples/program_examples/first_depth_search/Cargo.toml @@ -7,5 +7,5 @@ authors = ["TimonPost "] rand = "0.4.2" [dependencies.crossterm] -path = "../../../../" +path = "../../../" branch = "development" \ No newline at end of file diff --git a/examples/program_examples/first_depth_search/src/main.rs b/examples/program_examples/first_depth_search/src/main.rs index 1ce5e18..f6f8f65 100644 --- a/examples/program_examples/first_depth_search/src/main.rs +++ b/examples/program_examples/first_depth_search/src/main.rs @@ -9,11 +9,11 @@ mod variables; use crossterm::Crossterm; use crossterm::terminal::ClearType; use crossterm::style::Color; -use crossterm::screen; use self::variables::{Size, Position }; use self::messages::WELCOME_MESSAGE; +use std::io::Read; use std::iter::Iterator; use std::{thread, time}; @@ -26,21 +26,21 @@ fn main() pub fn run() { // // create new Crossterm instance. -// let mut crossterm = Crossterm::new(); -// -// print_welcome_screen(&crossterm); -// -// start_algorithm(&mut crossterm); -// -// print_end_screen(&crossterm); + let mut crossterm = Crossterm::new(); + // set size of terminal so the map we are going to draw is fitting the screen. + crossterm.terminal().set_size(110,50); + print_welcome_screen(&mut crossterm); + start_algorithm(&mut crossterm); + + print_end_screen(&crossterm); } fn start_algorithm(crossterm: &mut Crossterm) { // we first want to switch to alternate screen. On the alternate screen we are going to run or firstdepthsearch algorithm - let alternate_screen = screen::AlternateScreen::from(crossterm.context()); + crossterm.to_alternate_screen(); // setup the map size and the position to start searching for a path. let map_size = Size::new(100,40); @@ -60,11 +60,13 @@ fn print_end_screen(crossterm: &Crossterm) } -fn print_welcome_screen(crossterm: &Crossterm) +fn print_welcome_screen(crossterm: &mut Crossterm) { // create the handle for the cursor and terminal. - let mut cursor = crossterm.cursor(); + + crossterm.enable_raw_mode(); let mut terminal = crossterm.terminal(); + let mut cursor = crossterm.cursor(); // clear the screen and print the welcome message. terminal.clear(ClearType::All); @@ -74,15 +76,26 @@ fn print_welcome_screen(crossterm: &Crossterm) cursor.hide(); cursor.goto(0,10); terminal.write( - "The first depth search algorithm will start in: Seconds" + "The first depth search algorithm will start in: Seconds\n\ + Press `q` to abort the program" ); + let input = crossterm.input(); + let mut stdin = input.read_async().bytes(); + // print some progress example. for i in (1..5).rev() { + + let a = stdin.next(); + + if let Some(Ok(b'q')) = a { + terminal.exit(); + } + // print the current counter at the line of `Seconds to Go: {counter}` cursor .goto(48, 10) - .print(terminal.paint(format!("{}", i)).with(Color::Red).on(Color::Blue)); + .print(crossterm.paint(format!("{}", i)).with(Color::Red).on(Color::Blue)); // 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 b86d813..49ac529 100644 --- a/examples/program_examples/first_depth_search/src/map.rs +++ b/examples/program_examples/first_depth_search/src/map.rs @@ -1,10 +1,6 @@ use super::variables::{Cell, Position, Size }; -use crossterm::terminal::terminal; -use crossterm::cursor::cursor; use crossterm::Crossterm; use crossterm::style::{ObjectStyle, StyledObject, Color}; -use crossterm::Context; -use std::rc::Rc; use std::fmt::Display; diff --git a/examples/program_examples/first_depth_search/src/variables.rs b/examples/program_examples/first_depth_search/src/variables.rs index e724a53..03c3fdb 100644 --- a/examples/program_examples/first_depth_search/src/variables.rs +++ b/examples/program_examples/first_depth_search/src/variables.rs @@ -2,7 +2,6 @@ extern crate crossterm; use self::crossterm::terminal::{terminal, ClearType}; use self::crossterm::style::{Color, StyledObject, ObjectStyle }; -use self::crossterm::Context; use std::fmt::Debug; use std::fmt; diff --git a/examples/simple.rs b/examples/simple.rs index 9f8a27c..79f6eee 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,8 +1,10 @@ + //! This bin folder can be used to try the examples out located in the examples directory. //! //! All you need to do is: //! //! - Download the crossterm source code. +//! //! - Add this in the Cargo.toml file: //! ``` [[bin]] //! name = "example_bin" @@ -15,7 +17,7 @@ extern crate crossterm; use crossterm::style::Color; use crossterm::Crossterm; - mod terminal; +// mod terminal; // mod color; // mod cursor; // mod crossterm_type; @@ -26,5 +28,19 @@ use crossterm::Crossterm; use std::{thread, time}; fn main() { - use crossterm::Crossterm; + do_something(); } + +fn do_something() +{ + let mut crossterm = Crossterm::new(); + + { + let mut cursor = crossterm.cursor(); // <- Immutable borrow occurs here ( cursor(&self) ) end lives until the end of this function call. + cursor.goto(10, 10); + } + crossterm.to_alternate_screen(); // <- mutable borrow occurs here ( to_alternate_screen(&mut self) ) but because we already have borrowed immutable we can not mutate it. +} + + + diff --git a/examples/terminal/alternate_screen.rs b/examples/terminal/alternate_screen.rs index d935be7..dbbb7c9 100644 --- a/examples/terminal/alternate_screen.rs +++ b/examples/terminal/alternate_screen.rs @@ -1,20 +1,18 @@ extern crate crossterm; use crossterm::style::Color; -use crossterm::cursor::cursor; -use crossterm::screen::AlternateScreen; use crossterm::terminal::{self, ClearType}; -use crossterm::Context; +use crossterm::Crossterm; use std::io::{stdout, Write}; -use std::rc::Rc; use std::{thread, time}; -fn print_wait_screen(context: Rc) { - let mut terminal = terminal::terminal(&context); +fn print_wait_screen(crossterm: &mut Crossterm) { + let mut terminal = crossterm.terminal(); + let mut cursor = crossterm.cursor(); + terminal.clear(ClearType::All); - let mut cursor = cursor(&context); cursor.goto(0, 0); cursor.hide(); @@ -29,7 +27,7 @@ fn print_wait_screen(context: Rc) { // print the current counter at the line of `Seconds to Go: {counter}` cursor .goto(10, 2) - .print(terminal.paint(format!("{} of the 5 items processed", i)).with(Color::Red).on(Color::Blue)); + .print(crossterm.paint(format!("{} of the 5 items processed", i)).with(Color::Red).on(Color::Blue)); // 1 second delay thread::sleep(time::Duration::from_secs(1)); @@ -39,43 +37,37 @@ fn print_wait_screen(context: Rc) { } /// print wait screen on alternate screen, then swich back. -pub fn print_wait_screen_on_alternate_window(context: Rc) { - // create scope. If this scope ends the screen will be switched back to mainscreen. - // because `AlternateScreen` switches back to main screen when switching back. - { - // create new alternate screen instance and switch to the alternate screen. - let mut screen = AlternateScreen::from(context.clone()); +pub fn print_wait_screen_on_alternate_window() { - write!(screen, "test"); - println!(); - // Print the wait screen. - print_wait_screen(context.clone()); - } + let mut term = Crossterm::new(); + term.to_alternate_screen(); + + term.write(b"test"); + print_wait_screen(&mut term); } /// some stress test switch from and to alternate screen. pub fn switch_between_main_and_alternate_screen() { - let context = Context::new(); - let mut cursor = cursor(&context); { + let mut term = Crossterm::new(); + let mut cursor = term.cursor(); + // create new alternate screen instance and switch to the alternate screen. - let mut screen = AlternateScreen::from(context.clone()); - cursor.goto(0, 0); - write!(screen, "we are at the alternate screen!"); - screen.flush(); + let alternate = term.to_alternate_screen(); + + { cursor.goto(0, 0); } + write!(term, "we are at the alternate screen!"); thread::sleep(time::Duration::from_secs(3)); - screen.to_main(); - write!(screen, "we are at the main screen!"); - screen.flush(); + term.to_main_screen(); + write!(term, "we are at the alternate screen!"); thread::sleep(time::Duration::from_secs(3)); - screen.to_alternate(); - write!(screen, "we are at the alternate screen!"); - screen.flush(); + term.to_alternate_screen(); + write!(term, "we are at the alternate screen!"); thread::sleep(time::Duration::from_secs(3)); - } + } // <- Crossterm goes out of scope. println!("Whe are back at the main screen"); } diff --git a/examples/terminal/raw_mode.rs b/examples/terminal/raw_mode.rs index 2209d70..5bc1ce2 100644 --- a/examples/terminal/raw_mode.rs +++ b/examples/terminal/raw_mode.rs @@ -1,21 +1,19 @@ extern crate crossterm; -use crossterm::cursor::cursor; -use crossterm::screen::AlternateScreen; +use crossterm::Crossterm; + use crossterm::terminal::{self, ClearType}; -use crossterm::Context; use std::io::{stdout, Write}; -use std::rc::Rc; use std::{thread, time}; -use crossterm::raw::IntoRawMode; - // raw screen is not working correctly currently -fn print_wait_screen(context: Rc) { - terminal::terminal(&context).clear(ClearType::All); +fn print_wait_screen(crossterm: &mut Crossterm) { + let terminal = crossterm.terminal(); + let mut cursor = crossterm.cursor(); + + terminal.clear(ClearType::All); - let mut cursor = cursor(&context); cursor.goto(0, 0).print("Welcome to the wait screen."); cursor .goto(0, 1) @@ -35,7 +33,7 @@ fn print_wait_screen(context: Rc) { } pub fn print_wait_screen_on_alternate_window() { - let context = Context::new(); + let mut term = Crossterm::new(); // create scope. If this scope ends the screen will be switched back to mainscreen. // because `AlternateScreen` switches back to main screen when going out of scope. @@ -43,13 +41,13 @@ pub fn print_wait_screen_on_alternate_window() { // create new alternate screen instance this call is also switching the screen to alternate screen. // then convert the output of the program to raw mode. // then print the wait screen on the alternate screen in raw mode. - let mut screen = AlternateScreen::from(context.clone()); - let raw_screen = screen.into_raw_mode(context.clone()); + term.to_alternate_screen(); + term.enable_raw_mode(); // Print the wait screen. - print_wait_screen(context.clone()); + print_wait_screen(&mut term); - screen.flush(); + term.flush(); } println!("Whe are back at the main screen"); diff --git a/examples/terminal/terminal.rs b/examples/terminal/terminal.rs index 97afe3c..5ca260f 100644 --- a/examples/terminal/terminal.rs +++ b/examples/terminal/terminal.rs @@ -4,9 +4,8 @@ extern crate crossterm; -use crossterm::cursor; -use crossterm::terminal::{terminal, ClearType}; -use crossterm::Context; +use crossterm::terminal::ClearType; +use crossterm::Crossterm; fn print_test_data() { for i in 0..100 { @@ -16,10 +15,8 @@ fn print_test_data() { /// Clear all lines in terminal | demonstration pub fn clear_all_lines() { - let context = Context::new(); - - // Get terminal - let mut terminal = terminal(&context); + let term = Crossterm::new(); + let mut terminal = term.terminal(); print_test_data(); @@ -29,15 +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 context = Context::new(); - - // Get terminal - let mut terminal = terminal(&context); + let term = Crossterm::new(); + let mut terminal = term.terminal(); print_test_data(); // Set terminal cursor position (see example for more info). - cursor::cursor(&context).goto(4, 8); + term.cursor().goto(4, 8); // Clear all cells from current cursor position down. terminal.clear(ClearType::FromCursorDown); @@ -45,15 +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 context = Context::new(); - - // Get terminal - let mut terminal = terminal(&context); + let term = Crossterm::new(); + let mut terminal = term.terminal(); print_test_data(); // Set terminal cursor position (see example for more info). - cursor::cursor(&context).goto(4, 4); + term.cursor().goto(4, 4); // Clear all cells from current cursor position down. terminal.clear(ClearType::FromCursorUp); @@ -61,15 +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 context = Context::new(); - - // Get terminal - let mut terminal = terminal(&context); + let term = Crossterm::new(); + let mut terminal = term.terminal(); print_test_data(); // Set terminal cursor position (see example for more info). - cursor::cursor(&context).goto(4, 4); + term.cursor().goto(4, 4); // Clear current line cells. terminal.clear(ClearType::CurrentLine); @@ -77,15 +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 context = Context::new(); - - // Get terminal - let mut terminal = terminal(&context); + let term = Crossterm::new(); + let mut terminal = term.terminal(); print_test_data(); // Set terminal cursor position (see example for more info). - cursor::cursor(&context).goto(4, 20); + term.cursor().goto(4, 20); // Clear all the cells until next line. terminal.clear(ClearType::UntilNewLine); @@ -93,57 +82,50 @@ pub fn clear_until_new_line() { /// Print the the current terminal size | demonstration. pub fn print_terminal_size() { - let context = Context::new(); + let term = Crossterm::new(); + let mut terminal = term.terminal(); - // Get terminal - let mut terminal = terminal(&context); // Get terminal size - let terminal_size = terminal.terminal_size(); + let (width, height) = terminal.terminal_size(); + // Print results - print!("X: {}, y: {}", terminal_size.0, terminal_size.1); + print!("X: {}, y: {}", width, height); } /// Set the terminal size to width 10, height: 10 | demonstration. pub fn set_terminal_size() { - let context = Context::new(); - - let mut terminal = terminal(&context); + let term = Crossterm::new(); + let mut terminal = term.terminal(); terminal.set_size(10, 10); } /// Scroll down 10 lines | demonstration. pub fn scroll_down() { - let context = Context::new(); + let term = Crossterm::new(); + let mut terminal = term.terminal(); print_test_data(); - - - // Get terminal - let mut terminal = terminal(&context); // Scroll down 10 lines. terminal.scroll_down(10); } /// Scroll down 10 lines | demonstration. pub fn scroll_up() { - let context = Context::new(); + let term = Crossterm::new(); + let mut terminal = term.terminal(); print_test_data(); - // Get terminal - let mut terminal = terminal(&context); // Scroll up 10 lines. terminal.scroll_up(5); } /// Resize the terminal to X: 10, Y: 10 | demonstration. pub fn resize_terminal() { - let context = Context::new(); - - // Get terminal - let mut terminal = terminal(&context); + let term = Crossterm::new(); + let mut terminal = term.terminal(); // Get terminal size terminal.set_size(10, 10); @@ -151,10 +133,7 @@ pub fn resize_terminal() { /// exit the current proccess. pub fn exit() { - let context = Context::new(); - - // Get terminal - let mut terminal = terminal(&context); - // Get terminal size + let term = Crossterm::new(); + let mut terminal = term.terminal(); terminal.exit(); }