started working on refactoring examples
This commit is contained in:
parent
2eb6b1cd55
commit
5c5db7fdb0
@ -15,7 +15,7 @@ extern crate crossterm;
|
|||||||
use crossterm::Crossterm;
|
use crossterm::Crossterm;
|
||||||
use crossterm::style::Color;
|
use crossterm::style::Color;
|
||||||
|
|
||||||
// mod terminal;
|
mod terminal;
|
||||||
// mod color;
|
// mod color;
|
||||||
// mod cursor;
|
// mod cursor;
|
||||||
// mod crossterm_type;
|
// mod crossterm_type;
|
||||||
@ -27,8 +27,4 @@ use std::{thread, time};
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
use crossterm::Crossterm;
|
use crossterm::Crossterm;
|
||||||
|
|
||||||
let mut term = Crossterm::new();
|
|
||||||
let cursor = term.cursor();
|
|
||||||
let a = term.color();
|
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,18 @@
|
|||||||
extern crate crossterm;
|
extern crate crossterm;
|
||||||
|
|
||||||
use crossterm::style::Color;
|
use crossterm::style::Color;
|
||||||
use crossterm::cursor::cursor;
|
|
||||||
use crossterm::screen::AlternateScreen;
|
|
||||||
use crossterm::terminal::{self, ClearType};
|
use crossterm::terminal::{self, ClearType};
|
||||||
use crossterm::Context;
|
use crossterm::Crossterm;
|
||||||
|
|
||||||
use std::io::{stdout, Write};
|
use std::io::{stdout, Write};
|
||||||
use std::rc::Rc;
|
|
||||||
use std::{thread, time};
|
use std::{thread, time};
|
||||||
|
|
||||||
fn print_wait_screen(context: Rc<Context>) {
|
fn print_wait_screen(crossterm: &mut Crossterm) {
|
||||||
let mut terminal = terminal::terminal(&context);
|
let mut terminal = crossterm.terminal();
|
||||||
|
let mut cursor = crossterm.cursor();
|
||||||
|
|
||||||
terminal.clear(ClearType::All);
|
terminal.clear(ClearType::All);
|
||||||
|
|
||||||
let mut cursor = cursor(&context);
|
|
||||||
cursor.goto(0, 0);
|
cursor.goto(0, 0);
|
||||||
cursor.hide();
|
cursor.hide();
|
||||||
|
|
||||||
@ -29,7 +27,7 @@ fn print_wait_screen(context: Rc<Context>) {
|
|||||||
// print the current counter at the line of `Seconds to Go: {counter}`
|
// print the current counter at the line of `Seconds to Go: {counter}`
|
||||||
cursor
|
cursor
|
||||||
.goto(10, 2)
|
.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
|
// 1 second delay
|
||||||
thread::sleep(time::Duration::from_secs(1));
|
thread::sleep(time::Duration::from_secs(1));
|
||||||
@ -39,43 +37,36 @@ fn print_wait_screen(context: Rc<Context>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// print wait screen on alternate screen, then swich back.
|
/// print wait screen on alternate screen, then swich back.
|
||||||
pub fn print_wait_screen_on_alternate_window(context: Rc<Context>) {
|
pub fn print_wait_screen_on_alternate_window() {
|
||||||
// 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());
|
|
||||||
|
|
||||||
write!(screen, "test");
|
let mut term = Crossterm::new();
|
||||||
println!();
|
term.to_alternate_screen();
|
||||||
// Print the wait screen.
|
|
||||||
print_wait_screen(context.clone());
|
term.write(b"test");
|
||||||
}
|
print_wait_screen(&mut term);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// some stress test switch from and to alternate screen.
|
/// some stress test switch from and to alternate screen.
|
||||||
pub fn switch_between_main_and_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 cursor = term.cursor();
|
||||||
|
|
||||||
// create new alternate screen instance and switch to the alternate screen.
|
// create new alternate screen instance and switch to the alternate screen.
|
||||||
let mut screen = AlternateScreen::from(context.clone());
|
term.to_alternate_screen();
|
||||||
cursor.goto(0, 0);
|
cursor.goto(0, 0);
|
||||||
write!(screen, "we are at the alternate screen!");
|
write!(term, "we are at the alternate screen!");
|
||||||
screen.flush();
|
|
||||||
thread::sleep(time::Duration::from_secs(3));
|
thread::sleep(time::Duration::from_secs(3));
|
||||||
|
|
||||||
screen.to_main();
|
term.to_main_screen();
|
||||||
write!(screen, "we are at the main screen!");
|
write!(term, "we are at the alternate screen!");
|
||||||
screen.flush();
|
|
||||||
thread::sleep(time::Duration::from_secs(3));
|
thread::sleep(time::Duration::from_secs(3));
|
||||||
|
|
||||||
screen.to_alternate();
|
term.to_alternate_screen();
|
||||||
write!(screen, "we are at the alternate screen!");
|
write!(term, "we are at the alternate screen!");
|
||||||
screen.flush();
|
|
||||||
thread::sleep(time::Duration::from_secs(3));
|
thread::sleep(time::Duration::from_secs(3));
|
||||||
}
|
} // <- Crossterm goes out of scope.
|
||||||
|
|
||||||
println!("Whe are back at the main screen");
|
println!("Whe are back at the main screen");
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,19 @@
|
|||||||
extern crate crossterm;
|
extern crate crossterm;
|
||||||
|
|
||||||
use crossterm::cursor::cursor;
|
use crossterm::Crossterm;
|
||||||
use crossterm::screen::AlternateScreen;
|
|
||||||
use crossterm::terminal::{self, ClearType};
|
use crossterm::terminal::{self, ClearType};
|
||||||
use crossterm::Context;
|
|
||||||
|
|
||||||
use std::io::{stdout, Write};
|
use std::io::{stdout, Write};
|
||||||
use std::rc::Rc;
|
|
||||||
use std::{thread, time};
|
use std::{thread, time};
|
||||||
|
|
||||||
use crossterm::raw::IntoRawMode;
|
|
||||||
|
|
||||||
// raw screen is not working correctly currently
|
// raw screen is not working correctly currently
|
||||||
fn print_wait_screen(context: Rc<Context>) {
|
fn print_wait_screen(crossterm: &mut Crossterm) {
|
||||||
terminal::terminal(&context).clear(ClearType::All);
|
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, 0).print("Welcome to the wait screen.");
|
||||||
cursor
|
cursor
|
||||||
.goto(0, 1)
|
.goto(0, 1)
|
||||||
@ -35,7 +33,7 @@ fn print_wait_screen(context: Rc<Context>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_wait_screen_on_alternate_window() {
|
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.
|
// 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.
|
// 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.
|
// 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 convert the output of the program to raw mode.
|
||||||
// then print the wait screen on the alternate screen in raw mode.
|
// then print the wait screen on the alternate screen in raw mode.
|
||||||
let mut screen = AlternateScreen::from(context.clone());
|
term.to_alternate_screen();
|
||||||
let raw_screen = screen.into_raw_mode(context.clone());
|
term.enable_raw_mode();
|
||||||
|
|
||||||
// Print the wait screen.
|
// 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");
|
println!("Whe are back at the main screen");
|
||||||
|
@ -4,9 +4,8 @@
|
|||||||
|
|
||||||
extern crate crossterm;
|
extern crate crossterm;
|
||||||
|
|
||||||
use crossterm::cursor;
|
use crossterm::terminal::ClearType;
|
||||||
use crossterm::terminal::{terminal, ClearType};
|
use crossterm::Crossterm;
|
||||||
use crossterm::Context;
|
|
||||||
|
|
||||||
fn print_test_data() {
|
fn print_test_data() {
|
||||||
for i in 0..100 {
|
for i in 0..100 {
|
||||||
@ -16,10 +15,8 @@ fn print_test_data() {
|
|||||||
|
|
||||||
/// Clear all lines in terminal | demonstration
|
/// Clear all lines in terminal | demonstration
|
||||||
pub fn clear_all_lines() {
|
pub fn clear_all_lines() {
|
||||||
let context = Context::new();
|
let term = Crossterm::new();
|
||||||
|
let mut terminal = term.terminal();
|
||||||
// Get terminal
|
|
||||||
let mut terminal = terminal(&context);
|
|
||||||
|
|
||||||
print_test_data();
|
print_test_data();
|
||||||
|
|
||||||
@ -29,15 +26,13 @@ pub fn clear_all_lines() {
|
|||||||
|
|
||||||
/// Clear all lines from cursor position X:4, Y:4 down | demonstration
|
/// Clear all lines from cursor position X:4, Y:4 down | demonstration
|
||||||
pub fn clear_from_cursor_down() {
|
pub fn clear_from_cursor_down() {
|
||||||
let context = Context::new();
|
let term = Crossterm::new();
|
||||||
|
let mut terminal = term.terminal();
|
||||||
// Get terminal
|
|
||||||
let mut terminal = terminal(&context);
|
|
||||||
|
|
||||||
print_test_data();
|
print_test_data();
|
||||||
|
|
||||||
// Set terminal cursor position (see example for more info).
|
// 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.
|
// Clear all cells from current cursor position down.
|
||||||
terminal.clear(ClearType::FromCursorDown);
|
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
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
||||||
pub fn clear_from_cursor_up() {
|
pub fn clear_from_cursor_up() {
|
||||||
let context = Context::new();
|
let term = Crossterm::new();
|
||||||
|
let mut terminal = term.terminal();
|
||||||
// Get terminal
|
|
||||||
let mut terminal = terminal(&context);
|
|
||||||
|
|
||||||
print_test_data();
|
print_test_data();
|
||||||
|
|
||||||
// Set terminal cursor position (see example for more info).
|
// 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.
|
// Clear all cells from current cursor position down.
|
||||||
terminal.clear(ClearType::FromCursorUp);
|
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
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
||||||
pub fn clear_current_line() {
|
pub fn clear_current_line() {
|
||||||
let context = Context::new();
|
let term = Crossterm::new();
|
||||||
|
let mut terminal = term.terminal();
|
||||||
// Get terminal
|
|
||||||
let mut terminal = terminal(&context);
|
|
||||||
|
|
||||||
print_test_data();
|
print_test_data();
|
||||||
|
|
||||||
// Set terminal cursor position (see example for more info).
|
// Set terminal cursor position (see example for more info).
|
||||||
cursor::cursor(&context).goto(4, 4);
|
term.cursor().goto(4, 4);
|
||||||
|
|
||||||
// Clear current line cells.
|
// Clear current line cells.
|
||||||
terminal.clear(ClearType::CurrentLine);
|
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
|
/// Clear all lines from cursor position X:4, Y:7 up | demonstration
|
||||||
pub fn clear_until_new_line() {
|
pub fn clear_until_new_line() {
|
||||||
let context = Context::new();
|
let term = Crossterm::new();
|
||||||
|
let mut terminal = term.terminal();
|
||||||
// Get terminal
|
|
||||||
let mut terminal = terminal(&context);
|
|
||||||
|
|
||||||
print_test_data();
|
print_test_data();
|
||||||
|
|
||||||
// Set terminal cursor position (see example for more info).
|
// 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.
|
// Clear all the cells until next line.
|
||||||
terminal.clear(ClearType::UntilNewLine);
|
terminal.clear(ClearType::UntilNewLine);
|
||||||
@ -93,57 +82,50 @@ pub fn clear_until_new_line() {
|
|||||||
|
|
||||||
/// Print the the current terminal size | demonstration.
|
/// Print the the current terminal size | demonstration.
|
||||||
pub fn print_terminal_size() {
|
pub fn print_terminal_size() {
|
||||||
let context = Context::new();
|
let term = Crossterm::new();
|
||||||
|
let mut terminal = term.terminal();
|
||||||
|
|
||||||
// Get terminal
|
|
||||||
let mut terminal = terminal(&context);
|
|
||||||
// Get terminal size
|
// Get terminal size
|
||||||
let terminal_size = terminal.terminal_size();
|
let (width, height) = terminal.terminal_size();
|
||||||
|
|
||||||
// Print results
|
// 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.
|
/// Set the terminal size to width 10, height: 10 | demonstration.
|
||||||
pub fn set_terminal_size() {
|
pub fn set_terminal_size() {
|
||||||
let context = Context::new();
|
let term = Crossterm::new();
|
||||||
|
let mut terminal = term.terminal();
|
||||||
let mut terminal = terminal(&context);
|
|
||||||
|
|
||||||
terminal.set_size(10, 10);
|
terminal.set_size(10, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Scroll down 10 lines | demonstration.
|
/// Scroll down 10 lines | demonstration.
|
||||||
pub fn scroll_down() {
|
pub fn scroll_down() {
|
||||||
let context = Context::new();
|
let term = Crossterm::new();
|
||||||
|
let mut terminal = term.terminal();
|
||||||
|
|
||||||
print_test_data();
|
print_test_data();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Get terminal
|
|
||||||
let mut terminal = terminal(&context);
|
|
||||||
// Scroll down 10 lines.
|
// Scroll down 10 lines.
|
||||||
terminal.scroll_down(10);
|
terminal.scroll_down(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Scroll down 10 lines | demonstration.
|
/// Scroll down 10 lines | demonstration.
|
||||||
pub fn scroll_up() {
|
pub fn scroll_up() {
|
||||||
let context = Context::new();
|
let term = Crossterm::new();
|
||||||
|
let mut terminal = term.terminal();
|
||||||
|
|
||||||
print_test_data();
|
print_test_data();
|
||||||
|
|
||||||
// Get terminal
|
|
||||||
let mut terminal = terminal(&context);
|
|
||||||
// Scroll up 10 lines.
|
// Scroll up 10 lines.
|
||||||
terminal.scroll_up(5);
|
terminal.scroll_up(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resize the terminal to X: 10, Y: 10 | demonstration.
|
/// Resize the terminal to X: 10, Y: 10 | demonstration.
|
||||||
pub fn resize_terminal() {
|
pub fn resize_terminal() {
|
||||||
let context = Context::new();
|
let term = Crossterm::new();
|
||||||
|
let mut terminal = term.terminal();
|
||||||
// Get terminal
|
|
||||||
let mut terminal = terminal(&context);
|
|
||||||
|
|
||||||
// Get terminal size
|
// Get terminal size
|
||||||
terminal.set_size(10, 10);
|
terminal.set_size(10, 10);
|
||||||
@ -151,10 +133,7 @@ pub fn resize_terminal() {
|
|||||||
|
|
||||||
/// exit the current proccess.
|
/// exit the current proccess.
|
||||||
pub fn exit() {
|
pub fn exit() {
|
||||||
let context = Context::new();
|
let term = Crossterm::new();
|
||||||
|
let mut terminal = term.terminal();
|
||||||
// Get terminal
|
|
||||||
let mut terminal = terminal(&context);
|
|
||||||
// Get terminal size
|
|
||||||
terminal.exit();
|
terminal.exit();
|
||||||
}
|
}
|
||||||
|
@ -66,8 +66,8 @@ impl<'terminal> Terminal<'terminal> {
|
|||||||
/// term.clear(terminal::ClearType::UntilNewLine);
|
/// term.clear(terminal::ClearType::UntilNewLine);
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
pub fn clear(&mut self, clear_type: ClearType) {
|
pub fn clear(&self, clear_type: ClearType) {
|
||||||
self.terminal.clear(clear_type, &mut self.screen_manager);
|
self.terminal.clear(clear_type, &self.screen_manager);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the terminal size (x,y).
|
/// Get the terminal size (x,y).
|
||||||
|
Loading…
Reference in New Issue
Block a user