-
Notifications
You must be signed in to change notification settings - Fork 62
/
termion.rs
67 lines (61 loc) · 1.82 KB
/
termion.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
use ratatui::backend::TermionBackend;
use ratatui::widgets::{Block, Borders};
use ratatui::Terminal;
use std::error::Error;
use std::io;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use termion::event::Event as TermEvent;
use termion::input::{MouseTerminal, TermRead};
use termion::raw::IntoRawMode;
use termion::screen::IntoAlternateScreen;
use tui_textarea::{Input, Key, TextArea};
enum Event {
Term(TermEvent),
Tick,
}
fn main() -> Result<(), Box<dyn Error>> {
let stdout = io::stdout().into_raw_mode()?.into_alternate_screen()?;
let stdout = MouseTerminal::from(stdout);
let backend = TermionBackend::new(stdout);
let mut term = Terminal::new(backend)?;
let events = {
let events = io::stdin().events();
let (tx, rx) = mpsc::channel();
let keys_tx = tx.clone();
thread::spawn(move || {
for event in events.flatten() {
keys_tx.send(Event::Term(event)).unwrap();
}
});
thread::spawn(move || loop {
tx.send(Event::Tick).unwrap();
thread::sleep(Duration::from_millis(100));
});
rx
};
let mut textarea = TextArea::default();
textarea.set_block(
Block::default()
.borders(Borders::ALL)
.title("Termion Minimal Example"),
);
loop {
match events.recv()? {
Event::Term(event) => match event.into() {
Input { key: Key::Esc, .. } => break,
input => {
textarea.input(input);
}
},
Event::Tick => {}
}
term.draw(|f| {
f.render_widget(&textarea, f.area());
})?;
}
drop(term); // Leave terminal raw mode to print the following line
println!("Lines: {:?}", textarea.lines());
Ok(())
}