feat: remove forced UTF8

relates to #576
This commit is contained in:
Jan De Dobbeleer 2022-03-30 18:28:21 +02:00 committed by Jan De Dobbeleer
parent c1231aeb7b
commit d823b55e90

View file

@ -1,14 +1,35 @@
# Powershell doesn't default to UTF8 just yet, so we're forcing it as there are too many problems function Start-Utf8Process
# that pop up when we don't {
if ($ExecutionContext.SessionState.LanguageMode -ne "ConstrainedLanguage") { param(
[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding [string] $FileName,
[string] $Arguments
)
$Process = New-Object System.Diagnostics.Process
$StartInfo = $Process.StartInfo
$StartInfo.StandardErrorEncoding = $StartInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8
$StartInfo.RedirectStandardError = $StartInfo.RedirectStandardInput = $StartInfo.RedirectStandardOutput = $true
$StartInfo.FileName = $filename
$StartInfo.Arguments = $Arguments
$StartInfo.UseShellExecute = $false
$StartInfo.CreateNoWindow = $true
$_ = $Process.Start();
$_ = $Process.WaitForExit();
return $Process.StandardOutput.ReadToEnd() + $Process.StandardError.ReadToEnd()
} }
elseif ($env:POSH_CONSTRAINED_LANGUAGE_MODE -ne $true) {
Write-Host "[WARNING] ConstrainedLanguage mode detected, unable to set console to UTF-8. # Copyright (c) 2016 Michael Kelley
When using PowerShell in ConstrainedLanguage mode, please set the # https://github.com/kelleyma49/PSFzf/issues/71
console mode manually to UTF-8. See here for more information: function script:Invoke-Prompt()
https://ohmyposh.dev/docs/faq#powershell-running-in-constrainedlanguage-mode {
" $previousOutputEncoding = [Console]::OutputEncoding
[Console]::OutputEncoding = [Text.Encoding]::UTF8
try {
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
} finally {
[Console]::OutputEncoding = $previousOutputEncoding
}
} }
$env:POWERLINE_COMMAND = "oh-my-posh" $env:POWERLINE_COMMAND = "oh-my-posh"
@ -31,17 +52,16 @@ if (Test-Path $omp_config) {
Remove-Variable omp_value -Confirm:$false Remove-Variable omp_value -Confirm:$false
Remove-Variable omp_config -Confirm:$false Remove-Variable omp_config -Confirm:$false
# set secondary prompt # set secondary prompt`
$secondaryPrompt = @(&"::OMP::" print secondary --config="$Env:POSH_THEME" 2>&1) -join "`n" $secondaryPrompt = @(Start-Utf8Process "::OMP::" "print secondary --config=""$Env:POSH_THEME""") -join "`n"
Set-PSReadLineOption -ContinuationPrompt $secondaryPrompt Set-PSReadLineOption -ContinuationPrompt $secondaryPrompt
function global:Set-PoshContext {} function global:Set-PoshContext {}
function global:Get-PoshContext { function global:Get-PoshContext {
$config = $env:POSH_THEME
$cleanPWD = $PWD.ProviderPath $cleanPWD = $PWD.ProviderPath
$cleanPSWD = $PWD.ToString() $cleanPSWD = $PWD.ToString()
return $config, $cleanPWD, $cleanPSWD return $cleanPWD, $cleanPSWD
} }
function global:Initialize-ModuleSupport { function global:Initialize-ModuleSupport {
@ -60,16 +80,14 @@ function global:Initialize-ModuleSupport {
#store the last exit code for restore #store the last exit code for restore
$realLASTEXITCODE = $global:LASTEXITCODE $realLASTEXITCODE = $global:LASTEXITCODE
$omp = "::OMP::" $omp = "::OMP::"
$config, $cleanPWD, $cleanPSWD = Get-PoshContext $cleanPWD, $cleanPSWD = Get-PoshContext
if ($env:POSH_TRANSIENT -eq $true) { if ($env:POSH_TRANSIENT -eq $true) {
$standardOut = @(&$omp print transient --error="$global:OMP_ERRORCODE" --pwd="$cleanPWD" --pswd="$cleanPSWD" --execution-time="$global:OMP_EXECUTIONTIME" --config="$config" 2>&1) @(Start-Utf8Process $omp "print transient --error=$global:OMP_ERRORCODE --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --execution-time=$global:OMP_EXECUTIONTIME --config=""$Env:POSH_THEME""") -join "`n"
$standardOut -join "`n"
$env:POSH_TRANSIENT = $false $env:POSH_TRANSIENT = $false
return return
} }
if (Test-Path variable:/PSDebugContext) { if (Test-Path variable:/PSDebugContext) {
$standardOut = @(&$omp print debug --pwd="$cleanPWD" --pswd="$cleanPSWD" --config="$config" 2>&1) @(Start-Utf8Process $omp "print debug --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --config=""$Env:POSH_THEME""") -join "`n"
$standardOut -join "`n"
return return
} }
$global:OMP_ERRORCODE = 0 $global:OMP_ERRORCODE = 0
@ -101,7 +119,7 @@ function global:Initialize-ModuleSupport {
} }
Set-PoshContext Set-PoshContext
$terminalWidth = $Host.UI.RawUI.WindowSize.Width $terminalWidth = $Host.UI.RawUI.WindowSize.Width
$standardOut = @(&$omp print primary --error="$global:OMP_ERRORCODE" --pwd="$cleanPWD" --pswd="$cleanPSWD" --execution-time="$global:OMP_EXECUTIONTIME" --stack-count="$stackCount" --config="$config" --terminal-width=$terminalWidth 2>&1) $standardOut = @(Start-Utf8Process $omp "print primary --error=$global:OMP_ERRORCODE --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --execution-time=$global:OMP_EXECUTIONTIME --stack-count=$stackCount --config=""$Env:POSH_THEME"" --terminal-width=$terminalWidth")
# make sure PSReadLine knows we have a multiline prompt # make sure PSReadLine knows we have a multiline prompt
$extraLines = $standardOut.Count - 1 $extraLines = $standardOut.Count - 1
if ($extraLines -gt 0) { if ($extraLines -gt 0) {
@ -118,9 +136,8 @@ Set-Item -Path Function:prompt -Value $Prompt -Force
function global:Write-PoshDebug { function global:Write-PoshDebug {
$omp = "::OMP::" $omp = "::OMP::"
$config, $cleanPWD, $cleanPSWD = Get-PoshContext $cleanPWD, $cleanPSWD = Get-PoshContext
$standardOut = @(&$omp debug --config="$config" 2>&1) @(Start-Utf8Process $omp "debug --config=""$Env:POSH_THEME""") -join "`n"
$standardOut -join "`n"
} }
<# <#
@ -154,9 +171,8 @@ function global:Export-PoshTheme {
$Format = 'json' $Format = 'json'
) )
$config = $env:POSH_THEME
$omp = "::OMP::" $omp = "::OMP::"
$configString = @(&$omp config export --config="$config" --format="$Format" 2>&1) $configString = @(Start-Utf8Process $omp "config export --config=""$Env:POSH_THEME"" --format=$Format")
# if no path, copy to clipboard by default # if no path, copy to clipboard by default
if ($FilePath -ne "") { if ($FilePath -ne "") {
#https://stackoverflow.com/questions/3038337/powershell-resolve-path-that-might-not-exist #https://stackoverflow.com/questions/3038337/powershell-resolve-path-that-might-not-exist
@ -174,12 +190,12 @@ function global:Enable-PoshTooltips {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ') [Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
$position = $host.UI.RawUI.CursorPosition $position = $host.UI.RawUI.CursorPosition
$omp = "::OMP::" $omp = "::OMP::"
$config, $cleanPWD, $cleanPSWD = Get-PoshContext $cleanPWD, $cleanPSWD = Get-PoshContext
$command = $null $command = $null
$cursor = $null $cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$cursor) [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$cursor)
$command = $command -replace "`"", "'" $command = ($command -split " ")[0]
$standardOut = @(&$omp print tooltip --pwd="$cleanPWD" --pswd="$cleanPSWD" --config="$config" --command="$command" 2>&1) $standardOut = @(Start-Utf8Process $omp "print tooltip --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --config=""$Env:POSH_THEME"" --command=""$command""")
Write-Host $standardOut -NoNewline Write-Host $standardOut -NoNewline
$host.UI.RawUI.CursorPosition = $position $host.UI.RawUI.CursorPosition = $position
} }
@ -188,15 +204,21 @@ function global:Enable-PoshTooltips {
function global:Enable-PoshTransientPrompt { function global:Enable-PoshTransientPrompt {
Set-PSReadlineKeyHandler -Key Enter -ScriptBlock { Set-PSReadlineKeyHandler -Key Enter -ScriptBlock {
$env:POSH_TRANSIENT = $true $env:POSH_TRANSIENT = $true
$previousOutputEncoding = [Console]::OutputEncoding
[Console]::OutputEncoding = [Text.Encoding]::UTF8
try {
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt() [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
} finally {
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
[Console]::OutputEncoding = $previousOutputEncoding
}
} }
} }
function global:Enable-PoshLineError { function global:Enable-PoshLineError {
$omp = "::OMP::" $omp = "::OMP::"
$validLine = @(&$omp print valid --config="$Env:POSH_THEME" 2>&1) -join "`n" $validLine = @(Start-Utf8Process $omp "print valid --config=""$Env:POSH_THEME""") -join "`n"
$errorLine = @(&$omp print error --config="$Env:POSH_THEME" 2>&1) -join "`n" $errorLine = @(Start-Utf8Process $omp "print error --config=""$Env:POSH_THEME""") -join "`n"
Set-PSReadLineOption -PromptText $validLine, $errorLine Set-PSReadLineOption -PromptText $validLine, $errorLine
} }
@ -263,7 +285,7 @@ function global:Get-PoshThemes() {
$themes | ForEach-Object -Process { $themes | ForEach-Object -Process {
Write-Host "Theme: $(Get-Hyperlink -uri $_.fullname -name $_.BaseName.Replace('.omp', ''))" Write-Host "Theme: $(Get-Hyperlink -uri $_.fullname -name $_.BaseName.Replace('.omp', ''))"
Write-Host "" Write-Host ""
& $omp print primary --config $($_.FullName) --pwd $PWD --shell pwsh @(Start-Utf8Process $omp "print primary --config=""$($_.FullName)"" --pwd=""$PWD"" --shell pwsh")
Write-Host "" Write-Host ""
Write-Host "" Write-Host ""
} }