-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdiagram_formatting.rs
191 lines (175 loc) · 5.25 KB
/
diagram_formatting.rs
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
use super::file_tree::{File, FileTree, FileType};
use std::collections::HashMap;
use std::fs;
#[derive(Debug, Clone, PartialEq)]
enum PrefixSegment {
ShapeL, // "└── "
ShapeT, // "├── "
ShapeI, // "│ "
Empty, // " "
}
#[derive(Debug, Clone, PartialEq)]
pub struct FormattedEntry {
pub name: String,
pub path: String,
pub prefix: String,
pub link: Option<String>,
}
fn make_prefix(tree: &FileTree, file: &File, format_history: &HashMap<usize, usize>) -> String {
let mut segments = Vec::new();
let mut current = file;
if let Some(ancestor) = tree.get_parent(file) {
let count = format_history.get(&ancestor.id).unwrap_or(&0);
if *count >= ancestor.children_count() - 1 {
segments.push(PrefixSegment::ShapeL);
} else {
segments.push(PrefixSegment::ShapeT);
}
current = ancestor;
}
while let Some(ancestor) = tree.get_parent(current) {
let count = format_history.get(&ancestor.id).unwrap_or(&0);
if *count == ancestor.children_count() {
segments.push(PrefixSegment::Empty);
} else {
segments.push(PrefixSegment::ShapeI);
}
current = ancestor;
}
segments.reverse();
segments.iter().fold(String::new(), |s, seg| {
s + match seg {
PrefixSegment::ShapeL => "└── ",
PrefixSegment::ShapeT => "├── ",
PrefixSegment::ShapeI => "│ ",
PrefixSegment::Empty => " ",
}
})
}
fn format_file(
tree: &FileTree,
file: &File,
format_history: &mut HashMap<usize, usize>,
result: &mut Vec<FormattedEntry>,
make_absolute: bool,
) {
let prefix = make_prefix(tree, file, format_history);
let path = if make_absolute {
fs::canonicalize(&file.path).unwrap().display().to_string()
} else {
file.path.clone()
};
result.push(FormattedEntry {
name: file.display_name.clone(),
path,
prefix,
link: file.link(),
});
if let Some(parent) = tree.get_parent(file) {
if let Some(&n) = format_history.get(&parent.id) {
format_history.insert(parent.id, n + 1);
}
}
if let FileType::Directory = file.file_type {
format_history.insert(file.id, 0);
}
if let Some(children) = file.children() {
for child_id in children.values() {
format_file(
tree,
tree.get(*child_id),
format_history,
result,
make_absolute,
);
}
}
}
pub fn format_paths(
root_path: &str,
children: Vec<(String, FileType)>,
make_absolute: bool,
) -> Vec<FormattedEntry> {
let mut history = HashMap::new();
let mut result = Vec::new();
match FileTree::new(root_path, children) {
Some(tree) => {
let root = tree.get_root();
format_file(&tree, root, &mut history, &mut result, make_absolute);
result
}
None => Vec::new(),
}
}
#[cfg(test)]
mod test {
use super::FormattedEntry;
use crate::file_tree::FileType;
use std::path;
#[test]
fn formatting_works() {
let formatted = super::format_paths(
".",
vec![
("a".to_string(), FileType::File),
(format!("b{}c", path::MAIN_SEPARATOR), FileType::File),
],
false,
);
let bc_path = format!("b{}c", path::MAIN_SEPARATOR);
let b_path = format!(".{}b", path::MAIN_SEPARATOR);
let variant0 = vec![
FormattedEntry {
name: ".".to_string(),
path: ".".to_string(),
prefix: String::new(),
link: None,
},
FormattedEntry {
name: "a".to_string(),
path: "a".to_string(),
prefix: "├── ".to_string(),
link: None,
},
FormattedEntry {
name: "b".to_string(),
path: b_path.clone(),
prefix: "└── ".to_string(),
link: None,
},
FormattedEntry {
name: "c".to_string(),
path: bc_path.clone(),
prefix: " └── ".to_string(),
link: None,
},
];
let variant1 = vec![
FormattedEntry {
name: ".".to_string(),
path: ".".to_string(),
prefix: String::new(),
link: None,
},
FormattedEntry {
name: "b".to_string(),
path: b_path.clone(),
prefix: "├── ".to_string(),
link: None,
},
FormattedEntry {
name: "c".to_string(),
path: bc_path.clone(),
prefix: "│ └── ".to_string(),
link: None,
},
FormattedEntry {
name: "a".to_string(),
path: "a".to_string(),
prefix: "└── ".to_string(),
link: None,
},
];
assert!(formatted == variant0 || formatted == variant1);
}
}