From bde962bbf3b8773b8b83f7455286b367e055af2a Mon Sep 17 00:00:00 2001 From: YizhePKU Date: Sat, 6 Jul 2024 04:47:44 +0800 Subject: [PATCH] Add PWD to the `Reedline` state (#796) * Add PWD as part of the state * Allows cwd to be set to None --- src/engine.rs | 24 ++++++++++++++++++++++++ src/hinter/cwd_aware.rs | 2 ++ src/hinter/default.rs | 1 + src/hinter/mod.rs | 1 + src/history/base.rs | 18 ++++-------------- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 6b323c1e..e4da6e6a 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -142,6 +142,10 @@ pub struct Reedline { // Use ansi coloring or not use_ansi_coloring: bool, + // Current working directory as defined by the application. If set, it will + // override the actual working directory of the process. + cwd: Option, + // Engine Menus menus: Vec, @@ -224,6 +228,7 @@ impl Reedline { hide_hints: false, validator, use_ansi_coloring: true, + cwd: None, menus: Vec::new(), buffer_editor: None, cursor_shapes: None, @@ -360,6 +365,13 @@ impl Reedline { self } + /// Update current working directory. + #[must_use] + pub fn with_cwd(mut self, cwd: Option) -> Self { + self.cwd = cwd; + self + } + /// A builder that configures the highlighter for your instance of the Reedline engine /// # Example /// ```rust @@ -1557,6 +1569,12 @@ impl Reedline { .history .search(SearchQuery::last_with_prefix_and_cwd( parsed.prefix.unwrap().to_string(), + self.cwd.clone().unwrap_or_else(|| { + std::env::current_dir() + .unwrap_or_default() + .to_string_lossy() + .to_string() + }), self.get_history_session_id(), )) .unwrap_or_else(|_| Vec::new()) @@ -1739,6 +1757,12 @@ impl Reedline { cursor_position_in_buffer, self.history.as_ref(), self.use_ansi_coloring, + &self.cwd.clone().unwrap_or_else(|| { + std::env::current_dir() + .unwrap_or_default() + .to_string_lossy() + .to_string() + }), ) }) } else { diff --git a/src/hinter/cwd_aware.rs b/src/hinter/cwd_aware.rs index 67ab8697..63a70322 100644 --- a/src/hinter/cwd_aware.rs +++ b/src/hinter/cwd_aware.rs @@ -22,11 +22,13 @@ impl Hinter for CwdAwareHinter { #[allow(unused_variables)] pos: usize, history: &dyn History, use_ansi_coloring: bool, + cwd: &str, ) -> String { self.current_hint = if line.chars().count() >= self.min_chars { let with_cwd = history .search(SearchQuery::last_with_prefix_and_cwd( line.to_string(), + cwd.to_string(), history.session(), )) .or_else(|err| { diff --git a/src/hinter/default.rs b/src/hinter/default.rs index 08ae57e8..c9bea6ef 100644 --- a/src/hinter/default.rs +++ b/src/hinter/default.rs @@ -15,6 +15,7 @@ impl Hinter for DefaultHinter { #[allow(unused_variables)] pos: usize, history: &dyn History, use_ansi_coloring: bool, + _cwd: &str, ) -> String { self.current_hint = if line.chars().count() >= self.min_chars { history diff --git a/src/hinter/mod.rs b/src/hinter/mod.rs index cf6f4701..fcd29b67 100644 --- a/src/hinter/mod.rs +++ b/src/hinter/mod.rs @@ -40,6 +40,7 @@ pub trait Hinter: Send { pos: usize, history: &dyn History, use_ansi_coloring: bool, + cwd: &str, ) -> String; /// Return the current hint unformatted to perform the completion of the full hint diff --git a/src/history/base.rs b/src/history/base.rs index a7c56f6e..93f7f456 100644 --- a/src/history/base.rs +++ b/src/history/base.rs @@ -146,24 +146,14 @@ impl SearchQuery { )) } - /// Get the most recent entry starting with the `prefix` and `cwd` same as the current cwd + /// Get the most recent entry starting with the `prefix` and `cwd` pub fn last_with_prefix_and_cwd( prefix: String, + cwd: String, session: Option, ) -> SearchQuery { - let cwd = std::env::current_dir(); - if let Ok(cwd) = cwd { - SearchQuery::last_with_search(SearchFilter::from_text_search_cwd( - cwd.to_string_lossy().to_string(), - CommandLineSearch::Prefix(prefix), - session, - )) - } else { - SearchQuery::last_with_search(SearchFilter::from_text_search( - CommandLineSearch::Prefix(prefix), - session, - )) - } + let prefix = CommandLineSearch::Prefix(prefix); + SearchQuery::last_with_search(SearchFilter::from_text_search_cwd(cwd, prefix, session)) } /// Query to get all entries in the given [`SearchDirection`]