Skip to main content

binocular/preview/rich_text/
commands.rs

1use crate::app::{App, AppAction};
2use crate::preview::PreviewContent;
3
4pub fn save_file(app: &mut App) {
5    if let Some(path_str) = app.preview_file_path() {
6        if let Some(PreviewContent::RichText(text_file)) = &app.preview_session.preview.content {
7            let path = std::path::Path::new(path_str);
8            if let Ok(meta) = std::fs::symlink_metadata(path) {
9                if meta.file_type().is_symlink() {
10                    app.preview_session.preview.state.status_message = Some((
11                        "Error: cannot save through a symlink".to_string(),
12                        std::time::Instant::now(),
13                    ));
14                    return;
15                }
16            }
17            if let Err(e) = std::fs::write(path_str, text_file.content()) {
18                eprintln!("Error saving file: {}", e);
19                app.preview_session.preview.state.status_message =
20                    Some((format!("Error: {}", e), std::time::Instant::now()));
21            } else {
22                app.preview_session.preview.state.status_message =
23                    Some(("File saved".to_string(), std::time::Instant::now()));
24            }
25        }
26    }
27}
28
29pub fn execute_command(app: &mut App, cmd: &str) {
30    if cmd == "w" {
31        save_file(app);
32    } else if cmd == "q" {
33        app.ui.layout.preview_fullscreen = false;
34        app.apply_action(AppAction::FocusSearch);
35    } else if cmd == "wq" {
36        save_file(app);
37        app.apply_action(AppAction::Quit);
38    }
39}