romm-cli 0.37.0

Rust-based CLI and TUI for the ROMM API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::collections::HashMap;

use crate::config::{
    disk_has_unresolved_keyring_sentinel, Config, RomsLayoutConfig, SaveSyncConfig,
};
use crate::feature_compat::SaveSyncCompatibility;
use crate::tui::path_picker::{PathPicker, PathPickerMode};

use super::types::{
    ConsolePathKind, SettingsConfirm, SettingsPickerKind, SettingsRow, SettingsScreen, SettingsTab,
    APPEARANCE_ROWS, AUTH_MAINT_ROWS, CONNECTION_ROWS, EXTRAS_ROWS, SAVES_ROWS,
};
use crate::tui::theme::{next_theme_id, prev_theme_id, theme_display_name, MessageTone};

impl SettingsScreen {
    pub fn new(
        config: &Config,
        romm_server_version: Option<&str>,
        save_sync_compat: SaveSyncCompatibility,
    ) -> Self {
        let auth_status = match &config.auth {
            Some(crate::config::AuthConfig::Basic { username, .. }) => {
                format!("Basic (user: {})", username)
            }
            Some(crate::config::AuthConfig::Bearer { .. }) => "API Token".to_string(),
            Some(crate::config::AuthConfig::ApiKey { header, .. }) => {
                format!("API key (header: {})", header)
            }
            None => {
                if disk_has_unresolved_keyring_sentinel(config) {
                    "None — disk still references keyring; set API_TOKEN / ROMM_TOKEN_FILE or see docs/troubleshooting-auth.md"
                        .to_string()
                } else {
                    "None (no API credentials in env/keyring)".to_string()
                }
            }
        };

        let server_version = romm_server_version
            .map(String::from)
            .unwrap_or_else(|| "unavailable (heartbeat failed)".to_string());

        Self {
            base_url: config.base_url.clone(),
            download_dir: config.download_dir.clone(),
            save_dir: crate::config::resolved_save_dir(config)
                .display()
                .to_string(),
            sync_device_id: config.save_sync.device_id.clone(),
            use_https: config.use_https,
            extras_include_related_roms: config.extras_defaults.include_related_roms,
            extras_include_cover: config.extras_defaults.include_cover,
            extras_include_manual: config.extras_defaults.include_manual,
            auth_status,
            version: env!("CARGO_PKG_VERSION").to_string(),
            server_version,
            github_url: "https://github.com/patricksmill/romm-cli".to_string(),
            theme_id: config.theme.clone(),
            selected_tab: SettingsTab::Connection,
            selected_indices: [0; SettingsTab::COUNT],
            editing: false,
            confirm: None,
            edit_buffer: String::new(),
            edit_cursor: 0,
            path_picker: None,
            devices: Vec::new(),
            device_picker_open: false,
            device_picker_loading: false,
            device_picker_error: None,
            device_selected_index: 0,
            sync_inflight: false,
            message: None,
            save_sync_compat,
            rom_platform_dirs: config.roms_layout.platform_dirs.clone(),
            save_platform_dirs: config.save_sync.platform_dirs.clone(),
            console_picker_open: false,
            active_console_kind: None,
            console_picker_loading: false,
            console_picker_error: None,
            console_platforms: Vec::new(),
            console_selected_index: 0,
            console_path_picker: None,
        }
    }

    pub fn roms_layout_config(&self) -> RomsLayoutConfig {
        let mut layout = RomsLayoutConfig::default();
        layout.platform_dirs = self.rom_platform_dirs.clone();
        layout
    }

    pub fn save_sync_config(&self) -> SaveSyncConfig {
        SaveSyncConfig {
            save_dir: Some(self.save_dir.clone()),
            device_id: self.sync_device_id.clone(),
            platform_dirs: self.save_platform_dirs.clone(),
        }
    }

    /// Whether the on-screen values differ from the last saved in-memory config.
    pub fn has_unsaved_changes(&self, saved: &Config) -> bool {
        if self.base_url != saved.base_url {
            return true;
        }
        if self.download_dir != saved.download_dir {
            return true;
        }
        if self.use_https != saved.use_https {
            return true;
        }
        if self.extras_include_related_roms != saved.extras_defaults.include_related_roms {
            return true;
        }
        if self.extras_include_cover != saved.extras_defaults.include_cover {
            return true;
        }
        if self.extras_include_manual != saved.extras_defaults.include_manual {
            return true;
        }
        if self.theme_id != saved.theme {
            return true;
        }
        if self.roms_layout_config() != saved.roms_layout {
            return true;
        }
        if self.sync_device_id != saved.save_sync.device_id {
            return true;
        }
        if self.save_platform_dirs != saved.save_sync.platform_dirs {
            return true;
        }
        let saved_save_dir = crate::config::resolved_save_dir(saved)
            .display()
            .to_string();
        self.save_dir != saved_save_dir
    }

