fpv 0.1.12

A minimal, keyboard-first TUI file previewer with syntax highlighting
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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
use crate::app::preview_controller::refresh_preview;
use crate::app::state::{PreviewDocument, SessionState};
use crate::config::keymap::{default_keymap, UserKeymap};
use crate::config::load::{
    default_config_path, ensure_default_config_exists, load_user_config, StatusDisplayMode,
    ThemeProfile, UserConfig,
};
use crate::config::merge::{merge_keymaps, merge_theme_profile};
use crate::config::validate::validate_bindings;
use crate::fs::current_dir::{
    directory_access_error_message, list_current_directory_with_visibility,
};
use crate::fs::git::{GitStatusUpdate, GitStatusWorker};
use crate::fs::preview::ImagePreviewWorker;
use crate::highlight::syntax::HighlightContext;
use crate::tui::event_loop::process_once;
use crate::tui::preview_pane::{draw_preview, preview_total_lines};
use crate::tui::status_bar::{compose_shortcut_help_text, draw_status};
use crate::tui::tree_pane::{draw_current_directory_header, draw_tree};
use crate::{app::navigation::format_status_with_path, app::state::TreeNode};
use anyhow::Result;
use crossterm::event::DisableMouseCapture;
use crossterm::event::EnableMouseCapture;
use crossterm::execute;
use crossterm::terminal::{
    disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
};
use ratatui::backend::CrosstermBackend;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Style};
use ratatui::widgets::{Block, Borders, Clear, Paragraph, Wrap};
use ratatui::Terminal;
use std::env;
use std::io;
use std::path::{Path, PathBuf};
use std::time::Instant;

fn image_paths_match(left: &Path, right: &Path) -> bool {
    if left == right {
        return true;
    }

    let left_canon = left.canonicalize().ok();
    let right_canon = right.canonicalize().ok();
    match (left_canon, right_canon) {
        (Some(left_canonical), Some(right_canonical)) => left_canonical == right_canonical,
        _ => left.to_string_lossy() == right.to_string_lossy(),
    }
}

fn dir_mtime(path: &PathBuf) -> Option<std::time::SystemTime> {
    std::fs::metadata(path).and_then(|m| m.modified()).ok()
}

fn parse_args() -> (PathBuf, Option<PathBuf>) {
    let mut root = PathBuf::from(".");
    let mut cfg = None;
    let mut args = env::args().skip(1);

    while let Some(arg) = args.next() {
        if arg == "--config" {
            if let Some(v) = args.next() {
                cfg = Some(PathBuf::from(v));
            }
        } else if !arg.starts_with("--") {
            root = PathBuf::from(arg);
        }
    }

    (root, cfg)
}

fn load_bindings_and_theme(
    config_path: Option<PathBuf>,
) -> (
    std::collections::HashMap<crate::config::keymap::Action, crossterm::event::KeyEvent>,
    ThemeProfile,
    StatusDisplayMode,
    Vec<String>,
) {
    let defaults = default_keymap();
    let using_default_path = config_path.is_none();
    let path = config_path.unwrap_or_else(default_config_path);
    if using_default_path {
        let _ = ensure_default_config_exists(&path);
    }
    let user_config = load_user_config(&path).unwrap_or(UserConfig {
        mappings: Default::default(),
        theme: Default::default(),
        status_display_mode: None,
    });
    let user_keymap = UserKeymap {
        mappings: user_config.mappings,
    };
    let status_mode = user_config.status_display_mode.unwrap_or_default();
    let (merged, mut warnings) = merge_keymaps(defaults, &user_keymap);
    let theme = merge_theme_profile(ThemeProfile::default(), &user_config.theme);
    warnings.extend(validate_bindings(&merged));
    (merged, theme, status_mode, warnings)
}

fn centered_rect(percent_x: u16, percent_y: u16, area: Rect) -> Rect {
    let vertical = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage((100 - percent_y) / 2),
            Constraint::Percentage(percent_y),
            Constraint::Percentage((100 - percent_y) / 2),
        ])
        .split(area);

    Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage((100 - percent_x) / 2),
            Constraint::Percentage(percent_x),
            Constraint::Percentage((100 - percent_x) / 2),
        ])
        .split(vertical[1])[1]
}

fn draw_preview_resize_placeholder(frame: &mut ratatui::Frame<'_>, area: Rect, divider_col: u16) {
    if area.width == 0 || area.height == 0 {
        return;
    }
    let x = divider_col.clamp(area.x, area.x.saturating_add(area.width).saturating_sub(1));
    let buf = frame.buffer_mut();
    for y in area.y..area.y.saturating_add(area.height) {
        if let Some(cell) = buf.cell_mut((x, y)) {
            cell.set_symbol("│");
            cell.set_style(Style::default().fg(Color::Gray));
        }
    }
}

