add: example of simple_async copy and pasted

This commit is contained in:
2025-12-31 16:40:21 +00:00
parent e959f1ad99
commit d7733851f6
3 changed files with 2099 additions and 1 deletions

2012
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,3 +4,9 @@ version = "0.1.0"
edition = "2024"
[dependencies]
color-eyre = "0.6.3"
crossterm = { version = "0.28.1", features = ["event-stream"] }
futures = "0.3.31"
ratatui = "0.29.0"
smol = "2.0.2"
upower_dbus = "0.3.2"

View File

@@ -1,3 +1,83 @@
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use futures::{FutureExt, StreamExt};
use ratatui::{
DefaultTerminal, Frame, buffer::Buffer, style::Stylize, text::Line, widgets::{Block, Gauge, Paragraph, Widget}
};
fn main() {
println!("Hello, world!");
smol::block_on(async {
color_eyre::install().unwrap();
let terminal = ratatui::init();
let result = App::new().run(terminal).await;
ratatui::restore();
});
}
#[derive(Debug, Default)]
pub struct App {
/// Is the application running?
running: bool,
// Event stream.
event_stream: EventStream,
}
impl App {
/// Construct a new instance of [`App`].
pub fn new() -> Self {
Self::default()
}
/// Run the application's main loop.
pub async fn run(mut self, mut terminal: DefaultTerminal) -> color_eyre::Result<()> {
self.running = true;
while self.running {
terminal.draw(|frame| self.draw(frame))?;
self.handle_crossterm_events().await?;
}
Ok(())
}
/// Renders the user interface.
///
/// This is where you add new widgets. See the following resources for more information:
/// - <https://docs.rs/ratatui/latest/ratatui/widgets/index.html>
/// - <https://github.com/ratatui/ratatui/tree/master/examples>
fn draw(&mut self, frame: &mut Frame, buf &mut Buffer) {
let title = Line::from("Ratatui Simple Template")
.bold()
.blue()
.centered();
let text = "Hello, Ratatui!\n\n\
Created using https://github.com/ratatui/templates\n\
Press `Esc`, `Ctrl-C` or `q` to stop running.";
frame.render_widget();
/// Reads the crossterm events and updates the state of [`App`].
async fn handle_crossterm_events(&mut self) -> color_eyre::Result<()> {
let event = self.event_stream.next().fuse().await;
match event {
Some(Ok(evt)) => match evt {
Event::Key(key) if key.kind == KeyEventKind::Press => self.on_key_event(key),
Event::Mouse(_) => {}
Event::Resize(_, _) => {}
_ => {}
},
_ => {}
}
Ok(())
}
/// Handles the key events and updates the state of [`App`].
fn on_key_event(&mut self, key: KeyEvent) {
match (key.modifiers, key.code) {
(_, KeyCode::Esc | KeyCode::Char('q'))
| (KeyModifiers::CONTROL, KeyCode::Char('c') | KeyCode::Char('C')) => self.quit(),
// Add other key handlers here.
_ => {}
}
}
/// Set running to false to quit the application.
fn quit(&mut self) {
self.running = false;
}
}