Skip to content
Snippets Groups Projects
Commit 8b14661e authored by gwenn's avatar gwenn
Browse files

Make History ignore space/dups configurable (#50)

parent f22d3c27
No related branches found
No related tags found
No related merge requests found
......@@ -15,7 +15,7 @@ static PROMPT: &'static str = ">> ";
fn main() {
let c = FilenameCompleter::new();
let mut rl = Editor::new();
let mut rl = Editor::new().history_ignore_space(true);
rl.set_completer(Some(&c));
if let Err(_) = rl.load_history("history.txt") {
println!("No previous history.");
......
......@@ -9,6 +9,8 @@ use super::Result;
pub struct History {
entries: VecDeque<String>,
max_len: usize,
ignore_space: bool,
ignore_dups: bool,
}
const DEFAULT_HISTORY_MAX_LEN: usize = 100;
......@@ -18,9 +20,19 @@ impl History {
History {
entries: VecDeque::new(),
max_len: DEFAULT_HISTORY_MAX_LEN,
ignore_space: true,
ignore_dups: true,
}
}
pub fn ignore_space(&mut self, yes: bool) {
self.ignore_space = yes;
}
pub fn ignore_dups(&mut self, yes: bool) {
self.ignore_dups = yes;
}
/// Return the history entry at position `index`, starting from 0.
pub fn get(&self, index: usize) -> Option<&String> {
self.entries.get(index)
......@@ -31,14 +43,15 @@ impl History {
if self.max_len == 0 {
return false;
}
if line.is_empty() || line.chars().next().map_or(true, |c| c.is_whitespace()) {
// ignorespace
if line.is_empty() ||
(self.ignore_space && line.chars().next().map_or(true, |c| c.is_whitespace())) {
return false;
}
if let Some(s) = self.entries.back() {
if s == line {
// ignoredups
return false;
if self.ignore_dups {
if let Some(s) = self.entries.back() {
if s == line {
return false;
}
}
}
if self.entries.len() == self.max_len {
......
......@@ -936,6 +936,16 @@ impl<'completer> Editor<'completer> {
}
}
pub fn history_ignore_space(mut self, yes: bool) -> Editor<'completer> {
self.history.ignore_space(yes);
self
}
pub fn history_ignore_dups(mut self, yes: bool) -> Editor<'completer> {
self.history.ignore_dups(yes);
self
}
/// Load the history from the specified file.
pub fn load_history<P: AsRef<Path> + ?Sized>(&mut self, path: &P) -> Result<()> {
self.history.load(path)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment