1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-27 03:42:39 +00:00

fix(pwsh): preserve original console encondings (#1810)

This commit is contained in:
David Knaack 2020-10-19 19:52:15 +02:00 committed by GitHub
parent f9bed976c1
commit daf7976f9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,5 @@
#!/usr/bin/env pwsh #!/usr/bin/env pwsh
# Starship assumes UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
function global:prompt { function global:prompt {
$origDollarQuestion = $global:? $origDollarQuestion = $global:?
$origLastExitCode = $global:LASTEXITCODE $origLastExitCode = $global:LASTEXITCODE
@ -18,6 +16,9 @@ function global:prompt {
# doesn't show the last command as a failure (because it had a non-zero exit code). # doesn't show the last command as a failure (because it had a non-zero exit code).
$lastExitCodeForPrompt = if ($origLastExitCode) { $origLastExitCode } else { 0 } $lastExitCodeForPrompt = if ($origLastExitCode) { $origLastExitCode } else { 0 }
# Save old output encoding and set it to UTF-8
$origOutputEncoding = [Console]::OutputEncoding
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
if ($lastCmd = Get-History -Count 1) { if ($lastCmd = Get-History -Count 1) {
$duration = [math]::Round(($lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime).TotalMilliseconds) $duration = [math]::Round(($lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime).TotalMilliseconds)
# & ensures the path is interpreted as something to execute # & ensures the path is interpreted as something to execute
@ -25,6 +26,8 @@ function global:prompt {
} else { } else {
$out = @(&::STARSHIP:: prompt "--path=$current_directory" --status=$lastExitCodeForPrompt --jobs=$jobs) $out = @(&::STARSHIP:: prompt "--path=$current_directory" --status=$lastExitCodeForPrompt --jobs=$jobs)
} }
# Restore old output encoding
[Console]::OutputEncoding = $origOutputEncoding
# Convert stdout (array of lines) to expected return type string # Convert stdout (array of lines) to expected return type string
# `n is an escaped newline # `n is an escaped newline