mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
refactor(pwsh): use dynamic module for init
This commit is contained in:
parent
a2f5016e77
commit
a9671df378
|
@ -44,81 +44,3 @@
|
||||||
} # End of PSData hashtable
|
} # End of PSData hashtable
|
||||||
} # End of PrivateData hashtable
|
} # End of PrivateData hashtable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,23 @@
|
||||||
function global:Start-Utf8Process {
|
# remove any existing dynamic module of OMP
|
||||||
|
if ($null -ne (Get-Module -Name "oh-my-posh-core")) {
|
||||||
|
Remove-Module -Name "oh-my-posh-core" -Force
|
||||||
|
}
|
||||||
|
New-Module -Name "oh-my-posh-core" -ScriptBlock {
|
||||||
|
$script:ErrorCode = 0
|
||||||
|
$script:OMPExecutable = '::OMP::'
|
||||||
|
$script:PSVersion = $PSVersionTable.PSVersion.ToString()
|
||||||
|
$script:TransientPrompt = $false
|
||||||
|
$env:POWERLINE_COMMAND = "oh-my-posh"
|
||||||
|
$env:CONDA_PROMPT_MODIFIER = $false
|
||||||
|
if (Test-Path '::CONFIG::') {
|
||||||
|
$env:POSH_THEME = (Resolve-Path -Path '::CONFIG::').ProviderPath
|
||||||
|
}
|
||||||
|
# specific module support (disabled by default)
|
||||||
|
if ($null -eq $env:POSH_GIT_ENABLED) {
|
||||||
|
$env:POSH_GIT_ENABLED = $false
|
||||||
|
}
|
||||||
|
|
||||||
|
function Start-Utf8Process {
|
||||||
param(
|
param(
|
||||||
[string] $FileName,
|
[string] $FileName,
|
||||||
[string] $Arguments
|
[string] $Arguments
|
||||||
|
@ -22,120 +41,82 @@ function global:Start-Utf8Process {
|
||||||
$Host.UI.WriteErrorLine($stderr)
|
$Host.UI.WriteErrorLine($stderr)
|
||||||
}
|
}
|
||||||
return $Process.StandardOutput.ReadToEnd()
|
return $Process.StandardOutput.ReadToEnd()
|
||||||
}
|
}
|
||||||
|
|
||||||
$env:POWERLINE_COMMAND = "oh-my-posh"
|
function Set-PoshContext {}
|
||||||
$env:CONDA_PROMPT_MODIFIER = $false
|
|
||||||
$env:SHELL_VERSION = $PSVersionTable.PSVersion.ToString()
|
|
||||||
$global:POSH_TRANSIENT = $false
|
|
||||||
|
|
||||||
# specific module support (disabled by default)
|
function Get-PoshContext {
|
||||||
if ($null -eq $env:POSH_GIT_ENABLED) {
|
|
||||||
$env:POSH_GIT_ENABLED = $false
|
|
||||||
}
|
|
||||||
|
|
||||||
# used to detect empty hit
|
|
||||||
$global:OMP_LASTHISTORYID = -1
|
|
||||||
|
|
||||||
if (Test-Path '::CONFIG::') {
|
|
||||||
$env:POSH_THEME = (Resolve-Path -Path '::CONFIG::').ProviderPath
|
|
||||||
}
|
|
||||||
|
|
||||||
# set secondary prompt`
|
|
||||||
Set-PSReadLineOption -ContinuationPrompt (@(Start-Utf8Process '::OMP::' "print secondary --config=""$Env:POSH_THEME""") -join "`n")
|
|
||||||
|
|
||||||
function global:Set-PoshContext {}
|
|
||||||
|
|
||||||
function global:Get-PoshContext {
|
|
||||||
$cleanPWD = $PWD.ProviderPath
|
$cleanPWD = $PWD.ProviderPath
|
||||||
$cleanPSWD = $PWD.ToString()
|
$cleanPSWD = $PWD.ToString()
|
||||||
$cleanPWD = $cleanPWD.TrimEnd('\')
|
$cleanPWD = $cleanPWD.TrimEnd('\')
|
||||||
$cleanPSWD = $cleanPSWD.TrimEnd('\')
|
$cleanPSWD = $cleanPSWD.TrimEnd('\')
|
||||||
return $cleanPWD, $cleanPSWD
|
return $cleanPWD, $cleanPSWD
|
||||||
}
|
}
|
||||||
|
|
||||||
function global:Initialize-ModuleSupport {
|
function Initialize-ModuleSupport {
|
||||||
if ($env:POSH_GIT_ENABLED -eq $true) {
|
if ($env:POSH_GIT_ENABLED -eq $true) {
|
||||||
$global:GitStatus = Get-GitStatus
|
$env:POSH_GIT_STATUS = Write-GitStatus -Status (Get-GitStatus)
|
||||||
$env:POSH_GIT_STATUS = Write-GitStatus -Status $global:GitStatus
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Set-Item -Force -Path Function:prompt -Value {
|
function Enable-PoshTooltips {
|
||||||
#store if the last command was successful
|
Set-PSReadlineKeyHandler -Key SpaceBar -ScriptBlock {
|
||||||
$lastCommandSuccess = $?
|
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
|
||||||
#store the last exit code for restore
|
$position = $host.UI.RawUI.CursorPosition
|
||||||
$realLASTEXITCODE = $global:LASTEXITCODE
|
|
||||||
$omp = '::OMP::'
|
|
||||||
$cleanPWD, $cleanPSWD = Get-PoshContext
|
$cleanPWD, $cleanPSWD = Get-PoshContext
|
||||||
if ($global:POSH_TRANSIENT -eq $true) {
|
$command = $null
|
||||||
@(Start-Utf8Process $omp "print transient --error=$global:OMP_ERRORCODE --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --execution-time=$global:OMP_EXECUTIONTIME --config=""$Env:POSH_THEME"" --shell-version=""$env:SHELL_VERSION""") -join "`n"
|
$cursor = $null
|
||||||
$global:POSH_TRANSIENT = $false
|
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$cursor)
|
||||||
return
|
$command = ($command -split " ")[0]
|
||||||
}
|
$standardOut = @(Start-Utf8Process $script:OMPExecutable "print tooltip --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --config=""$env:POSH_THEME"" --command=""$command"" --shell-version=""$script:PSVersion""")
|
||||||
if (Test-Path variable:/PSDebugContext) {
|
Write-Host $standardOut -NoNewline
|
||||||
@(Start-Utf8Process $omp "print debug --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --config=""$Env:POSH_THEME""") -join "`n"
|
$host.UI.RawUI.CursorPosition = $position
|
||||||
return
|
|
||||||
}
|
|
||||||
$global:OMP_ERRORCODE = 0
|
|
||||||
Initialize-ModuleSupport
|
|
||||||
if ($lastCommandSuccess -eq $false) {
|
|
||||||
# native app exit code
|
|
||||||
if ($realLASTEXITCODE -is [int] -and $realLASTEXITCODE -ne 0) {
|
|
||||||
$global:OMP_ERRORCODE = $realLASTEXITCODE
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$global:OMP_ERRORCODE = 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# read stack count from current stack(if invoked from profile=right value, otherwise use the global variable set in Set-PoshPrompt(stack scoped to module))
|
function Enable-PoshTransientPrompt {
|
||||||
$stackCount = (Get-Location -stack).Count
|
Set-PSReadlineKeyHandler -Key Enter -ScriptBlock {
|
||||||
|
$script:TransientPrompt = $true
|
||||||
|
$previousOutputEncoding = [Console]::OutputEncoding
|
||||||
|
[Console]::OutputEncoding = [Text.Encoding]::UTF8
|
||||||
try {
|
try {
|
||||||
if ($global:OMP_GLOBAL_SESSIONSTATE -ne $null) {
|
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
|
||||||
$stackCount = ($global:OMP_GLOBAL_SESSIONSTATE).path.locationstack('').count
|
} finally {
|
||||||
|
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
|
||||||
|
[Console]::OutputEncoding = $previousOutputEncoding
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch {}
|
|
||||||
|
|
||||||
$global:OMP_EXECUTIONTIME = -1
|
function Enable-PoshLineError {
|
||||||
$history = Get-History -ErrorAction Ignore -Count 1
|
$validLine = @(Start-Utf8Process $script:OMPExecutable "print valid --config=""$env:POSH_THEME""") -join "`n"
|
||||||
if ($null -ne $history -and $null -ne $history.EndExecutionTime -and $null -ne $history.StartExecutionTime -and $global:OMP_LASTHISTORYID -ne $history.Id) {
|
$errorLine = @(Start-Utf8Process $script:OMPExecutable "print error --config=""$env:POSH_THEME""") -join "`n"
|
||||||
$global:OMP_EXECUTIONTIME = ($history.EndExecutionTime - $history.StartExecutionTime).TotalMilliseconds
|
Set-PSReadLineOption -PromptText $validLine, $errorLine
|
||||||
$global:OMP_LASTHISTORYID = $history.Id
|
|
||||||
}
|
}
|
||||||
Set-PoshContext
|
|
||||||
$terminalWidth = $Host.UI.RawUI.WindowSize.Width
|
|
||||||
$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"" --shell-version=""$env:SHELL_VERSION"" --terminal-width=$terminalWidth")
|
|
||||||
# make sure PSReadLine knows we have a multiline prompt
|
|
||||||
$extraLines = ($standardOut | Measure-Object -Line).Lines - 1
|
|
||||||
if ($extraLines -gt 0) {
|
|
||||||
Set-PSReadlineOption -ExtraPromptLineCount $extraLines
|
|
||||||
}
|
|
||||||
# the output can be multiline, joining these ensures proper rendering by adding line breaks with `n
|
|
||||||
$standardOut -join "`n"
|
|
||||||
$global:LASTEXITCODE = $realLASTEXITCODE
|
|
||||||
}
|
|
||||||
|
|
||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Exports the current oh-my-posh theme
|
Exports the current oh-my-posh theme.
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
By default the config is exported in json to the clipboard
|
By default the config is exported in JSON to the clipboard.
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
Export-PoshTheme
|
Export-PoshTheme
|
||||||
Current theme exported in json to clipboard
|
|
||||||
.EXAMPLE
|
Export the current theme in JSON to the clipboard.
|
||||||
|
.EXAMPLE
|
||||||
Export-PoshTheme -Format toml
|
Export-PoshTheme -Format toml
|
||||||
Current theme exported in toml to clipboard
|
|
||||||
.EXAMPLE
|
Export the current theme in TOML to the clipboard.
|
||||||
Export-PoshTheme c:\temp\theme.toml toml
|
.EXAMPLE
|
||||||
Current theme exported in toml to c:\temp\theme.toml
|
Export-PoshTheme C:\temp\theme.yaml yaml
|
||||||
.EXAMPLE
|
|
||||||
|
Export the current theme in YAML to 'C:\temp\theme.yaml'.
|
||||||
|
.EXAMPLE
|
||||||
Export-PoshTheme ~\theme.toml toml
|
Export-PoshTheme ~\theme.toml toml
|
||||||
Current theme exported in toml to your home\theme.toml
|
|
||||||
#>
|
Export the current theme in TOML to '$HOME\theme.toml'
|
||||||
function global:Export-PoshTheme {
|
#>
|
||||||
|
function Export-PoshTheme {
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
[string]
|
[string]
|
||||||
|
@ -148,66 +129,19 @@ function global:Export-PoshTheme {
|
||||||
$Format = 'json'
|
$Format = 'json'
|
||||||
)
|
)
|
||||||
|
|
||||||
$omp = '::OMP::'
|
$configString = @(Start-Utf8Process $script:OMPExecutable "config export --config=""$env:POSH_THEME"" --format=$Format")
|
||||||
$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 ('' -ne $FilePath) {
|
||||||
#https://stackoverflow.com/questions/3038337/powershell-resolve-path-that-might-not-exist
|
# https://stackoverflow.com/questions/3038337/powershell-resolve-path-that-might-not-exist
|
||||||
$FilePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($FilePath)
|
$FilePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($FilePath)
|
||||||
[IO.File]::WriteAllLines($FilePath, $configString)
|
[IO.File]::WriteAllLines($FilePath, $configString)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
Set-Clipboard $configString
|
Set-Clipboard $configString
|
||||||
Write-Output "Theme copied to clipboard"
|
Write-Output "Theme copied to clipboard"
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function global:Enable-PoshTooltips {
|
|
||||||
Set-PSReadlineKeyHandler -Key SpaceBar -ScriptBlock {
|
|
||||||
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
|
|
||||||
$position = $host.UI.RawUI.CursorPosition
|
|
||||||
$omp = '::OMP::'
|
|
||||||
$cleanPWD, $cleanPSWD = Get-PoshContext
|
|
||||||
$command = $null
|
|
||||||
$cursor = $null
|
|
||||||
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$cursor)
|
|
||||||
$command = ($command -split " ")[0]
|
|
||||||
$standardOut = @(Start-Utf8Process $omp "print tooltip --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --config=""$Env:POSH_THEME"" --command=""$command"" --shell-version=""$env:SHELL_VERSION""")
|
|
||||||
Write-Host $standardOut -NoNewline
|
|
||||||
$host.UI.RawUI.CursorPosition = $position
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function global:Enable-PoshTransientPrompt {
|
function Get-FileHyperlink {
|
||||||
Set-PSReadlineKeyHandler -Key Enter -ScriptBlock {
|
|
||||||
$global:POSH_TRANSIENT = $true
|
|
||||||
$previousOutputEncoding = [Console]::OutputEncoding
|
|
||||||
[Console]::OutputEncoding = [Text.Encoding]::UTF8
|
|
||||||
try {
|
|
||||||
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
|
|
||||||
[Console]::OutputEncoding = $previousOutputEncoding
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function global:Enable-PoshLineError {
|
|
||||||
$omp = '::OMP::'
|
|
||||||
$validLine = @(Start-Utf8Process $omp "print valid --config=""$Env:POSH_THEME""") -join "`n"
|
|
||||||
$errorLine = @(Start-Utf8Process $omp "print error --config=""$Env:POSH_THEME""") -join "`n"
|
|
||||||
Set-PSReadLineOption -PromptText $validLine, $errorLine
|
|
||||||
}
|
|
||||||
|
|
||||||
<#
|
|
||||||
.SYNOPSIS
|
|
||||||
Returns an ansi formatted hyperlink
|
|
||||||
if name not set, uri is used as the name of the hyperlink
|
|
||||||
.EXAMPLE
|
|
||||||
Get-Hyperlink
|
|
||||||
#>
|
|
||||||
function global:Get-Hyperlink {
|
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory, ValuefromPipeline = $True)]
|
[Parameter(Mandatory, ValuefromPipeline = $True)]
|
||||||
[string]$uri,
|
[string]$uri,
|
||||||
|
@ -216,16 +150,18 @@ function global:Get-Hyperlink {
|
||||||
)
|
)
|
||||||
$esc = [char]27
|
$esc = [char]27
|
||||||
if ("" -eq $name) {
|
if ("" -eq $name) {
|
||||||
|
# if name not set, uri is used as the name of the hyperlink
|
||||||
$name = $uri
|
$name = $uri
|
||||||
}
|
}
|
||||||
if ($null -ne $env:WSL_DISTRO_NAME) {
|
if ($null -ne $env:WSL_DISTRO_NAME) {
|
||||||
# wsl conversion if needed
|
# wsl conversion if needed
|
||||||
$uri = &wslpath -m $uri
|
$uri = &wslpath -m $uri
|
||||||
}
|
}
|
||||||
|
# return an ANSI formatted hyperlink
|
||||||
return "$esc]8;;file://$uri$esc\$name$esc]8;;$esc\"
|
return "$esc]8;;file://$uri$esc\$name$esc]8;;$esc\"
|
||||||
}
|
}
|
||||||
|
|
||||||
function global:Get-PoshThemes() {
|
function Get-PoshThemes {
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory = $false, HelpMessage = "The themes folder")]
|
[Parameter(Mandatory = $false, HelpMessage = "The themes folder")]
|
||||||
[string]
|
[string]
|
||||||
|
@ -256,23 +192,94 @@ function global:Get-PoshThemes() {
|
||||||
Write-Host $logo
|
Write-Host $logo
|
||||||
$themes = Get-ChildItem -Path "$Path\*" -Include '*.omp.json' | Sort-Object Name
|
$themes = Get-ChildItem -Path "$Path\*" -Include '*.omp.json' | Sort-Object Name
|
||||||
if ($List -eq $true) {
|
if ($List -eq $true) {
|
||||||
$themes | Select-Object @{ Name = 'hyperlink'; Expression = { Get-Hyperlink -uri $_.fullname } } | Format-Table -HideTableHeaders
|
$themes | Select-Object @{ Name = 'hyperlink'; Expression = { Get-FileHyperlink -uri $_.FullName } } | Format-Table -HideTableHeaders
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
$omp = '::OMP::'
|
|
||||||
$themes | ForEach-Object -Process {
|
$themes | ForEach-Object -Process {
|
||||||
Write-Host "Theme: $(Get-Hyperlink -uri $_.fullname -name $_.BaseName.Replace('.omp', ''))`n"
|
Write-Host "Theme: $(Get-FileHyperlink -uri $_.FullName -Name ($_.BaseName -replace '\.omp$', ''))`n"
|
||||||
@(Start-Utf8Process $omp "print primary --config=""$($_.FullName)"" --pwd=""$PWD"" --shell pwsh")
|
@(Start-Utf8Process $script:OMPExecutable "print primary --config=""$($_.FullName)"" --pwd=""$PWD"" --shell pwsh")
|
||||||
Write-Host "`n"
|
Write-Host "`n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Write-Host @"
|
Write-Host @"
|
||||||
|
|
||||||
Themes location: $(Get-Hyperlink -uri "$Path")
|
Themes location: $(Get-FileHyperlink -uri "$Path")
|
||||||
|
|
||||||
To change your theme, adjust the init script in $PROFILE.
|
To change your theme, adjust the init script in $PROFILE.
|
||||||
Example:
|
Example:
|
||||||
oh-my-posh init pwsh --config $Path/jandedobbeleer.omp.json | Invoke-Expression
|
oh-my-posh init pwsh --config $Path/jandedobbeleer.omp.json | Invoke-Expression
|
||||||
|
|
||||||
"@
|
"@
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function prompt {
|
||||||
|
# store if the last command was successful
|
||||||
|
$lastCommandSuccess = $?
|
||||||
|
# store the last exit code for restore
|
||||||
|
$realLASTEXITCODE = $global:LASTEXITCODE
|
||||||
|
$cleanPWD, $cleanPSWD = Get-PoshContext
|
||||||
|
if ($script:TransientPrompt -eq $true) {
|
||||||
|
@(Start-Utf8Process $script:OMPExecutable "print transient --error=$script:ErrorCode --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --execution-time=$script:ExecutionTime --config=""$env:POSH_THEME"" --shell-version=""$script:PSVersion""") -join "`n"
|
||||||
|
$script:TransientPrompt = $false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (Test-Path variable:/PSDebugContext) {
|
||||||
|
@(Start-Utf8Process $script:OMPExecutable "print debug --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --config=""$env:POSH_THEME""") -join "`n"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Initialize-ModuleSupport
|
||||||
|
|
||||||
|
$script:ExecutionTime = -1
|
||||||
|
$lastHistory = Get-History -ErrorAction Ignore -Count 1
|
||||||
|
if ($null -ne $lastHistory -and $script:LastHistoryId -ne $lastHistory.Id) {
|
||||||
|
$script:LastHistoryId = $lastHistory.Id
|
||||||
|
$script:ExecutionTime = ($lastHistory.EndExecutionTime - $lastHistory.StartExecutionTime).TotalMilliseconds
|
||||||
|
# error code should be changed only when a non-empty command is called
|
||||||
|
$script:ErrorCode = 0
|
||||||
|
if (!$lastCommandSuccess) {
|
||||||
|
$invocationInfo = try {
|
||||||
|
# retrieve info of the most recent error
|
||||||
|
$global:Error[0] | Where-Object { $_ -ne $null } | Select-Object -ExpandProperty InvocationInfo
|
||||||
|
} catch { $null }
|
||||||
|
# check if the last command caused the last error
|
||||||
|
if ($null -ne $invocationInfo -and $lastHistory.CommandLine -eq $invocationInfo.Line) {
|
||||||
|
$script:ErrorCode = 1
|
||||||
|
} elseif ($realLASTEXITCODE -is [int] -and $realLASTEXITCODE -ne 0) {
|
||||||
|
# native app exit code
|
||||||
|
$script:ErrorCode = $realLASTEXITCODE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$stackCount = (Get-Location -Stack).Count
|
||||||
|
try {
|
||||||
|
if ($null -ne $global:OMP_GLOBAL_SESSIONSTATE) {
|
||||||
|
$stackCount = $global:OMP_GLOBAL_SESSIONSTATE.Path.LocationStack('').Count
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
Set-PoshContext
|
||||||
|
$terminalWidth = $Host.UI.RawUI.WindowSize.Width
|
||||||
|
$standardOut = @(Start-Utf8Process $script:OMPExecutable "print primary --error=$script:ErrorCode --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --execution-time=$script:ExecutionTime --stack-count=$stackCount --config=""$env:POSH_THEME"" --shell-version=""$script:PSVersion"" --terminal-width=$terminalWidth")
|
||||||
|
# make sure PSReadLine knows we have a multiline prompt
|
||||||
|
$extraLines = ($standardOut | Measure-Object -Line).Lines - 1
|
||||||
|
if ($extraLines -gt 0) {
|
||||||
|
Set-PSReadlineOption -ExtraPromptLineCount $extraLines
|
||||||
|
}
|
||||||
|
# the output can be multiline, joining these ensures proper rendering by adding line breaks with `n
|
||||||
|
$standardOut -join "`n"
|
||||||
|
$global:LASTEXITCODE = $realLASTEXITCODE
|
||||||
|
}
|
||||||
|
|
||||||
|
# set secondary prompt
|
||||||
|
Set-PSReadLineOption -ContinuationPrompt (@(Start-Utf8Process $script:OMPExecutable "print secondary --config=""$env:POSH_THEME""") -join "`n")
|
||||||
|
|
||||||
|
Export-ModuleMember -Function @(
|
||||||
|
"Set-PoshContext"
|
||||||
|
"Enable-PoshTooltips"
|
||||||
|
"Enable-PoshTransientPrompt"
|
||||||
|
"Enable-PoshLineError"
|
||||||
|
"Export-PoshTheme"
|
||||||
|
"Get-PoshThemes"
|
||||||
|
"prompt"
|
||||||
|
)
|
||||||
|
} | Import-Module -Global
|
||||||
|
|
Loading…
Reference in a new issue