Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle PS2 prompts that require prompt-expansion #239

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions brush-core/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,11 +685,31 @@ impl Shell {

/// Composes the shell's prompt, applying all appropriate expansions.
pub async fn compose_prompt(&mut self) -> Result<String, error::Error> {
self.prompt_from_var_or_default("PS1", self.default_prompt())
.await
}

/// Compose's the shell's alternate-side prompt, applying all appropriate expansions.
#[allow(clippy::unused_async)]
pub async fn compose_alt_side_prompt(&mut self) -> Result<String, error::Error> {
Ok(String::new())
}

/// Composes the shell's continuation prompt.
pub async fn compose_continuation_prompt(&mut self) -> Result<String, error::Error> {
self.prompt_from_var_or_default("PS2", "> ").await
}

async fn prompt_from_var_or_default(
&mut self,
var_name: &str,
default: &str,
) -> Result<String, error::Error> {
// Retrieve the spec.
let ps1 = self.parameter_or_default("PS1", self.default_prompt());
let prompt_spec = self.parameter_or_default(var_name, default);

// Expand it.
let formatted_prompt = prompt::expand_prompt(self, ps1.as_ref())?;
let formatted_prompt = prompt::expand_prompt(self, prompt_spec.as_ref())?;

// NOTE: We're having difficulty with xterm escape sequences going through rustyline;
// so we strip them here.
Expand All @@ -703,18 +723,6 @@ impl Shell {
Ok(formatted_prompt)
}

/// Compose's the shell's alternate-side prompt, applying all appropriate expansions.
#[allow(clippy::unused_async)]
pub async fn compose_alt_side_prompt(&mut self) -> Result<String, error::Error> {
Ok(String::new())
}

/// Retrieve's the shell's continuation prompt.
pub fn continuation_prompt(&self) -> Result<String, error::Error> {
// TODO: Evaluate expansion?
Ok(self.parameter_or_default("PS2", "> "))
}

/// Returns the exit status of the last command executed in this shell.
pub fn last_result(&self) -> u8 {
self.last_exit_status
Expand Down
2 changes: 1 addition & 1 deletion brush-interactive/src/interactive_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub trait InteractiveShell {
let prompt = InteractivePrompt {
prompt: shell_mut.as_mut().compose_prompt().await?,
alt_side_prompt: shell_mut.as_mut().compose_alt_side_prompt().await?,
continuation_prompt: shell_mut.as_mut().continuation_prompt()?,
continuation_prompt: shell_mut.as_mut().compose_continuation_prompt().await?,
};

drop(shell_mut);
Expand Down
Loading