Skip to content

Commit

Permalink
Add PWD to the Reedline state (#796)
Browse files Browse the repository at this point in the history
* Add PWD as part of the state

* Allows cwd to be set to None
  • Loading branch information
YizhePKU authored Jul 5, 2024
1 parent a580ea5 commit bde962b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
24 changes: 24 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

// Engine Menus
menus: Vec<ReedlineMenu>,

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -360,6 +365,13 @@ impl Reedline {
self
}

/// Update current working directory.
#[must_use]
pub fn with_cwd(mut self, cwd: Option<String>) -> Self {
self.cwd = cwd;
self
}

/// A builder that configures the highlighter for your instance of the Reedline engine
/// # Example
/// ```rust
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/hinter/cwd_aware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
1 change: 1 addition & 0 deletions src/hinter/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/hinter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 4 additions & 14 deletions src/history/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HistorySessionId>,
) -> 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`]
Expand Down

0 comments on commit bde962b

Please sign in to comment.