summaryrefslogtreecommitdiffstats
path: root/src/commands/reload_dir.rs
blob: e8cb24e4d5bd2bb4dead26dd61cba3e22c93fee1 (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
use crate::commands::{JoshutoCommand, JoshutoRunnable};
use crate::context::JoshutoContext;
use crate::error::JoshutoResult;
use crate::ui::TuiBackend;
use crate::util::load_child::LoadChild;

#[derive(Clone, Debug)]
pub struct ReloadDirList;

impl ReloadDirList {
    pub fn new() -> Self {
        ReloadDirList
    }
    pub const fn command() -> &'static str {
        "reload_dir_list"
    }

    pub fn soft_reload(index: usize, context: &mut JoshutoContext) -> std::io::Result<()> {
        let sort_option = context.config_t.sort_option.clone();
        if let Some(curr_tab) = context.tab_context_mut().tab_mut(index) {
            if let Some(curr_list) = curr_tab.curr_list_mut() {
                if curr_list.need_update() {
                    curr_list.reload_contents(&sort_option)?;
                }
            }
            if let Some(curr_list) = curr_tab.parent_list_mut() {
                if curr_list.need_update() {
                    curr_list.reload_contents(&sort_option)?;
                }
            }
            if let Some(curr_list) = curr_tab.child_list_mut() {
                if curr_list.need_update() {
                    curr_list.reload_contents(&sort_option)?;
                }
            }
        }
        Ok(())
    }

    pub fn reload(index: usize, context: &mut JoshutoContext) -> std::io::Result<()> {
        let sort_option = context.config_t.sort_option.clone();
        if let Some(curr_tab) = context.tab_context_mut().tab_mut(index) {
            if let Some(curr_list) = curr_tab.curr_list_mut() {
                curr_list.reload_contents(&sort_option)?;
            }
            if let Some(curr_list) = curr_tab.parent_list_mut() {
                curr_list.reload_contents(&sort_option)?;
            }
            if let Some(curr_list) = curr_tab.child_list_mut() {
                curr_list.reload_contents(&sort_option)?;
            }
        }
        Ok(())
    }
}

impl JoshutoCommand for ReloadDirList {}

impl std::fmt::Display for ReloadDirList {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(Self::command())
    }
}

impl JoshutoRunnable for ReloadDirList {
    fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> {
        Self::reload(context.tab_context_ref().get_index(), context)?;
        LoadChild::load_child(context)?;
        Ok(())
    }
}