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
|
use dua::path_of;
use dua::traverse::{EntryData, Tree, TreeIndex};
use itertools::Itertools;
use petgraph::Direction;
use unicode_segmentation::UnicodeSegmentation;
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq)]
pub enum SortMode {
SizeDescending,
SizeAscending,
}
impl SortMode {
pub fn toggle_size(&mut self) {
use SortMode::*;
*self = match self {
SizeAscending => SizeDescending,
SizeDescending => SizeAscending,
}
}
}
impl Default for SortMode {
fn default() -> Self {
SortMode::SizeDescending
}
}
pub struct EntryDataBundle {
pub index: TreeIndex,
pub data: EntryData,
pub is_dir: bool,
pub exists: bool,
}
pub fn sorted_entries(tree: &Tree, node_idx: TreeIndex, sorting: SortMode) -> Vec<EntryDataBundle> {
use SortMode::*;
tree.neighbors_directed(node_idx, Direction::Outgoing)
.filter_map(|idx| {
tree.node_weight(idx).map(|w| {
let p = path_of(tree, idx);
EntryDataBundle {
index: idx,
data: w.clone(),
is_dir: p.is_dir(),
exists: p.exists(),
}
})
})
.sorted_by(|l, r| match sorting {
SizeDescending => r.data.size.cmp(&l.data.size),
SizeAscending => l.data.size.cmp(&r.data.size),
})
.collect()
}
pub fn fit_string_graphemes_with_ellipsis(
s: impl Into<String>,
path_graphemes_count: usize,
mut desired_graphemes: usize,
) -> (String, usize) {
const ELLIPSIS: usize = 1;
const MIN_GRAPHEMES_ON_SIDE: usize = 1;
const MIN_LEN: usize = ELLIPSIS + MIN_GRAPHEMES_ON_SIDE;
const USE_EXTENDED: bool = true;
let s = s.into();
desired_graphemes = desired_graphemes.max(MIN_LEN);
debug_assert!(
path_graphemes_count == s.graphemes(USE_EXTENDED).count(),
"input grapheme count is actually correct"
);
let gc = path_graphemes_count;
if gc <= desired_graphemes {
return (s, gc);
}
let mut n = String::with_capacity(desired_graphemes);
let to_be_removed = gc - desired_graphemes + ELLIPSIS;
let gmi = s.graphemes(USE_EXTENDED);
n.push('…');
n.extend(gmi.skip(to_be_removed));
(n, desired_graphemes)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fit_string_inputs() {
assert_eq!(
("aaa".into(), 3),
fit_string_graphemes_with_ellipsis("aaa", 3, 4)
);
assert_eq!(
("…a".to_string(), 2),
fit_string_graphemes_with_ellipsis("abbbba", 6, 1),
"even amount of chars, desired too small"
);
assert_eq!(
("…ca".to_string(), 3),
fit_string_graphemes_with_ellipsis("abbbbca", 7, 3),
"uneven amount of chars, desired too small"
);
assert_eq!(
("… a".to_string(), 3),
fit_string_graphemes_with_ellipsis("a a", 6, 3),
"spaces are counted as graphemes, too"
);
}
}
|