worked on some examples

This commit is contained in:
TimonPost 2018-08-03 12:01:04 +02:00
parent 406f2046f9
commit f6b1955cae
14 changed files with 118 additions and 385 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -6,3 +6,4 @@ 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.
- 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.

View File

@ -7,5 +7,5 @@ authors = ["TimonPost <timonpost@hotmail.nl>"]
rand = "0.4.2"
[dependencies.crossterm]
path = "../../../../"
path = "../../../"
branch = "development"

View File

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

View File

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

View File

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

View File

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

View File

@ -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,37 @@ 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 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");
}

View File

@ -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");

View File

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