blob: 178bfe141a39c303ee59baaf3f62e9b1041cfcf7 (
plain)
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
|
use std::path::PathBuf;
#[derive(Default)]
pub struct History {
pub visited: Vec<PathBuf>,
pub index: usize,
}
impl History {
pub fn push(&mut self, path: &PathBuf) {
if !self.visited.contains(path) {
self.visited.push(path.to_owned());
self.index = self.len() - 1
}
}
pub fn is_empty(&self) -> bool {
self.visited.is_empty()
}
pub fn len(&self) -> usize {
self.visited.len()
}
pub fn next(&mut self) {
if self.is_empty() {
self.index = 0
} else if self.index > 0 {
self.index -= 1;
} else {
self.index = self.len() - 1
}
}
pub fn prev(&mut self) {
if self.is_empty() {
self.index = 0;
} else {
self.index = (self.index + 1) % self.len()
}
}
pub fn selected(&self) -> Option<PathBuf> {
if self.index < self.len() {
Some(self.visited[self.index].clone())
} else {
None
}
}
pub fn drop_queue(&mut self) {
if self.is_empty() {
return;
}
let final_length = self.len() - self.index;
self.visited.truncate(final_length);
if self.is_empty() {
self.index = 0
} else {
self.index = self.len() - 1
}
}
}
|