feat: set a continuation prompt for supporting shells (#3322)
* feat: set a continuation prompt for supporting shells (#3134) * docs: fixed wording of documentation * fix: continuation prompt is now only set once * fix(docs): fixed typo in advanced-config/README.md Co-authored-by: Segev Finer <segev208@gmail.com> * fix: update --continuation argument Co-authored-by: David Knaack <davidkna@users.noreply.github.com> * fix: updated continuation prompt - PROMPT2 was fixed to be set only once in zsh. - `continuation_symbol` and `continuation_format` were removed in place of a single variable; `continuation_prompt`. - The continuation prompt was moved out of the character module. * fix: ran rustfmt * docs: updated continuation prompt docs Co-authored-by: Segev Finer <segev208@gmail.com> Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
This commit is contained in:
parent
3f97068538
commit
4deaa02d6f
|
@ -141,6 +141,28 @@ Produces a prompt like the following:
|
||||||
▶ starship on rprompt [!] is 📦 v0.57.0 via 🦀 v1.54.0 took 17s
|
▶ starship on rprompt [!] is 📦 v0.57.0 via 🦀 v1.54.0 took 17s
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Continuation Prompt
|
||||||
|
|
||||||
|
Some shells support a continuation prompt along with the normal prompt. This prompt is rendered instead of the normal prompt when the user has entered an incomplete statement (such as a single left parenthesis or quote).
|
||||||
|
|
||||||
|
Starship can set the continuation prompt using the `continuation_prompt` option. The default prompt is `"[❯](bold yellow)"`.
|
||||||
|
|
||||||
|
Note: `continuation_prompt` should be set to a literal string without any variables.
|
||||||
|
|
||||||
|
Note: Continuation prompts are only available in the following shells:
|
||||||
|
|
||||||
|
- `bash`
|
||||||
|
- `zsh`
|
||||||
|
- `PowerShell`
|
||||||
|
|
||||||
|
### Example
|
||||||
|
|
||||||
|
```toml
|
||||||
|
# ~/.config/starship.toml
|
||||||
|
|
||||||
|
# A continuation prompt that displays two filled in arrows
|
||||||
|
continuation_prompt = "▶▶"
|
||||||
|
```
|
||||||
|
|
||||||
## Style Strings
|
## Style Strings
|
||||||
|
|
||||||
|
|
|
@ -80,6 +80,7 @@ pub struct FullConfig<'a> {
|
||||||
// Root config
|
// Root config
|
||||||
pub format: String,
|
pub format: String,
|
||||||
pub right_format: String,
|
pub right_format: String,
|
||||||
|
pub continuation_prompt: String,
|
||||||
pub scan_timeout: u64,
|
pub scan_timeout: u64,
|
||||||
pub command_timeout: u64,
|
pub command_timeout: u64,
|
||||||
pub add_newline: bool,
|
pub add_newline: bool,
|
||||||
|
@ -158,6 +159,7 @@ impl<'a> Default for FullConfig<'a> {
|
||||||
Self {
|
Self {
|
||||||
format: "$all".to_string(),
|
format: "$all".to_string(),
|
||||||
right_format: "".to_string(),
|
right_format: "".to_string(),
|
||||||
|
continuation_prompt: "[❯](bold yellow)".to_string(),
|
||||||
scan_timeout: 30,
|
scan_timeout: 30,
|
||||||
command_timeout: 500,
|
command_timeout: 500,
|
||||||
add_newline: true,
|
add_newline: true,
|
||||||
|
|
|
@ -8,6 +8,7 @@ use std::cmp::Ordering;
|
||||||
pub struct StarshipRootConfig {
|
pub struct StarshipRootConfig {
|
||||||
pub format: String,
|
pub format: String,
|
||||||
pub right_format: String,
|
pub right_format: String,
|
||||||
|
pub continuation_prompt: String,
|
||||||
pub scan_timeout: u64,
|
pub scan_timeout: u64,
|
||||||
pub command_timeout: u64,
|
pub command_timeout: u64,
|
||||||
pub add_newline: bool,
|
pub add_newline: bool,
|
||||||
|
@ -95,6 +96,7 @@ impl<'a> Default for StarshipRootConfig {
|
||||||
StarshipRootConfig {
|
StarshipRootConfig {
|
||||||
format: "$all".to_string(),
|
format: "$all".to_string(),
|
||||||
right_format: "".to_string(),
|
right_format: "".to_string(),
|
||||||
|
continuation_prompt: "[❯](bold yellow)".to_string(),
|
||||||
scan_timeout: 30,
|
scan_timeout: 30,
|
||||||
command_timeout: 500,
|
command_timeout: 500,
|
||||||
add_newline: true,
|
add_newline: true,
|
||||||
|
@ -108,6 +110,7 @@ impl<'a> ModuleConfig<'a> for StarshipRootConfig {
|
||||||
config.iter().for_each(|(k, v)| match k.as_str() {
|
config.iter().for_each(|(k, v)| match k.as_str() {
|
||||||
"format" => self.format.load_config(v),
|
"format" => self.format.load_config(v),
|
||||||
"right_format" => self.right_format.load_config(v),
|
"right_format" => self.right_format.load_config(v),
|
||||||
|
"continuation_prompt" => self.continuation_prompt.load_config(v),
|
||||||
"scan_timeout" => self.scan_timeout.load_config(v),
|
"scan_timeout" => self.scan_timeout.load_config(v),
|
||||||
"command_timeout" => self.command_timeout.load_config(v),
|
"command_timeout" => self.command_timeout.load_config(v),
|
||||||
"add_newline" => self.add_newline.load_config(v),
|
"add_newline" => self.add_newline.load_config(v),
|
||||||
|
@ -122,6 +125,7 @@ impl<'a> ModuleConfig<'a> for StarshipRootConfig {
|
||||||
// Root options
|
// Root options
|
||||||
"format",
|
"format",
|
||||||
"right_format",
|
"right_format",
|
||||||
|
"continuation_prompt",
|
||||||
"scan_timeout",
|
"scan_timeout",
|
||||||
"command_timeout",
|
"command_timeout",
|
||||||
"add_newline",
|
"add_newline",
|
||||||
|
|
|
@ -51,6 +51,9 @@ pub struct Context<'a> {
|
||||||
/// Construct the right prompt instead of the left prompt
|
/// Construct the right prompt instead of the left prompt
|
||||||
pub right: bool,
|
pub right: bool,
|
||||||
|
|
||||||
|
/// Construct the continuation prompt instead of the normal prompt
|
||||||
|
pub continuation: bool,
|
||||||
|
|
||||||
/// Width of terminal, or zero if width cannot be detected.
|
/// Width of terminal, or zero if width cannot be detected.
|
||||||
pub width: usize,
|
pub width: usize,
|
||||||
|
|
||||||
|
@ -134,6 +137,7 @@ impl<'a> Context<'a> {
|
||||||
.map_or_else(StarshipRootConfig::default, StarshipRootConfig::load);
|
.map_or_else(StarshipRootConfig::default, StarshipRootConfig::load);
|
||||||
|
|
||||||
let right = arguments.is_present("right");
|
let right = arguments.is_present("right");
|
||||||
|
let continuation = arguments.is_present("continuation");
|
||||||
|
|
||||||
let width = arguments
|
let width = arguments
|
||||||
.value_of("terminal_width")
|
.value_of("terminal_width")
|
||||||
|
@ -151,6 +155,7 @@ impl<'a> Context<'a> {
|
||||||
repo: OnceCell::new(),
|
repo: OnceCell::new(),
|
||||||
shell,
|
shell,
|
||||||
right,
|
right,
|
||||||
|
continuation,
|
||||||
width,
|
width,
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
env: HashMap::new(),
|
env: HashMap::new(),
|
||||||
|
|
|
@ -100,3 +100,7 @@ export STARSHIP_SHELL="bash"
|
||||||
STARSHIP_SESSION_KEY="$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM"; # Random generates a number b/w 0 - 32767
|
STARSHIP_SESSION_KEY="$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM"; # Random generates a number b/w 0 - 32767
|
||||||
STARSHIP_SESSION_KEY="${STARSHIP_SESSION_KEY}0000000000000000" # Pad it to 16+ chars.
|
STARSHIP_SESSION_KEY="${STARSHIP_SESSION_KEY}0000000000000000" # Pad it to 16+ chars.
|
||||||
export STARSHIP_SESSION_KEY=${STARSHIP_SESSION_KEY:0:16}; # Trim to 16-digits if excess.
|
export STARSHIP_SESSION_KEY=${STARSHIP_SESSION_KEY:0:16}; # Trim to 16-digits if excess.
|
||||||
|
|
||||||
|
# Set the continuation prompt
|
||||||
|
PS2="$(::STARSHIP:: prompt --continuation)"
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#!/usr/bin/env pwsh
|
#!/usr/bin/env pwsh
|
||||||
|
|
||||||
function global:prompt {
|
function Get-Arguments {
|
||||||
|
|
||||||
function Get-Cwd {
|
function Get-Cwd {
|
||||||
$cwd = Get-Location
|
$cwd = Get-Location
|
||||||
$provider_prefix = "$($cwd.Provider.ModuleName)\$($cwd.Provider.Name)::"
|
$provider_prefix = "$($cwd.Provider.ModuleName)\$($cwd.Provider.Name)::"
|
||||||
|
@ -22,6 +21,39 @@ function global:prompt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# @ makes sure the result is an array even if single or no values are returned
|
||||||
|
$jobs = @(Get-Job | Where-Object { $_.State -eq 'Running' }).Count
|
||||||
|
|
||||||
|
$cwd = Get-Cwd
|
||||||
|
$arguments = @(
|
||||||
|
"prompt"
|
||||||
|
"--path=$($cwd.Path)",
|
||||||
|
"--logical-path=$($cwd.LogicalPath)",
|
||||||
|
"--terminal-width=$($Host.UI.RawUI.WindowSize.Width)",
|
||||||
|
"--jobs=$($jobs)"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Whe start from the premise that the command executed correctly, which covers also the fresh console.
|
||||||
|
$lastExitCodeForPrompt = 0
|
||||||
|
if ($lastCmd = Get-History -Count 1) {
|
||||||
|
# In case we have a False on the Dollar hook, we know there's an error.
|
||||||
|
if (-not $origDollarQuestion) {
|
||||||
|
# We retrieve the InvocationInfo from the most recent error using $error[0]
|
||||||
|
$lastCmdletError = try { $error[0] | Where-Object { $_ -ne $null } | Select-Object -ExpandProperty InvocationInfo } catch { $null }
|
||||||
|
# We check if the last command executed matches the line that caused the last error, in which case we know
|
||||||
|
# it was an internal Powershell command, otherwise, there MUST be an error code.
|
||||||
|
$lastExitCodeForPrompt = if ($null -ne $lastCmdletError -and $lastCmd.CommandLine -eq $lastCmdletError.Line) { 1 } else { $origLastExitCode }
|
||||||
|
}
|
||||||
|
$duration = [math]::Round(($lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime).TotalMilliseconds)
|
||||||
|
|
||||||
|
$arguments += "--cmd-duration=$($duration)"
|
||||||
|
}
|
||||||
|
|
||||||
|
$arguments += "--status=$($lastExitCodeForPrompt)"
|
||||||
|
|
||||||
|
return $arguments
|
||||||
|
}
|
||||||
|
|
||||||
function Invoke-Native {
|
function Invoke-Native {
|
||||||
param($Executable, $Arguments)
|
param($Executable, $Arguments)
|
||||||
$startInfo = New-Object System.Diagnostics.ProcessStartInfo -ArgumentList $Executable -Property @{
|
$startInfo = New-Object System.Diagnostics.ProcessStartInfo -ArgumentList $Executable -Property @{
|
||||||
|
@ -63,6 +95,7 @@ function global:prompt {
|
||||||
$process.StandardOutput.ReadToEnd();
|
$process.StandardOutput.ReadToEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function global:prompt {
|
||||||
$origDollarQuestion = $global:?
|
$origDollarQuestion = $global:?
|
||||||
$origLastExitCode = $global:LASTEXITCODE
|
$origLastExitCode = $global:LASTEXITCODE
|
||||||
|
|
||||||
|
@ -73,35 +106,8 @@ function global:prompt {
|
||||||
}
|
}
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|
||||||
# @ makes sure the result is an array even if single or no values are returned
|
# Get arguments for starship prompt
|
||||||
$jobs = @(Get-Job | Where-Object { $_.State -eq 'Running' }).Count
|
$arguments = Get-Arguments
|
||||||
|
|
||||||
$cwd = Get-Cwd
|
|
||||||
$arguments = @(
|
|
||||||
"prompt"
|
|
||||||
"--path=$($cwd.Path)",
|
|
||||||
"--logical-path=$($cwd.LogicalPath)",
|
|
||||||
"--terminal-width=$($Host.UI.RawUI.WindowSize.Width)",
|
|
||||||
"--jobs=$($jobs)"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Whe start from the premise that the command executed correctly, which covers also the fresh console.
|
|
||||||
$lastExitCodeForPrompt = 0
|
|
||||||
if ($lastCmd = Get-History -Count 1) {
|
|
||||||
# In case we have a False on the Dollar hook, we know there's an error.
|
|
||||||
if (-not $origDollarQuestion) {
|
|
||||||
# We retrieve the InvocationInfo from the most recent error using $error[0]
|
|
||||||
$lastCmdletError = try { $error[0] | Where-Object { $_ -ne $null } | Select-Object -ExpandProperty InvocationInfo } catch { $null }
|
|
||||||
# We check if the last command executed matches the line that caused the last error, in which case we know
|
|
||||||
# it was an internal Powershell command, otherwise, there MUST be an error code.
|
|
||||||
$lastExitCodeForPrompt = if ($null -ne $lastCmdletError -and $lastCmd.CommandLine -eq $lastCmdletError.Line) { 1 } else { $origLastExitCode }
|
|
||||||
}
|
|
||||||
$duration = [math]::Round(($lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime).TotalMilliseconds)
|
|
||||||
|
|
||||||
$arguments += "--cmd-duration=$($duration)"
|
|
||||||
}
|
|
||||||
|
|
||||||
$arguments += "--status=$($lastExitCodeForPrompt)"
|
|
||||||
|
|
||||||
# Invoke Starship
|
# Invoke Starship
|
||||||
Invoke-Native -Executable ::STARSHIP:: -Arguments $arguments
|
Invoke-Native -Executable ::STARSHIP:: -Arguments $arguments
|
||||||
|
@ -130,6 +136,14 @@ function global:prompt {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Get arguments for starship continuation prompt
|
||||||
|
$arguments = Get-Arguments
|
||||||
|
$arguments += "--continuation"
|
||||||
|
|
||||||
|
# Invoke Starship and set continuation prompt
|
||||||
|
$continuation = Invoke-Native -Executable ::STARSHIP:: -Arguments $arguments
|
||||||
|
Set-PSReadLineOption -ContinuationPrompt $continuation
|
||||||
|
|
||||||
# Disable virtualenv prompt, it breaks starship
|
# Disable virtualenv prompt, it breaks starship
|
||||||
$ENV:VIRTUAL_ENV_DISABLE_PROMPT=1
|
$ENV:VIRTUAL_ENV_DISABLE_PROMPT=1
|
||||||
|
|
||||||
|
|
|
@ -94,4 +94,5 @@ setopt promptsubst
|
||||||
|
|
||||||
PROMPT='$(::STARSHIP:: prompt --terminal-width="$COLUMNS" --keymap="${KEYMAP:-}" --status="$STARSHIP_CMD_STATUS" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --cmd-duration="${STARSHIP_DURATION:-}" --jobs="$STARSHIP_JOBS_COUNT")'
|
PROMPT='$(::STARSHIP:: prompt --terminal-width="$COLUMNS" --keymap="${KEYMAP:-}" --status="$STARSHIP_CMD_STATUS" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --cmd-duration="${STARSHIP_DURATION:-}" --jobs="$STARSHIP_JOBS_COUNT")'
|
||||||
RPROMPT='$(::STARSHIP:: prompt --right --terminal-width="$COLUMNS" --keymap="${KEYMAP:-}" --status="$STARSHIP_CMD_STATUS" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --cmd-duration="${STARSHIP_DURATION:-}" --jobs="$STARSHIP_JOBS_COUNT")'
|
RPROMPT='$(::STARSHIP:: prompt --right --terminal-width="$COLUMNS" --keymap="${KEYMAP:-}" --status="$STARSHIP_CMD_STATUS" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --cmd-duration="${STARSHIP_DURATION:-}" --jobs="$STARSHIP_JOBS_COUNT")'
|
||||||
|
PROMPT2="$(::STARSHIP:: prompt --continuation)"
|
||||||
|
|
||||||
|
|
15
src/main.rs
15
src/main.rs
|
@ -88,8 +88,7 @@ fn main() {
|
||||||
.help("Print the main initialization script (as opposed to the init stub)");
|
.help("Print the main initialization script (as opposed to the init stub)");
|
||||||
|
|
||||||
let long_version = crate::shadow::clap_version();
|
let long_version = crate::shadow::clap_version();
|
||||||
let mut app =
|
let mut app = App::new("starship")
|
||||||
App::new("starship")
|
|
||||||
.about("The cross-shell prompt for astronauts. ☄🌌️")
|
.about("The cross-shell prompt for astronauts. ☄🌌️")
|
||||||
// pull the version number from Cargo.toml
|
// pull the version number from Cargo.toml
|
||||||
.version(shadow::PKG_VERSION)
|
.version(shadow::PKG_VERSION)
|
||||||
|
@ -112,6 +111,12 @@ fn main() {
|
||||||
.long("right")
|
.long("right")
|
||||||
.help("Print the right prompt (instead of the standard left prompt)"),
|
.help("Print the right prompt (instead of the standard left prompt)"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("continuation")
|
||||||
|
.long("continuation")
|
||||||
|
.help("Print the continuation prompt (instead of the standard left prompt)")
|
||||||
|
.conflicts_with("right"),
|
||||||
|
)
|
||||||
.arg(&status_code_arg)
|
.arg(&status_code_arg)
|
||||||
.arg(&pipestatus_arg)
|
.arg(&pipestatus_arg)
|
||||||
.arg(&terminal_width_arg)
|
.arg(&terminal_width_arg)
|
||||||
|
@ -189,9 +194,11 @@ fn main() {
|
||||||
.required_unless("name"),
|
.required_unless("name"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.subcommand(SubCommand::with_name("bug-report").about(
|
.subcommand(
|
||||||
|
SubCommand::with_name("bug-report").about(
|
||||||
"Create a pre-populated GitHub issue with information about your configuration",
|
"Create a pre-populated GitHub issue with information about your configuration",
|
||||||
))
|
),
|
||||||
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
SubCommand::with_name("time")
|
SubCommand::with_name("time")
|
||||||
.about("Prints time in milliseconds")
|
.about("Prints time in milliseconds")
|
||||||
|
|
33
src/print.rs
33
src/print.rs
|
@ -114,7 +114,8 @@ pub fn get_prompt(context: Context) -> String {
|
||||||
);
|
);
|
||||||
|
|
||||||
let module_strings = root_module.ansi_strings_for_shell(context.shell, Some(context.width));
|
let module_strings = root_module.ansi_strings_for_shell(context.shell, Some(context.width));
|
||||||
if config.add_newline {
|
if config.add_newline && !context.continuation {
|
||||||
|
// continuation prompts normally do not include newlines, but they can
|
||||||
writeln!(buf).unwrap();
|
writeln!(buf).unwrap();
|
||||||
}
|
}
|
||||||
write!(buf, "{}", ANSIStrings(&module_strings)).unwrap();
|
write!(buf, "{}", ANSIStrings(&module_strings)).unwrap();
|
||||||
|
@ -416,19 +417,27 @@ fn load_formatter_and_modules<'a>(context: &'a Context) -> (StringFormatter<'a>,
|
||||||
|
|
||||||
let lformatter = StringFormatter::new(&config.format);
|
let lformatter = StringFormatter::new(&config.format);
|
||||||
let rformatter = StringFormatter::new(&config.right_format);
|
let rformatter = StringFormatter::new(&config.right_format);
|
||||||
|
let cformatter = StringFormatter::new(&config.continuation_prompt);
|
||||||
if lformatter.is_err() {
|
if lformatter.is_err() {
|
||||||
log::error!("Error parsing `format`")
|
log::error!("Error parsing `format`")
|
||||||
}
|
}
|
||||||
if rformatter.is_err() {
|
if rformatter.is_err() {
|
||||||
log::error!("Error parsing `right_format`")
|
log::error!("Error parsing `right_format`")
|
||||||
}
|
}
|
||||||
|
if cformatter.is_err() {
|
||||||
|
log::error!("Error parsing `continuation_prompt`")
|
||||||
|
}
|
||||||
|
|
||||||
match (lformatter, rformatter) {
|
match (lformatter, rformatter, cformatter) {
|
||||||
(Ok(lf), Ok(rf)) => {
|
(Ok(lf), Ok(rf), Ok(cf)) => {
|
||||||
let mut modules: BTreeSet<String> = BTreeSet::new();
|
let mut modules: BTreeSet<String> = BTreeSet::new();
|
||||||
|
if !context.continuation {
|
||||||
modules.extend(lf.get_variables());
|
modules.extend(lf.get_variables());
|
||||||
modules.extend(rf.get_variables());
|
modules.extend(rf.get_variables());
|
||||||
if context.right {
|
}
|
||||||
|
if context.continuation {
|
||||||
|
(cf, modules)
|
||||||
|
} else if context.right {
|
||||||
(rf, modules)
|
(rf, modules)
|
||||||
} else {
|
} else {
|
||||||
(lf, modules)
|
(lf, modules)
|
||||||
|
@ -461,4 +470,20 @@ mod test {
|
||||||
let actual = get_prompt(context);
|
let actual = get_prompt(context);
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn continuation_prompt() {
|
||||||
|
let mut context = default_context();
|
||||||
|
context.config = StarshipConfig {
|
||||||
|
config: Some(toml::toml! {
|
||||||
|
continuation_prompt="><>"
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
context.root_config.continuation_prompt = "><>".to_string();
|
||||||
|
context.continuation = true;
|
||||||
|
|
||||||
|
let expected = String::from("><>");
|
||||||
|
let actual = get_prompt(context);
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue