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
use {
super::*,
crate::{
app::AppContext,
app::{Selection, SelectionType},
file_sum::FileSum,
git::LineGitStatus,
},
std::{
cmp::{self, Ord, Ordering, PartialOrd},
fs,
path::{Path, PathBuf},
},
};
#[cfg(unix)]
use {std::os::unix::fs::MetadataExt, umask::Mode};
#[cfg(windows)]
use is_executable::IsExecutable;
#[derive(Debug, Clone)]
pub struct TreeLine {
pub left_branchs: Box<[bool]>,
pub depth: u16,
pub path: PathBuf,
pub subpath: String,
pub name: String,
pub line_type: TreeLineType,
pub has_error: bool,
pub nb_kept_children: usize,
pub unlisted: usize,
pub score: i32,
pub direct_match: bool,
pub sum: Option<FileSum>,
pub metadata: fs::Metadata,
pub git_status: Option<LineGitStatus>,
}
impl TreeLine {
pub fn make_displayable_name(
name: &str,
path: &std::path::PathBuf,
tree_line_type: &TreeLineType,
con: &AppContext,
) -> String {
let newline_replaced_name = name.replace('\n', "");
match &con.icons {
None => newline_replaced_name,
Some(icon_plugin) => {
let extension = Self::extension_from_name(name);
let double_extension = if extension.is_some() {
Self::double_extension_from_name(name)
} else {
None
};
let icon = icon_plugin.get_icon(
tree_line_type,
path,
&name,
double_extension,
extension,
);
format!("{} {}", icon, newline_replaced_name)
}
}
}
pub fn double_extension_from_name(name: &str) -> Option<&str> {
regex!(r"\.([^.]+\.[^.]+)")
.captures(&name)
.and_then(|c| c.get(1))
.map(|e| e.as_str())
}
pub fn extension_from_name(name: &str) -> Option<&str> {
regex!(r"\.([^.]+)$")
.captures(&name)
.and_then(|c| c.get(1))
.map(|e| e.as_str())
}
pub fn is_selectable(&self) -> bool {
!matches!(&self.line_type, TreeLineType::Pruning)
}
pub fn is_dir(&self) -> bool {
match &self.line_type {
TreeLineType::Dir => true,
TreeLineType::SymLink { final_is_dir, .. } if *final_is_dir => true,
_ => false,
}
}
pub fn is_file(&self) -> bool {
matches!(&self.line_type, TreeLineType::File)
}
pub fn is_of(&self, selection_type: SelectionType) -> bool {
match selection_type {
SelectionType::Any => true,
SelectionType::File => self.is_file(),
SelectionType::Directory => self.is_dir(),
}
}
pub fn extension(&self) -> Option<&str> {
Self::extension_from_name(&self.name)
}
pub fn selection_type(&self) -> SelectionType {
use TreeLineType::*;
match &self.line_type {
File => SelectionType::File,
Dir | BrokenSymLink(_) => SelectionType::Directory,
SymLink { final_is_dir, .. } => {
if *final_is_dir {
SelectionType::Directory
} else {
SelectionType::File
}
}
Pruning => SelectionType::Any,
}
}
pub fn as_selection(&self) -> Selection<'_> {
Selection {
path: &self.path,
stype: self.selection_type(),
is_exe: self.is_exe(),
line: 0,
}
}
#[cfg(unix)]
pub fn mode(&self) -> Mode {
Mode::from(self.metadata.mode())
}
#[cfg(unix)]
pub fn mount(&self) -> Option<lfs_core::Mount> {
use crate::filesystems::*;
let mut mount_list = MOUNTS.lock().unwrap();
if mount_list.load().is_ok() {
mount_list
.get_by_device_id(self.metadata.dev().into())
.cloned()
} else {
None
}
}
pub fn is_exe(&self) -> bool {
#[cfg(unix)]
return self.mode().is_exe();
#[cfg(windows)]
return self.path.is_executable();
}
pub fn target(&self) -> &Path {
match &self.line_type {
TreeLineType::SymLink { final_target, .. } => final_target,
_ => &self.path,
}
}
}
impl PartialEq for TreeLine {
fn eq(&self, other: &TreeLine) -> bool {
self.path == other.path
}
}
impl Eq for TreeLine {}
impl Ord for TreeLine {
fn cmp(&self, other: &TreeLine) -> Ordering {
let mut sci = self.path.components();
let mut oci = other.path.components();
loop {
match sci.next() {
Some(sc) => {
match oci.next() {
Some(oc) => {
let scs = sc.as_os_str().to_string_lossy();
let ocs = oc.as_os_str().to_string_lossy();
let lower_ordering = scs.to_lowercase().cmp(&ocs.to_lowercase());
if lower_ordering != Ordering::Equal {
return lower_ordering;
}
let ordering = scs.cmp(&ocs);
if ordering != Ordering::Equal {
return ordering;
}
}
None => {
return Ordering::Greater;
}
};
}
None => {
if oci.next().is_some() {
return Ordering::Less;
} else {
return Ordering::Equal;
}
}
};
}
}
}
impl PartialOrd for TreeLine {
fn partial_cmp(&self, other: &TreeLine) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}