Skip to main content

hal9001/
lib.rs

1pub mod app;
2pub mod ascii;
3pub mod backend;
4pub mod config;
5pub mod events;
6pub mod i18n;
7pub mod logging;
8pub mod ui;
9
10use std::time::Duration;
11
12use anyhow::Result;
13use ratatui::DefaultTerminal;
14use tokio::sync::{broadcast, mpsc};
15
16use crate::app::App;
17use crate::config::Config;
18use crate::events::input::InputStream;
19use crate::events::{Action, AppEvent, SudoPasswordRequest};
20
21pub async fn run(mut terminal: DefaultTerminal, config: Config) -> Result<()> {
22    let (event_tx, mut event_rx) = mpsc::unbounded_channel::<AppEvent>();
23    let (action_tx, _action_rx) = broadcast::channel::<Action>(64);
24    let (sudo_tx, mut sudo_rx) = mpsc::unbounded_channel::<SudoPasswordRequest>();
25
26    let mut app = App::new(config);
27    backend::spawn_all(
28        &app.config,
29        app.shared_lang.clone(),
30        event_tx.clone(),
31        &action_tx,
32        sudo_tx,
33    );
34
35    let mut input = InputStream::new();
36    let mut render_tick = tokio::time::interval(Duration::from_millis(250));
37    render_tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
38
39    terminal.draw(|f| ui::draw(&app, f))?;
40
41    loop {
42        tokio::select! {
43            Some(ev) = event_rx.recv() => {
44                for follow_up in app.handle_event(ev) {
45                    let _ = action_tx.send(follow_up);
46                }
47                terminal.draw(|f| ui::draw(&app, f))?;
48            }
49            Some(action) = input.next(
50                app.active,
51                app.storage_modal_open(),
52                app.text_input_active(),
53                app.sudo_prompt_open(),
54                app.storage_analyzer_open(),
55                app.battery_bypass.is_some(),
56            ) => {
57                app.dispatch(action, &action_tx);
58                terminal.draw(|f| ui::draw(&app, f))?;
59            }
60            Some(req) = sudo_rx.recv() => {
61                app.open_sudo_prompt(req);
62                terminal.draw(|f| ui::draw(&app, f))?;
63            }
64            _ = render_tick.tick(), if app.needs_continuous_tick() => {
65                app.on_tick();
66                terminal.draw(|f| ui::draw(&app, f))?;
67            }
68        }
69
70        if app.should_quit {
71            break;
72        }
73    }
74
75    Ok(())
76}