mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
feat: Implement PowerShell support (#470)
This commit is contained in:
committed by
Matan Kushner
parent
b5bb6d6994
commit
6ab70796db
@@ -94,6 +94,21 @@ fi"#,
|
||||
);
|
||||
Some(script)
|
||||
}
|
||||
Some("powershell") => {
|
||||
// Explanation of syntax:
|
||||
// &: Explicitly tells powershell to execute path with starship executable.
|
||||
//
|
||||
// @: multi-line stdout is returned as an array, but a single line or no lines
|
||||
// are returned as-is. @ ensures it's always an array.
|
||||
//
|
||||
// -join "`n": Joins the stdout array together as a string with newlines.
|
||||
// Powershell escapes with ` instead of \ thus `n translates to a newline.
|
||||
let script = format!(
|
||||
"Invoke-Expression (@(&\"{}\" init powershell --print-full-init) -join \"`n\")",
|
||||
starship
|
||||
);
|
||||
Some(script)
|
||||
}
|
||||
None => {
|
||||
println!(
|
||||
"Invalid shell name provided: {}\\n\
|
||||
@@ -130,6 +145,7 @@ pub fn init_main(shell_name: &str) -> io::Result<()> {
|
||||
"bash" => Some(BASH_INIT),
|
||||
"zsh" => Some(ZSH_INIT),
|
||||
"fish" => Some(FISH_INIT),
|
||||
"powershell" => Some(PWSH_INIT),
|
||||
_ => {
|
||||
println!(
|
||||
"printf \"Shell name detection failed on phase two init.\\n\
|
||||
@@ -168,3 +184,5 @@ const BASH_INIT: &str = include_str!("starship.bash");
|
||||
const ZSH_INIT: &str = include_str!("starship.zsh");
|
||||
|
||||
const FISH_INIT: &str = include_str!("starship.fish");
|
||||
|
||||
const PWSH_INIT: &str = include_str!("starship.ps1");
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
# Starship assumes UTF-8
|
||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
function global:prompt {
|
||||
$out = $null
|
||||
# @ makes sure the result is an array even if single or no values are returned
|
||||
$jobs = @(Get-Job | Where-Object { $_.State -eq 'Running' }).Count
|
||||
|
||||
if ($lastCmd = Get-History -Count 1) {
|
||||
$duration = [math]::Round(($lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime).TotalSeconds)
|
||||
# & ensures the path is interpreted as something to execute
|
||||
$out = @(&::STARSHIP:: prompt --status=$lastexitcode --jobs=$jobs --cmd-duration=$duration)
|
||||
} else {
|
||||
$out = @(&::STARSHIP:: prompt --status=$lastexitcode --jobs=$jobs)
|
||||
}
|
||||
|
||||
# Convert stdout (array of lines) to expected return type string
|
||||
# `n is an escaped newline
|
||||
$out -join "`n"
|
||||
}
|
||||
|
||||
$ENV:STARSHIP_SHELL = "powershell"
|
||||
+1
-1
@@ -34,7 +34,7 @@ fn main() {
|
||||
let shell_arg = Arg::with_name("shell")
|
||||
.value_name("SHELL")
|
||||
.help(
|
||||
"The name of the currently running shell\nCurrently supported options: bash, zsh, fish",
|
||||
"The name of the currently running shell\nCurrently supported options: bash, zsh, fish, powershell",
|
||||
)
|
||||
.required(true);
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default();
|
||||
let percentage_char = match shell.as_str() {
|
||||
"zsh" => "%%", // % is an escape in zsh, see PROMPT in `man zshmisc`
|
||||
"powershell" => "`%",
|
||||
_ => "%",
|
||||
};
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ use crate::configs::conda::CondaConfig;
|
||||
/// Will display the Conda environment iff `$CONDA_DEFAULT_ENV` is set.
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
// Reference implementation: https://github.com/denysdovhan/spaceship-prompt/blob/master/sections/conda.zsh
|
||||
let conda_env = env::var("CONDA_DEFAULT_ENV").ok()?;
|
||||
if conda_env.is_empty() {
|
||||
let conda_env = env::var("CONDA_DEFAULT_ENV").unwrap_or_else(|_| "".into());
|
||||
if conda_env.trim().is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user