fn apply_directory_entries(
    state: &mut SessionState,
    nodes: &mut Vec<TreeNode>,
    entries: Vec<TreeNode>,
    preferred: Option<&PathBuf>,
) {
    state.clear_current_dir_error();
    *nodes = entries;
    state.restore_or_default_selection(nodes, preferred);
    state.update_selected_path(nodes);
}

fn apply_directory_error(
    state: &mut SessionState,
    nodes: &mut Vec<TreeNode>,
    error: &anyhow::Error,
) -> String {
    let message = directory_access_error_message(error);
    state.set_current_dir_error(message.clone());
    nodes.clear();
    state.selected_index = 0;
    state.set_selected_path(state.current_path.clone());
    message
}

fn request_git_status_refresh(
    worker: &GitStatusWorker,
    state: &mut SessionState,
    last_requested_path: &mut Option<PathBuf>,
    force: bool,
) {
    if !force && last_requested_path.as_ref() == Some(&state.current_path) {
        return;
    }

    state.git_status = None;
    if worker.request(state.current_path.clone()) {
        *last_requested_path = Some(state.current_path.clone());
    } else {
        *last_requested_path = None;
    }
}

fn apply_git_status_update(
    update: GitStatusUpdate,
    state: &mut SessionState,
    last_requested_path: &mut Option<PathBuf>,
) -> bool {
    if update.requested_path != state.current_path {
        return false;
    }

    state.git_status = update.status;
    *last_requested_path = Some(update.requested_path);
    true
}

fn current_image_target_width(state: &SessionState) -> u16 {
    state.preview_width_cols.saturating_sub(4).max(1)
}

const IMAGE_PREVIEW_PROGRESS_STEPS: [&str; 3] = ["Load image", "Decode Image", "Draw Image"];

fn image_preview_step_index(step_name: &str) -> Option<usize> {
    IMAGE_PREVIEW_PROGRESS_STEPS
        .iter()
        .position(|name| *name == step_name)
}

fn parse_image_preview_step_timings(content: &str) -> [Option<f64>; 3] {
    let mut step_timings = [None, None, None];
    for line in content.lines() {
        let line = line.trim();
        let Some((step_name, value_part)) = line.rsplit_once(" (") else {
            continue;
        };
        let Some(value_part) = value_part.strip_suffix(")") else {
            continue;
        };
        let Some(value_part) = value_part.strip_suffix("s") else {
            continue;
        };
        let Ok(duration) = value_part.parse::<f64>() else {
            continue;
        };
        if let Some(step_index) = image_preview_step_index(step_name) {
            step_timings[step_index] = Some(duration.max(0.0));
        }
    }
    step_timings
}

fn parse_image_preview_step_count(content: &str) -> usize {
    parse_image_preview_step_timings(content)
        .iter()
        .filter(|value| value.is_some())
        .count()
}

fn format_step_duration(duration_secs: f64) -> String {
    format!("{duration_secs:.2}s")
}

fn refresh_image_preview_loading_text(
    _state: &SessionState,
    preview: &mut PreviewDocument,
    step_count: usize,
    active_step_index: &mut Option<usize>,
    active_step_started_at: &mut Option<Instant>,
) {
    if !preview.image_preview_pending {
        *active_step_index = None;
        *active_step_started_at = None;
        return;
    }

    let step_timings = parse_image_preview_step_timings(&preview.content_excerpt);
    let active_step = match step_count {
        0 => 0,
        1 => 1,
        _ => 2,
    };

    if *active_step_index != Some(active_step) {
        *active_step_index = Some(active_step);
        *active_step_started_at = Some(Instant::now());
    }
    let Some(active_started_at) = active_step_started_at.as_ref() else {
        return;
    };

    let active_elapsed = active_started_at.elapsed().as_secs_f64();
    let mut lines = Vec::with_capacity(active_step + 1);
    for index in 0..=active_step {
        let duration = if index == active_step {
            active_elapsed
        } else {
            step_timings[index].unwrap_or(0.0)
        };
        lines.push(format!(
            "{} ({})",
            IMAGE_PREVIEW_PROGRESS_STEPS[index],
            format_step_duration(duration)
        ));
    }
    preview.content_excerpt = lines.join("\n");
}

