From b12a8077cdf17890c506b5cc2dd37c444342175e Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Thu, 28 Mar 2024 09:34:52 -0500 Subject: [PATCH 1/6] add bashism `!term` to prefix search for last command beginning with `term` --- src/engine.rs | 26 ++++++++++++++++++++++++++ src/menu/menu_functions.rs | 21 +++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/engine.rs b/src/engine.rs index b26d3c1e..a3716ef2 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1495,6 +1495,8 @@ impl Reedline { fn parse_bang_command(&mut self) -> Option { let buffer = self.editor.get_buffer(); let parsed = parse_selection_char(buffer, '!'); + let parsed_prefix = parsed.prefix.unwrap_or_default().to_string(); + let parsed_marker = parsed.marker.unwrap_or_default().to_string(); if let Some(last) = parsed.remainder.chars().last() { if last != ' ' { @@ -1546,6 +1548,29 @@ impl Reedline { history.command_line.clone(), ) }), + ParseAction::BackwardPrefixSearch => { + self.history + .search( + // TODO: Allow this to work when using history isolation + // Not quite sure how to check that anymore + /*SearchQuery::last_with_prefix_and_cwd( + parsed.prefix.unwrap().to_string(), + self.get_history_session_id(),*/ + SearchQuery::last_with_prefix( + parsed_prefix.clone(), + self.get_history_session_id(), + ), + ) + .unwrap_or_else(|_| Vec::new()) + .get(index.saturating_sub(1)) + .map(|history| { + ( + parsed.remainder.len(), + parsed_prefix.len() + parsed_marker.len(), + history.command_line.clone(), + ) + }) + } ParseAction::ForwardSearch => self .history .search(SearchQuery { @@ -1573,6 +1598,7 @@ impl Reedline { ))) .unwrap_or_else(|_| Vec::new()) .first() + //BUGBUG: This returns the wrong results with paths with spaces in them .and_then(|history| history.command_line.split_whitespace().next_back()) .map(|token| (parsed.remainder.len(), indicator.len(), token.to_string())), }); diff --git a/src/menu/menu_functions.rs b/src/menu/menu_functions.rs index cc48d85a..f27249f8 100644 --- a/src/menu/menu_functions.rs +++ b/src/menu/menu_functions.rs @@ -17,6 +17,8 @@ pub struct ParseResult<'buffer> { pub marker: Option<&'buffer str>, /// Direction of the search based on the marker pub action: ParseAction, + /// Prefix to search for + pub prefix: Option<&'buffer str>, } /// Direction of the index found in the string @@ -30,6 +32,8 @@ pub enum ParseAction { LastToken, /// Last executed command. LastCommand, + /// Backward search for a prefix + BackwardPrefixSearch, } /// Splits a string that contains a marker character @@ -58,6 +62,7 @@ pub fn parse_selection_char(buffer: &str, marker: char) -> ParseResult { index: None, marker: None, action: ParseAction::ForwardSearch, + prefix: None, }; } @@ -75,6 +80,7 @@ pub fn parse_selection_char(buffer: &str, marker: char) -> ParseResult { index: Some(0), marker: Some(&buffer[index..index + 2 * marker.len_utf8()]), action: ParseAction::LastCommand, + prefix: None, } } #[cfg(feature = "bashisms")] @@ -84,6 +90,7 @@ pub fn parse_selection_char(buffer: &str, marker: char) -> ParseResult { index: Some(0), marker: Some(&buffer[index..index + 2]), action: ParseAction::LastToken, + prefix: None, } } Some(&x) if x.is_ascii_digit() || x == '-' => { @@ -106,6 +113,7 @@ pub fn parse_selection_char(buffer: &str, marker: char) -> ParseResult { index: Some(count), marker: Some(&buffer[index..index + size]), action, + prefix: None, }; } } @@ -114,14 +122,26 @@ pub fn parse_selection_char(buffer: &str, marker: char) -> ParseResult { index: Some(count), marker: Some(&buffer[index..index + size]), action, + prefix: None, }; } + #[cfg(feature = "bashisms")] + Some(&x) if x.is_ascii_alphabetic() => { + return ParseResult { + remainder: &buffer[0..index], + index: Some(0), + marker: Some(&buffer[index..index + marker.len_utf8()]), + action: ParseAction::BackwardPrefixSearch, + prefix: Some(&buffer[index + marker.len_utf8()..buffer.len()]), + } + } None => { return ParseResult { remainder: &buffer[0..index], index: Some(0), marker: Some(&buffer[index..buffer.len()]), action, + prefix: Some(&buffer[index..buffer.len()]), } } _ => {} @@ -135,6 +155,7 @@ pub fn parse_selection_char(buffer: &str, marker: char) -> ParseResult { index: None, marker: None, action, + prefix: None, } } From d0142c3af9c103b978afbea6890964d3a9cbd1f3 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Thu, 28 Mar 2024 10:14:52 -0500 Subject: [PATCH 2/6] missed doc comment --- src/menu/menu_functions.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/menu/menu_functions.rs b/src/menu/menu_functions.rs index f27249f8..5ce00818 100644 --- a/src/menu/menu_functions.rs +++ b/src/menu/menu_functions.rs @@ -51,6 +51,7 @@ pub enum ParseAction { /// index: Some(10), /// marker: Some("!10"), /// action: ParseAction::ForwardSearch +/// prefix: None, /// } /// ) /// From a8b9de389f576d103ab24b46738f273a49b9acbf Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Thu, 28 Mar 2024 10:18:43 -0500 Subject: [PATCH 3/6] one more try with doc comments --- src/menu/menu_functions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/menu/menu_functions.rs b/src/menu/menu_functions.rs index 5ce00818..bf9ded7b 100644 --- a/src/menu/menu_functions.rs +++ b/src/menu/menu_functions.rs @@ -50,7 +50,7 @@ pub enum ParseAction { /// remainder: "this is an example", /// index: Some(10), /// marker: Some("!10"), -/// action: ParseAction::ForwardSearch +/// action: ParseAction::ForwardSearch, /// prefix: None, /// } /// ) From fe54004dbbc51b14cc0d014d134443222b87e5b2 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:20:38 -0500 Subject: [PATCH 4/6] add ability to search session first, then globally --- src/engine.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index a3716ef2..044ac27d 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1549,18 +1549,12 @@ impl Reedline { ) }), ParseAction::BackwardPrefixSearch => { - self.history - .search( - // TODO: Allow this to work when using history isolation - // Not quite sure how to check that anymore - /*SearchQuery::last_with_prefix_and_cwd( + let history_search_by_session = self + .history + .search(SearchQuery::last_with_prefix_and_cwd( parsed.prefix.unwrap().to_string(), - self.get_history_session_id(),*/ - SearchQuery::last_with_prefix( - parsed_prefix.clone(), - self.get_history_session_id(), - ), - ) + self.get_history_session_id(), + )) .unwrap_or_else(|_| Vec::new()) .get(index.saturating_sub(1)) .map(|history| { @@ -1569,7 +1563,28 @@ impl Reedline { parsed_prefix.len() + parsed_marker.len(), history.command_line.clone(), ) - }) + }); + // If we don't find any history searching by session id, then let's + // search everything, otherwise use the result from the session search + if history_search_by_session.is_none() { + eprintln!("Using global search"); + self.history + .search(SearchQuery::last_with_prefix( + parsed_prefix.clone(), + self.get_history_session_id(), + )) + .unwrap_or_else(|_| Vec::new()) + .get(index.saturating_sub(1)) + .map(|history| { + ( + parsed.remainder.len(), + parsed_prefix.len() + parsed_marker.len(), + history.command_line.clone(), + ) + }) + } else { + history_search_by_session + } } ParseAction::ForwardSearch => self .history From 4901fc9cf967960d8f87711443a3e0202a7e1021 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:24:10 -0500 Subject: [PATCH 5/6] unrelates types for list_menu test --- .typos.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.typos.toml b/.typos.toml index a1802fae..feeba5a1 100644 --- a/.typos.toml +++ b/.typos.toml @@ -11,3 +11,9 @@ descriptio = "descriptio" ot = "ot" # for sqlite backed history wheres = "wheres" +# for list_menu tests +an1other = "an1other" +ver2y = "ver2y" +l3ine = "l3ine" +4should = "4should" +wr5ap = "wr5ap" From a219762eed7f84e44efc334fa2e343ee42968d79 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:27:52 -0500 Subject: [PATCH 6/6] missed on --- .typos.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/.typos.toml b/.typos.toml index feeba5a1..7a64bdaf 100644 --- a/.typos.toml +++ b/.typos.toml @@ -17,3 +17,4 @@ ver2y = "ver2y" l3ine = "l3ine" 4should = "4should" wr5ap = "wr5ap" +ine = "ine"