summaryrefslogtreecommitdiffstats
path: root/src/commands/rename_file.rs
blob: 981cfcfcbd6d69874415d77e04341c1b2e44e751 (plain)
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
use std::path;

use crate::config::AppKeyMapping;
use crate::context::AppContext;
use crate::error::JoshutoResult;
use crate::history::create_dirlist_with_history;
use crate::ui::TuiBackend;

use super::command_line;

pub fn _rename_file(
    context: &mut AppContext,
    src: &path::Path,
    dest: &path::Path,
) -> std::io::Result<()> {
    let new_path = dest;
    if new_path.exists() {
        let err = std::io::Error::new(std::io::ErrorKind::AlreadyExists, "Filename already exists");
        return Err(err);
    }
    std::fs::rename(&src, &dest)?;

    let path = context
        .tab_context_ref()
        .curr_tab_ref()
        .curr_list_ref()
        .map(|lst| lst.file_path().to_path_buf());

    if let Some(path) = path {
        let options = context.config_ref().display_options_ref().clone();

        let history = context.tab_context_mut().curr_tab_mut().history_mut();
        let new_dirlist = create_dirlist_with_history(history, path.as_path(), &options)?;
        history.insert(path, new_dirlist);
    }
    Ok(())
}

pub fn rename_file(context: &mut AppContext, dest: &path::Path) -> JoshutoResult {
    let path: Option<path::PathBuf> = context
        .tab_context_ref()
        .curr_tab_ref()
        .curr_list_ref()
        .and_then(|s| s.curr_entry_ref())
        .map(|s| s.file_path().to_path_buf());

    if let Some(path) = path {
        _rename_file(context, path.as_path(), dest)?;
    }
    Ok(())
}

pub fn _rename_file_append(
    context: &mut AppContext,
    backend: &mut TuiBackend,
    keymap_t: &AppKeyMapping,
    file_name: &str,
) -> JoshutoResult {
    let (prefix, suffix): (String, String) = match file_name.rfind('.') {
        Some(ext) => (
            format!("rename {}", &file_name[0..ext]),
            file_name[ext..].to_string(),
        ),
        None => (format!("rename {}", file_name), "".to_string()),
    };
    command_line::read_and_execute(context, backend, keymap_t, &prefix, &suffix)
}

pub fn rename_file_append(
    context: &mut AppContext,
    backend: &mut TuiBackend,
    keymap_t: &AppKeyMapping,
) -> JoshutoResult {
    let mut file_name: Option<String> = None;

    if let Some(curr_list) = context.tab_context_ref().curr_tab_ref().curr_list_ref() {
        file_name = curr_list
            .curr_entry_ref()
            .map(|s| s.file_name().to_string());
    }

    if let Some(file_name) = file_name {
        _rename_file_append(context, backend, keymap_t, file_name.as_str())?;
    }
    Ok(())
}

pub fn _rename_file_prepend(
    context: &mut AppContext,
    backend: &mut TuiBackend,
    keymap_t: &AppKeyMapping,
    file_name: String,
) -> JoshutoResult {
    let prefix = String::from("rename ");
    let suffix = file_name;
    command_line::read_and_execute(context, backend, keymap_t, &prefix, &suffix)
}

pub fn rename_file_prepend(
    context: &mut AppContext,
    backend: &mut TuiBackend,
    keymap_t: &AppKeyMapping,
) -> JoshutoResult {
    let mut file_name: Option<String> = None;

    if let Some(curr_list) = context.tab_context_ref().curr_tab_ref().curr_list_ref() {
        file_name = curr_list
            .curr_entry_ref()
            .map(|s| s.file_name().to_string());
    }

    if let Some(file_name) = file_name {
        _rename_file_prepend(context, backend, keymap_t, file_name)?;
    }
    Ok(())
}