gravityfile-tui 0.4.1

Terminal user interface for gravityfile
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
//! Moving between directories.
//!
//! Every change of the viewed directory goes through [`App::enter`],
//! [`App::go_back`], [`App::go_to`] or [`App::place`]. They keep the tab's
//! history, every layout's selection and the state derived from the view in
//! step, so no caller has to remember which caches to refresh.

use std::path::{Path, PathBuf};
use std::sync::Arc;

use gravityfile_core::FileNode;

use super::state::{LayoutMode, Listed, ListingFor, View, Visit};
use super::{App, quick_list_with_settings};
use crate::ui::{TreeState, TreeView};

impl App {
    /// Go into `dir`, recording the current place so going back returns to
    /// it. Selects `select` there; without one, entering a place that was
    /// left by going back resumes it, and anywhere else starts at the top.
    pub(super) fn enter(&mut self, dir: PathBuf, select: Option<PathBuf>) {
        let origin = self.current_visit();
        self.enter_from(origin, dir, select);
    }

    /// Go to `dir` from anywhere: a bookmark, a typed path or a search.
    /// A directory outside the current tree is listed, and scanned when
    /// scanning on startup is enabled, replacing the tree.
    pub(super) fn go_to(&mut self, dir: PathBuf) {
        let origin = self.current_visit();
        if let Some(dir) = self.reach(&dir, true) {
            self.enter_from(origin, dir, None);
        }
    }

    fn enter_from(&mut self, origin: Visit, dir: PathBuf, select: Option<PathBuf>) {
        if dir == origin.dir {
            if select.is_some() {
                self.place(dir, select);
            }
            return;
        }
        self.load_children(&dir);
        let resumed = match self.nav.forward.iter().rposition(|visit| visit.dir == dir) {
            Some(index) => {
                let visit = self.nav.forward.remove(index);
                self.nav.forward.truncate(index);
                Some(visit)
            }
            None => {
                self.nav.forward.clear();
                None
            }
        };
        self.nav.back.push(origin);
        let select = select.or_else(|| {
            let visit = resumed?;
            self.nav.tree_state.expanded = visit.expanded;
            visit.selected
        });
        self.place(dir, select);
    }

    /// Return to the previous place. With no history, go up to the parent
    /// directory, beyond the scan root if need be, selecting the directory
    /// just left.
    pub(super) fn go_back(&mut self) {
        let current = self.current_visit();
        if let Some(visit) = self.nav.back.last().cloned() {
            let Some(dir) = self.reach(&visit.dir, false) else {
                return;
            };
            self.nav.back.pop();
            self.nav.forward.push(current);
            self.nav.tree_state.expanded = visit.expanded;
            self.place(dir, visit.selected);
            return;
        }
        let Some(parent) = self.nav.view_root.parent().map(Path::to_path_buf) else {
            return;
        };
        let left = self.nav.view_root.clone();
        let Some(parent) = self.reach(&parent, false) else {
            return;
        };
        self.nav.tree_state.collapse(&left);
        self.nav.forward.push(current);
        self.place(parent, Some(left));
    }

    /// Return to the scan root, forgetting the history.
    pub(super) fn go_to_root(&mut self) {
        self.nav.back.clear();
        self.nav.forward.clear();
        self.place(self.path.clone(), None);
    }

    /// Enter the selected directory in the active layout or the treemap.
    pub(super) fn enter_selected(&mut self) {
        let Some(path) = self.get_selected_path() else {
            return;
        };
        let is_dir = self
            .tree
            .as_ref()
            .and_then(|tree| tree.find(&path))
            .is_some_and(FileNode::is_dir);
        if !is_dir {
            return;
        }
        if path == self.nav.view_root {
            self.nav.tree_state.expand(&path);
            self.update_cached_tree_len();
            return;
        }
        self.enter(path, None);
    }

