summaryrefslogtreecommitdiffstats
path: root/src/fs/entry.rs
blob: 84e818cf6c217629c442c5a1d7293a3523c84666 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use std::{fs, path};

use tui::style::{Color, Modifier, Style};

use crate::fs::JoshutoMetadata;

use crate::util::unix;
use crate::THEME_T;

#[derive(Clone, Debug)]
pub struct JoshutoDirEntry {
    name: String,
    path: path::PathBuf,
    pub metadata: JoshutoMetadata,
    selected: bool,
    marked: bool,
}

impl JoshutoDirEntry {
    pub fn from(direntry: &fs::DirEntry) -> std::io::Result<Self> {
        let name = match direntry.file_name().into_string() {
            Ok(s) => s,
            Err(_) => {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    "Failed converting OsString to String",
                ));
            }
        };

        let path = direntry.path();
        let metadata = JoshutoMetadata::from(&path)?;

        Ok(JoshutoDirEntry {
            name,
            path,
            metadata,
            selected: false,
            marked: false,
        })
    }

    pub fn file_name(&self) -> &str {
        self.name.as_str()
    }

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

    /*
        pub fn is_marked(&self) -> bool {
            self.marked
        }

        pub fn set_marked(&mut self, marked: bool) {
            self.marked = marked;
        }
    */

    pub fn is_selected(&self) -> bool {
        self.selected
    }

    pub fn set_selected(&mut self, selected: bool) {
        self.selected = selected;
    }

    pub fn get_fg_color(&self) -> Color {
        let metadata = &self.metadata;
        let filetype = metadata.file_type;

        if self.is_selected() {
            THEME_T.selection.fg
        } else if filetype.is_dir() {
            THEME_T.directory.fg
        } else if filetype.is_symlink() {
            THEME_T.link.fg
        } else {
            match self.file_path().extension() {
                None => Color::White,
                Some(os_str) => match os_str.to_str() {
                    None => Color::White,
                    Some(s) => match THEME_T.ext.get(s) {
                        None => Color::White,
                        Some(t) => t.fg,
                    },
                },
            }
        }
    }

    pub fn get_bg_color(&self) -> Color {
        let metadata = &self.metadata;
        let filetype = metadata.file_type;

        if self.is_selected() {
            THEME_T.selection.bg
        } else if filetype.is_dir() {
            THEME_T.directory.bg
        } else if filetype.is_symlink() {
            THEME_T.link.bg
        } else {
            match self.file_path().extension() {
                None => Color::Reset,
                Some(os_str) => match os_str.to_str() {
                    None => Color::Reset,
                    Some(s) => match THEME_T.ext.get(s) {
                        None => Color::Reset,
                        Some(t) => t.bg,
                    },
                },
            }
        }
    }

    pub fn get_modifier(&self) -> Modifier {
        let metadata = &self.metadata;
        let filetype = metadata.file_type;

        use std::os::unix::fs::MetadataExt;

        let mut modifier = Modifier::empty();

        if filetype.is_dir() {
            if THEME_T.directory.bold {
                modifier.insert(Modifier::BOLD);
            }
            if THEME_T.directory.underline {
                modifier.insert(Modifier::UNDERLINED);
            }
        } else if filetype.is_symlink() {
            if THEME_T.link.bold {
                modifier.insert(Modifier::BOLD);
            }
            if THEME_T.link.underline {
                modifier.insert(Modifier::UNDERLINED);
            }
        } else {
            match self.file_path().extension() {
                None => {}
                Some(os_str) => match os_str.to_str() {
                    None => {}
                    Some(s) => match THEME_T.ext.get(s) {
                        None => {}
                        Some(t) => {
                            if t.bold {
                                modifier.insert(Modifier::BOLD);
                            }
                            if t.underline {
                                modifier.insert(Modifier::UNDERLINED);
                            }
                        }
                    },
                },
            };
        }
        modifier
    }

    pub fn get_style(&self) -> Style {
        use std::os::unix::fs::MetadataExt;

        let metadata = &self.metadata;
        let filetype = metadata.file_type;

        let mut style = Style::default();

        if self.is_selected() {
            let mut modifier = Modifier::empty();
            if THEME_T.selection.bold {
                modifier.insert(Modifier::BOLD);
            }
            if THEME_T.selection.underline {
                modifier.insert(Modifier::UNDERLINED);
            }

            style = style.fg(THEME_T.selection.fg).bg(THEME_T.selection.bg);
            style = style.modifier(modifier);
        } else if filetype.is_dir() {
            let mut modifier = Modifier::empty();
            if THEME_T.directory.bold {
                modifier.insert(Modifier::BOLD);
            }
            if THEME_T.directory.underline {
                modifier.insert(Modifier::UNDERLINED);
            }

            style = style.fg(THEME_T.directory.fg).bg(THEME_T.directory.bg);
            style = style.modifier(modifier);
        } else if filetype.is_symlink() {
            let mut modifier = Modifier::empty();
            if THEME_T.link.bold {
                modifier.insert(Modifier::BOLD);
            }
            if THEME_T.link.underline {
                modifier.insert(Modifier::UNDERLINED);
            }

            style = style.fg(THEME_T.link.fg).bg(THEME_T.link.bg);
            style = style.modifier(modifier);
        } else if unix::is_executable(metadata.mode) {
            let mut modifier = Modifier::empty();
            if THEME_T.link.bold {
                modifier.insert(Modifier::BOLD);
            }
            if THEME_T.link.underline {
                modifier.insert(Modifier::UNDERLINED);
            }

            style = style.fg(THEME_T.executable.fg).bg(THEME_T.executable.bg);
            style = style.modifier(modifier);
        } else {
            match self.file_path().extension() {
                None => {}
                Some(os_str) => match os_str.to_str() {
                    None => {}
                    Some(s) => match THEME_T.ext.get(s) {
                        None => {}
                        Some(t) => {
                            let mut modifier = Modifier::empty();
                            if t.bold {
                                modifier.insert(Modifier::BOLD);
                            }
                            if t.underline {
                                modifier.insert(Modifier::UNDERLINED);
                            }
                            style = style.fg(t.fg).bg(t.bg);
                            style = style.modifier(modifier);
                        }
                    },
                },
            }
        }
        style
    }
}

impl std::fmt::Display for JoshutoDirEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(f, "{}", self.file_name())
    }
}

impl std::convert::AsRef<str> for JoshutoDirEntry {
    fn as_ref(&self) -> &str {
        self.file_name()
    }
}

impl std::cmp::PartialEq for JoshutoDirEntry {
    fn eq(&self, other: &Self) -> bool {
        self.file_path() == other.file_path()
    }
}
impl std::cmp::Eq