2019-10-15 15:10:16 +00:00
|
|
|
#!/usr/bin/env pwsh
|
|
|
|
|
|
|
|
function global:prompt {
|
2020-09-09 15:52:30 +00:00
|
|
|
$origDollarQuestion = $global:?
|
|
|
|
$origLastExitCode = $global:LASTEXITCODE
|
|
|
|
|
2019-10-15 15:10:16 +00:00
|
|
|
$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
|
|
|
|
|
2020-04-30 08:40:56 +00:00
|
|
|
$env:PWD = $PWD
|
2020-08-20 04:45:10 +00:00
|
|
|
$current_directory = (Convert-Path -LiteralPath $PWD)
|
2020-09-09 15:52:30 +00:00
|
|
|
|
|
|
|
# If an external command has not been executed, then the $LASTEXITCODE will be $null.
|
|
|
|
# For the purposes of the prompt, replace this with a 0 exit code so that the prompt
|
|
|
|
# doesn't show the last command as a failure (because it had a non-zero exit code).
|
|
|
|
$lastExitCodeForPrompt = if ($origLastExitCode) { $origLastExitCode } else { 0 }
|
2020-04-30 08:40:56 +00:00
|
|
|
|
2020-10-19 17:52:15 +00:00
|
|
|
# Save old output encoding and set it to UTF-8
|
|
|
|
$origOutputEncoding = [Console]::OutputEncoding
|
|
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
2019-10-15 15:10:16 +00:00
|
|
|
if ($lastCmd = Get-History -Count 1) {
|
2019-12-19 22:38:06 +00:00
|
|
|
$duration = [math]::Round(($lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime).TotalMilliseconds)
|
2019-10-15 15:10:16 +00:00
|
|
|
# & ensures the path is interpreted as something to execute
|
2020-09-09 15:52:30 +00:00
|
|
|
$out = @(&::STARSHIP:: prompt "--path=$current_directory" --status=$lastExitCodeForPrompt --jobs=$jobs --cmd-duration=$duration)
|
2019-10-15 15:10:16 +00:00
|
|
|
} else {
|
2020-09-09 15:52:30 +00:00
|
|
|
$out = @(&::STARSHIP:: prompt "--path=$current_directory" --status=$lastExitCodeForPrompt --jobs=$jobs)
|
2019-10-15 15:10:16 +00:00
|
|
|
}
|
2020-10-19 17:52:15 +00:00
|
|
|
# Restore old output encoding
|
|
|
|
[Console]::OutputEncoding = $origOutputEncoding
|
2019-10-15 15:10:16 +00:00
|
|
|
|
|
|
|
# Convert stdout (array of lines) to expected return type string
|
|
|
|
# `n is an escaped newline
|
|
|
|
$out -join "`n"
|
2020-09-09 15:52:30 +00:00
|
|
|
|
|
|
|
# Propagate the original $LASTEXITCODE from before the prompt function was invoked.
|
|
|
|
$global:LASTEXITCODE = $origLastExitCode
|
|
|
|
|
|
|
|
# Propagate the original $? automatic variable value from before the prompt function was invoked.
|
|
|
|
#
|
|
|
|
# $? is a read-only or constant variable so we can't directly override it.
|
|
|
|
# In order to propagate up its original boolean value we will take an action
|
|
|
|
# which will produce the desired value.
|
|
|
|
#
|
|
|
|
# This has to be the very last thing that happens in the prompt function
|
|
|
|
# since every PowerShell command sets the $? variable.
|
|
|
|
if ($global:? -ne $origDollarQuestion) {
|
|
|
|
if ($origDollarQuestion) {
|
|
|
|
# Simple command which will execute successfully and set $? = True without any other side affects.
|
|
|
|
1+1
|
|
|
|
} else {
|
|
|
|
# Write-Error will set $? to False.
|
|
|
|
# ErrorAction Ignore will prevent the error from being added to the $Error collection.
|
|
|
|
Write-Error '' -ErrorAction 'Ignore'
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 15:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$ENV:STARSHIP_SHELL = "powershell"
|
2020-09-28 20:38:50 +00:00
|
|
|
|
|
|
|
# Set up the session key that will be used to store logs
|
2020-09-30 16:14:10 +00:00
|
|
|
$ENV:STARSHIP_SESSION_KEY = (& ::STARSHIP:: session)
|