    /// Go to a search result: into a directory, or to a file's directory
    /// with the file selected.
    pub(super) fn navigate_to_search_result(&mut self, target: &Path) {
        let is_dir = self
            .tree
            .as_ref()
            .and_then(|tree| tree.find(target))
            .is_some_and(FileNode::is_dir);
        if is_dir {
            self.enter(target.to_path_buf(), None);
        } else if let Some(parent) = target.parent() {
            self.enter(parent.to_path_buf(), Some(target.to_path_buf()));
        }
    }

    /// Switch between the tree and Miller layouts, keeping the selected
    /// entry selected. Miller lists one directory's children, so an entry
    /// deeper in the tree moves the view to its directory.
    pub(super) fn toggle_layout(&mut self) {
        let selected = self.get_selected_path();
        self.layout_mode = self.layout_mode.toggle();
        if self.layout_mode == LayoutMode::Miller
            && let Some(parent) = selected.as_deref().and_then(Path::parent)
            && parent != self.nav.view_root
            && parent.starts_with(&self.nav.view_root)
        {
            self.nav.view_root = parent.to_path_buf();
        }
        self.select_entry(selected.as_deref());
        self.view_changed();
    }

    /// Bring everything up to date after another tab became active. A tab
    /// whose directory has since been removed moves to its nearest surviving
    /// ancestor; one outside the current scan has its directory listed and
    /// scanned, as going there would.
    pub(super) fn after_tab_switch(&mut self) {
        let outside_scan = self
            .tree
            .as_ref()
            .is_some_and(|tree| !self.nav.view_root.starts_with(&tree.root_path));
        if outside_scan {
            let dir = self.nav.view_root.clone();
            if let Some(dir) = self.reach(&dir, true) {
                self.place(dir, None);
            }
        } else {
            self.leave_vanished_view();
        }
        self.view_changed();
    }

    /// Move off a viewed directory that no longer exists in the tree, to its
    /// nearest surviving ancestor.
    pub(super) fn leave_vanished_view(&mut self) {
        let Some(tree) = &self.tree else {
            return;
        };
        let mut view = self.nav.view_root.clone();
        while !tree.find(&view).is_some_and(FileNode::is_dir) {
            if !view.pop() || !view.starts_with(&tree.root_path) {
                view = tree.root_path.clone();
                break;
            }
        }
        if view != self.nav.view_root {
            self.place(view, None);
        }
    }

    /// Show `dir` without touching the history, selecting `select` there or
    /// the first entry.
    pub(super) fn place(&mut self, dir: PathBuf, select: Option<PathBuf>) {
        self.nav.view_root = dir;
        self.nav.tree_state.expand(&self.nav.view_root);
        self.select_entry(select.as_deref());
        self.view_changed();
    }

    /// The current place, to come back to.
    fn current_visit(&self) -> Visit {
        let selected = matches!(self.view, View::Explorer | View::Treemap)
            .then(|| self.get_selected_path())
            .flatten();
        Visit {
            dir: self.nav.view_root.clone(),
            selected,
            expanded: self.nav.tree_state.expanded.clone(),
        }
    }

    /// Select `target` in every layout, or the first entry where it is not
    /// shown. The tree selects its row; Miller and the treemap select the
    /// viewed directory's child that is or contains it.
    pub(super) fn select_entry(&mut self, target: Option<&Path>) {
        let (row, child, area) = match (self.get_view_root_node(), target) {
            (Some((node, root)), Some(target)) => {
                let row = TreeView::new(node, &root, &self.theme, &self.marked, &self.clipboard)
                    .position(&self.nav.tree_state, target);
                let name = target
                    .strip_prefix(&root)
                    .ok()
                    .and_then(|rest| rest.components().next())
                    .map(|component| component.as_os_str());
                let child = name.and_then(|name| {
                    node.children
                        .iter()
                        .position(|child| child.file_name() == name)
                });
                let area = name.and_then(|name| {
                    Self::treemap_order(node)
                        .iter()
                        .position(|child| child.file_name() == name)
                });
                (row, child, area)
            }
            _ => (None, None, None),
        };
        self.nav.tree_state.selected = row.unwrap_or(0);
        self.nav.tree_state.offset = 0;
        self.nav.miller_state.selected = child.unwrap_or(0);
        self.nav.miller_state.offset = 0;
        self.nav.treemap_state.selected = area.unwrap_or(0);
    }

