mirror of
https://github.com/nushell/nushell
synced 2024-12-27 05:23:11 +00:00
Rename histsearch to fuzzysearch
This commit is contained in:
parent
1c95bf05dc
commit
0a0be19bed
3 changed files with 12 additions and 14 deletions
13
src/cli.rs
13
src/cli.rs
|
@ -9,8 +9,8 @@ use crate::commands::whole_stream_command;
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::data::Value;
|
use crate::data::Value;
|
||||||
pub(crate) use crate::errors::ShellError;
|
pub(crate) use crate::errors::ShellError;
|
||||||
|
use crate::fuzzysearch::{interactive_fuzzy_search, SelectionResult};
|
||||||
use crate::git::current_branch;
|
use crate::git::current_branch;
|
||||||
use crate::histsearch;
|
|
||||||
use crate::parser::registry::Signature;
|
use crate::parser::registry::Signature;
|
||||||
use crate::parser::{hir, CallNode, Pipeline, PipelineElement, TokenNode};
|
use crate::parser::{hir, CallNode, Pipeline, PipelineElement, TokenNode};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
@ -354,19 +354,18 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
||||||
readline = rl.readline_with_initial(prompt, (&cmd, ""));
|
readline = rl.readline_with_initial(prompt, (&cmd, ""));
|
||||||
if let Err(ReadlineError::Eof) = &readline {
|
if let Err(ReadlineError::Eof) = &readline {
|
||||||
// Fuzzy search in history
|
// Fuzzy search in history
|
||||||
let hist = std::fs::read_to_string("history.txt").expect("Cannot open history.txt");
|
let lines = rl.history().iter().rev().map(|s| s.as_str()).collect();
|
||||||
let lines = hist.lines().rev().collect();
|
let selection = interactive_fuzzy_search(&lines, 5); // Clears last line with prompt
|
||||||
let selection = histsearch::select_from_list(&lines); // Clears last line with prompt
|
|
||||||
match selection {
|
match selection {
|
||||||
histsearch::SelectionResult::Selected(line) => {
|
SelectionResult::Selected(line) => {
|
||||||
println!("{}{}", &prompt, &line); // TODO: colorize prompt
|
println!("{}{}", &prompt, &line); // TODO: colorize prompt
|
||||||
readline = Ok(line.clone());
|
readline = Ok(line.clone());
|
||||||
initial_command = None;
|
initial_command = None;
|
||||||
}
|
}
|
||||||
histsearch::SelectionResult::Edit(line) => {
|
SelectionResult::Edit(line) => {
|
||||||
initial_command = Some(line);
|
initial_command = Some(line);
|
||||||
}
|
}
|
||||||
histsearch::SelectionResult::NoSelection => {
|
SelectionResult::NoSelection => {
|
||||||
readline = Ok("".to_string());
|
readline = Ok("".to_string());
|
||||||
initial_command = None;
|
initial_command = None;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,7 @@ pub enum SelectionResult {
|
||||||
NoSelection,
|
NoSelection,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_from_list(lines: &Vec<&str>) -> SelectionResult {
|
pub fn interactive_fuzzy_search(lines: &Vec<&str>, max_results: usize) -> SelectionResult {
|
||||||
const MAX_RESULTS: usize = 5;
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
enum State {
|
enum State {
|
||||||
Selecting,
|
Selecting,
|
||||||
|
@ -30,7 +29,7 @@ pub fn select_from_list(lines: &Vec<&str>) -> SelectionResult {
|
||||||
let mut sync_stdin = input.read_sync();
|
let mut sync_stdin = input.read_sync();
|
||||||
|
|
||||||
while state == State::Selecting {
|
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
|
let selected_lines: Vec<&str> = search_result
|
||||||
.iter()
|
.iter()
|
||||||
.map(|item| &item.highlighted_text as &str)
|
.map(|item| &item.highlighted_text as &str)
|
||||||
|
@ -96,8 +95,8 @@ struct Match {
|
||||||
text_idx: usize,
|
text_idx: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search(input: &String, lines: &Vec<&str>, max_results: usize) -> Vec<Match> {
|
fn fuzzy_search(searchstr: &str, lines: &Vec<&str>, max_results: usize) -> Vec<Match> {
|
||||||
if input.is_empty() {
|
if searchstr.is_empty() {
|
||||||
return lines
|
return lines
|
||||||
.iter()
|
.iter()
|
||||||
.take(max_results)
|
.take(max_results)
|
||||||
|
@ -112,7 +111,7 @@ fn search(input: &String, lines: &Vec<&str>, max_results: usize) -> Vec<Match> {
|
||||||
let mut matches = lines
|
let mut matches = lines
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(idx, line)| (idx, best_match(&input, line)))
|
.map(|(idx, line)| (idx, best_match(&searchstr, line)))
|
||||||
.filter(|(_i, m)| m.is_some())
|
.filter(|(_i, m)| m.is_some())
|
||||||
.map(|(i, m)| (i, m.unwrap()))
|
.map(|(i, m)| (i, m.unwrap()))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
|
@ -12,8 +12,8 @@ mod env;
|
||||||
mod errors;
|
mod errors;
|
||||||
mod evaluate;
|
mod evaluate;
|
||||||
mod format;
|
mod format;
|
||||||
|
mod fuzzysearch;
|
||||||
mod git;
|
mod git;
|
||||||
mod histsearch;
|
|
||||||
mod parser;
|
mod parser;
|
||||||
mod plugin;
|
mod plugin;
|
||||||
mod shell;
|
mod shell;
|
||||||
|
|
Loading…
Reference in a new issue