Rename histsearch to fuzzysearch

This commit is contained in:
Pirmin Kalberer 2019-09-18 18:27:53 +02:00
parent 1c95bf05dc
commit 0a0be19bed
3 changed files with 12 additions and 14 deletions

View file

@ -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<dyn Error>> {
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;
}

View file

@ -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<Match> {
if input.is_empty() {
fn fuzzy_search(searchstr: &str, lines: &Vec<&str>, max_results: usize) -> Vec<Match> {
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<Match> {
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::<Vec<_>>();

View file

@ -12,8 +12,8 @@ mod env;
mod errors;
mod evaluate;
mod format;
mod fuzzysearch;
mod git;
mod histsearch;
mod parser;
mod plugin;
mod shell;