rmcl 0.4.0

A fully featured Minecraft TUI launcher
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// tui entrypoint: sets up the terminal, runs the app, cleans up on exit.

pub mod app;
mod event;
mod input;
pub mod logging;
mod render;
pub mod widgets;

use crate::feedback::request_redraw;

#[cfg(test)]
pub(crate) mod tests;

pub type Tui = ratatui::DefaultTerminal;

pub async fn show() -> color_eyre::Result<()> {
    // restore the terminal before printing a panic. without this, a panic
    // leaves raw mode + alternate screen active and looks like a freeze
    let default_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |info| {
        let _ = crossterm::execute!(
            std::io::stdout(),
            crossterm::event::DisableMouseCapture,
            crossterm::event::PopKeyboardEnhancementFlags
        );
        ratatui::restore();
        default_hook(info);
    }));

    let mut terminal = ratatui::init();

    // opt into enhanced keyboard protocol to distinguish key press vs release
    let _ = crossterm::execute!(
        std::io::stdout(),
        crossterm::event::EnableMouseCapture,
        crossterm::event::PushKeyboardEnhancementFlags(
            crossterm::event::KeyboardEnhancementFlags::REPORT_EVENT_TYPES
                | crossterm::event::KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
        )
    );

    let result = async {
        // figure out the terminal's font cell size for rendering images.
        // falls back to halfblock characters if the terminal doesn't respond
        let mut picker = ratatui_image::picker::Picker::from_query_stdio()
            .unwrap_or_else(|_| ratatui_image::picker::Picker::halfblocks());
        let detected_protocol = picker.protocol_type();
        let requested_protocol = match crate::config::SETTINGS.ui.image_protocol {
            crate::config::settings::ImageProtocol::Halfblocks
            | crate::config::settings::ImageProtocol::Quadrants => {
                ratatui_image::picker::ProtocolType::Halfblocks
            }
            crate::config::settings::ImageProtocol::Kitty
                if detected_protocol == ratatui_image::picker::ProtocolType::Kitty =>
            {
                ratatui_image::picker::ProtocolType::Kitty
            }
            crate::config::settings::ImageProtocol::Iterm2
                if detected_protocol == ratatui_image::picker::ProtocolType::Iterm2 =>
            {
                ratatui_image::picker::ProtocolType::Iterm2
            }
            _ => ratatui_image::picker::ProtocolType::Halfblocks,
        };
        picker.set_protocol_type(requested_protocol);

        let mut app = app::App::new(picker);
        match run_layout_migration_screen(&mut terminal, &mut app).await? {
            MigrationScreenOutcome::NotNeeded => {}
            MigrationScreenOutcome::Migrated => {
                // the modal uses pre-migration state as its background. rebuild the
                // app after confirmation so no instance or profile data stays stale
                let picker = app.into_picker();
                app = app::App::new(picker);
            }
            MigrationScreenOutcome::Quit => return Ok(()),
        }
        app.run(&mut terminal).await
    }
    .await;

    let _ = crossterm::execute!(
        std::io::stdout(),
        crossterm::event::DisableMouseCapture,
        crossterm::event::PopKeyboardEnhancementFlags
    );

    ratatui::restore();
    result
}

enum MigrationScreenOutcome {
    NotNeeded,
    Migrated,
    Quit,
}

