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
use std::path::{Path, PathBuf};
use crate::io::DrawMenu;
use crate::{impl_content, impl_selectable};
/// A stack of visited paths.
/// We save the last folder and the selected file every time a `PatchContent` is updated.
/// We also ensure not to save the same pair multiple times.
#[derive(Default, Clone)]
pub struct History {
pub content: Vec<PathBuf>,
pub index: usize,
}
impl History {
/// Creates a new history with the starting path.
pub fn new(start_dir: &std::path::Path) -> Self {
Self {
content: vec![start_dir.to_path_buf()],
index: 0,
}
}
/// Add a new path and a selected file in the stack, without duplicates, and select the last
/// one.
pub fn push(&mut self, file: &Path) {
if !self.content.contains(&file.to_path_buf()) {
self.content.push(file.to_owned());
self.index = self.len() - 1;
}
// TODO! Else ... ?
}
/// Drop the last visited paths from the stack, after the selected one.
/// Used to go back a few steps in time.
pub fn drop_queue(&mut self) {
if self.is_empty() {
return;
}
let final_length = self.len() - self.index + 1;
self.content.truncate(final_length);
if self.is_empty() {
self.index = 0;
} else {
self.index = self.len() - 1;
}
}
/// True iff the last element of the stack has the same
/// path as the one given.
/// Doesn't check the associated file.
/// false if the stack is empty.
#[must_use]
pub fn is_this_the_last(&self, path: &Path) -> bool {
if self.is_empty() {
return false;
}
self.content[self.len() - 1] == path
}
}
impl_content!(History, PathBuf);
impl DrawMenu<PathBuf> for History {}