Skip to content

Commit

Permalink
Flipped order of entries in VecDeque (more natural for import/export).
Browse files Browse the repository at this point in the history
  • Loading branch information
ollbx authored and zyansheep committed Dec 30, 2024
1 parent bd8c596 commit c0e0d23
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/history.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::VecDeque;

pub struct History {
// Note: old entries in front, new ones at the back.
pub entries: VecDeque<String>,
pub max_size: usize,
current_position: Option<usize>,
Expand All @@ -21,15 +22,15 @@ impl History {
// Reset offset to newest entry
self.current_position = None;
// Don't add entry if last entry was same, or line was empty.
if self.entries.front() == Some(&line) || line.is_empty() {
if self.entries.back() == Some(&line) || line.is_empty() {
return;
}
// Add entry to front of history
self.entries.push_front(line);
// Add entry to back of history
self.entries.push_back(line);
// Check if already have enough entries
if self.entries.len() > self.max_size {
// Remove oldest entry
self.entries.pop_back();
self.entries.pop_front();
}
}

Expand All @@ -41,25 +42,26 @@ impl History {
// Find next history that matches a given string from an index
pub fn search_next(&mut self, _current: &str) -> Option<&str> {
if let Some(index) = &mut self.current_position {
if *index < self.entries.len() - 1 {
*index += 1;
if *index > 0 {
*index -= 1;
}
Some(&self.entries[*index])
} else if !self.entries.is_empty() {
self.current_position = Some(0);
Some(&self.entries[0])
} else if let Some(last) = self.entries.back() {
self.current_position = Some(self.entries.len() - 1);
Some(last)
} else {
None
}
}

// Find previous history item that matches a given string from an index
pub fn search_previous(&mut self, _current: &str) -> Option<&str> {
if let Some(index) = &mut self.current_position {
if *index == 0 {
if *index == self.entries.len() - 1 {
self.current_position = None;
return Some("");
}
*index -= 1;
*index += 1;
Some(&self.entries[*index])
} else {
None
Expand Down

0 comments on commit c0e0d23

Please sign in to comment.