summaryrefslogtreecommitdiffstats
path: root/src/io/dirlist.rs
blob: 0ecd33347df403ad6cfa07105de27a8921e31438 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::{fs, path};

use crate::io::{JoshutoDirEntry, JoshutoMetadata};
use crate::sort;
use crate::window::JoshutoPageState;

#[derive(Debug)]
pub struct JoshutoDirList {
    pub index: Option<usize>,
    path: path::PathBuf,
    outdated: bool,
    pub metadata: JoshutoMetadata,
    pub contents: Vec<JoshutoDirEntry>,
    pub pagestate: JoshutoPageState,
}

impl JoshutoDirList {
    pub fn new(
        path: path::PathBuf,
        sort_option: &sort::SortOption,
    ) -> Result<Self, std::io::Error> {
        let mut contents = read_dir_list(path.as_path(), sort_option)?;
        contents.sort_by(&sort_option.compare_func());

        let index = if contents.is_empty() { None } else { Some(0) };

        let metadata = JoshutoMetadata::from(&path)?;
        let pagestate = JoshutoPageState::default();

        Ok(JoshutoDirList {
            index,
            path,
            outdated: false,
            metadata,
            contents,
            pagestate,
        })
    }

    pub fn depreciate(&mut self) {
        self.outdated = true;
    }

    pub fn need_update(&self) -> bool {
        self.outdated
    }

    pub fn file_path(&self) -> &path::PathBuf {
        &self.path
    }

    pub fn update_contents(
        &mut self,
        sort_option: &sort::SortOption,
    ) -> Result<(), std::io::Error> {
        let sort_func = sort_option.compare_func();
        let mut contents = read_dir_list(&self.path, sort_option)?;
        contents.sort_by(&sort_func);

        let contents_len = contents.len();
        if contents_len == 0 {
            self.index = None;
        } else {
            self.index = match self.index {
                Some(index) => {
                    if index >= contents_len {
                        Some(contents_len - 1)
                    } else {
                        self.index
                    }
                }
                None => Some(0),
            };
        }

        let metadata = JoshutoMetadata::from(&self.path)?;
        self.metadata = metadata;
        self.contents = contents;
        self.outdated = false;

        Ok(())
    }

    pub fn selected_entries(&self) -> impl Iterator<Item = &JoshutoDirEntry> {
        self.contents.iter().filter(|entry| entry.is_selected())
    }

    pub fn get_selected_paths(&self) -> Option<Vec<path::PathBuf>> {
        let vec: Vec<path::PathBuf> = self
            .selected_entries()
            .map(|e| e.file_path().clone())
            .collect();
        if vec.is_empty() {
            Some(vec![self.get_curr_ref()?.file_path().clone()])
        } else {
            Some(vec)
        }
    }

    pub fn get_curr_ref(&self) -> Option<&JoshutoDirEntry> {
        self.get_curr_ref_(self.index?)
    }

    pub fn get_curr_mut(&mut self) -> Option<&mut JoshutoDirEntry> {
        self.get_curr_mut_(self.index?)
    }

    fn get_curr_mut_(&mut self, index: usize) -> Option<&mut JoshutoDirEntry> {
        if index < self.contents.len() {
            Some(&mut self.contents[index])
        } else {
            None
        }
    }

    fn get_curr_ref_(&self, index: usize) -> Option<&JoshutoDirEntry> {
        if index < self.contents.len() {
            Some(&self.contents[index])
        } else {
            None
        }
    }
}

fn read_dir_list(
    path: &path::Path,
    sort_option: &sort::SortOption,
) -> Result<Vec<JoshutoDirEntry>, std::io::Error> {
    let filter_func = sort_option.filter_func();
    let results: fs::ReadDir = fs::read_dir(path)?;
    let result_vec: Vec<JoshutoDirEntry> = results
        .filter(filter_func)
        .filter_map(sort::map_entry_default)
        .collect();
    Ok(result_vec)
}