Examples Clean-Up (#119)

This commit is contained in:
Lazarus Holl 2019-04-13 05:44:31 -07:00 committed by Timon
parent 41f68bda25
commit f468cf6a9a
6 changed files with 95 additions and 63 deletions

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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() {

View File

@ -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.

View File

@ -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