kuizer-gui 0.3.2

Desktop app for kuizer, a keyboard-driven PC cleaner that also deeply cleans developer artifacts, caches, toolchains, Docker and duplicates, built on GPUI
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! The tree behind the space map: scan roots, then folders, then the reclaimable items found
//! in them, with sizes added up the path. Pure data, built from a report's paths.

use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};

use kuizer::detect::{Category, Safety};
use kuizer::platform::display_path;

use crate::model::Source;

/// Tiles below this share of their level are grouped into one "smaller" tile.
pub const MIN_SHARE: f64 = 0.004;
/// The most tiles one level shows; the rest are grouped into one "smaller" tile.
pub const MAX_TILES: usize = 80;

const CACHES: &str = "\0caches";
const ELSEWHERE: &str = "\0elsewhere";
const REST: &str = "\0rest";

/// A reclaimable item the map shows as a leaf tile.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Item {
    pub source: Source,
    pub id: u64,
    pub category: Category,
    pub safety: Safety,
}

/// One item of the report, as the tree builder takes it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Leaf {
    pub path: PathBuf,
    pub bytes: u64,
    pub item: Item,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Kind {
    /// The whole map, a scan root, the global caches or items outside every root.
    Group,
    Folder,
    Item(Item),
    /// Several small tiles of one level, grouped. They are kept as its children.
    Rest(usize),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Node {
    /// Stable across rebuilds: the path for folders and items, a fixed name for groups.
    pub key: String,
    pub name: String,
    pub path: Option<PathBuf>,
    pub bytes: u64,
    pub items: usize,
    pub kind: Kind,
    /// Largest first, with at most one `Rest` tile at the end.
    pub children: Vec<Node>,
}

impl Node {
    /// Whether clicking or Enter opens this tile as a level of its own.
    pub fn opens(&self) -> bool {
        matches!(self.kind, Kind::Group | Kind::Folder) && !self.children.is_empty()
    }

    pub fn item(&self) -> Option<Item> {
        match self.kind {
            Kind::Item(item) => Some(item),
            _ => None,
        }
    }

    pub fn child(&self, key: &str) -> Option<&Node> {
        self.children.iter().find(|child| child.key == key)
    }

    pub fn fold_leaves<T>(&self, initial: T, fold: &impl Fn(T, Item, u64) -> T) -> T {
        if let Some(item) = self.item() {
            return fold(initial, item, self.bytes);
        }
        self.children
            .iter()
            .fold(initial, |value, child| child.fold_leaves(value, fold))
    }

    /// The nodes from this one down along `path`, stopping at the first key that is gone.
    pub fn trail(&self, path: &[String]) -> Vec<&Node> {
        let mut trail = vec![self];
        for key in path {
            let Some(next) = trail.last().and_then(|node| node.child(key)) else {
                break;
            };
            if !next.opens() {
                break;
            }
            trail.push(next);
        }
        trail
    }
}

#[derive(Default)]
struct Trie<'a> {
    leaf: Option<usize>,
    children: BTreeMap<&'a OsStr, Trie<'a>>,
}

impl<'a> Trie<'a> {
    fn insert(&mut self, relative: &'a Path, index: usize) {
        let mut node = self;
        for part in relative.components() {
            node = node.children.entry(part.as_os_str()).or_default();
        }
        if node.leaf.is_none() {
            node.leaf = Some(index);
        }
    }
}

struct Builder<'a> {
    base: &'a Path,
    leaves: &'a [Leaf],
}

impl<'a> Builder<'a> {
    /// Turns a folder of the trie into a node, folding a folder with a single child into that
    /// child so a chain of folders reads as one `a/b/c` tile.
    fn tile(&self, mut trie: Trie<'a>, parts: &mut Vec<&'a OsStr>) -> Option<Node> {
        let start = parts.len() - 1;
        while trie.leaf.is_none() && trie.children.len() == 1 {
            let (part, only) = trie.children.pop_first()?;
            parts.push(part);
            trie = only;
        }
        let name = chain_name(&parts[start..]);
        let depth = parts.len();
        let node = if let Some(index) = trie.leaf {
            self.item(index, name)
        } else if trie.children.is_empty() {
            None
        } else {
            let mut path = self.base.to_path_buf();
            path.extend(parts.iter().copied());
            let children = trie
                .children
                .into_iter()
                .filter_map(|(part, child)| {
                    parts.push(part);
                    let node = self.tile(child, parts);
                    parts.truncate(depth);
                    node
                })
                .collect();
            Some(container(
                path.to_string_lossy().into_owned(),
                name,
                Some(path),
                Kind::Folder,
                children,
            ))
        };
        parts.truncate(start + 1);
        node
    }

