From 0a0be19bed94f960b8b427e0bda75ef739cbb4cc Mon Sep 17 00:00:00 2001 From: Pirmin Kalberer Date: Wed, 18 Sep 2019 18:27:53 +0200 Subject: [PATCH] Rename histsearch to fuzzysearch --- src/cli.rs | 13 ++++++------- src/{histsearch.rs => fuzzysearch.rs} | 11 +++++------ src/lib.rs | 2 +- 3 files changed, 12 insertions(+), 14 deletions(-) rename src/{histsearch.rs => fuzzysearch.rs} (93%) diff --git a/src/cli.rs b/src/cli.rs index 5cfcb1475a..36ef87dcb7 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -9,8 +9,8 @@ use crate::commands::whole_stream_command; use crate::context::Context; use crate::data::Value; pub(crate) use crate::errors::ShellError; +use crate::fuzzysearch::{interactive_fuzzy_search, SelectionResult}; use crate::git::current_branch; -use crate::histsearch; use crate::parser::registry::Signature; use crate::parser::{hir, CallNode, Pipeline, PipelineElement, TokenNode}; use crate::prelude::*; @@ -354,19 +354,18 @@ pub async fn cli() -> Result<(), Box> { readline = rl.readline_with_initial(prompt, (&cmd, "")); if let Err(ReadlineError::Eof) = &readline { // Fuzzy search in history - let hist = std::fs::read_to_string("history.txt").expect("Cannot open history.txt"); - let lines = hist.lines().rev().collect(); - let selection = histsearch::select_from_list(&lines); // Clears last line with prompt + let lines = rl.history().iter().rev().map(|s| s.as_str()).collect(); + let selection = interactive_fuzzy_search(&lines, 5); // Clears last line with prompt match selection { - histsearch::SelectionResult::Selected(line) => { + SelectionResult::Selected(line) => { println!("{}{}", &prompt, &line); // TODO: colorize prompt readline = Ok(line.clone()); initial_command = None; } - histsearch::SelectionResult::Edit(line) => { + SelectionResult::Edit(line) => { initial_command = Some(line); } - histsearch::SelectionResult::NoSelection => { + SelectionResult::NoSelection => { readline = Ok("".to_string()); initial_command = None; } diff --git a/src/histsearch.rs b/src/fuzzysearch.rs similarity index 93% rename from src/histsearch.rs rename to src/fuzzysearch.rs index 25e1fb7196..5a253328e4 100644 --- a/src/histsearch.rs +++ b/src/fuzzysearch.rs @@ -9,8 +9,7 @@ pub enum SelectionResult { NoSelection, } -pub fn select_from_list(lines: &Vec<&str>) -> SelectionResult { - const MAX_RESULTS: usize = 5; +pub fn interactive_fuzzy_search(lines: &Vec<&str>, max_results: usize) -> SelectionResult { #[derive(PartialEq)] enum State { Selecting, @@ -30,7 +29,7 @@ pub fn select_from_list(lines: &Vec<&str>) -> SelectionResult { let mut sync_stdin = input.read_sync(); while state == State::Selecting { - let search_result = search(&searchinput, &lines, MAX_RESULTS); + let search_result = fuzzy_search(&searchinput, &lines, max_results); let selected_lines: Vec<&str> = search_result .iter() .map(|item| &item.highlighted_text as &str) @@ -96,8 +95,8 @@ struct Match { text_idx: usize, } -fn search(input: &String, lines: &Vec<&str>, max_results: usize) -> Vec { - if input.is_empty() { +fn fuzzy_search(searchstr: &str, lines: &Vec<&str>, max_results: usize) -> Vec { + if searchstr.is_empty() { return lines .iter() .take(max_results) @@ -112,7 +111,7 @@ fn search(input: &String, lines: &Vec<&str>, max_results: usize) -> Vec { let mut matches = lines .iter() .enumerate() - .map(|(idx, line)| (idx, best_match(&input, line))) + .map(|(idx, line)| (idx, best_match(&searchstr, line))) .filter(|(_i, m)| m.is_some()) .map(|(i, m)| (i, m.unwrap())) .collect::>(); diff --git a/src/lib.rs b/src/lib.rs index 9802e2f686..f4ccb4e4e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,8 +12,8 @@ mod env; mod errors; mod evaluate; mod format; +mod fuzzysearch; mod git; -mod histsearch; mod parser; mod plugin; mod shell;