perga 0.1.5

A terminal Markdown browser with a persistent file tree, tabs, wiki-links and backlinks
Documentation
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
//! The lazy file-tree model backing the files sidebar.
//!
//! The tree is an arena of [`Node`]s indexed by their path relative to the
//! vault root. The walker streams entries in whatever order it finds them, so
//! insertion creates any missing ancestors and only re-sorts the directories
//! that actually gained a child.
//!
//! # What the tree filters and what the walk filters
//!
//! The walk always finds dotted entries; the tree decides whether to show
//! them. That is what makes `.` and `a` instant — toggling either re-flattens
//! the visible rows rather than re-reading the filesystem. `.gitignore` is the
//! exception: honouring it is a property of the walk, so changing it needs a
//! new walk, and nothing in the UI offers to.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

use crate::config::schema::{FilesConfig, SortKey};
use crate::vault::walker::Entry;

/// One entry in the tree.
#[derive(Debug, Clone)]
pub struct Node {
    /// The entry's own name, without any parent components.
    pub name: String,
    /// The path relative to the vault root. Empty for the root itself.
    pub path: PathBuf,
    /// Whether this is a directory.
    pub is_dir: bool,
    /// Whether the name begins with a dot.
    pub hidden: bool,
    /// Last modification time, when the filesystem gave one.
    pub mtime: Option<SystemTime>,
    /// Size in bytes. Zero for directories.
    pub size: u64,
    /// Whether a directory's children are shown.
    pub expanded: bool,
    /// The parent's index, or `None` for the root.
    parent: Option<usize>,
    /// Child indices, kept in display order.
    children: Vec<usize>,
}

/// One visible line of the tree.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Row {
    /// The node this row draws.
    pub node: usize,
    /// How deep it sits below the root, for indentation.
    pub depth: usize,
}

/// The vault tree.
#[derive(Debug)]
pub struct Tree {
    /// The arena. Index 0 is always the root.
    nodes: Vec<Node>,
    /// Path to arena index, so a streamed entry finds its place in one lookup.
    index: HashMap<PathBuf, usize>,
    /// How entries within a directory are ordered.
    sort: SortKey,
    /// Reverse that order.
    sort_reverse: bool,
    /// Extensions treated as Markdown.
    extensions: Vec<String>,
    /// Show dotted entries.
    pub include_hidden: bool,
    /// Show files that are not Markdown.
    pub show_all: bool,
    /// The active name filter, if any.
    filter: Option<String>,
    /// The selected node.
    selected: usize,
    /// How many entries the walk has delivered.
    pub entries: usize,
    /// Whether the walk has finished.
    pub complete: bool,
}

/// The root's index in the arena. Always present, never drawn as a row.
const ROOT: usize = 0;

impl Tree {
    /// An empty tree configured from the `[files]` table.
    pub fn new(config: &FilesConfig) -> Self {
        let root = Node {
            name: String::new(),
            path: PathBuf::new(),
            is_dir: true,
            hidden: false,
            mtime: None,
            size: 0,
            expanded: true,
            parent: None,
            children: Vec::new(),
        };

        Tree {
            nodes: vec![root],
            index: HashMap::new(),
            sort: config.sort,
            sort_reverse: config.sort_reverse,
            extensions: config.extensions.clone(),
            include_hidden: config.include_hidden,
            show_all: config.show_all,
            filter: None,
            selected: ROOT,
            entries: 0,
            complete: false,
        }
    }

    /// The node at an arena index.
    pub fn node(&self, index: usize) -> &Node {
        &self.nodes[index]
    }

    /// Whether the walk has delivered nothing yet.
    pub fn is_empty(&self) -> bool {
        self.nodes.len() == 1
    }

    /// The active name filter.
    pub fn filter(&self) -> Option<&str> {
        self.filter.as_deref()
    }

    /// Filter the tree by a substring of the entry name.
    ///
    /// An empty filter is the same as none, so backspacing a filter away
    /// restores the tree rather than leaving it matching everything.
    pub fn set_filter(&mut self, filter: Option<String>) {
        self.filter = filter.filter(|f| !f.is_empty());
        self.keep_selection_visible();
    }

    /// Add a batch of entries from the walker.
    pub fn insert_all(&mut self, entries: impl IntoIterator<Item = Entry>) {
        let mut touched: Vec<usize> = Vec::new();

        for entry in entries {
            self.entries += 1;
            if let Some(parent) = self.insert(entry) {
                if !touched.contains(&parent) {
                    touched.push(parent);
                }
            }
        }

        // Only the directories that gained a child are re-sorted; a batch
        // landing in one directory must not cost a sort of the whole vault.
        for parent in touched {
            self.sort_children(parent);
        }
    }