    fn item(&self, index: usize, name: String) -> Option<Node> {
        let leaf = self.leaves.get(index)?;
        Some(Node {
            key: leaf.path.to_string_lossy().into_owned(),
            name,
            path: Some(leaf.path.clone()),
            bytes: leaf.bytes,
            items: 1,
            kind: Kind::Item(leaf.item),
            children: Vec::new(),
        })
    }
}

fn chain_name(parts: &[&OsStr]) -> String {
    let mut name = String::new();
    for (index, part) in parts.iter().enumerate() {
        if index > 0 {
            name.push('/');
        }
        name.push_str(&part.to_string_lossy());
    }
    name
}

fn container(
    key: String,
    name: String,
    path: Option<PathBuf>,
    kind: Kind,
    children: Vec<Node>,
) -> Node {
    Node {
        key,
        name,
        path,
        bytes: children.iter().map(|child| child.bytes).sum(),
        items: children.iter().map(|child| child.items).sum(),
        kind,
        children,
    }
}

/// A group of leaves under `base`, such as one scan root, as a node whose children are the
/// folders and items right below it.
fn group(key: String, name: String, base: &Path, leaves: &[Leaf]) -> Option<Node> {
    if leaves.is_empty() {
        return None;
    }
    let mut trie = Trie::default();
    for (index, leaf) in leaves.iter().enumerate() {
        let relative = leaf.path.strip_prefix(base).unwrap_or(Path::new(""));
        trie.insert(relative, index);
    }
    let builder = Builder { base, leaves };
    let children = if let Some(index) = trie.leaf.take() {
        let path = &leaves.get(index)?.path;
        let name = path.file_name().map_or_else(
            || display_path(path),
            |name| name.to_string_lossy().into_owned(),
        );
        vec![builder.item(index, name)?]
    } else {
        let mut parts = Vec::new();
        trie.children
            .into_iter()
            .filter_map(|(part, child)| {
                parts.push(part);
                let node = builder.tile(child, &mut parts);
                parts.pop();
                node
            })
            .collect()
    };
    Some(container(
        key,
        name,
        Some(base.to_path_buf()),
        Kind::Group,
        children,
    ))
}

/// The deepest folder that holds every path.
fn common_ancestor<'a>(paths: impl IntoIterator<Item = &'a Path>) -> PathBuf {
    let mut paths = paths.into_iter();
    let Some(first) = paths.next() else {
        return PathBuf::new();
    };
    let mut base = first.parent().unwrap_or(first).to_path_buf();
    for path in paths {
        while !path.starts_with(&base) {
            if !base.pop() {
                return PathBuf::new();
            }
        }
    }
    base
}

/// Sorts every level largest first and folds the small tail of a level into one tile.
fn arrange(node: &mut Node) {
    for child in &mut node.children {
        arrange(child);
    }
    node.children
        .sort_by(|a, b| b.bytes.cmp(&a.bytes).then_with(|| a.name.cmp(&b.name)));
    #[allow(
        clippy::cast_precision_loss,
        reason = "a share only needs a few digits"
    )]
    let floor = node.bytes as f64 * MIN_SHARE;
    #[allow(
        clippy::cast_precision_loss,
        reason = "a share only needs a few digits"
    )]
    let cut = node
        .children
        .iter()
        .position(|child| (child.bytes as f64) < floor)
        .unwrap_or(node.children.len())
        .min(MAX_TILES - 1);
    if node.children.len() - cut >= 2 {
        let rest: Vec<Node> = node.children.drain(cut..).collect();
        let count = rest.len();
        node.children.push(Node {
            key: format!("{}{REST}", node.key),
            name: format!("{count} smaller"),
            path: None,
            bytes: rest.iter().map(|child| child.bytes).sum(),
            items: rest.iter().map(|child| child.items).sum(),
            kind: Kind::Rest(count),
            children: rest,
        });
    }
}

