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::style::Color;
|
||||
|
||||
// mod terminal;
|
||||
mod terminal;
|
||||
// mod color;
|
||||
// mod cursor;
|
||||
// mod crossterm_type;
|
||||
@ -27,8 +27,4 @@ use std::{thread, time};
|
||||
|
||||
fn main() {
|
||||
use crossterm::Crossterm;
|
||||
|
||||
let mut term = Crossterm::new();
|
||||
let cursor = term.cursor();
|
||||
let a = term.color();
|
||||
}
|
||||
|
@ -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<Context>) {
|
||||
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<Context>) {
|
||||
// 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,36 @@ fn print_wait_screen(context: Rc<Context>) {
|
||||
}
|
||||
|
||||
/// print wait screen on alternate screen, then swich back.
|
||||
pub fn print_wait_screen_on_alternate_window(context: Rc<Context>) {
|
||||
// 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 cursor = term.cursor();
|
||||
|
||||
// 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);
|
||||
write!(screen, "we are at the alternate screen!");
|
||||
screen.flush();
|
||||
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");
|
||||
}
|
||||
|
@ -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<Context>) {
|
||||
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<Context>) {
|
||||
}
|
||||
|
||||
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");
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -66,8 +66,8 @@ impl<'terminal> Terminal<'terminal> {
|
||||
/// term.clear(terminal::ClearType::UntilNewLine);
|
||||
///
|
||||
/// ```
|
||||
pub fn clear(&mut self, clear_type: ClearType) {
|
||||
self.terminal.clear(clear_type, &mut self.screen_manager);
|
||||
pub fn clear(&self, clear_type: ClearType) {
|
||||
self.terminal.clear(clear_type, &self.screen_manager);
|
||||
}
|
||||
|
||||
/// Get the terminal size (x,y).
|
||||
|
Loading…
Reference in New Issue
Block a user