Skip to content

Commit f12c85e

Browse files
authored
refactor: remove App from simple templates (#94)
1 parent 2ea7781 commit f12c85e

File tree

10 files changed

+360
-384
lines changed

10 files changed

+360
-384
lines changed

simple-async-generated/src/app.rs

-95
This file was deleted.

simple-async-generated/src/main.rs

+95-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
pub use app::App;
2-
3-
pub mod app;
1+
use color_eyre::Result;
2+
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
3+
use futures::{FutureExt, StreamExt};
4+
use ratatui::{
5+
style::Stylize,
6+
text::Line,
7+
widgets::{Block, Paragraph},
8+
DefaultTerminal, Frame,
9+
};
410

511
#[tokio::main]
612
async fn main() -> color_eyre::Result<()> {
@@ -10,3 +16,89 @@ async fn main() -> color_eyre::Result<()> {
1016
ratatui::restore();
1117
result
1218
}
19+
20+
#[derive(Debug, Default)]
21+
pub struct App {
22+
/// Is the application running?
23+
running: bool,
24+
// Event stream.
25+
event_stream: EventStream,
26+
}
27+
28+
impl App {
29+
/// Construct a new instance of [`App`].
30+
pub fn new() -> Self {
31+
Self::default()
32+
}
33+
34+
/// Run the application's main loop.
35+
pub async fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
36+
self.running = true;
37+
while self.running {
38+
terminal.draw(|frame| self.draw(frame))?;
39+
self.handle_crossterm_events().await?;
40+
}
41+
Ok(())
42+
}
43+
44+
/// Renders the user interface.
45+
///
46+
/// This is where you add new widgets. See the following resources for more information:
47+
/// - <https://docs.rs/ratatui/latest/ratatui/widgets/index.html>
48+
/// - <https://github.com/ratatui/ratatui/tree/master/examples>
49+
fn draw(&mut self, frame: &mut Frame) {
50+
let title = Line::from("Ratatui Simple Template")
51+
.bold()
52+
.blue()
53+
.centered();
54+
let text = "Hello, Ratatui!\n\n\
55+
Created using https://github.com/ratatui/templates\n\
56+
Press `Esc`, `Ctrl-C` or `q` to stop running.";
57+
frame.render_widget(
58+
Paragraph::new(text)
59+
.block(Block::bordered().title(title))
60+
.centered(),
61+
frame.area(),
62+
)
63+
}
64+
65+
/// Reads the crossterm events and updates the state of [`App`].
66+
async fn handle_crossterm_events(&mut self) -> Result<()> {
67+
tokio::select! {
68+
event = self.event_stream.next().fuse() => {
69+
match event {
70+
Some(Ok(evt)) => {
71+
match evt {
72+
Event::Key(key)
73+
if key.kind == KeyEventKind::Press
74+
=> self.on_key_event(key),
75+
Event::Mouse(_) => {}
76+
Event::Resize(_, _) => {}
77+
_ => {}
78+
}
79+
}
80+
_ => {}
81+
}
82+
}
83+
_ = tokio::time::sleep(tokio::time::Duration::from_millis(100)) => {
84+
// Sleep for a short duration to avoid busy waiting.
85+
}
86+
}
87+
Ok(())
88+
}
89+
90+
/// Handles the key events and updates the state of [`App`].
91+
fn on_key_event(&mut self, key: KeyEvent) {
92+
match (key.modifiers, key.code) {
93+
(_, KeyCode::Esc | KeyCode::Char('q'))
94+
| (KeyModifiers::CONTROL, KeyCode::Char('c') | KeyCode::Char('C')) => self.quit(),
95+
// Add other key handlers here.
96+
_ => {}
97+
}
98+
}
99+
100+
/// Set running to false to quit the application.
101+
fn quit(&mut self) {
102+
self.running = false;
103+
}
104+
}

simple-async/README.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
# Ratatui Simple template
22

3-
The simple template will create the following project structure:
4-
5-
```text
6-
src/
7-
├── app.rs -> holds the state and application logic
8-
└── main.rs -> entry-point
9-
```
3+
A simple example of a Ratatui application.
104

115
This is identical to the [simple](../simple) template but has `async` events out of the box with `tokio` and
126
`crossterm`'s `EventStream`.

simple-async/template/src/app.rs

-95
This file was deleted.

0 commit comments

Comments
 (0)