/// @name build
/// @description Builds the space map's tree: one group per scan root holding the folders and
/// project artifacts below it, a group for the global caches, and a group for artifacts outside
/// every root. Sizes add up the path, a chain of single folders becomes one `a/b` tile, and a
/// level's tiny tiles are folded into one "n smaller" tile. With a single group, that group is
/// the top of the map.
///
/// @example
/// let tree = build(&report.roots, artifacts, caches);
pub fn build(roots: &[PathBuf], artifacts: Vec<Leaf>, caches: Vec<Leaf>) -> Node {
    let mut by_root: Vec<(&PathBuf, Vec<Leaf>)> =
        roots.iter().map(|root| (root, Vec::new())).collect();
    by_root.sort_by_key(|(root, _)| std::cmp::Reverse(root.components().count()));
    let mut elsewhere = Vec::new();
    for leaf in artifacts {
        match by_root
            .iter_mut()
            .find(|(root, _)| leaf.path.starts_with(root))
        {
            Some((_, leaves)) => leaves.push(leaf),
            None => elsewhere.push(leaf),
        }
    }
    let mut groups: Vec<Node> = by_root
        .into_iter()
        .filter_map(|(root, leaves)| {
            group(
                root.to_string_lossy().into_owned(),
                display_path(root),
                root,
                &leaves,
            )
        })
        .collect();
    let base = common_ancestor(elsewhere.iter().map(|leaf| leaf.path.as_path()));
    groups.extend(group(
        ELSEWHERE.to_owned(),
        "Outside the scanned folders".to_owned(),
        &base,
        &elsewhere,
    ));
    let base = common_ancestor(caches.iter().map(|leaf| leaf.path.as_path()));
    groups.extend(group(
        CACHES.to_owned(),
        "Global caches".to_owned(),
        &base,
        &caches,
    ));
    let mut top = if groups.len() == 1 {
        groups.remove(0)
    } else {
        let name = match roots {
            [root] => display_path(root),
            _ => "Everything".to_owned(),
        };
        container(String::new(), name, None, Kind::Group, groups)
    };
    arrange(&mut top);
    top
}

#[cfg(test)]
mod tests {
    use super::*;

    fn leaf(id: u64, path: &str, bytes: u64) -> Leaf {
        Leaf {
            path: PathBuf::from(path),
            bytes,
            item: Item {
                source: Source::Artifact,
                id,
                category: Category::Dependencies,
                safety: Safety::Safe,
            },
        }
    }

    fn cache(id: u64, path: &str, bytes: u64) -> Leaf {
        let mut leaf = leaf(id, path, bytes);
        leaf.item.source = Source::Cache;
        leaf.item.category = Category::Cache;
        leaf
    }

    fn names(node: &Node) -> Vec<&str> {
        node.children
            .iter()
            .map(|child| child.name.as_str())
            .collect()
    }

    #[test]
    fn sizes_add_up_the_path() {
        let roots = [PathBuf::from("/p")];
        let tree = build(
            &roots,
            vec![
                leaf(1, "/p/team/a/node_modules", 300),
                leaf(2, "/p/team/b/node_modules", 200),
                leaf(3, "/p/team/b/dist", 150),
                leaf(4, "/p/solo/target", 500),
            ],
            Vec::new(),
        );
        assert_eq!(tree.key, "/p");
        assert_eq!(tree.kind, Kind::Group);
        assert_eq!(tree.bytes, 1150);
        assert_eq!(tree.items, 4);
        assert_eq!(names(&tree), ["team", "solo/target"]);
        let team = &tree.children[0];
        assert_eq!(team.bytes, 650);
        assert_eq!(team.kind, Kind::Folder);
        assert_eq!(team.key, "/p/team");
        assert!(team.opens());
        assert_eq!(names(team), ["b", "a/node_modules"]);
        let b = &team.children[0];
        assert_eq!(b.bytes, 350);
        assert_eq!(names(b), ["node_modules", "dist"]);
        let solo = &tree.children[1];
        assert_eq!(solo.key, "/p/solo/target");
        assert_eq!(solo.item().map(|item| item.id), Some(4));
        assert!(!solo.opens());
    }

    #[test]
    fn single_child_chains_collapse_into_one_tile() {
        let roots = [PathBuf::from("/p")];
        let tree = build(
            &roots,
            vec![
                leaf(1, "/p/deep/er/still/x/node_modules", 10),
                leaf(2, "/p/deep/er/still/y/node_modules", 10),
                leaf(3, "/p/other/node_modules", 30),
            ],
            Vec::new(),
        );
        assert_eq!(names(&tree), ["other/node_modules", "deep/er/still"]);
        let chain = &tree.children[1];
        assert_eq!(
            chain.key, "/p/deep/er/still",
            "the key is the deepest folder"
        );
        assert_eq!(chain.path.as_deref(), Some(Path::new("/p/deep/er/still")));
        assert_eq!(names(chain), ["x/node_modules", "y/node_modules"]);
    }