    pub(crate) fn console_dirs(&self, kind: ConsolePathKind) -> &HashMap<u64, String> {
        match kind {
            ConsolePathKind::Roms => &self.rom_platform_dirs,
            ConsolePathKind::Saves => &self.save_platform_dirs,
        }
    }

    pub(crate) fn console_dirs_mut(&mut self, kind: ConsolePathKind) -> &mut HashMap<u64, String> {
        match kind {
            ConsolePathKind::Roms => &mut self.rom_platform_dirs,
            ConsolePathKind::Saves => &mut self.save_platform_dirs,
        }
    }

    pub fn visible_rows(&self) -> Vec<SettingsRow> {
        match self.selected_tab {
            SettingsTab::Connection => CONNECTION_ROWS.to_vec(),
            SettingsTab::Roms => vec![SettingsRow::RomsDir, SettingsRow::ConsolePaths],
            SettingsTab::Saves => SAVES_ROWS.to_vec(),
            SettingsTab::Extras => EXTRAS_ROWS.to_vec(),
            SettingsTab::Appearance => APPEARANCE_ROWS.to_vec(),
            SettingsTab::AuthMaintenance => AUTH_MAINT_ROWS.to_vec(),
        }
    }

    pub fn save_sync_supported(&self) -> bool {
        self.save_sync_compat.supported
    }

    pub fn set_save_sync_unsupported_message(&mut self) {
        self.message = Some((
            self.save_sync_compat.unsupported_message(),
            MessageTone::Warning,
        ));
    }

    pub fn selected_row_index(&self) -> usize {
        let rows = self.visible_rows();
        self.selected_indices[self.selected_tab.index()].min(rows.len().saturating_sub(1))
    }

    fn set_selected_row_index(&mut self, index: usize) {
        let max = self.visible_rows().len().saturating_sub(1);
        self.selected_indices[self.selected_tab.index()] = index.min(max);
    }

    pub fn selected_row(&self) -> SettingsRow {
        let rows = self.visible_rows();
        rows[self.selected_row_index()]
    }

    pub fn active_rows(&self) -> &[SettingsRow] {
        // Legacy helper for tests; prefer visible_rows().
        match self.selected_tab {
            SettingsTab::Connection => &CONNECTION_ROWS,
            SettingsTab::Saves => &SAVES_ROWS,
            SettingsTab::Extras => &EXTRAS_ROWS,
            SettingsTab::Appearance => &APPEARANCE_ROWS,
            SettingsTab::AuthMaintenance => &AUTH_MAINT_ROWS,
            SettingsTab::Roms => &[],
        }
    }

    pub fn cycle_theme_next(&mut self) {
        self.theme_id = next_theme_id(&self.theme_id);
    }

    pub fn cycle_theme_prev(&mut self) {
        self.theme_id = prev_theme_id(&self.theme_id);
    }

    pub fn theme_display_name(&self) -> String {
        theme_display_name(&self.theme_id)
    }

    pub fn next_tab(&mut self) {
        if self.editing || self.confirm.is_some() {
            return;
        }
        let next = (self.selected_tab.index() + 1) % SettingsTab::COUNT;
        self.selected_tab = SettingsTab::ALL[next];
        self.set_selected_row_index(self.selected_row_index());
    }

    pub fn previous_tab(&mut self) {
        if self.editing || self.confirm.is_some() {
            return;
        }
        let previous = (self.selected_tab.index() + SettingsTab::COUNT - 1) % SettingsTab::COUNT;
        self.selected_tab = SettingsTab::ALL[previous];
        self.set_selected_row_index(self.selected_row_index());
    }

    pub fn next(&mut self) {
        if !self.editing && self.confirm.is_none() {
            let len = self.visible_rows().len();
            if len > 0 {
                self.set_selected_row_index((self.selected_row_index() + 1) % len);
            }
        }
    }

    pub fn previous(&mut self) {
        if !self.editing && self.confirm.is_none() {
            let len = self.visible_rows().len();
            if len == 0 {
                return;
            }
            if self.selected_row_index() == 0 {
                self.set_selected_row_index(len - 1);
            } else {
                self.set_selected_row_index(self.selected_row_index() - 1);
            }
        }
    }

