summaryrefslogtreecommitdiffstats
path: root/src/joshuto/structs.rs
blob: 0b51285da3fb2ee9b99e39516935953cb027621d (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std;
use std::fs;
use std::ffi;
use std::path;
use std::time;

use joshuto::sort;
use joshuto::ui;
use joshuto::window;

#[derive(Debug)]
pub struct JoshutoDirEntry {
    pub file_name: ffi::OsString,
    pub file_name_as_string: String,
    pub path: path::PathBuf,
    pub file_type: Result<fs::FileType, std::io::Error>,
    pub selected: bool,
    pub marked: bool,
}

#[derive(Debug)]
pub struct JoshutoDirList {
    pub index: i32,
    pub path: path::PathBuf,
    pub update_needed: bool,
    pub modified: time::SystemTime,
    pub contents: Vec<JoshutoDirEntry>,
    pub selected: usize
}

impl JoshutoDirList {

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

        contents.sort_by(&sort_type.compare_func());

        let modified = std::fs::metadata(&path)?.modified()?;

        let index = if contents.len() > 0 {
                0
            } else {
                -1
            };

        Ok(JoshutoDirList {
            index,
            path,
            update_needed: false,
            modified,
            contents,
            selected: 0,
        })
    }

    pub fn need_update(&self) -> bool
    {
        if self.update_needed {
            return true;
        }
        if let Ok(metadata) = std::fs::metadata(&self.path) {
            if let Ok(modified) = metadata.modified() {
                return self.modified < modified;
            }
        }
        return true;
    }

    pub fn update(&mut self, sort_type: &sort::SortType)
    {
        let sort_func = sort_type.compare_func();

        self.update_needed = false;

        if let Ok(mut dir_contents) = Self::read_dir_list(&self.path, sort_type) {
            dir_contents.sort_by(&sort_func);

            if dir_contents.len() == 0 {
                self.index = -1;
            } else if self.index >= dir_contents.len() as i32 {
                self.index = dir_contents.len() as i32 - 1;
            } else if self.index >= 0 && (self.index as usize) < self.contents.len() {
                let index = self.index;
                let curr_file_name = &self.contents[index as usize].file_name;

                for (i, entry) in dir_contents.iter().enumerate() {
                    if *curr_file_name == entry.file_name {
                        self.index = i as i32;
                        break;
                    }
                }
            } else {
                self.index = 0;
            }
            self.contents = dir_contents;
        }

        if let Ok(metadata) = std::fs::metadata(&self.path) {
            match metadata.modified() {
                Ok(s) => { self.modified = s; },
                Err(e) => { eprintln!("{}", e); },
            };
        }
    }


    pub fn display_contents(&self, win: &window::JoshutoPanel)
    {
        ui::display_contents(win, self);
    }

    fn read_dir_list(path : &path::Path, sort_type: &sort::SortType)
            -> Result<Vec<JoshutoDirEntry>, std::io::Error>
    {
        let filter_func = sort_type.filter_func();

        match fs::read_dir(path) {
            Ok(results) => {
                let mut result_vec : Vec<JoshutoDirEntry> = results
                        .filter_map(filter_func)
                        .collect();
                Ok(result_vec)
            },
            Err(e) => {
                Err(e)
            },
        }
    }

    pub fn get_curr_entry(&self) -> Option<&JoshutoDirEntry>
    {
        self.get_dir_entry(self.index)
    }

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

    pub fn curr_toggle_select(&mut self)
    {
        let index = self.index;
        self.toggle_select(index);
    }

    fn toggle_select(&mut self, index: i32) {
        if index >= 0 && (index as usize) < self.contents.len() {
            let tmp_bool = !self.contents[index as usize].selected;
            self.contents[index as usize].selected = tmp_bool;
            if tmp_bool {
                self.selected = self.selected + 1;
            } else {
                self.selected = self.selected - 1;
            }
        }
    }
}