    /// Bring what depends on the view up to date: row counts, selections
    /// kept in range and on screen, the Miller parent column and the preview.
    pub(super) fn view_changed(&mut self) {
        self.nav.tree_state.expand(&self.nav.view_root);
        self.update_cached_tree_len();
        self.update_cached_miller_len();
        self.update_cached_treemap_len();
        let clamp =
            |selected: &mut usize, len: usize| *selected = (*selected).min(len.saturating_sub(1));
        clamp(&mut self.nav.tree_state.selected, self.cached_tree_len);
        clamp(&mut self.nav.miller_state.selected, self.cached_miller_len);
        clamp(
            &mut self.nav.treemap_state.selected,
            self.cached_treemap_len,
        );
        self.nav.tree_state.ensure_visible(self.viewport_height);
        self.nav.miller_state.ensure_visible(self.viewport_height);
        if self.layout_mode == LayoutMode::Miller {
            self.update_cached_parent();
        }
        self.update_preview();
        self.needs_redraw = true;
    }

    /// The treemap's order: largest first, then by name.
    pub(super) fn treemap_order(node: &FileNode) -> Vec<&FileNode> {
        let mut children: Vec<&FileNode> = node.children.iter().collect();
        children.sort_by(|a, b| b.size.cmp(&a.size).then_with(|| a.compare_name(b)));
        children
    }

    /// Load `dir`'s entries if the tree has it without them, as a quick
    /// listing does for directories it has not opened yet.
    pub(super) fn load_children(&mut self, dir: &Path) {
        // A snapshot is the whole record; the filesystem is not consulted.
        if self.snapshot.is_some() {
            return;
        }
        let unloaded = self
            .tree
            .as_ref()
            .and_then(|tree| tree.find(dir))
            .is_some_and(|node| node.is_dir() && node.children.is_empty());
        if unloaded {
            self.lazy_load_directory(dir);
        }
    }

    /// Make sure `dir` is in the tree. When it is not, the tree is replaced
    /// with a listing of `dir`, keeping a finished scan so its sizes show
    /// when coming back, and `scan` starts scanning it if scanning on
    /// startup is enabled. Returns the directory to view, or `None` when it
    /// cannot be listed, in which case nothing changes.
    fn reach(&mut self, dir: &Path, scan: bool) -> Option<PathBuf> {
        let loaded = self
            .tree
            .as_ref()
            .and_then(|tree| tree.find(dir))
            .is_some_and(FileNode::is_dir);
        if loaded {
            return Some(dir.to_path_buf());
        }
        if self.snapshot.is_some() {
            self.operation_message = Some((
                false,
                format!(
                    "{} is outside the snapshot",
                    gravityfile_core::escape_filename(dir.as_os_str())
                ),
            ));
            return None;
        }
        let mut listing = self.load_directory_listing(dir)?;
        if let Some(tree) = &self.tree
            && self.has_full_scan
            && let Some(cache) = &mut self.scanned_cache
        {
            cache.insert(tree.root_path.clone(), Arc::clone(tree));
        }
        let root = listing.root_path.clone();
        self.merge_cached_scans_into_tree(&mut listing.root, &root);
        self.cancel_scan();
        self.path = root.clone();
        self.install_tree(listing);
        self.nav.tree_state = TreeState::new(root.clone());
        self.duplicates = None;
        self.age_report = None;
        self.has_full_scan = false;
        if scan && self.scan_on_startup {
            self.start_scan();
        }
        Some(root)
    }

