/ src / runner / mod.rs
mod.rs
 1  // SPDX-FileCopyrightText: 2024 sntx <sntx@sntx.space>
 2  // SPDX-License-Identifier: AGPL-3.0-or-later
 3  
 4  use bevy_app::prelude::{App, AppExit};
 5  use bevy_utils::tracing::error;
 6  use ratatui::crossterm::event::{poll, read, KeyCode, KeyModifiers};
 7  use std::time::Duration;
 8  
 9  use crate::{Event, State};
10  
11  pub fn runner(mut app: App) -> AppExit {
12      app.finish();
13      app.cleanup();
14  
15      loop {
16          app.update();
17  
18          if app.world().resource::<State>() == &State::Exit {
19              return AppExit::Success;
20          }
21  
22          // `read()` blocks until an `Event` is available
23          match poll(Duration::from_millis(20)) {
24              Ok(true) => match read() {
25                  // Always exit on `C-d`
26                  Ok(ratatui::crossterm::event::Event::Key(key_event))
27                      if key_event.code == KeyCode::Char('d')
28                          && key_event.modifiers == KeyModifiers::CONTROL =>
29                  {
30                      return AppExit::Success;
31                  }
32                  Ok(event) => {
33                      app.world_mut().send_event(Event(event));
34                  }
35                  _ => (),
36              },
37              Ok(false) => continue,
38              Err(e) => error!("{}", e),
39          }
40      }
41  }