Skip to content

Commit

Permalink
Remove duplicates from history completer
Browse files Browse the repository at this point in the history
  • Loading branch information
saep committed Feb 1, 2024
1 parent 9f0095f commit 1810d9d
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/completion/history.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::Deref;
use std::{collections::HashSet, ops::Deref};

use crate::{
history::SearchQuery, menu_functions::parse_selection_char, Completer, History, Span,
Expand All @@ -25,8 +25,10 @@ impl<'menu> Completer for HistoryCompleter<'menu> {
))
.expect("todo: error handling");

let mut seen_command_lines = HashSet::new();
values
.into_iter()
.filter(|value| seen_command_lines.insert(value.command_line.clone()))
.map(|value| self.create_suggestion(line, pos, value.command_line.deref()))
.collect()
}
Expand Down Expand Up @@ -66,3 +68,48 @@ impl<'menu> HistoryCompleter<'menu> {
}
}
}

#[cfg(test)]
mod tests {
use rstest::rstest;

use super::*;
use crate::*;

fn new_history_item(command_line: &str) -> HistoryItem {
HistoryItem {
id: None,
start_timestamp: None,
command_line: command_line.to_string(),
session_id: None,
hostname: None,
cwd: None,
duration: None,
exit_status: None,
more_info: None,
}
}

#[rstest]
#[case(vec![], "any", vec![])]
#[case(vec!["old match","recent match","between","recent match"], "match", vec!["recent match","old match"])]
#[case(vec!["a","b","c","a","b","c"], "", vec!["c","b","a"])]
fn complete_doesnt_return_duplicates(
#[case] history_items: Vec<&str>,
#[case] line: &str,
#[case] expected: Vec<&str>,
) -> Result<()> {
let mut history = FileBackedHistory::new(history_items.len())?;
for history_item in history_items {
history.save(new_history_item(history_item))?;
}
let mut sut = HistoryCompleter::new(&history);
let actual: Vec<String> = sut
.complete(line, line.len())
.into_iter()
.map(|suggestion| suggestion.value)
.collect();
assert_eq!(actual, expected);
Ok(())
}
}

0 comments on commit 1810d9d

Please sign in to comment.