Skip to main content

romm_cli/tui/
mod.rs

1//! Terminal UI module.
2//!
3//! This module contains all ratatui / crossterm code and is responsible
4//! purely for presentation and interaction. It talks to the rest of the
5//! application through:
6//! - `RommClient` (HTTP / data access),
7//! - `core::cache::RomCache` (disk-backed ROM cache), and
8//! - `core::download::DownloadManager` (background ROM downloads).
9//!
10//! Keeping those \"service\" types UI-agnostic makes it easy to add other
11//! frontends (e.g. a GUI) reusing the same core logic.
12
13pub mod app;
14pub mod keyboard_help;
15pub mod openapi;
16pub mod openapi_sync;
17pub mod screens;
18pub mod text_search;
19pub mod utils;
20
21use anyhow::Result;
22
23use crate::client::RommClient;
24use crate::config::{openapi_cache_path, Config};
25
26use self::app::App;
27use self::openapi_sync::sync_openapi_registry;
28use self::screens::connected_splash::StartupSplash;
29use self::screens::setup_wizard::SetupWizard;
30
31fn install_panic_hook() {
32    let original_hook = std::panic::take_hook();
33    std::panic::set_hook(Box::new(move |panic| {
34        let _ = crossterm::terminal::disable_raw_mode();
35        let _ = crossterm::execute!(
36            std::io::stdout(),
37            crossterm::terminal::LeaveAlternateScreen,
38            crossterm::event::DisableMouseCapture
39        );
40        original_hook(panic);
41    }));
42}
43
44fn startup_splash_for(
45    from_setup_wizard: bool,
46    config: &Config,
47    server_version: &Option<String>,
48) -> Option<StartupSplash> {
49    if from_setup_wizard {
50        return Some(StartupSplash::new(
51            config.base_url.clone(),
52            server_version.clone(),
53        ));
54    }
55    if server_version.is_some() {
56        return Some(StartupSplash::new(
57            config.base_url.clone(),
58            server_version.clone(),
59        ));
60    }
61    None
62}
63
64async fn run_started(client: RommClient, config: Config, from_setup_wizard: bool) -> Result<()> {
65    install_panic_hook();
66    let cache_path = openapi_cache_path()?;
67    let (registry, server_version) = sync_openapi_registry(&client, &cache_path).await?;
68
69    let splash = startup_splash_for(from_setup_wizard, &config, &server_version);
70    let mut app = App::new(client, config, registry, server_version, splash);
71    app.run().await
72}
73
74/// Launch the TUI when the caller already has a [`RommClient`] and [`Config`].
75pub async fn run(client: RommClient, config: Config) -> Result<()> {
76    run_started(client, config, false).await
77}
78
79/// Load config, run first-time setup in the terminal if `API_BASE_URL` is missing, then start the TUI.
80pub async fn run_interactive(verbose: bool) -> Result<()> {
81    let (from_wizard, config) = match crate::config::load_config() {
82        Ok(c) => (false, c),
83        Err(_) => (true, SetupWizard::new().run(verbose).await?),
84    };
85    let client = RommClient::new(&config, verbose)?;
86    run_started(client, config, from_wizard).await
87}