/// Check the git status worker for updates and refresh the preview if the
/// current selection is a directory whose listing depends on git annotations.
fn poll_git_status(
    git_worker: &GitStatusWorker,
    state: &mut SessionState,
    nodes: &[TreeNode],
    last_requested_git_path: &mut Option<PathBuf>,
    preview: &mut PreviewDocument,
    highlight: &HighlightContext,
) {
    if let Some(update) = git_worker.latest_update() {
        if apply_git_status_update(update, state, last_requested_git_path)
            && nodes
                .get(state.selected_index)
                .is_some_and(|node| node.node_type == crate::app::state::NodeType::Directory)
        {
            *preview = refresh_preview(state, nodes, highlight, 1024 * 1024);
        }
    }
}

/// Accept a completed image preview from the background worker when it matches
/// the currently selected file.
fn poll_image_worker(
    image_worker: &ImagePreviewWorker,
    state: &SessionState,
    preview: &mut PreviewDocument,
    pending_image_path: &mut Option<PathBuf>,
    pending_image_step_count: &mut usize,
    active_image_step_index: &mut Option<usize>,
    active_image_step_started_at: &mut Option<Instant>,
) {
    if let Some(update) = image_worker.latest_update() {
        let matches_selected = image_paths_match(&state.selected_path, &update.path);
        let matches_pending = pending_image_path
            .as_ref()
            .is_some_and(|pending| image_paths_match(pending, &update.path));
        if matches_selected {
            if update.preview.image_preview_pending {
                let update_step_count = parse_image_preview_step_count(&update.preview.content_excerpt);
                let should_apply = update_step_count >= *pending_image_step_count;
                if should_apply {
                    *pending_image_step_count = update_step_count;
                    *preview = update.preview;
                    *active_image_step_index = None;
                    *active_image_step_started_at = Some(Instant::now());
                }
            } else {
                *preview = update.preview;
                *pending_image_step_count = 0;
                *pending_image_path = None;
                *active_image_step_index = None;
                *active_image_step_started_at = None;
            }
        } else if matches_pending && !update.preview.image_preview_pending {
            *pending_image_path = None;
            *active_image_step_index = None;
            *active_image_step_started_at = None;
        }
    }
}

/// Dispatch the image preview request to the background worker while preserving
/// immediate responsiveness in the preview pane.
fn dispatch_pending_image(
    image_worker: &ImagePreviewWorker,
    state: &SessionState,
    preview: &PreviewDocument,
    pending_image_path: &mut Option<PathBuf>,
    pending_image_step_count: &mut usize,
    active_image_step_index: &mut Option<usize>,
    active_image_step_started_at: &mut Option<Instant>,
) {
    if preview.image_preview_pending {
        let target_path = state.selected_path.clone();
        if pending_image_path.as_ref() != Some(&target_path)
            && image_worker.request(target_path.clone(), current_image_target_width(state))
        {
            *pending_image_step_count = 0;
            *active_image_step_index = None;
            *active_image_step_started_at = None;
            *pending_image_path = Some(target_path);
        }
    }
}

/// Re-read the directory listing when the filesystem modification time has
/// changed, keeping the selection stable when possible.  Returns `true` if a
/// refresh was performed.
fn auto_refresh_directory(
    state: &mut SessionState,
    nodes: &mut Vec<TreeNode>,
    git_worker: &GitStatusWorker,
    last_requested_git_path: &mut Option<PathBuf>,
    preview: &mut PreviewDocument,
    pending_image_path: &mut Option<PathBuf>,
    pending_image_step_count: &mut usize,
    active_image_step_index: &mut Option<usize>,
    active_image_step_started_at: &mut Option<Instant>,
    highlight: &HighlightContext,
    last_auto_refresh: &mut Instant,
    auto_refresh_interval: std::time::Duration,
    last_dir_mtime: &mut Option<std::time::SystemTime>,
) -> bool {
    if last_auto_refresh.elapsed() < auto_refresh_interval {
        return false;
    }

    *last_auto_refresh = Instant::now();
    let current_mtime = dir_mtime(&state.current_path);
    if current_mtime == *last_dir_mtime {
        return false;
    }

    *last_dir_mtime = current_mtime;
    let prev_path = state.selected_path.clone();
    match list_current_directory_with_visibility(&state.current_path, 2000, state.show_hidden) {
        Ok(entries) => apply_directory_entries(state, nodes, entries, Some(&prev_path)),
        Err(error) => {
            let message = apply_directory_error(state, nodes, &error);
            state.status_message = format_status_with_path(&message, &state.current_path);
        }
    }
    request_git_status_refresh(git_worker, state, last_requested_git_path, true);
    *preview = refresh_preview(state, nodes, highlight, 1024 * 1024);
    if preview.image_preview_pending {
        *pending_image_step_count = 0;
        *active_image_step_index = None;
        *active_image_step_started_at = None;
    }
    *pending_image_path = None;
    true
}

