From 9d02d2ea37a75625da64d1e3f16cf6026d30ea8d Mon Sep 17 00:00:00 2001 From: qkzk Date: Tue, 20 Dec 2022 22:18:55 +0100 Subject: use common trait for indexed vectors. harmonize variable names --- src/visited.rs | 56 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 26 deletions(-) (limited to 'src/visited.rs') 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, + pub content: Vec, /// 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 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 { + 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 - } - } } -- cgit v1.2.3