/ tui / src / simulator / minimal.rs
minimal.rs
 1  use embedded_graphics_simulator::{OutputSettings, SimulatorDisplay, SimulatorEvent, Window};
 2  use mousefood::{embedded_graphics::geometry, error::Error, prelude::*};
 3  use ratatui::{
 4      Frame, Terminal,
 5      style::*,
 6      widgets::{Block, Paragraph, Wrap},
 7  };
 8  
 9  fn main() -> Result<(), Error> {
10      // Create window where the simulation will happen
11      let mut simulator_window = Window::new(
12          "mousefood simulator",
13          &OutputSettings {
14              scale: 4,
15              ..Default::default()
16          },
17      );
18      simulator_window.set_max_fps(30);
19  
20      // Define properties of the display which will be shown in the simulator window
21      let mut display = SimulatorDisplay::<Bgr565>::new(geometry::Size::new(128, 64));
22  
23      let backend_config = EmbeddedBackendConfig {
24          // Define how to display newly rendered widgets to the simulator window
25          flush_callback: Box::new(move |display| {
26              simulator_window.update(display);
27              if simulator_window.events().any(|e| e == SimulatorEvent::Quit) {
28                  panic!("simulator window closed");
29              }
30          }),
31          ..Default::default()
32      };
33      let backend: EmbeddedBackend<SimulatorDisplay<_>, _> =
34          EmbeddedBackend::new(&mut display, backend_config);
35  
36      // Start ratatui with our simulator backend
37      let mut terminal = Terminal::new(backend)?;
38  
39      // Run an infinite loop, where widgets will be rendered
40      loop {
41          terminal.draw(draw)?;
42      }
43  }
44  
45  fn draw(frame: &mut Frame) {
46      let text = "Ratatui on embedded devices!";
47      let paragraph = Paragraph::new(text.dark_gray()).wrap(Wrap { trim: true });
48      let bordered_block = Block::bordered()
49          .border_style(Style::new().yellow())
50          .title("Mousefood");
51      frame.render_widget(paragraph.block(bordered_block), frame.area());
52  }