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_sync;
16pub mod path_picker;
17pub mod screens;
18pub mod text_search;
19pub mod utils;
20
21use anyhow::Result;
22use std::time::Duration;
23
24use crate::client::RommClient;
25use crate::config::{openapi_cache_path, should_check_updates, Config};
26
27use self::app::App;
28use self::openapi_sync::sync_openapi_registry;
29use self::screens::connected_splash::StartupSplash;
30use self::screens::setup_wizard::SetupWizard;
31
32fn install_panic_hook() {
33    let original_hook = std::panic::take_hook();
34    std::panic::set_hook(Box::new(move |panic| {
35        let _ = crossterm::terminal::disable_raw_mode();
36        let _ = crossterm::execute!(
37            std::io::stdout(),
38            crossterm::terminal::LeaveAlternateScreen,
39            crossterm::event::DisableMouseCapture
40        );
41        original_hook(panic);
42    }));
43}
44
45fn startup_splash_for(
46    from_setup_wizard: bool,
47    config: &Config,
48    server_version: &Option<String>,
49) -> Option<StartupSplash> {
50    if from_setup_wizard {
51        return Some(StartupSplash::new(
52            config.base_url.clone(),
53            server_version.clone(),
54        ));
55    }
56    if server_version.is_some() {
57        return Some(StartupSplash::new(
58            config.base_url.clone(),
59            server_version.clone(),
60        ));
61    }
62    None
63}
64
65async fn run_started(
66    client: RommClient,
67    config: Config,
68    from_setup_wizard: bool,
69    mock_update: bool,
70) -> Result<()> {
71    install_panic_hook();
72    let cache_path = openapi_cache_path()?;
73    let (registry, server_version) = sync_openapi_registry(&client, &cache_path).await?;
74
75    let startup_update = if mock_update {
76        Some(crate::update::UpdateStatus {
77            current_version: format!("{} (dev)", env!("CARGO_PKG_VERSION")),
78            latest_version: "9.9.9-mock".into(),
79            release_tag: "v9.9.9-mock".into(),
80            should_update: true,
81            release_url: "https://github.com/patricksmill/romm-cli".into(),
82            changelog_url: crate::update::changelog_url().to_string(),
83        })
84    } else if should_check_updates() {
85        match tokio::time::timeout(Duration::from_secs(2), crate::update::check_for_update()).await
86        {
87            Ok(Ok(status)) if status.should_update => Some(status),
88            _ => None,
89        }
90    } else {
91        None
92    };
93
94    let splash = startup_splash_for(from_setup_wizard, &config, &server_version);
95    let mut app = App::new(
96        client,
97        config,
98        registry,
99        server_version,
100        splash,
101        startup_update,
102    );
103    app.run().await
104}
105
106/// Launch the TUI when the caller already has a [`RommClient`] and [`Config`].
107pub async fn run(client: RommClient, config: Config, mock_update: bool) -> Result<()> {
108    run_started(client, config, false, mock_update).await
109}
110
111/// Load config, run first-time setup in the terminal if `API_BASE_URL` is missing, then start the TUI.
112pub async fn run_interactive(verbose: bool, mock_update: bool) -> Result<()> {
113    let (from_wizard, config) = match crate::config::load_config() {
114        Ok(c) => (false, c),
115        Err(_) => (true, SetupWizard::new().run(verbose).await?),
116    };
117    let client = RommClient::new(&config, verbose)?;
118    run_started(client, config, from_wizard, mock_update).await
119}