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

Provide a basic default implementation of History #209

Merged
merged 6 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions examples/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ fn main() {

let mut history = MyHistory::default();

/// We can also use `Vec` or `VecDeque` directly for a simple infinite history.
// let mut history = Vec::new();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this line and update the comment above this line accordingly?

// let mut history = VecDeque::new();

loop {
if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("dialoguer")
Expand Down
12 changes: 12 additions & 0 deletions src/history.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::VecDeque;

/// Trait for history handling.
pub trait History<T> {
/// This is called with the current position that should
Expand All @@ -13,3 +15,13 @@ pub trait History<T> {
/// is implemented as a FIFO queue.
fn write(&mut self, val: &T);
}

impl<T: ToString> History<T> for VecDeque<String> {
fn read(&self, pos: usize) -> Option<String> {
self.get(pos).cloned()
}

fn write(&mut self, val: &T) {
self.push_front(val.to_string())
}
}