async fn run_layout_migration_screen(
    terminal: &mut Tui,
    app: &mut app::App,
) -> color_eyre::Result<MigrationScreenOutcome> {
    use std::sync::{Arc, Mutex};
    use std::time::Duration;

    let instances_dir = crate::config::SETTINGS.paths.resolve_instances_dir();
    let meta_dir = crate::config::SETTINGS.paths.resolve_meta_dir();
    if !crate::layout_migration::is_needed(&instances_dir, &meta_dir) {
        crate::layout_migration::initialize_new_layout(&meta_dir)?;
        return Ok(MigrationScreenOutcome::NotNeeded);
    }
    let config = crate::config::get_config_path().join("config.toml");

    loop {
        let progress = Arc::new(Mutex::new(crate::layout_migration::MigrationProgress {
            phase: "Preparing migration".to_owned(),
            item: "Inventorying launcher data".to_owned(),
            current: 0,
            total: 1,
            item_current: None,
            item_total: None,
            backup_dir: None,
        }));
        let task_progress = progress.clone();
        let task_instances = instances_dir.clone();
        let task_meta = meta_dir.clone();
        let task_config = config.clone();
        let task = tokio::task::spawn_blocking(move || {
            crate::layout_migration::run(&task_instances, &task_meta, &task_config, |update| {
                if let Ok(mut current) = task_progress.lock() {
                    *current = update;
                }
                request_redraw();
            })
        });

        while !task.is_finished() {
            let current = progress.lock().ok().map(|state| state.clone());
            terminal.draw(|frame| {
                app.render_migration_frame(frame);
                if let Some(current) = &current {
                    let item_fraction = match (current.item_current, current.item_total) {
                        (Some(value), Some(total)) if total > 0 => {
                            value.min(total) as f64 / total as f64
                        }
                        _ => 0.0,
                    };
                    let ratio = if current.total == 0 {
                        0.0
                    } else {
                        ((current.current as f64 + item_fraction) / current.total as f64)
                            .clamp(0.0, 1.0)
                    };
                    render_migration_progress_popup(
                        frame,
                        ratio,
                        current.phase.clone(),
                        current.item.clone(),
                    );
                }
            })?;
            tokio::time::sleep(Duration::from_millis(50)).await;
        }

        match task.await? {
            Ok(backup) => {
                tracing::info!("Layout migration complete; backup at {}", backup.display());
                if crate::layout_migration::cache_rebuild_pending(&meta_dir) {
                    if let Err(error) =
                        rebuild_runtime_cache_screen(terminal, app, &instances_dir, &meta_dir).await
                    {
                        if migration_retry_requested(terminal, app, &error).await? {
                            continue;
                        }
                        return Ok(MigrationScreenOutcome::Quit);
                    }
                    crate::layout_migration::finish_cache_rebuild(&meta_dir)?;
                }
                migration_completion_confirmation(terminal, app, &backup).await?;
                return Ok(MigrationScreenOutcome::Migrated);
            }
            Err(error) => {
                if !migration_retry_requested(terminal, app, &error.to_string()).await? {
                    return Ok(MigrationScreenOutcome::Quit);
                }
            }
        }
    }
}

async fn rebuild_runtime_cache_screen(
    terminal: &mut Tui,
    app: &mut app::App,
    instances_dir: &std::path::Path,
    meta_dir: &std::path::Path,
) -> Result<(), String> {
    use std::sync::{Arc, Mutex};
    use std::time::Duration;

    let manager = crate::instance::InstanceManager::new(instances_dir, meta_dir);
    let instances = manager.load_all();
    let instance_total = instances.len() as u64;
    let instance_progress = Arc::new(Mutex::new((0_u64, String::new())));
    let task_progress = instance_progress.clone();
    let task = tokio::spawn(async move {
        for (index, instance) in instances.iter().enumerate() {
            if let Ok(mut state) = task_progress.lock() {
                state.0 = index as u64;
                state.1 = instance.name.clone();
            }
            request_redraw();
            manager
                .repair_runtime_cache(instance)
                .await
                .map_err(|error| error.to_string())?;
        }
        if let Ok(mut state) = task_progress.lock() {
            state.0 = instance_total;
            state.1.clear();
        }
        Ok::<(), String>(())
    });

    while !task.is_finished() {
        let progress = crate::feedback::progress::PROGRESS
            .lock()
            .ok()
            .map(|progress| progress.clone())
            .unwrap_or_default();
        let (instance_current, instance_name) = instance_progress
            .lock()
            .ok()
            .map(|state| state.clone())
            .unwrap_or_default();
        terminal
            .draw(|frame| {
                app.render_migration_frame(frame);
                let action = progress
                    .current_action
                    .as_deref()
                    .unwrap_or("Checking cached runtime files");
                let detail = progress.sub_action.as_deref().unwrap_or("");
                let (current, total) = progress.progress.unwrap_or((0, 0));
                let instance_ratio = if instance_total == 0 {
                    1.0
                } else {
                    instance_current.min(instance_total) as f64 / instance_total as f64
                };
                let ratio = if total == 0 {
                    instance_ratio
                } else {
                    current.min(total) as f64 / total as f64
                };
                let action = if instance_name.is_empty() {
                    action.to_owned()
                } else {
                    format!("{action} — {instance_name}")
                };
                render_migration_progress_popup(frame, ratio, action, detail.to_owned());
            })
            .map_err(|error| error.to_string())?;
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    task.await.map_err(|error| error.to_string())?
}

async fn migration_retry_requested(
    terminal: &mut Tui,
    app: &mut app::App,
    error: &str,
) -> color_eyre::Result<bool> {
    use crate::config::theme::{BORDER_STYLE, THEME};
    use crossterm::event::{Event, KeyCode};
    use ratatui::layout::{Constraint, Direction, Layout};
    use ratatui::style::Style;
    use ratatui::widgets::{Block, Paragraph, Wrap};
    use std::time::Duration;

    loop {
        terminal.draw(|frame| {
            app.render_migration_frame(frame);
            let theme = THEME.as_ref();
            let rows = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Fill(1),
                    Constraint::Length(9),
                    Constraint::Fill(1),
                ])
                .split(frame.area());
            let columns = Layout::default()
                .direction(Direction::Horizontal)
                .constraints([
                    Constraint::Percentage(12),
                    Constraint::Percentage(76),
                    Constraint::Percentage(12),
                ])
                .split(rows[1]);
            frame.render_widget(
                Paragraph::new(format!(
                    "Migration stopped safely:\n\n{error}\n\n[r] retry    [q] quit"
                ))
                .block(
                    Block::bordered()
                        .title(" Migration needs attention ")
                        .border_type(BORDER_STYLE.to_border_type())
                        .border_style(Style::default().fg(theme.error()))
                        .style(Style::default().fg(theme.text()).bg(theme.surface())),
                )
                .wrap(Wrap { trim: false }),
                columns[1],
            );
        })?;
        if crossterm::event::poll(Duration::from_millis(100))?
            && let Event::Key(key) = crossterm::event::read()?
        {
            match key.code {
                KeyCode::Char('r') => return Ok(true),
                KeyCode::Char('q') | KeyCode::Esc => return Ok(false),
                _ => {}
            }
        }
    }
}

async fn migration_completion_confirmation(
    terminal: &mut Tui,
    app: &mut app::App,
    backup: &std::path::Path,
) -> color_eyre::Result<()> {
    use crate::config::theme::THEME;
    use crate::tui::widgets::popups::{base::PopupFrame, keybind_line};
    use crossterm::event::{Event, KeyCode};
    use ratatui::style::{Modifier, Style};
    use ratatui::text::Line;
    use ratatui::widgets::{Paragraph, Widget, Wrap};
    use std::time::Duration;

    loop {
        terminal.draw(|frame| {
            app.render_migration_frame(frame);
            let theme = THEME.as_ref();
            let backup = backup.display().to_string();
            frame.render_widget(
                PopupFrame {
                    title: Line::from(" Migration finished ").style(
                        Style::default()
                            .fg(theme.success())
                            .add_modifier(Modifier::BOLD),
                    ),
                    border_color: theme.success(),
                    bg: Some(theme.surface()),
                    keybinds: Some(keybind_line(&[("Enter", " continue")])),
                    search_line: None,
                    content: Box::new(move |area, buffer| {
                        Paragraph::new(format!("Backup:\n{backup}"))
                            .style(Style::default().fg(theme.text()))
                            .wrap(Wrap { trim: false })
                            .render(area, buffer);
                    }),
                },
                migration_popup_area(frame.area(), 6),
            );
        })?;
        if crossterm::event::poll(Duration::from_millis(100))?
            && let Event::Key(key) = crossterm::event::read()?
            && key.code == KeyCode::Enter
        {
            return Ok(());
        }
    }
}

fn render_migration_progress_popup(
    frame: &mut ratatui::Frame,
    ratio: f64,
    action: String,
    detail: String,
) {
    use crate::config::theme::THEME;
    use crate::tui::widgets::popups::base::PopupFrame;
    use ratatui::layout::{Constraint, Layout};
    use ratatui::style::{Modifier, Style};
    use ratatui::text::Line;
    use ratatui::widgets::{Gauge, Paragraph, Widget};

    let theme = THEME.as_ref();
    frame.render_widget(
        PopupFrame {
            title: Line::from(" Migration ").style(
                Style::default()
                    .fg(theme.text_dim())
                    .add_modifier(Modifier::BOLD),
            ),
            border_color: theme.accent(),
            bg: Some(theme.surface()),
            keybinds: None,
            search_line: None,
            content: Box::new(move |area, buffer| {
                let rows = Layout::vertical([
                    Constraint::Length(1),
                    Constraint::Length(1),
                    Constraint::Length(1),
                ])
                .split(area);
                Gauge::default()
                    .gauge_style(
                        Style::default()
                            .fg(theme.success())
                            .bg(theme.surface())
                            .add_modifier(Modifier::BOLD),
                    )
                    .percent((ratio.clamp(0.0, 1.0) * 100.0) as u16)
                    .render(rows[0], buffer);
                Paragraph::new(action.as_str())
                    .style(Style::default().fg(theme.text()))
                    .render(rows[1], buffer);
                Paragraph::new(detail.as_str())
                    .style(Style::default().fg(theme.text_dim()))
                    .render(rows[2], buffer);
            }),
        },
        migration_popup_area(frame.area(), 5),
    );
}

fn migration_popup_area(area: ratatui::layout::Rect, height: u16) -> ratatui::layout::Rect {
    use ratatui::layout::Constraint;

    area.centered(
        Constraint::Length(area.width.saturating_sub(4).min(72)),
        Constraint::Length(area.height.saturating_sub(2).min(height)),
    )
}