refactor: align pwsh module with init

This commit is contained in:
Jan De Dobbeleer 2021-01-17 13:13:00 +01:00 committed by Jan De Dobbeleer
parent e26326001e
commit edebc1d465
2 changed files with 10 additions and 69 deletions

View file

@ -3,10 +3,6 @@
Generates the prompt before each line in the console Generates the prompt before each line in the console
#> #>
$global:PoshSettings = New-Object -TypeName PSObject -Property @{
Theme = "$PSScriptRoot\themes\jandedobbeleer.json";
}
function Get-PoshCommand { function Get-PoshCommand {
$poshCommand = "$PSScriptRoot/bin/posh-windows-amd64.exe" $poshCommand = "$PSScriptRoot/bin/posh-windows-amd64.exe"
if ($IsMacOS) { if ($IsMacOS) {
@ -24,15 +20,6 @@ if ($PSVersionTable.PSEdition -eq "Core" -and !$IsWindows) {
Invoke-Expression -Command "chmod +x $executable" Invoke-Expression -Command "chmod +x $executable"
} }
function Set-PoshContext {}
function Set-GitStatus {
if (Get-Module -Name "posh-git") {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideCommentHelp', '', Justification='Variable used later(not in this scope)')]
$Global:GitStatus = Get-GitStatus
}
}
function Set-PoshPrompt { function Set-PoshPrompt {
param( param(
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
@ -40,66 +27,19 @@ function Set-PoshPrompt {
$Theme $Theme
) )
$config = ""
if (Test-Path "$PSScriptRoot/themes/$Theme.omp.json") { if (Test-Path "$PSScriptRoot/themes/$Theme.omp.json") {
$global:PoshSettings.Theme = "$PSScriptRoot/themes/$Theme.omp.json" $config = "$PSScriptRoot/themes/$Theme.omp.json"
} }
elseif (Test-Path $Theme) { elseif (Test-Path $Theme) {
$global:PoshSettings.Theme = Resolve-Path -Path $Theme $config = Resolve-Path -Path $Theme
} }
else { else {
$global:PoshSettings.Theme = "$PSScriptRoot/themes/jandedobbeleer.omp.json" $config = "$PSScriptRoot/themes/jandedobbeleer.omp.json"
} }
[ScriptBlock]$Prompt = { $poshCommand = Get-PoshCommand
#store if the last command was successfull Invoke-Expression (& $poshCommand --init --shell pwsh --config $config)
$lastCommandSuccess = $?
#store the last exit code for restore
$realLASTEXITCODE = $global:LASTEXITCODE
$errorCode = 0
Set-PoshContext
if ($lastCommandSuccess -eq $false) {
#native app exit code
if ($realLASTEXITCODE -is [int] -and $realLASTEXITCODE -gt 0) {
$errorCode = $realLASTEXITCODE
}
else {
$errorCode = 1
}
}
$executionTime = -1
$history = Get-History -ErrorAction Ignore -Count 1
if ($null -ne $history -and $null -ne $history.EndExecutionTime -and $null -ne $history.StartExecutionTime) {
$executionTime = ($history.EndExecutionTime - $history.StartExecutionTime).TotalMilliseconds
}
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = Get-PoshCommand
$config = $global:PoshSettings.Theme
$cleanPWD = $PWD.ProviderPath.TrimEnd("\")
$cleanPSWD = $PWD.ToString().TrimEnd("\")
$startInfo.Arguments = "--config=""$config"" --error=$errorCode --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --execution-time=$executionTime"
$startInfo.Environment["TERM"] = "xterm-256color"
$startInfo.CreateNoWindow = $true
$startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8
$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
if ($PWD.Provider.Name -eq 'FileSystem') {
$startInfo.WorkingDirectory = $PWD.ProviderPath
}
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$standardOut = $process.StandardOutput.ReadToEnd()
$process.WaitForExit()
$standardOut
Set-GitStatus
$global:LASTEXITCODE = $realLASTEXITCODE
#remove temp variables
Remove-Variable realLASTEXITCODE -Confirm:$false
Remove-Variable lastCommandSuccess -Confirm:$false
}
Set-Item -Path Function:prompt -Value $Prompt -Force
} }
function Get-PoshThemes { function Get-PoshThemes {

View file

@ -5,9 +5,10 @@ $global:PoshSettings = New-Object -TypeName PSObject -Property @{
if (Test-Path "::CONFIG::") { if (Test-Path "::CONFIG::") {
$global:PoshSettings.Theme = Resolve-Path -Path "::CONFIG::" $global:PoshSettings.Theme = Resolve-Path -Path "::CONFIG::"
} }
function Set-PoshContext {}
function Set-GitStatus { function global:Set-PoshContext {}
function global:Set-PoshGitStatus {
if (Get-Module -Name "posh-git") { if (Get-Module -Name "posh-git") {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideCommentHelp', '', Justification='Variable used later(not in this scope)')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideCommentHelp', '', Justification='Variable used later(not in this scope)')]
$Global:GitStatus = Get-GitStatus $Global:GitStatus = Get-GitStatus
@ -47,7 +48,7 @@ function Set-GitStatus {
# Restore initial encoding # Restore initial encoding
[Console]::OutputEncoding = $originalOutputEncoding [Console]::OutputEncoding = $originalOutputEncoding
$standardOut $standardOut
Set-GitStatus Set-PoshGitStatus
$global:LASTEXITCODE = $realLASTEXITCODE $global:LASTEXITCODE = $realLASTEXITCODE
#remove temp variables #remove temp variables
Remove-Variable realLASTEXITCODE -Confirm:$false Remove-Variable realLASTEXITCODE -Confirm:$false