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 crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
use std::io;
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout},
widgets::Paragraph,
Frame, Terminal,
};
use pfc::ui;
enum Signal {
None,
Exit,
}
#[derive(Default)]
struct App {}
impl App {
fn handle_input(&mut self, key: KeyEvent) -> Signal {
match key.modifiers {
KeyModifiers::CONTROL => match key.code {
KeyCode::Char('c') => {
return Signal::Exit;
}
_ => {}
},
KeyModifiers::NONE => match key.code {
KeyCode::Char('q') => {
return Signal::Exit;
}
_ => {}
},
_ => {}
}
return Signal::None;
}
fn draw<B: Backend>(&self, f: &mut Frame<B>) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(100)].as_ref())
.split(f.size());
f.render_widget(Paragraph::new("test"), chunks[0]);
}
}
fn main() -> io::Result<()> {
let app = App::default();
let mut terminal = ui::init_terminal()?;
let result = run(app, &mut terminal);
ui::cleanup_terminal(terminal)?;
result
}
fn run<B: Backend>(mut app: App, terminal: &mut Terminal<B>) -> io::Result<()> {
loop {
terminal.draw(|f| app.draw(f))?;
if let Event::Key(key) = event::read()? {
if let Signal::Exit = app.handle_input(key) {
return Ok(());
}
}
}
}
|