    /// Lazy load a directory's children using quick_list.
    /// This updates the tree in-place with the new children.
    pub(super) fn lazy_load_directory(&mut self, path: &Path) {
        // Captured up front: the mutable borrow taken by `Arc::make_mut` below
        // rules out reading the tree's own root path at the same time.
        let Some(root_path) = self.tree.as_ref().map(|tree| tree.root_path.clone()) else {
            return;
        };
        let sort_mode = self.sort_mode;

        // First check if we have cached scan data for this directory
        if let Some(cached_tree) = self
            .scanned_cache
            .as_ref()
            .and_then(|c| c.get(path))
            .cloned()
        {
            // Use cached scan data - it has full size/structure information
            if let Some(tree) = &mut self.tree
                && let Some(node) =
                    Self::find_node_mut(&mut Arc::make_mut(tree).root, path, &root_path)
            {
                // Replace with the cached node (preserves all scan data)
                *node = cached_tree.root.clone();
                Self::order_new_subtree(node, sort_mode);
            }
            return;
        }

        // List it off the UI thread; a slow disk or network mount must not
        // freeze the explorer. A full scan replaces this with accurate data.
        self.request_listing(path.to_path_buf(), ListingFor::Children);
    }

    /// List `dir` on the blocking pool, unless that listing is already
    /// running; the result arrives through the run loop.
    pub(super) fn request_listing(&mut self, dir: PathBuf, purpose: ListingFor) {
        if !self.listings.insert((purpose, dir.clone())) {
            return;
        }
        let settings = self.user_settings.clone();
        let apparent = self.apparent_size;
        let list = move || {
            let result = quick_list_with_settings(&dir, &settings, apparent)
                .map_err(|error| format!("Cannot list {}: {error}", dir.display()));
            Listed {
                dir,
                purpose,
                result,
            }
        };
        // Without a runtime there is no loop to report to; list in place.
        let Ok(runtime) = tokio::runtime::Handle::try_current() else {
            self.finish_listing(list());
            return;
        };
        let sender = self.listing_tx.clone();
        runtime.spawn_blocking(move || {
            let _ = sender.send(list());
        });
    }

    /// Put a finished listing in place if it still applies: the directory is
    /// still unread, or the scan root still has that parent. A scan that
    /// finished meanwhile has newer data, so a stale listing is dropped.
    pub(super) fn finish_listing(&mut self, listed: Listed) {
        self.listings.remove(&(listed.purpose, listed.dir.clone()));
        let mut listing = match listed.result {
            Ok(listing) => listing,
            Err(error) => {
                if listed.purpose == ListingFor::Children {
                    self.error = Some(error);
                }
                return;
            }
        };
        match listed.purpose {
            ListingFor::Children => {
                let sort_mode = self.sort_mode;
                let selected = self.get_selected_path();
                let Some(tree) = &mut self.tree else {
                    return;
                };
                let root_path = tree.root_path.clone();
                let tree = Arc::make_mut(tree);
                let Some(node) = Self::find_node_mut(&mut tree.root, &listed.dir, &root_path)
                else {
                    return;
                };
                if !node.is_dir() || !node.children.is_empty() {
                    return;
                }
                node.children = std::mem::take(&mut listing.root.children);
                Self::order_new_subtree(node, sort_mode);
                // New rows can shift the selection; keep the same entry.
                self.select_entry(selected.as_deref());
                self.view_changed();
            }
            ListingFor::Parent => {
                let current = self.tree.as_ref().is_some_and(|tree| {
                    self.nav.view_root == tree.root_path
                        && tree.root_path.parent() == Some(listed.dir.as_path())
                });
                if current {
                    self.merge_cached_scans_into_tree(&mut listing.root, &listed.dir);
                    self.cached_parent_tree = Some(listing);
                    self.needs_redraw = true;
                }
            }
        }
    }

