fm/modes/menu/
history.rs

1use std::path::{Path, PathBuf};
2
3use crate::io::DrawMenu;
4use crate::{impl_content, impl_selectable};
5
6/// A stack of visited paths.
7/// We save the last folder and the selected file every time a `PatchContent` is updated.
8/// We also ensure not to save the same pair multiple times.
9#[derive(Default, Clone)]
10pub struct History {
11    pub content: Vec<PathBuf>,
12    pub index: usize,
13}
14
15impl History {
16    /// Add a new path and a selected file in the stack, without duplicates, and select the last
17    /// one.
18    pub fn push(&mut self, file: &Path) {
19        if !self.content.contains(&file.to_path_buf()) {
20            self.content.push(file.to_owned());
21            self.index = self.len() - 1;
22        }
23        // TODO! Else ... ?
24    }
25
26    /// Drop the last visited paths from the stack, after the selected one.
27    /// Used to go back a few steps in time.
28    pub fn drop_queue(&mut self) {
29        if self.is_empty() {
30            return;
31        }
32        let final_length = self.len() - self.index + 1;
33        self.content.truncate(final_length);
34        if self.is_empty() {
35            self.index = 0;
36        } else {
37            self.index = self.len() - 1;
38        }
39    }
40
41    /// True iff the last element of the stack has the same
42    /// path as the one given.
43    /// Doesn't check the associated file.
44    /// false if the stack is empty.
45    #[must_use]
46    pub fn is_this_the_last(&self, path: &Path) -> bool {
47        if self.is_empty() {
48            return false;
49        }
50        self.content[self.len() - 1] == path
51    }
52}
53
54impl_selectable!(History);
55impl_content!(History, PathBuf);
56
57impl DrawMenu<PathBuf> for History {}