    pub fn enter_edit(&mut self) {
        match self.selected_row() {
            SettingsRow::ResetConfiguration => self.confirm = Some(SettingsConfirm::Reset),
            SettingsRow::ClearCache => self.confirm = Some(SettingsConfirm::ClearCache),
            SettingsRow::SyncDevice => {
                if !self.save_sync_supported() {
                    self.set_save_sync_unsupported_message();
                    return;
                }
                self.device_picker_open = true;
                self.device_picker_loading = true;
                self.device_picker_error = None;
                self.message = Some(("Loading devices...".to_string(), MessageTone::Warning));
            }
            SettingsRow::SyncNow => {
                if !self.save_sync_supported() {
                    self.set_save_sync_unsupported_message();
                    return;
                }
                self.message = Some(("Starting save sync...".to_string(), MessageTone::Warning));
            }
            SettingsRow::ExtrasManual => {
                self.extras_include_manual = !self.extras_include_manual;
                self.message = Some((
                    format!(
                        "Extras default (manual): {}",
                        if self.extras_include_manual {
                            "on"
                        } else {
                            "off"
                        }
                    ),
                    MessageTone::Success,
                ));
            }
            SettingsRow::ExtrasCover => {
                self.extras_include_cover = !self.extras_include_cover;
                self.message = Some((
                    format!(
                        "Extras default (cover): {}",
                        if self.extras_include_cover {
                            "on"
                        } else {
                            "off"
                        }
                    ),
                    MessageTone::Success,
                ));
            }
            SettingsRow::ExtrasRelatedRoms => {
                self.extras_include_related_roms = !self.extras_include_related_roms;
                self.message = Some((
                    format!(
                        "Extras default (updates/DLC): {}",
                        if self.extras_include_related_roms {
                            "on"
                        } else {
                            "off"
                        }
                    ),
                    MessageTone::Success,
                ));
            }
            SettingsRow::UseHttps => {
                // Toggle HTTPS directly and keep the Base URL scheme in sync.
                self.use_https = !self.use_https;
                if self.use_https && self.base_url.starts_with("http://") {
                    self.base_url = self.base_url.replace("http://", "https://");
                    self.message = Some((
                        "Updated URL scheme (HTTPS)".to_string(),
                        MessageTone::Success,
                    ));
                } else if !self.use_https && self.base_url.starts_with("https://") {
                    self.base_url = self.base_url.replace("https://", "http://");
                    self.message = Some((
                        "Updated URL scheme (HTTP)".to_string(),
                        MessageTone::Success,
                    ));
                }
            }
            SettingsRow::RomsDir => {
                self.path_picker = Some((
                    SettingsPickerKind::RomsDir,
                    PathPicker::new(PathPickerMode::Directory, self.download_dir.as_str()),
                ));
            }
            SettingsRow::ConsolePaths | SettingsRow::SaveConsolePaths => {}
            SettingsRow::SaveDir => {
                self.path_picker = Some((
                    SettingsPickerKind::SaveDir,
                    PathPicker::new(PathPickerMode::Directory, self.save_dir.as_str()),
                ));
            }
            SettingsRow::BaseUrl => {
                self.editing = true;
                self.edit_buffer = self.base_url.clone();
                self.edit_cursor = self.edit_buffer.len();
            }
            SettingsRow::Theme => {}
            SettingsRow::Auth => {}
        }
    }

    pub fn save_edit(&mut self) -> bool {
        if !self.editing {
            return true; // UseHttps toggle is "saved" immediately in memory
        }
        if self.selected_row() == SettingsRow::BaseUrl {
            self.base_url = self.edit_buffer.trim().to_string();
        }
        self.editing = false;
        true
    }

    pub fn cancel_edit(&mut self) {
        self.editing = false;
        self.confirm = None;
        self.path_picker = None;
        self.console_path_picker = None;
        self.console_picker_open = false;
        self.active_console_kind = None;
        self.message = None;
    }

    pub fn add_char(&mut self, c: char) {
        if self.editing {
            self.edit_buffer.insert(self.edit_cursor, c);
            self.edit_cursor += 1;
        }
    }

    pub fn delete_char(&mut self) {
        if self.editing && self.edit_cursor > 0 {
            self.edit_buffer.remove(self.edit_cursor - 1);
            self.edit_cursor -= 1;
        }
    }

    pub fn move_cursor_left(&mut self) {
        if self.editing && self.edit_cursor > 0 {
            self.edit_cursor -= 1;
        }
    }

    pub fn move_cursor_right(&mut self) {
        if self.editing && self.edit_cursor < self.edit_buffer.len() {
            self.edit_cursor += 1;
        }
    }
}