impl Trait(can run)
This commit is contained in:
46
src/app.rs
Normal file
46
src/app.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use std::io;
|
||||
|
||||
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
|
||||
use crate::view::home::{self, Home};
|
||||
|
||||
pub struct App {
|
||||
running: bool,
|
||||
view: Home,
|
||||
}
|
||||
|
||||
impl Default for App {
|
||||
fn default() -> Self {
|
||||
App {
|
||||
running: true,
|
||||
view: Home::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
|
||||
while self.running {
|
||||
terminal.draw(|frame| self.draw(frame))?;
|
||||
self.handle_event()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw(&self, frame: &mut Frame) {
|
||||
self.view.draw(frame);
|
||||
}
|
||||
|
||||
fn handle_event(&mut self) -> io::Result<()> {
|
||||
match event::read()? {
|
||||
Event::Key(KeyEvent {
|
||||
kind: KeyEventKind::Press,
|
||||
code: KeyCode::Esc,
|
||||
..
|
||||
}) => self.running = false,
|
||||
event => self.view.handle_event(event)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user