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
|
use std::path;
use crate::context::AppContext;
use crate::fs::JoshutoMetadata;
use crate::preview::{preview_dir, preview_file};
#[derive(Debug, Clone)]
pub enum PreviewState {
Loading,
Error { message: String },
}
impl PreviewState {
pub fn is_loading(&self) -> bool {
matches!(*self, Self::Loading)
}
}
pub fn load_preview_path(context: &mut AppContext, p: path::PathBuf, metadata: JoshutoMetadata) {
let preview_options = context.config_ref().preview_options_ref();
if metadata.is_dir() {
let tab = context.tab_context_ref().curr_tab_ref();
// only load if there doesn't already exist a loading thread and
// there isn't an entry in history
let need_to_load = tab
.history_metadata_ref()
.get(p.as_path())
.map(|m| m.is_loading())
.unwrap_or(true)
&& tab
.history_ref()
.get(p.as_path())
.map(|e| e.need_update())
.unwrap_or(true);
if need_to_load {
preview_dir::Background::load_preview(context, p);
}
} else if metadata.len() <= preview_options.max_preview_size {
let need_to_load = context
.preview_context_ref()
.get_preview_ref(p.as_path())
.map(|p| {
p.as_ref()
.map(|p| p.modified < metadata.modified())
.unwrap_or(true)
})
.unwrap_or(true);
if need_to_load {
preview_file::Background::preview_path_with_script(context, p);
}
} else {
}
}
pub fn load_preview(context: &mut AppContext) {
let mut load_list = Vec::with_capacity(2);
let curr_tab = context.tab_context_ref().curr_tab_ref();
match curr_tab.curr_list_ref() {
Some(curr_list) => {
if let Some(index) = curr_list.get_index() {
let entry = &curr_list.contents[index];
load_list.push((entry.file_path().to_path_buf(), entry.metadata.clone()));
}
}
None => {
if let Ok(metadata) = JoshutoMetadata::from(curr_tab.cwd()) {
load_list.push((curr_tab.cwd().to_path_buf(), metadata));
}
}
}
for (path, metadata) in load_list {
load_preview_path(context, path, metadata);
}
}
|