/// After `process_once` reports what changed, reload the tree listing and/or
/// preview as needed.
fn apply_input_results(
    should_refresh_tree: bool,
    should_refresh_preview: bool,
    previous_path: &PathBuf,
    state: &mut SessionState,
    nodes: &mut Vec<TreeNode>,
    git_worker: &GitStatusWorker,
    last_requested_git_path: &mut Option<PathBuf>,
    last_dir_mtime: &mut Option<std::time::SystemTime>,
    preview: &mut PreviewDocument,
    pending_image_path: &mut Option<PathBuf>,
    pending_image_step_count: &mut usize,
    active_image_step_index: &mut Option<usize>,
    active_image_step_started_at: &mut Option<Instant>,
    highlight: &HighlightContext,
) {
    if should_refresh_tree {
        let prev_selected = state.selected_path.clone();
        match list_current_directory_with_visibility(&state.current_path, 2000, state.show_hidden) {
            Ok(entries) => {
                apply_directory_entries(state, nodes, entries, Some(&prev_selected));
            }
            Err(error) => {
                let message = apply_directory_error(state, nodes, &error);
                state.status_message = format_status_with_path(&message, &state.current_path);
            }
        }
        request_git_status_refresh(git_worker, state, last_requested_git_path, true);
        *last_dir_mtime = dir_mtime(&state.current_path);
        *preview = refresh_preview(state, nodes, highlight, 1024 * 1024);
        if preview.image_preview_pending {
            *pending_image_step_count = 0;
            *active_image_step_index = None;
            *active_image_step_started_at = None;
        }
        *pending_image_path = None;
    } else {
        if state.current_path != *previous_path {
            request_git_status_refresh(git_worker, state, last_requested_git_path, true);
            *last_dir_mtime = dir_mtime(&state.current_path);
        }
        if should_refresh_preview {
            *preview = refresh_preview(state, nodes, highlight, 1024 * 1024);
            if preview.image_preview_pending {
                *pending_image_step_count = 0;
                *active_image_step_index = None;
                *active_image_step_started_at = None;
            }
            *pending_image_path = None;
        }
    }
}

/// Render a single frame: directory header, tree pane, preview pane, status
/// bar, and the optional help overlay.
fn draw_frame(
    frame: &mut ratatui::Frame<'_>,
    state: &mut SessionState,
    nodes: &[TreeNode],
    preview: &PreviewDocument,
    theme: &ThemeProfile,
    bindings: &std::collections::HashMap<crate::config::keymap::Action, crossterm::event::KeyEvent>,
) {
    frame.render_widget(Clear, frame.area());
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Min(3),
            Constraint::Length(1),
        ])
        .split(frame.area());
    frame.render_widget(Clear, chunks[1]);

    if state.layout_regions.top_directory_header && !state.preview_fullscreen {
        draw_current_directory_header(frame, chunks[0], state, theme);
    }

    if state.preview_fullscreen {
        draw_preview(frame, chunks[1], preview, state, theme);
    } else {
        let main = Layout::default()
            .direction(Direction::Horizontal)
            .constraints({
                let (tree_width, preview_width) = state.panel_widths(chunks[1].width);
                [
                    Constraint::Length(tree_width),
                    Constraint::Length(preview_width),
                ]
            })
            .split(chunks[1]);
        draw_tree(frame, main[0], nodes, state, theme);
        draw_preview(frame, main[1], preview, state, theme);
        if state.divider_drag_active {
            if let Some(divider_col) = state.divider_drag_column {
                draw_preview_resize_placeholder(frame, chunks[1], divider_col);
            }
        }
    }
    draw_status(frame, chunks[2], state, bindings);

    if state.help_overlay_visible {
        let modal = centered_rect(72, 78, frame.area());
        let help_body = compose_shortcut_help_text(bindings);
        let help = Paragraph::new(help_body)
            .block(
                Block::default()
                    .title(" Shortcut Help ")
                    .title_alignment(Alignment::Left)
                    .borders(Borders::ALL),
            )
            .wrap(Wrap { trim: false });
        frame.render_widget(Clear, modal);
        frame.render_widget(help, modal);
    }
}

