Skip to content

Commit

Permalink
Merge pull request #34 from ollbx/main
Browse files Browse the repository at this point in the history
Fix: ensure reset of history position after line entry
  • Loading branch information
zyansheep authored Nov 19, 2024
2 parents d589463 + ee591a2 commit 1c41e34
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ impl History {
pub async fn update(&mut self) {
// Receive a new line
if let Some(line) = self.receiver.next().await {
// 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() {
return;
}
// Add entry to front of history
self.entries.push_front(line);
// Reset offset to newest entry
self.current_position = None;
// Check if already have enough entries
if self.entries.len() > self.max_size {
// Remove oldest entry
Expand All @@ -45,6 +45,11 @@ impl History {
}
}

// Sets the history position back to the start.
pub fn reset_position(&mut self) {
self.current_position = None;
}

// 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 {
Expand Down
1 change: 1 addition & 0 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ impl LineState {
// Render new line from beginning
self.move_cursor(-100000)?;
self.clear_and_render(term)?;
self.history.reset_position();

// Return line
return Ok(Some(ReadlineEvent::Line(line)));
Expand Down

0 comments on commit 1c41e34

Please sign in to comment.