    /// Add one entry, creating any missing ancestors.
    ///
    /// Returns the parent whose children need re-sorting, or `None` when the
    /// entry was already known.
    fn insert(&mut self, entry: Entry) -> Option<usize> {
        if let Some(&existing) = self.index.get(&entry.path) {
            // A watcher re-reporting a path updates it in place rather than
            // duplicating the row.
            let node = &mut self.nodes[existing];
            node.mtime = entry.mtime;
            node.size = entry.size;
            return None;
        }

        let parent = match entry.path.parent() {
            Some(parent) if !parent.as_os_str().is_empty() => self.ensure_directory(parent),
            _ => ROOT,
        };

        let name = entry
            .path
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_default();

        let node = Node {
            hidden: name.starts_with('.'),
            name,
            path: entry.path.clone(),
            is_dir: entry.is_dir,
            mtime: entry.mtime,
            size: entry.size,
            expanded: false,
            parent: Some(parent),
            children: Vec::new(),
        };

        let index = self.nodes.len();
        self.nodes.push(node);
        self.index.insert(entry.path, index);
        self.nodes[parent].children.push(index);

        Some(parent)
    }

    /// Find or create the directory node for a relative path.
    fn ensure_directory(&mut self, path: &Path) -> usize {
        if let Some(&existing) = self.index.get(path) {
            return existing;
        }

        // The walk yields parents before children, so this is the rare case of
        // a path arriving without its directory — a watcher event, say.
        let parent = match path.parent() {
            Some(parent) if !parent.as_os_str().is_empty() => self.ensure_directory(parent),
            _ => ROOT,
        };

        let name = path
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_default();

        let index = self.nodes.len();
        self.nodes.push(Node {
            hidden: name.starts_with('.'),
            name,
            path: path.to_path_buf(),
            is_dir: true,
            mtime: None,
            size: 0,
            expanded: false,
            parent: Some(parent),
            children: Vec::new(),
        });
        self.index.insert(path.to_path_buf(), index);
        self.nodes[parent].children.push(index);

        index
    }

    /// Order one directory's children: directories first, then by the sort key.
    fn sort_children(&mut self, parent: usize) {
        let mut children = std::mem::take(&mut self.nodes[parent].children);

        children.sort_by(|&a, &b| {
            let (a, b) = (&self.nodes[a], &self.nodes[b]);

            // Directories first, always, whatever the sort key says.
            b.is_dir
                .cmp(&a.is_dir)
                .then_with(|| self.compare(a, b))
                .then_with(|| a.name.cmp(&b.name))
        });

        self.nodes[parent].children = children;
    }

    /// Compare two entries of the same kind by the configured sort key.
    fn compare(&self, a: &Node, b: &Node) -> std::cmp::Ordering {
        let ordering = match self.sort {
            SortKey::Name => a.name.to_lowercase().cmp(&b.name.to_lowercase()),
            // Most recent and largest first: the interesting end of both of
            // those orders is the top.
            SortKey::Mtime => b.mtime.cmp(&a.mtime),
            SortKey::Size => b.size.cmp(&a.size),
        };

        if self.sort_reverse {
            ordering.reverse()
        } else {
            ordering
        }
    }

    /// Whether a node is a Markdown file by extension.
    pub fn is_markdown(&self, node: &Node) -> bool {
        let Some(extension) = Path::new(&node.name).extension().and_then(|e| e.to_str()) else {
            return false;
        };
        self.extensions
            .iter()
            .any(|known| known.eq_ignore_ascii_case(extension))
    }

    /// Whether a node passes the hidden and non-Markdown toggles.
    fn shown(&self, index: usize) -> bool {
        let node = &self.nodes[index];

        if node.hidden && !self.include_hidden {
            return false;
        }

        // A directory is shown even when the tree is Markdown-only: it may
        // contain Markdown, and pruning it would need the whole subtree walked
        // before the first frame.
        node.is_dir || self.show_all || self.is_markdown(node)
    }

    /// Whether a node or any of its descendants matches the active filter.
    fn matches_filter(&self, index: usize) -> bool {
        let Some(filter) = &self.filter else {
            return true;
        };

        let node = &self.nodes[index];
        if node.name.to_lowercase().contains(&filter.to_lowercase()) {
            return true;
        }

        node.children
            .iter()
            .any(|&child| self.shown(child) && self.matches_filter(child))
    }

