-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasic.rs
69 lines (52 loc) · 1.75 KB
/
basic.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::{fs::File, thread, time::Duration};
use terminal::{error, stderr, stdout, Action, Clear, Retrieved, Terminal, Value};
fn different_buffers() {
let _stdout = stdout();
let _stderr = stderr();
let _file = Terminal::custom(File::create("./test.txt").unwrap());
}
/// Gets values from the terminal.
fn get_value() -> error::Result<()> {
let stdout = stdout();
if let Retrieved::CursorPosition(x, y) = stdout.get(Value::CursorPosition)? {
println!("X: {}, Y: {}", x, y);
}
if let Retrieved::TerminalSize(column, row) = stdout.get(Value::TerminalSize)? {
println!("columns: {}, rows: {}", column, row);
}
// see '/examples/event.rs'
if let Retrieved::Event(event) = stdout.get(Value::Event(None))? {
println!("Event: {:?}\r", event);
}
Ok(())
}
fn perform_action() -> error::Result<()> {
let stdout = stdout();
stdout.act(Action::MoveCursorTo(10, 10))
}
/// Batches multiple actions before executing.
fn batch_actions() -> error::Result<()> {
let terminal = stdout();
terminal.batch(Action::ClearTerminal(Clear::All))?;
terminal.batch(Action::MoveCursorTo(5, 5))?;
thread::sleep(Duration::from_millis(2000));
println!("@");
terminal.flush_batch()
}
/// Acquires lock once, and uses that lock to do actions.
fn lock_terminal() -> error::Result<()> {
let terminal = Terminal::custom(File::create("./test.txt").unwrap());
let mut lock = terminal.lock_mut()?;
for i in 0..10000 {
println!("{}", i);
if i % 100 == 0 {
lock.act(Action::ClearTerminal(Clear::All))?;
lock.act(Action::MoveCursorTo(0, 0))?;
}
thread::sleep(Duration::from_millis(10));
}
Ok(())
}
fn main() {
get_value().unwrap();
}