diff options
author | qkzk <qu3nt1n@gmail.com> | 2022-12-20 22:18:55 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2022-12-20 22:18:55 +0100 |
commit | 9d02d2ea37a75625da64d1e3f16cf6026d30ea8d (patch) | |
tree | 0726a5580e393267f4a37c5f456f17c1be308724 /src/visited.rs | |
parent | dccc841dbc23fc09aaa67df04426b54a1b9c604d (diff) |
use common trait for indexed vectors. harmonize variable names
Diffstat (limited to 'src/visited.rs')
-rw-r--r-- | src/visited.rs | 56 |
1 files changed, 30 insertions, 26 deletions
diff --git a/src/visited.rs b/src/visited.rs index 1dbd251..7f8ea27 100644 --- a/src/visited.rs +++ b/src/visited.rs @@ -1,5 +1,7 @@ use std::path::PathBuf; +use crate::indexed_vector::IndexedVector; + /// A Vec of pathbuf of visited files. /// It's mostly used as a stack but we want to avoid multiple instances of the /// same path and visit in a certain order. @@ -8,7 +10,7 @@ use std::path::PathBuf; #[derive(Default, Clone)] pub struct History { /// The visited paths - pub visited: Vec<PathBuf>, + pub content: Vec<PathBuf>, /// The currently selected index. By default it's the last one. pub index: usize, } @@ -17,24 +19,41 @@ impl History { /// Add a new path in the stack, without duplicates, and select the last /// one. pub fn push(&mut self, path: &PathBuf) { - if !self.visited.contains(path) { - self.visited.push(path.to_owned()); + if !self.content.contains(path) { + self.content.push(path.to_owned()); + self.index = self.len() - 1 + } + } + + /// 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; + self.content.truncate(final_length); + if self.is_empty() { + self.index = 0 + } else { self.index = self.len() - 1 } } +} +impl IndexedVector<PathBuf> for History { /// True if nothing was visited. Shouldn't be the case... - pub fn is_empty(&self) -> bool { - self.visited.is_empty() + fn is_empty(&self) -> bool { + self.content.is_empty() } /// Number of visited paths. - pub fn len(&self) -> usize { - self.visited.len() + fn len(&self) -> usize { + self.content.len() } /// Select the next visited path. - pub fn next(&mut self) { + fn next(&mut self) { if self.is_empty() { self.index = 0 } else if self.index > 0 { @@ -45,7 +64,7 @@ impl History { } /// Select the previously visited path. - pub fn prev(&mut self) { + fn prev(&mut self) { if self.is_empty() { self.index = 0; } else { @@ -54,26 +73,11 @@ impl History { } /// Returns the currently selected visited path. - pub fn selected(&self) -> Option<PathBuf> { + fn selected(&self) -> Option<&PathBuf> { if self.index < self.len() { - Some(self.visited[self.index].clone()) + Some(&self.content[self.index]) } else { None } } - - /// 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; - self.visited.truncate(final_length); - if self.is_empty() { - self.index = 0 - } else { - self.index = self.len() - 1 - } - } } |