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
use ratatui::backend::TermwizBackend;
use ratatui::widgets::{Block, Borders};
use ratatui::Terminal;
use std::error::Error;
use std::time::Duration;
use termwiz::input::InputEvent;
use termwiz::terminal::Terminal as _;
use tui_textarea::{Input, Key, TextArea};

fn main() -> Result<(), Box<dyn Error>> {
    let backend = TermwizBackend::new()?;
    let mut term = Terminal::new(backend)?;
    term.hide_cursor()?;

    let mut textarea = TextArea::default();
    textarea.set_block(
        Block::default()
            .borders(Borders::ALL)
            .title("Termwiz Minimal Example"),
    );

    // The event loop
    loop {
        term.draw(|f| {
            let widget = textarea.widget();
            f.render_widget(widget, f.size());
        })?;

        if let Some(input) = term
            .backend_mut()
            .buffered_terminal_mut()
            .terminal()
            .poll_input(Some(Duration::from_millis(100)))?
        {
            if let InputEvent::Resized { cols, rows } = input {
                term.backend_mut()
                    .buffered_terminal_mut()
                    .resize(cols, rows);
            } else {
                match input.into() {
                    Input { key: Key::Esc, .. } => break,
                    input => {
                        textarea.input(input);
                    }
                }
            }
        }
    }

    term.show_cursor()?;
    term.flush()?;
    drop(term); // Leave terminal raw mode to print the following line

    println!("Lines: {:?}", textarea.lines());
    Ok(())
}