summaryrefslogtreecommitdiffstats
path: root/src/commands/reload.rs
blob: bee7754c7fcb0bc00365e68a4d3533e211b013d5 (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
use crate::context::AppContext;
use crate::error::AppResult;
use crate::history::create_dirlist_with_history;

use uuid::Uuid;

// reload only if we have a queued reload
pub fn soft_reload(context: &mut AppContext, id: &Uuid) -> std::io::Result<()> {
    let mut paths = Vec::with_capacity(3);
    if let Some(curr_tab) = context.tab_context_ref().tab_ref(id) {
        if let Some(curr_list) = curr_tab.curr_list_ref() {
            if curr_list.need_update() {
                paths.push(curr_list.file_path().to_path_buf());
            }
        }
        if let Some(curr_list) = curr_tab.parent_list_ref() {
            if curr_list.need_update() {
                paths.push(curr_list.file_path().to_path_buf());
            }
        }
        if let Some(curr_list) = curr_tab.child_list_ref() {
            if curr_list.need_update() {
                paths.push(curr_list.file_path().to_path_buf());
            }
        }
    }

    if !paths.is_empty() {
        let config = context.config_ref().clone();
        let options = context.config_ref().display_options_ref().clone();
        let tab_options = context
            .tab_context_ref()
            .curr_tab_ref()
            .option_ref()
            .clone();
        if let Some(history) = context
            .tab_context_mut()
            .tab_mut(id)
            .map(|t| t.history_mut())
        {
            for path in paths {
                let new_dirlist = create_dirlist_with_history(
                    history,
                    path.as_path(),
                    &config,
                    &options,
                    &tab_options,
                )?;
                history.insert(path, new_dirlist);
            }
        }
    }
    Ok(())
}

pub fn soft_reload_curr_tab(context: &mut AppContext) -> std::io::Result<()> {
    let curr_tab_id = context.tab_context_ref().curr_tab_id();
    soft_reload(context, &curr_tab_id)
}

pub fn reload(context: &mut AppContext, id: &Uuid) -> std::io::Result<()> {
    let mut paths = Vec::with_capacity(3);
    if let Some(curr_tab) = context.tab_context_ref().tab_ref(id) {
        if let Some(curr_list) = curr_tab.curr_list_ref() {
            paths.push(curr_list.file_path().to_path_buf());
        }
        if let Some(curr_list) = curr_tab.parent_list_ref() {
            paths.push(curr_list.file_path().to_path_buf());
        }
        if let Some(curr_list) = curr_tab.child_list_ref() {
            paths.push(curr_list.file_path().to_path_buf());
        }
    }

    if !paths.is_empty() {
        let config = context.config_ref().clone();
        let options = context.config_ref().display_options_ref().clone();
        let tab_options = context
            .tab_context_ref()
            .curr_tab_ref()
            .option_ref()
            .clone();
        if let Some(history) = context
            .tab_context_mut()
            .tab_mut(id)
            .map(|t| t.history_mut())
        {
            for path in paths {
                let new_dirlist = create_dirlist_with_history(
                    history,
                    path.as_path(),
                    &config,
                    &options,
                    &tab_options,
                )?;
                history.insert(path, new_dirlist);
            }
        }
    }
    context
        .message_queue_mut()
        .push_success("Directory listing reloaded!".to_string());
    Ok(())
}

pub fn reload_dirlist(context: &mut AppContext) -> AppResult {
    reload(context, &context.tab_context_ref().curr_tab_id())?;
    Ok(())
}