1a60924abd
- Command API to introduce easier usability, better performance, and more control over to which buffer to write, and when to flush the buffer to the terminal.
154 lines
3.6 KiB
Rust
154 lines
3.6 KiB
Rust
//!
|
|
//! Examples of actions that could be performed with the cursor.
|
|
//!
|
|
|
|
extern crate crossterm_cursor;
|
|
|
|
use crossterm_cursor::cursor;
|
|
|
|
/// Set the cursor to position X: 10, Y: 5 in the terminal.
|
|
pub fn goto() {
|
|
// Get the cursor
|
|
let cursor = cursor();
|
|
// Set the cursor to position X: 10, Y: 5 in the terminal
|
|
cursor.goto(10, 5);
|
|
}
|
|
|
|
/// get the cursor position
|
|
pub fn pos() {
|
|
// Get the cursor
|
|
let cursor = cursor();
|
|
// get the cursor position.
|
|
let (x, y) = cursor.pos();
|
|
|
|
println!("{} {}", x, y);
|
|
}
|
|
|
|
/// Move the cursor 3 up | demonstration.
|
|
pub fn move_up() {
|
|
// Get the cursor
|
|
let mut cursor = cursor();
|
|
|
|
// Move the cursor to position 3 times to the up in the terminal
|
|
cursor.move_up(10);
|
|
}
|
|
|
|
/// Move the cursor 3 to the right | demonstration.
|
|
pub fn move_right() {
|
|
let mut cursor = cursor();
|
|
// Move the cursor to position 3 times to the right in the terminal
|
|
cursor.move_right(3);
|
|
}
|
|
|
|
/// Move the cursor 3 down | demonstration.
|
|
pub fn move_down() {
|
|
let mut cursor = cursor();
|
|
// Move the cursor to position 3 times to the down in the terminal
|
|
cursor.move_down(3);
|
|
}
|
|
|
|
/// Save and reset cursor position | demonstration..
|
|
pub fn save_and_reset_position() {
|
|
let cursor = cursor();
|
|
|
|
// Goto X: 5 Y: 5
|
|
cursor.goto(5, 5);
|
|
// Safe cursor position: X: 5 Y: 5
|
|
cursor.save_position();
|
|
// Goto X: 5 Y: 20
|
|
cursor.goto(5, 20);
|
|
// Print at X: 5 Y: 20.
|
|
println!("Yea!");
|
|
// Reset back to X: 5 Y: 5.
|
|
cursor.reset_position();
|
|
// Print Back at X: 5 Y: 5.
|
|
println!("Back");
|
|
|
|
println!()
|
|
}
|
|
|
|
/// Hide cursor display | demonstration.
|
|
pub fn hide_cursor() {
|
|
let cursor = cursor();
|
|
cursor.hide();
|
|
}
|
|
|
|
/// Show cursor display | demonstration.
|
|
pub fn show_cursor() {
|
|
let cursor = cursor();
|
|
cursor.show();
|
|
}
|
|
|
|
/// Show cursor display, only works on certain terminals.| demonstration
|
|
pub fn blink_cursor() {
|
|
let cursor = cursor();
|
|
cursor.blink(false);
|
|
cursor.blink(false);
|
|
}
|
|
|
|
use self::crossterm_cursor::{
|
|
execute, queue, BlinkOff, BlinkOn, Command, Down, ExecutableCommand, Goto, Hide, Left, Output,
|
|
QueueableCommand, ResetPos, Right, SavePos, Show, Up,
|
|
};
|
|
use std::fmt::Display;
|
|
use std::io::{stdout, Write};
|
|
use std::thread;
|
|
use std::time::{Duration, Instant};
|
|
|
|
fn benchmark_cursor_goto() -> f32 {
|
|
let mut stdout = ::std::io::stdout();
|
|
|
|
let instant1 = Instant::now();
|
|
for i in 0..10 {
|
|
for x in 0..200 {
|
|
for y in 0..50 {
|
|
queue!(stdout, Goto(x, y), Hide, Output(y.to_string()));
|
|
}
|
|
}
|
|
}
|
|
|
|
let new_api = instant1.elapsed();
|
|
let cursor = cursor();
|
|
let instant2 = Instant::now();
|
|
for i in 0..10 {
|
|
for x in 0..200 {
|
|
for y in 0..50 {
|
|
cursor.goto(x, y);
|
|
print!("{}", y.to_string());
|
|
}
|
|
}
|
|
}
|
|
let old_api = instant2.elapsed();
|
|
|
|
let speed_improvement = ((old_api.as_millis() as f32 - new_api.as_millis() as f32)
|
|
/ old_api.as_millis() as f32)
|
|
* 100.;
|
|
|
|
speed_improvement
|
|
}
|
|
|
|
fn start_goto_benchmark() {
|
|
let mut stdout = ::std::io::stdout();
|
|
|
|
let mut performance_metrics = Vec::new();
|
|
for i in 1..=20 {
|
|
performance_metrics.push(benchmark_cursor_goto());
|
|
}
|
|
|
|
println!(
|
|
"Average Performance Improvement mesearued 10 times {:.2} %",
|
|
performance_metrics.iter().sum::<f32>() / 20.
|
|
);
|
|
}
|
|
|
|
fn main() {
|
|
let mut stdout = ::std::io::stdout();
|
|
|
|
stdout
|
|
.queue(Goto(5, 5))
|
|
.queue(Output("#".to_string()))
|
|
.flush();
|
|
|
|
println!("out: {}", Output("1".to_string()));
|
|
}
|