From f468cf6a9a74df931f5d2b92bacb18fc26b51852 Mon Sep 17 00:00:00 2001 From: Lazarus Holl Date: Sat, 13 Apr 2019 05:44:31 -0700 Subject: [PATCH] Examples Clean-Up (#119) --- examples/alternate_screen.rs | 26 ++++++++------- examples/cursor.rs | 31 ++++++++++-------- examples/raw_mode.rs | 34 ++++++++++--------- examples/style.rs | 2 +- examples/terminal.rs | 63 +++++++++++++++++++++++------------- src/crossterm.rs | 2 +- 6 files changed, 95 insertions(+), 63 deletions(-) diff --git a/examples/alternate_screen.rs b/examples/alternate_screen.rs index 059938a..0b53b60 100644 --- a/examples/alternate_screen.rs +++ b/examples/alternate_screen.rs @@ -1,25 +1,25 @@ extern crate crossterm; use crossterm::{style, AlternateScreen, ClearType, Color, Crossterm}; -use std::{thread, time}; +use std::{thread, time, io}; -fn print_wait_screen() { +fn print_wait_screen() -> io::Result<()> { let crossterm = Crossterm::new(); let terminal = crossterm.terminal(); let cursor = crossterm.cursor(); - terminal.clear(ClearType::All); - cursor.goto(0, 0); - cursor.hide(); + 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); + cursor.goto(10, 2)?; println!( "{}", style(format!("{} of the 5 items processed", i)) @@ -30,15 +30,19 @@ fn print_wait_screen() { // 1 second delay thread::sleep(time::Duration::from_secs(1)); } + + Ok(()) } /// print wait screen on alternate screen, then switch back. -pub fn print_wait_screen_on_alternate_window() { - if let Ok(alternate) = AlternateScreen::to_alternate(false) { - print_wait_screen(); +pub fn print_wait_screen_on_alternate_window() -> io::Result<()> { + if let Ok(_alternate) = AlternateScreen::to_alternate(false) { + print_wait_screen()?; } + + Ok(()) } fn main() { - print_wait_screen_on_alternate_window(); + print_wait_screen_on_alternate_window().unwrap(); } diff --git a/examples/cursor.rs b/examples/cursor.rs index 404ce3d..7bae615 100644 --- a/examples/cursor.rs +++ b/examples/cursor.rs @@ -5,13 +5,16 @@ extern crate crossterm_cursor; use crossterm_cursor::cursor; +use std::io; /// Set the cursor to position X: 10, Y: 5 in the terminal. -pub fn goto() { +pub fn goto() -> io::Result<()> { // Get the cursor let cursor = cursor(); // Set the cursor to position X: 10, Y: 5 in the terminal - cursor.goto(10, 5); + cursor.goto(10, 5)?; + + Ok(()) } /// get the cursor position @@ -48,44 +51,46 @@ pub fn move_down() { } /// Save and reset cursor position | demonstration.. -pub fn save_and_reset_position() { +pub fn save_and_reset_position() -> io::Result<()> { let cursor = cursor(); // Goto X: 5 Y: 5 - cursor.goto(5, 5); + cursor.goto(5, 5)?; // Safe cursor position: X: 5 Y: 5 - cursor.save_position(); + cursor.save_position()?; // Goto X: 5 Y: 20 - cursor.goto(5, 20); + cursor.goto(5, 20)?; // Print at X: 5 Y: 20. println!("Yea!"); // Reset back to X: 5 Y: 5. - cursor.reset_position(); + cursor.reset_position()?; // Print Back at X: 5 Y: 5. println!("Back"); - println!() + println!(); + + Ok(()) } /// Hide cursor display | demonstration. pub fn hide_cursor() { let cursor = cursor(); - cursor.hide(); + cursor.hide().unwrap(); } /// Show cursor display | demonstration. pub fn show_cursor() { let cursor = cursor(); - cursor.show(); + cursor.show().unwrap(); } /// Show cursor display, only works on certain terminals.| demonstration pub fn blink_cursor() { let cursor = cursor(); - cursor.blink(false); - cursor.blink(false); + cursor.blink(false).unwrap(); + cursor.blink(false).unwrap(); } fn main() { - save_and_reset_position(); + save_and_reset_position().unwrap(); } diff --git a/examples/raw_mode.rs b/examples/raw_mode.rs index 8045d26..697b31a 100644 --- a/examples/raw_mode.rs +++ b/examples/raw_mode.rs @@ -1,30 +1,30 @@ extern crate crossterm; -use crossterm::{style, terminal, AlternateScreen, ClearType, Color, Crossterm, RawScreen}; +use crossterm::{style, AlternateScreen, ClearType, Color, Crossterm}; -use std::io::{stdout, Write}; +use std::io::{self, stdout, Write}; use std::{thread, time}; -fn print_wait_screen() { +fn print_wait_screen() -> io::Result<()> { let crossterm = Crossterm::new(); let terminal = crossterm.terminal(); let cursor = crossterm.cursor(); - terminal.clear(ClearType::All); + terminal.clear(ClearType::All)?; - cursor.hide(); - cursor.goto(0, 0); + cursor.hide()?; + cursor.goto(0, 0)?; println!("Welcome to the wait screen."); - cursor.goto(0, 1); + cursor.goto(0, 1)?; println!("Please wait a few seconds until we arrive back at the main screen."); - cursor.goto(0, 2); + cursor.goto(0, 2)?; println!("Progress:"); - cursor.goto(0, 3); + cursor.goto(0, 3)?; // 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); + cursor.goto(10, 2)?; print!( "{}", style(format!("{} of the 5 items processed", i)) @@ -32,20 +32,24 @@ fn print_wait_screen() { .on(Color::Blue) ); - stdout().flush(); + stdout().flush()?; // 1 second delay thread::sleep(time::Duration::from_secs(1)); } + + Ok(()) } -pub fn print_wait_screen_on_alternate_window() { +pub fn print_wait_screen_on_alternate_window() -> io::Result<()> { // by passing in 'true' the alternate screen will be in raw modes. - if let Ok(alternate) = AlternateScreen::to_alternate(true) { - print_wait_screen(); + if let Ok(_alternate) = AlternateScreen::to_alternate(true) { + print_wait_screen()?; } // <- drop alternate screen; this will cause the alternate screen to drop. + + Ok(()) } fn main() { - print_wait_screen_on_alternate_window(); + print_wait_screen_on_alternate_window().unwrap(); } diff --git a/examples/style.rs b/examples/style.rs index 56fd186..a9f2c46 100644 --- a/examples/style.rs +++ b/examples/style.rs @@ -3,7 +3,7 @@ //! extern crate crossterm; -use self::crossterm::{color, style, Attribute, Color, Colored, Colorize, Styler}; +use self::crossterm::{color, Attribute, Color, Colored, Colorize, Styler}; /// print some red font | demonstration. pub fn paint_foreground() { diff --git a/examples/terminal.rs b/examples/terminal.rs index 57eb596..9627d4e 100644 --- a/examples/terminal.rs +++ b/examples/terminal.rs @@ -5,6 +5,7 @@ extern crate crossterm; use crossterm::{cursor, terminal, ClearType}; +use std::io; fn print_test_data() { for i in 0..100 { @@ -13,65 +14,75 @@ fn print_test_data() { } /// Clear all lines in terminal | demonstration -pub fn clear_all_lines() { +pub fn clear_all_lines() -> io::Result<()> { let terminal = terminal(); print_test_data(); // Clear all lines in terminal; - terminal.clear(ClearType::All); + terminal.clear(ClearType::All)?; + + Ok(()) } /// Clear all lines from cursor position X:4, Y:4 down | demonstration -pub fn clear_from_cursor_down() { +pub fn clear_from_cursor_down() -> io::Result<()> { let terminal = terminal(); print_test_data(); // Set terminal cursor position (see example for more info). - cursor().goto(4, 8); + cursor().goto(4, 8)?; // Clear all cells from current cursor position down. - terminal.clear(ClearType::FromCursorDown); + terminal.clear(ClearType::FromCursorDown)?; + + Ok(()) } /// Clear all lines from cursor position X:4, Y:4 up | demonstration -pub fn clear_from_cursor_up() { +pub fn clear_from_cursor_up() -> io::Result<()> { let terminal = terminal(); print_test_data(); // Set terminal cursor position (see example for more info). - cursor().goto(4, 4); + cursor().goto(4, 4)?; // Clear all cells from current cursor position down. - terminal.clear(ClearType::FromCursorUp); + terminal.clear(ClearType::FromCursorUp)?; + + Ok(()) } /// Clear all lines from cursor position X:4, Y:4 up | demonstration -pub fn clear_current_line() { +pub fn clear_current_line() -> io::Result<()>{ let terminal = terminal(); print_test_data(); // Set terminal cursor position (see example for more info). - cursor().goto(4, 3); + cursor().goto(4, 3)?; // Clear current line cells. - terminal.clear(ClearType::CurrentLine); + terminal.clear(ClearType::CurrentLine)?; + + Ok(()) } /// Clear all lines from cursor position X:4, Y:7 up | demonstration -pub fn clear_until_new_line() { +pub fn clear_until_new_line() -> io::Result<()> { let terminal = terminal(); print_test_data(); // Set terminal cursor position (see example for more info). - cursor().goto(4, 20); + cursor().goto(4, 20)?; // Clear all the cells until next line. - terminal.clear(ClearType::UntilNewLine); + terminal.clear(ClearType::UntilNewLine)?; + + Ok(()) } /// Print the the current terminal size | demonstration. @@ -86,38 +97,46 @@ pub fn print_terminal_size() { } /// Set the terminal size to width 10, height: 10 | demonstration. -pub fn set_terminal_size() { +pub fn set_terminal_size() -> io::Result<()> { let terminal = terminal(); - terminal.set_size(10, 10); + terminal.set_size(10, 10)?; + + Ok(()) } /// Scroll down 10 lines | demonstration. -pub fn scroll_down() { +pub fn scroll_down() -> io::Result<()> { let terminal = terminal(); print_test_data(); // Scroll down 10 lines. - terminal.scroll_down(10); + terminal.scroll_down(10)?; + + Ok(()) } /// Scroll down 10 lines | demonstration. -pub fn scroll_up() { +pub fn scroll_up() -> io::Result<()> { let terminal = terminal(); print_test_data(); // Scroll up 10 lines. - terminal.scroll_up(5); + terminal.scroll_up(5)?; + + Ok(()) } /// Resize the terminal to X: 10, Y: 10 | demonstration. -pub fn resize_terminal() { +pub fn resize_terminal() -> io::Result<()> { let terminal = terminal(); // Get terminal size - terminal.set_size(10, 10); + terminal.set_size(10, 10)?; + + Ok(()) } /// exit the current proccess. diff --git a/src/crossterm.rs b/src/crossterm.rs index 4d6e3ff..5f69253 100644 --- a/src/crossterm.rs +++ b/src/crossterm.rs @@ -21,7 +21,7 @@ use std::fmt::Display; /// - checkout the crossterm book for more information about feature flags or alternate screen. pub struct Crossterm; -impl<'crossterm> Crossterm { +impl Crossterm { /// Create a new instance of `Crossterm` pub fn new() -> Crossterm { Crossterm