    /// The visible rows, top to bottom.
    ///
    /// Pure: rendering calls this every frame and must never mutate the tree.
    pub fn rows(&self) -> Vec<Row> {
        let mut rows = Vec::new();
        self.push_rows(ROOT, 0, &mut rows);
        rows
    }

    fn push_rows(&self, parent: usize, depth: usize, rows: &mut Vec<Row>) {
        for &child in &self.nodes[parent].children {
            if !self.shown(child) || !self.matches_filter(child) {
                continue;
            }

            rows.push(Row { node: child, depth });

            // A filter opens every branch it matches: a match the user cannot
            // see is not a match as far as they are concerned.
            let open = self.nodes[child].expanded || self.filter.is_some();
            if self.nodes[child].is_dir && open {
                self.push_rows(child, depth + 1, rows);
            }
        }
    }

    /// The selected node, or `None` when nothing is selected.
    pub fn selected(&self) -> Option<&Node> {
        (self.selected != ROOT).then(|| &self.nodes[self.selected])
    }

    /// The selected node's index within [`Tree::rows`].
    pub fn selected_row(&self, rows: &[Row]) -> Option<usize> {
        rows.iter().position(|row| row.node == self.selected)
    }

    /// Move the selection down or up by one visible row.
    pub fn move_selection(&mut self, delta: isize) {
        let rows = self.rows();
        if rows.is_empty() {
            self.selected = ROOT;
            return;
        }

        let current = self.selected_row(&rows);
        let next = match current {
            Some(at) => (at as isize + delta).clamp(0, rows.len() as isize - 1) as usize,
            // Nothing selected yet: either end of the list, depending on which
            // way the user moved.
            None if delta < 0 => rows.len() - 1,
            None => 0,
        };

        self.selected = rows[next].node;
    }

    /// Select a specific row.
    pub fn select_row(&mut self, row: usize) {
        let rows = self.rows();
        if let Some(row) = rows.get(row) {
            self.selected = row.node;
        }
    }

    /// Select the node at a path, if the tree knows it.
    pub fn select_path(&mut self, path: &Path) -> bool {
        match self.index.get(path) {
            Some(&index) => {
                self.selected = index;
                true
            }
            None => false,
        }
    }

    /// Expand the selected directory, and report whether anything happened.
    ///
    /// A directory that is already open moves the selection to its first
    /// child, so `l` walks into a tree the way it does in a file manager.
    pub fn expand_selected(&mut self) -> bool {
        let selected = self.selected;
        if selected == ROOT || !self.nodes[selected].is_dir {
            return false;
        }

        if self.nodes[selected].expanded {
            let rows = self.rows();
            if let Some(at) = self.selected_row(&rows) {
                if let Some(next) = rows.get(at + 1) {
                    if self.nodes[next.node].parent == Some(selected) {
                        self.selected = next.node;
                    }
                }
            }
        } else {
            self.nodes[selected].expanded = true;
        }

        true
    }

    /// Collapse the selected directory, or move to its parent.
    pub fn collapse_selected(&mut self) {
        let selected = self.selected;
        if selected == ROOT {
            return;
        }

        if self.nodes[selected].is_dir && self.nodes[selected].expanded {
            self.nodes[selected].expanded = false;
            return;
        }

        if let Some(parent) = self.nodes[selected].parent.filter(|&p| p != ROOT) {
            self.selected = parent;
        }
    }

    /// Expand every directory on the way to a path and select it.
    ///
    /// Used when a document is opened from anywhere other than the tree, so
    /// the tree always shows where the reader is.
    pub fn reveal(&mut self, path: &Path) -> bool {
        let Some(&index) = self.index.get(path) else {
            return false;
        };

        let mut at = self.nodes[index].parent;
        while let Some(node) = at {
            self.nodes[node].expanded = true;
            at = self.nodes[node].parent;
        }

        self.selected = index;
        true
    }

    /// Forget an entry, and any children it had.
    ///
    /// Used when a file is renamed or deleted. The arena keeps the slot: node
    /// indices are handed out to the renderer and the selection, and
    /// compacting the arena would invalidate both.
    pub fn remove(&mut self, path: &Path) {
        let Some(index) = self.index.remove(path) else {
            return;
        };

        if let Some(parent) = self.nodes[index].parent {
            self.nodes[parent].children.retain(|&child| child != index);
        }

        let orphans: Vec<PathBuf> = self.nodes[index]
            .children
            .iter()
            .map(|&child| self.nodes[child].path.clone())
            .collect();
        for orphan in orphans {
            self.remove(&orphan);
        }

        self.nodes[index].parent = None;
        self.keep_selection_visible();
    }

