2021-03-14 00:22:01 -08:00
|
|
|
# Powershell doesn't default to UTF8 just yet, so we're forcing it as there are too many problems
|
|
|
|
# that pop up when we don't
|
|
|
|
[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
|
|
|
|
|
2020-12-22 10:31:20 -08:00
|
|
|
$global:PoshSettings = New-Object -TypeName PSObject -Property @{
|
|
|
|
Theme = "";
|
|
|
|
}
|
|
|
|
|
2021-03-18 13:24:26 -07:00
|
|
|
# used to detect empty hit
|
|
|
|
$global:omp_lastHistoryId = -1
|
|
|
|
|
2021-01-18 09:58:17 -08:00
|
|
|
$config = "::CONFIG::"
|
|
|
|
if (Test-Path $config) {
|
|
|
|
$global:PoshSettings.Theme = (Resolve-Path -Path $config).Path
|
2020-12-22 10:31:20 -08:00
|
|
|
}
|
|
|
|
|
2021-01-17 04:13:00 -08:00
|
|
|
function global:Set-PoshContext {}
|
|
|
|
|
2021-03-27 06:52:27 -07:00
|
|
|
function global:Initialize-ModuleSupport {
|
2021-01-07 04:17:06 -08:00
|
|
|
if (Get-Module -Name "posh-git") {
|
2021-03-18 13:24:26 -07:00
|
|
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideCommentHelp', '', Justification = 'Variable used later(not in this scope)')]
|
2021-03-27 09:04:09 -07:00
|
|
|
$global:GitStatus = Get-GitStatus
|
|
|
|
$env:POSH_GIT_STATUS = Write-GitStatus -Status $global:GitStatus
|
2020-12-22 10:31:20 -08:00
|
|
|
}
|
2021-03-27 06:52:27 -07:00
|
|
|
|
2021-04-04 16:47:03 -07:00
|
|
|
|
|
|
|
if ($null -eq $env:AZ_ENABLED) {
|
|
|
|
if (Get-Module -ListAvailable -Name "Az.Accounts") {
|
|
|
|
$env:AZ_ENABLED = $true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$env:AZ_ENABLED = $false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 15:52:43 -07:00
|
|
|
$env:AZ_SUBSCRIPTION_NAME = $null
|
|
|
|
$env:AZ_SUBSCRIPTION_ID = $null
|
|
|
|
|
2021-04-04 16:47:03 -07:00
|
|
|
if ($env:AZ_ENABLED -eq $true) {
|
2021-03-27 06:52:27 -07:00
|
|
|
try {
|
|
|
|
$subscription = Get-AzContext | Select-Object -ExpandProperty "Subscription" | Select-Object "Name", "Id"
|
|
|
|
if ($null -ne $subscription) {
|
|
|
|
$env:AZ_SUBSCRIPTION_NAME = $subscription.Name
|
|
|
|
$env:AZ_SUBSCRIPTION_ID = $subscription.Id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch {}
|
|
|
|
}
|
2020-12-22 10:31:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[ScriptBlock]$Prompt = {
|
2021-03-15 09:49:13 -07:00
|
|
|
#store if the last command was successful
|
2020-12-22 10:31:20 -08:00
|
|
|
$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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 15:15:32 -07:00
|
|
|
$stackCount = 0
|
|
|
|
try {
|
|
|
|
$stackCount = ($global:omp_global_sessionstate).path.locationstack('').count
|
|
|
|
}
|
|
|
|
catch {}
|
|
|
|
|
2020-12-22 10:31:20 -08:00
|
|
|
$executionTime = -1
|
|
|
|
$history = Get-History -ErrorAction Ignore -Count 1
|
2021-03-18 13:24:26 -07:00
|
|
|
if ($null -ne $history -and $null -ne $history.EndExecutionTime -and $null -ne $history.StartExecutionTime -and $global:omp_lastHistoryId -ne $history.Id) {
|
2021-03-27 06:52:27 -07:00
|
|
|
$executionTime = ($history.EndExecutionTime - $history.StartExecutionTime).TotalMilliseconds
|
|
|
|
$global:omp_lastHistoryId = $history.Id
|
2020-12-22 10:31:20 -08:00
|
|
|
}
|
2021-03-27 06:52:27 -07:00
|
|
|
Initialize-ModuleSupport
|
2021-01-17 04:08:08 -08:00
|
|
|
$omp = "::OMP::"
|
2020-12-22 10:31:20 -08:00
|
|
|
$config = $global:PoshSettings.Theme
|
|
|
|
$cleanPWD = $PWD.ProviderPath.TrimEnd("\")
|
2021-01-09 04:48:54 -08:00
|
|
|
$cleanPSWD = $PWD.ToString().TrimEnd("\")
|
2021-04-12 01:58:03 -07:00
|
|
|
$standardOut = @(&$omp --error="$errorCode" --pwd="$cleanPWD" --pswd="$cleanPSWD" --execution-time="$executionTime" --stack-count="$stackCount" --config="$config" 2>&1)
|
2021-03-15 09:49:13 -07:00
|
|
|
# the output can be multiline, joining these ensures proper rendering by adding line breaks with `n
|
2021-01-18 02:16:31 -08:00
|
|
|
$standardOut -join "`n"
|
2020-12-22 10:31:20 -08:00
|
|
|
$global:LASTEXITCODE = $realLASTEXITCODE
|
|
|
|
#remove temp variables
|
|
|
|
Remove-Variable realLASTEXITCODE -Confirm:$false
|
|
|
|
Remove-Variable lastCommandSuccess -Confirm:$false
|
|
|
|
}
|
|
|
|
Set-Item -Path Function:prompt -Value $Prompt -Force
|
2021-03-10 23:32:32 -08:00
|
|
|
|
|
|
|
function global:Write-PoshDebug {
|
|
|
|
$omp = "::OMP::"
|
|
|
|
$config = $global:PoshSettings.Theme
|
|
|
|
$cleanPWD = $PWD.ProviderPath.TrimEnd("\")
|
|
|
|
$cleanPSWD = $PWD.ToString().TrimEnd("\")
|
|
|
|
$standardOut = @(&$omp --error=1337 --pwd="$cleanPWD" --pswd="$cleanPSWD" --execution-time=9001 --config="$config" --debug 2>&1)
|
2021-03-14 00:22:01 -08:00
|
|
|
$standardOut -join "`n"
|
2021-03-10 23:32:32 -08:00
|
|
|
}
|
2021-03-20 11:32:15 -07:00
|
|
|
|
|
|
|
function global:Export-PoshTheme {
|
|
|
|
param(
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
|
|
[string]
|
|
|
|
$FilePath,
|
|
|
|
[Parameter(Mandatory = $false)]
|
2021-03-27 06:52:27 -07:00
|
|
|
[ValidateSet('json', 'yaml', 'toml')]
|
2021-03-20 11:32:15 -07:00
|
|
|
[string]
|
|
|
|
$Format = 'json'
|
|
|
|
)
|
|
|
|
|
2021-03-26 10:56:40 -07:00
|
|
|
if ($FilePath.StartsWith('~')) {
|
|
|
|
$FilePath = $FilePath.Replace('~', $HOME)
|
|
|
|
}
|
|
|
|
|
2021-03-20 11:32:15 -07:00
|
|
|
$config = $global:PoshSettings.Theme
|
|
|
|
$omp = "::OMP::"
|
|
|
|
$configString = @(&$omp --config="$config" --config-format="$Format" --print-config 2>&1)
|
|
|
|
[IO.File]::WriteAllLines($FilePath, $configString)
|
|
|
|
}
|
2021-04-04 11:28:41 -07:00
|
|
|
|
|
|
|
function global:Export-PoshImage {
|
|
|
|
param(
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[int]
|
|
|
|
$RPromptOffset = 40,
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[int]
|
|
|
|
$CursorPadding = 30,
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[string]
|
|
|
|
$Author
|
|
|
|
)
|
|
|
|
|
|
|
|
if ($Author) {
|
|
|
|
$Author = "--author=$Author"
|
|
|
|
}
|
|
|
|
|
|
|
|
$omp = "::OMP::"
|
|
|
|
$config = $global:PoshSettings.Theme
|
|
|
|
$cleanPWD = $PWD.ProviderPath.TrimEnd("\")
|
|
|
|
$cleanPSWD = $PWD.ToString().TrimEnd("\")
|
|
|
|
$standardOut = @(&$omp --config="$config" --pwd="$cleanPWD" --pswd="$cleanPSWD" --export-png --rprompt-offset="$RPromptOffset" --cursor-padding="$CursorPadding" $Author 2>&1)
|
|
|
|
$standardOut -join "`n"
|
|
|
|
}
|