    #[test]
    fn tiny_items_are_grouped_per_level() {
        let roots = [PathBuf::from("/p")];
        let mut leaves = vec![leaf(1, "/p/big/node_modules", 100_000)];
        for index in 0..5 {
            leaves.push(leaf(
                10 + index,
                &format!("/p/tiny{index}/node_modules"),
                10,
            ));
        }
        let tree = build(&roots, leaves, Vec::new());
        assert_eq!(tree.children.len(), 2);
        let rest = &tree.children[1];
        assert_eq!(rest.kind, Kind::Rest(5));
        assert_eq!(rest.name, "5 smaller");
        assert_eq!(rest.bytes, 50);
        assert_eq!(rest.items, 5);
        assert!(!rest.opens());
        let folded = tree.fold_leaves((0, 0), &|(count, total), _, bytes| {
            (count + 1, total + bytes)
        });
        assert_eq!(folded, (6, 100_050), "grouped items still count");

        let many: Vec<Leaf> = (0..200)
            .map(|index| leaf(index, &format!("/p/p{index:03}/node_modules"), 1_000))
            .collect();
        let tree = build(&roots, many, Vec::new());
        assert_eq!(tree.children.len(), MAX_TILES);
        assert_eq!(
            tree.children[MAX_TILES - 1].kind,
            Kind::Rest(200 - (MAX_TILES - 1))
        );

        let one_small = vec![
            leaf(1, "/p/a/node_modules", 100_000),
            leaf(2, "/p/b/node_modules", 1),
        ];
        let tree = build(&roots, one_small, Vec::new());
        assert_eq!(
            names(&tree),
            ["a/node_modules", "b/node_modules"],
            "one is not a group"
        );
    }

    #[test]
    fn roots_and_caches_are_groups_of_their_own() {
        let roots = [PathBuf::from("/p"), PathBuf::from("/q")];
        let tree = build(
            &roots,
            vec![
                leaf(1, "/p/a/node_modules", 40),
                leaf(2, "/p/b/node_modules", 40),
                leaf(3, "/q/c/node_modules", 10),
                leaf(4, "/elsewhere/z/dist", 5),
            ],
            vec![
                cache(5, "/home/u/.cache/pip", 70),
                cache(6, "/home/u/.npm", 30),
            ],
        );
        assert_eq!(tree.key, "");
        assert_eq!(tree.name, "Everything");
        assert_eq!(tree.bytes, 195);
        assert_eq!(
            names(&tree),
            ["Global caches", "/p", "/q", "Outside the scanned folders"]
        );
        let caches = &tree.children[0];
        assert_eq!(caches.key, CACHES);
        assert_eq!(names(caches), [".cache/pip", ".npm"]);
        assert_eq!(caches.children[0].item().unwrap().source, Source::Cache);
        let trail = tree.trail(&["/p".to_owned(), "missing".to_owned()]);
        assert_eq!(trail.len(), 2, "the walk stops at a key that is gone");
        assert_eq!(trail[1].name, "/p");
    }

    #[test]
    fn an_item_holding_another_item_wins() {
        let roots = [PathBuf::from("/p")];
        let tree = build(
            &roots,
            vec![
                leaf(1, "/p/a/node_modules", 50),
                leaf(2, "/p/a/node_modules/x/node_modules", 20),
                leaf(3, "/p/b/dist", 10),
            ],
            Vec::new(),
        );
        assert_eq!(tree.bytes, 60, "nothing is counted twice");
        assert_eq!(tree.items, 2);
    }

    #[test]
    fn the_same_report_builds_the_same_tree() {
        let roots = [PathBuf::from("/p")];
        let leaves = vec![
            leaf(1, "/p/a/node_modules", 10),
            leaf(2, "/p/b/node_modules", 10),
            leaf(3, "/p/c/node_modules", 10),
        ];
        let mut reversed = leaves.clone();
        reversed.reverse();
        let first = build(&roots, leaves, Vec::new());
        assert_eq!(first, build(&roots, reversed, Vec::new()));
        assert_eq!(
            names(&first),
            ["a/node_modules", "b/node_modules", "c/node_modules"]
        );
        assert!(build(&roots, Vec::new(), Vec::new()).children.is_empty());
    }
}