Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

history iterator #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions shrs_line/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ pub trait History {
fn add(&mut self, cmd: Self::HistoryItem);
/// Remove all history entries
fn clear(&mut self);
// fn iter(&self) -> impl Iterator<Item = Self::HistoryItem>;
/// Query for a history entry
fn search(&self, query: &str) -> Option<&Self::HistoryItem>;
/// Get number of history entries
fn len(&self) -> usize;
/// Get a history entry by index
fn get(&self, i: usize) -> Option<&Self::HistoryItem>;
fn iter(&self) -> HistoryIter<Self::HistoryItem>;
}

pub struct HistoryIter<'a, It>(Box<dyn Iterator<Item = &'a It>>);

impl<'a, It> Iterator for HistoryIter<'a, It> {
type Item = &'a It;

fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}

/// Default implementation of [History] that saves history in process memory
Expand All @@ -48,10 +58,6 @@ impl History for DefaultHistory {
self.hist.clear();
}

// fn iter(&self) -> impl Iterator<Item = Self::HistoryItem> {
// todo!()
// }

fn search(&self, _query: &str) -> Option<&Self::HistoryItem> {
todo!()
}
Expand All @@ -64,6 +70,10 @@ impl History for DefaultHistory {
fn get(&self, i: usize) -> Option<&Self::HistoryItem> {
self.hist.get(i)
}

fn iter(&self) -> HistoryIter<Self::HistoryItem> {
HistoryIter(Box::new(self.hist.iter()))
}
}

/// Store the history persistantly in a file on disk
Expand Down Expand Up @@ -124,10 +134,6 @@ impl History for FileBackedHistory {
self.flush().unwrap();
}

// fn iter(&self) -> impl Iterator<Item = Self::HistoryItem> {
// todo!()
// }

fn search(&self, _query: &str) -> Option<&Self::HistoryItem> {
todo!()
}
Expand All @@ -139,6 +145,10 @@ impl History for FileBackedHistory {
fn get(&self, i: usize) -> Option<&Self::HistoryItem> {
self.hist.get(i)
}

fn iter(&self) -> HistoryIter<Self::HistoryItem> {
HistoryIter(Box::new(self.hist.iter()))
}
}

fn parse_history_file(hist_file: PathBuf) -> Result<Vec<String>, FileBackedHistoryError> {
Expand Down