blob: c9b27bfe713f0df7cfc8b3c26fbfe35410363c3e (
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
64
65
66
67
68
69
70
71
72
73
|
use std::path::PathBuf;
#[derive(Clone, Debug)]
pub struct History {
pub visited: Vec<PathBuf>,
pub index: usize,
}
impl Default for History {
fn default() -> Self {
Self {
visited: vec![],
index: 0,
}
}
}
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 pop(&mut self) -> Option<PathBuf> {
self.visited.pop()
}
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) {
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
}
}
}
|