2018-07-02 06:43:43 +10:00
|
|
|
//!
|
2019-06-22 02:10:46 +10:00
|
|
|
//! Examples of actions that could be performed with the cursor.
|
2018-01-18 09:06:45 +11:00
|
|
|
//!
|
|
|
|
|
2019-01-28 07:16:14 +11:00
|
|
|
extern crate crossterm_cursor;
|
|
|
|
|
2019-04-11 07:46:30 +10:00
|
|
|
use crossterm_cursor::cursor;
|
2018-01-18 09:06:45 +11:00
|
|
|
|
|
|
|
/// Set the cursor to position X: 10, Y: 5 in the terminal.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn goto() {
|
2018-01-18 09:06:45 +11:00
|
|
|
// Get the cursor
|
2019-01-01 05:55:48 +11:00
|
|
|
let cursor = cursor();
|
2018-01-18 09:06:45 +11:00
|
|
|
// Set the cursor to position X: 10, Y: 5 in the terminal
|
2018-07-02 06:43:43 +10:00
|
|
|
cursor.goto(10, 5);
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
2018-06-17 04:10:51 +10:00
|
|
|
/// get the cursor position
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn pos() {
|
2018-06-17 04:10:51 +10:00
|
|
|
// Get the cursor
|
2019-01-01 05:55:48 +11:00
|
|
|
let cursor = cursor();
|
2018-06-17 04:10:51 +10:00
|
|
|
// get the cursor position.
|
2018-10-08 04:15:17 +11:00
|
|
|
let (x, y) = cursor.pos();
|
2019-01-01 05:55:48 +11:00
|
|
|
|
|
|
|
println!("{} {}", x, y);
|
2018-06-17 04:10:51 +10:00
|
|
|
}
|
|
|
|
|
2018-01-18 09:06:45 +11:00
|
|
|
/// Move the cursor 3 up | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn move_up() {
|
2018-01-18 09:06:45 +11:00
|
|
|
// Get the cursor
|
2018-11-15 02:53:27 +11:00
|
|
|
let mut cursor = cursor();
|
2018-07-19 06:32:17 +10:00
|
|
|
|
2018-01-18 09:06:45 +11:00
|
|
|
// Move the cursor to position 3 times to the up in the terminal
|
2018-07-19 06:32:17 +10:00
|
|
|
cursor.move_up(10);
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Move the cursor 3 to the right | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn move_right() {
|
2018-11-15 02:53:27 +11:00
|
|
|
let mut cursor = cursor();
|
2018-01-18 09:06:45 +11:00
|
|
|
// Move the cursor to position 3 times to the right in the terminal
|
|
|
|
cursor.move_right(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Move the cursor 3 down | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn move_down() {
|
2018-11-15 02:53:27 +11:00
|
|
|
let mut cursor = cursor();
|
2018-01-18 09:06:45 +11:00
|
|
|
// Move the cursor to position 3 times to the down in the terminal
|
|
|
|
cursor.move_down(3);
|
|
|
|
}
|
|
|
|
|
2018-01-31 07:20:11 +11:00
|
|
|
/// Save and reset cursor position | demonstration..
|
2019-04-11 07:46:30 +10:00
|
|
|
pub fn save_and_reset_position() {
|
2019-01-01 05:55:48 +11:00
|
|
|
let cursor = cursor();
|
2018-08-15 05:40:07 +10:00
|
|
|
|
2018-01-28 04:26:08 +11:00
|
|
|
// Goto X: 5 Y: 5
|
2018-07-02 06:43:43 +10:00
|
|
|
cursor.goto(5, 5);
|
2018-01-28 04:26:08 +11:00
|
|
|
// Safe cursor position: X: 5 Y: 5
|
2018-01-31 07:20:11 +11:00
|
|
|
cursor.save_position();
|
2018-01-28 04:26:08 +11:00
|
|
|
// Goto X: 5 Y: 20
|
2018-07-02 06:43:43 +10:00
|
|
|
cursor.goto(5, 20);
|
2018-01-28 04:26:08 +11:00
|
|
|
// Print at X: 5 Y: 20.
|
2018-01-31 07:20:11 +11:00
|
|
|
println!("Yea!");
|
2018-01-28 04:26:08 +11:00
|
|
|
// Reset back to X: 5 Y: 5.
|
|
|
|
cursor.reset_position();
|
|
|
|
// Print Back at X: 5 Y: 5.
|
2018-01-31 07:20:11 +11:00
|
|
|
println!("Back");
|
2018-01-28 04:26:08 +11:00
|
|
|
|
|
|
|
println!()
|
2018-01-26 04:26:08 +11:00
|
|
|
}
|
|
|
|
|
2018-06-24 03:48:22 +10:00
|
|
|
/// Hide cursor display | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn hide_cursor() {
|
2019-01-01 05:55:48 +11:00
|
|
|
let cursor = cursor();
|
2018-06-24 03:48:22 +10:00
|
|
|
cursor.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Show cursor display | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn show_cursor() {
|
2019-01-01 05:55:48 +11:00
|
|
|
let cursor = cursor();
|
2018-06-24 03:48:22 +10:00
|
|
|
cursor.show();
|
|
|
|
}
|
|
|
|
|
2018-07-02 06:40:07 +10:00
|
|
|
/// Show cursor display, only works on certain terminals.| demonstration
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn blink_cursor() {
|
2019-01-01 05:55:48 +11:00
|
|
|
let cursor = cursor();
|
2018-06-27 04:56:34 +10:00
|
|
|
cursor.blink(false);
|
|
|
|
cursor.blink(false);
|
|
|
|
}
|
2019-01-28 07:16:14 +11:00
|
|
|
|
2019-07-25 04:10:27 +10:00
|
|
|
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.
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-28 07:16:14 +11:00
|
|
|
fn main() {
|
2019-07-25 04:10:27 +10:00
|
|
|
let mut stdout = ::std::io::stdout();
|
|
|
|
|
|
|
|
stdout
|
|
|
|
.queue(Goto(5, 5))
|
|
|
|
.queue(Output("#".to_string()))
|
|
|
|
.flush();
|
|
|
|
|
|
|
|
println!("out: {}", Output("1".to_string()));
|
2019-01-28 07:16:14 +11:00
|
|
|
}
|