    /// Show or hide dotted entries.
    pub fn toggle_hidden(&mut self) {
        self.include_hidden = !self.include_hidden;
        self.keep_selection_visible();
    }

    /// Show or hide files that are not Markdown.
    pub fn toggle_all_files(&mut self) {
        self.show_all = !self.show_all;
        self.keep_selection_visible();
    }

    /// Drop a selection that a toggle or a filter has just hidden.
    fn keep_selection_visible(&mut self) {
        let rows = self.rows();
        if self.selected != ROOT && self.selected_row(&rows).is_none() {
            self.selected = rows.first().map_or(ROOT, |row| row.node);
        }
    }
}

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

    fn entry(path: &str, is_dir: bool) -> Entry {
        Entry {
            path: PathBuf::from(path),
            is_dir,
            mtime: None,
            size: 0,
        }
    }

    /// A tree with a small vault in it, in an order the walk might produce.
    fn tree() -> Tree {
        let mut tree = Tree::new(&FilesConfig::default());
        tree.insert_all([
            entry("README.md", false),
            entry("docs", true),
            entry("docs/setup.md", false),
            entry("docs/api", true),
            entry("docs/api/auth.md", false),
            entry(".github", true),
            entry(".github/CONTRIBUTING.md", false),
            entry("logo.png", false),
        ]);
        tree
    }

    /// The names of the visible rows, indented by depth.
    fn visible(tree: &Tree) -> Vec<String> {
        tree.rows()
            .iter()
            .map(|row| format!("{}{}", "  ".repeat(row.depth), tree.node(row.node).name))
            .collect()
    }

    #[test]
    fn directories_come_first_and_names_sort_case_insensitively() {
        let mut tree = Tree::new(&FilesConfig::default());
        tree.insert_all([
            entry("zebra.md", false),
            entry("Apple.md", false),
            entry("middle", true),
        ]);

        assert_eq!(visible(&tree), ["middle", "Apple.md", "zebra.md"]);
    }

    #[test]
    fn directories_are_collapsed_until_they_are_expanded() {
        let mut tree = tree();
        assert_eq!(visible(&tree), [".github", "docs", "README.md"]);

        tree.select_path(Path::new("docs"));
        tree.expand_selected();
        assert_eq!(
            visible(&tree),
            [".github", "docs", "  api", "  setup.md", "README.md"]
        );
    }

    #[test]
    fn dotted_directories_are_shown_by_default_and_can_be_hidden() {
        let mut tree = tree();
        assert!(visible(&tree).contains(&".github".to_string()));

        tree.toggle_hidden();
        assert!(!visible(&tree).contains(&".github".to_string()));

        tree.toggle_hidden();
        assert!(visible(&tree).contains(&".github".to_string()));
    }

    #[test]
    fn non_markdown_files_are_hidden_until_asked_for() {
        let mut tree = tree();
        assert!(!visible(&tree).contains(&"logo.png".to_string()));

        tree.toggle_all_files();
        assert!(visible(&tree).contains(&"logo.png".to_string()));
    }

    #[test]
    fn a_toggle_that_hides_the_selection_moves_it_somewhere_visible() {
        let mut tree = tree();
        assert!(tree.select_path(Path::new(".github")));

        tree.toggle_hidden();
        let selected = tree.selected().expect("something stays selected");
        assert_ne!(selected.path, PathBuf::from(".github"));
    }

    #[test]
    fn the_selection_walks_the_visible_rows() {
        let mut tree = tree();

        tree.move_selection(1);
        assert_eq!(tree.selected().unwrap().name, ".github");
        tree.move_selection(1);
        assert_eq!(tree.selected().unwrap().name, "docs");

        // ...and stops at the ends rather than wrapping.
        tree.move_selection(50);
        assert_eq!(tree.selected().unwrap().name, "README.md");
        tree.move_selection(-50);
        assert_eq!(tree.selected().unwrap().name, ".github");
    }

    #[test]
    fn expanding_an_open_directory_steps_into_it() {
        let mut tree = tree();
        tree.select_path(Path::new("docs"));

        tree.expand_selected();
        assert_eq!(tree.selected().unwrap().name, "docs");

        tree.expand_selected();
        assert_eq!(tree.selected().unwrap().path, PathBuf::from("docs/api"));
    }

    #[test]
    fn collapsing_closes_a_directory_then_moves_to_the_parent() {
        let mut tree = tree();
        tree.select_path(Path::new("docs"));
        tree.expand_selected();
        tree.select_path(Path::new("docs/setup.md"));

        // A file has nothing to collapse, so it goes to its parent.
        tree.collapse_selected();
        assert_eq!(tree.selected().unwrap().name, "docs");

        // ...which is open, so the first press closes it.
        tree.collapse_selected();
        assert_eq!(visible(&tree), [".github", "docs", "README.md"]);
    }

    #[test]
    fn revealing_a_path_opens_every_directory_above_it() {
        let mut tree = tree();
        assert!(tree.reveal(Path::new("docs/api/auth.md")));

        assert_eq!(
            visible(&tree),
            [
                ".github",
                "docs",
                "  api",
                "    auth.md",
                "  setup.md",
                "README.md"
            ]
        );
        assert_eq!(
            tree.selected().unwrap().path,
            PathBuf::from("docs/api/auth.md")
        );
    }

    #[test]
    fn a_removed_entry_takes_its_children_with_it() {
        let mut tree = tree();
        tree.reveal(Path::new("docs/api/auth.md"));

        tree.remove(Path::new("docs/api"));

        assert_eq!(
            visible(&tree),
            [".github", "docs", "  setup.md", "README.md"]
        );
        assert!(!tree.select_path(Path::new("docs/api/auth.md")));
    }

    #[test]
    fn removing_something_that_is_not_there_is_a_no_op() {
        let mut tree = tree();
        let before = tree.rows().len();
        tree.remove(Path::new("nowhere.md"));
        assert_eq!(tree.rows().len(), before);
    }

    #[test]
    fn a_filter_shows_matches_and_the_directories_holding_them() {
        let mut tree = tree();
        tree.set_filter(Some("auth".to_string()));

        assert_eq!(visible(&tree), ["docs", "  api", "    auth.md"]);

        // An empty filter is no filter, so backspacing it away restores the
        // tree rather than matching everything.
        tree.set_filter(Some(String::new()));
        assert_eq!(tree.filter(), None);
        assert_eq!(visible(&tree), [".github", "docs", "README.md"]);
    }

    #[test]
    fn a_filter_is_case_insensitive() {
        let mut tree = tree();
        tree.set_filter(Some("README".to_string()));
        assert_eq!(visible(&tree), ["README.md"]);

        tree.set_filter(Some("readme".to_string()));
        assert_eq!(visible(&tree), ["README.md"]);
    }

    #[test]
    fn re_reporting_a_path_updates_it_rather_than_duplicating_the_row() {
        let mut tree = tree();
        let before = tree.rows().len();

        tree.insert_all([Entry {
            path: PathBuf::from("README.md"),
            is_dir: false,
            mtime: Some(SystemTime::UNIX_EPOCH),
            size: 42,
        }]);

        assert_eq!(tree.rows().len(), before);
        assert!(tree.select_path(Path::new("README.md")));
        assert_eq!(tree.selected().unwrap().size, 42);
    }

    #[test]
    fn an_entry_arriving_before_its_directory_still_lands_in_place() {
        let mut tree = Tree::new(&FilesConfig::default());
        tree.insert_all([entry("a/b/c.md", false)]);
        assert!(tree.reveal(Path::new("a/b/c.md")));
        assert_eq!(visible(&tree), ["a", "  b", "    c.md"]);
    }

    #[test]
    fn sorting_by_mtime_puts_the_most_recent_first() {
        let config = FilesConfig {
            sort: SortKey::Mtime,
            ..FilesConfig::default()
        };

        let mut tree = Tree::new(&config);
        tree.insert_all([
            Entry {
                path: PathBuf::from("old.md"),
                is_dir: false,
                mtime: Some(SystemTime::UNIX_EPOCH),
                size: 0,
            },
            Entry {
                path: PathBuf::from("new.md"),
                is_dir: false,
                mtime: Some(SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(60)),
                size: 0,
            },
        ]);

        assert_eq!(visible(&tree), ["new.md", "old.md"]);
    }

    #[test]
    fn sort_reverse_turns_the_order_around() {
        let config = FilesConfig {
            sort_reverse: true,
            ..FilesConfig::default()
        };

        let mut tree = Tree::new(&config);
        tree.insert_all([entry("a.md", false), entry("b.md", false), entry("d", true)]);

        // Directories still come first: reversing orders entries of the same
        // kind, it does not turn the tree upside down.
        assert_eq!(visible(&tree), ["d", "b.md", "a.md"]);
    }
}