Skip to content

Commit

Permalink
add bashism !term to prefix search for last command beginning with …
Browse files Browse the repository at this point in the history
…`term`
  • Loading branch information
fdncred committed Mar 28, 2024
1 parent b7209b6 commit b12a807
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,8 @@ impl Reedline {
fn parse_bang_command(&mut self) -> Option<ReedlineEvent> {
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 != ' ' {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())),
});
Expand Down
21 changes: 21 additions & 0 deletions src/menu/menu_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -58,6 +62,7 @@ pub fn parse_selection_char(buffer: &str, marker: char) -> ParseResult {
index: None,
marker: None,
action: ParseAction::ForwardSearch,
prefix: None,
};
}

Expand All @@ -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")]
Expand All @@ -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 == '-' => {
Expand All @@ -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,
};
}
}
Expand All @@ -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()]),
}
}
_ => {}
Expand All @@ -135,6 +155,7 @@ pub fn parse_selection_char(buffer: &str, marker: char) -> ParseResult {
index: None,
marker: None,
action,
prefix: None,
}
}

Expand Down

0 comments on commit b12a807

Please sign in to comment.