basic/
basic.rs

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