    pub(super) fn load_directory_listing(
        &mut self,
        path: &Path,
    ) -> Option<gravityfile_core::FileTree> {
        match quick_list_with_settings(path, &self.user_settings, self.apparent_size) {
            Ok(tree) => Some(tree),
            Err(error) => {
                self.error = Some(format!("Cannot list {}: {error}", path.display()));
                self.needs_redraw = true;
                None
            }
        }
    }

    /// Find a mutable reference to a node by path.
    pub(super) fn find_node_mut<'a>(
        node: &'a mut gravityfile_core::FileNode,
        target: &Path,
        current_path: &Path,
    ) -> Option<&'a mut gravityfile_core::FileNode> {
        let relative = target.strip_prefix(current_path).ok()?;
        let mut found = node;
        for component in relative.components() {
            let std::path::Component::Normal(name) = component else {
                return None;
            };
            found = found
                .children
                .iter_mut()
                .find(|child| child.file_name() == name)?;
        }
        Some(found)
    }

    /// Merge cached scan data into a tree node's children.
    /// For each child directory in the node, if we have cached scan data,
    /// replace the placeholder child with the fully scanned version.
    pub(super) fn merge_cached_scans_into_tree(
        &self,
        node: &mut gravityfile_core::FileNode,
        node_path: &Path,
    ) {
        for child in &mut node.children {
            if child.is_dir() {
                let child_path = node_path.join(child.file_name());
                if let Some(cached_tree) =
                    self.scanned_cache.as_ref().and_then(|c| c.get(&child_path))
                {
                    // Replace this child with the cached scan data
                    *child = cached_tree.root.clone();
                }
            }
        }
    }

    /// Update the cached parent tree for Miller columns display.
    /// Called when navigating to ensure the parent column can be shown.
    pub(super) fn update_cached_parent(&mut self) {
        // Only the scan root needs a parent from outside the tree, and a
        // snapshot has nothing outside it.
        let parent = self
            .tree
            .as_ref()
            .filter(|tree| self.snapshot.is_none() && self.nav.view_root == tree.root_path)
            .and_then(|tree| tree.root_path.parent())
            .map(Path::to_path_buf);
        let Some(parent) = parent else {
            self.cached_parent_tree = None;
            return;
        };
        // Keep showing the same parent while it is listed again, but never a
        // previous location's.
        if !self
            .cached_parent_tree
            .as_ref()
            .is_some_and(|cached| cached.root_path == parent)
        {
            self.cached_parent_tree = None;
        }
        self.request_listing(parent, ListingFor::Parent);
    }

    /// Change directory within the loaded scan tree. Leading `/` is scan-root relative.
    pub(super) fn cd_to_path(&mut self, target: &str) {
        if target == "/" || target == "~" {
            self.go_to_root();
            return;
        }
        if target == ".." {
            self.go_back();
            return;
        }
        let Some(tree) = &self.tree else {
            self.error = Some("Cannot change directory: no directory tree is loaded".into());
            return;
        };
        let mut target_path = if target.starts_with('/') {
            self.path.clone()
        } else {
            self.nav.view_root.clone()
        };
        for component in Path::new(target.trim_start_matches('/')).components() {
            match component {
                std::path::Component::CurDir => {}
                std::path::Component::Normal(name) => target_path.push(name),
                std::path::Component::ParentDir => {
                    if target_path == self.path || !target_path.starts_with(&self.path) {
                        self.error =
                            Some("Cannot change directory: path leaves the scan root".into());
                        return;
                    }
                    target_path.pop();
                }
                _ => {
                    self.error = Some("Cannot change directory: unsupported path prefix".into());
                    return;
                }
            }
        }
        let Some(node) = Self::find_node_at_path(&tree.root, &target_path, &tree.root_path) else {
            self.error = Some(format!(
                "Directory is not in the loaded tree: {}",
                target_path.display()
            ));
            return;
        };
        if !node.is_dir() {
            self.error = Some(format!("Not a directory: {}", target_path.display()));
            return;
        }
        self.enter(target_path, None);
    }
}