pub fn run() -> Result<()> {
    let (root, cfg_path) = parse_args();
    let mut state = SessionState::new(root);
    let mut nodes = Vec::new();
    match list_current_directory_with_visibility(&state.current_path, 2000, state.show_hidden) {
        Ok(entries) => apply_directory_entries(&mut state, &mut nodes, entries, None),
        Err(error) => {
            let message = apply_directory_error(&mut state, &mut nodes, &error);
            state.status_message = format_status_with_path(&message, &state.current_path);
        }
    }

    let highlight = HighlightContext::new();
    let git_worker = GitStatusWorker::spawn();
    let image_worker = ImagePreviewWorker::spawn();
    let mut last_requested_git_path = None;
    let mut pending_image_path: Option<PathBuf> = None;
    let mut pending_image_active_step_index: Option<usize> = None;
    let mut pending_image_active_step_started_at: Option<Instant> = None;
    let (bindings, theme, status_mode, warnings) = load_bindings_and_theme(cfg_path);
    state.status_display_mode = status_mode;
    request_git_status_refresh(&git_worker, &mut state, &mut last_requested_git_path, true);
    let mut preview = refresh_preview(&mut state, &nodes, &highlight, 1024 * 1024);
    state.status_message = if let Some(message) = state.current_dir_error.clone() {
        format_status_with_path(&message, &state.current_path)
    } else if warnings.is_empty() {
        format!("Ready. Path: {}", state.current_path.display())
    } else {
        format!(
            "{} Path: {}",
            crate::tui::config_warnings::render_warning_text(&warnings),
            state.current_path.display()
        )
    };

    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let mut last_auto_refresh = Instant::now();
    let auto_refresh_interval = std::time::Duration::from_secs(2);
    let mut last_dir_mtime = dir_mtime(&state.current_path);
    let mut pending_image_step_count = 0usize;

    loop {
        let frame_size = terminal.size()?;
        state.normalize_preview_width(frame_size.width);

        poll_git_status(
            &git_worker,
            &mut state,
            &nodes,
            &mut last_requested_git_path,
            &mut preview,
            &highlight,
        );
        poll_image_worker(
            &image_worker,
            &state,
            &mut preview,
            &mut pending_image_path,
            &mut pending_image_step_count,
            &mut pending_image_active_step_index,
            &mut pending_image_active_step_started_at,
        );
        dispatch_pending_image(
            &image_worker,
            &state,
            &preview,
            &mut pending_image_path,
            &mut pending_image_step_count,
            &mut pending_image_active_step_index,
            &mut pending_image_active_step_started_at,
        );
        auto_refresh_directory(
            &mut state,
            &mut nodes,
            &git_worker,
            &mut last_requested_git_path,
            &mut preview,
            &mut pending_image_path,
            &mut pending_image_step_count,
            &mut pending_image_active_step_index,
            &mut pending_image_active_step_started_at,
            &highlight,
            &mut last_auto_refresh,
            auto_refresh_interval,
            &mut last_dir_mtime,
        );

        let previous_path = state.current_path.clone();
        let preview_viewport_rows = frame_size.height.saturating_sub(4) as usize;
        let total_preview_lines = preview_total_lines(&preview);
        state.clamp_preview_scroll(total_preview_lines, preview_viewport_rows);
        let (should_quit, should_refresh_preview, should_refresh_tree) = process_once(
            &mut state,
            &mut nodes,
            &bindings,
            total_preview_lines,
            preview_viewport_rows,
            &preview,
        )?;
        if should_quit {
            break;
        }

        apply_input_results(
            should_refresh_tree,
            should_refresh_preview,
            &previous_path,
            &mut state,
            &mut nodes,
            &git_worker,
            &mut last_requested_git_path,
            &mut last_dir_mtime,
            &mut preview,
            &mut pending_image_path,
            &mut pending_image_step_count,
            &mut pending_image_active_step_index,
            &mut pending_image_active_step_started_at,
            &highlight,
        );

        refresh_image_preview_loading_text(
            &state,
            &mut preview,
            pending_image_step_count,
            &mut pending_image_active_step_index,
            &mut pending_image_active_step_started_at,
        );

        let frame_size = terminal.size()?;
        let preview_viewport_rows = frame_size.height.saturating_sub(4) as usize;
        let total_preview_lines = preview_total_lines(&preview);
        state.clamp_preview_scroll(total_preview_lines, preview_viewport_rows);

        terminal.draw(|f| {
            draw_frame(f, &mut state, &nodes, &preview, &theme, &bindings);
        })?;
    }

    disable_raw_mode()?;
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture
    )?;
    terminal.show_cursor()?;
    Ok(())
}