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
2021-05-16 10:13:15 -07:00
$env:POWERLINE_COMMAND = " oh-my-posh "
2021-05-20 10:24:58 -07:00
$env:CONDA_PROMPT_MODIFIER = $false
2021-03-14 00:22:01 -08:00
2021-05-20 23:50:41 -07:00
# specific module support (disabled by default)
function Set-DefaultEnvValue {
[ CmdletBinding ( ) ]
param (
[ Parameter ( Mandatory = $true ) ]
[ string ]
$Name
)
$value = [ System.Environment ] :: GetEnvironmentVariable ( $Name )
if ( $value -eq $null ) {
[ System.Environment ] :: SetEnvironmentVariable ( $Name , $false )
}
}
Set-DefaultEnvValue ( " AZ_ENABLED " )
Set-DefaultEnvValue ( " POSH_GIT_ENABLED " )
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 ) {
2021-05-20 23:23:12 -07:00
$global:PoshSettings . Theme = ( Resolve-Path -Path $config ) . ProviderPath
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-05-20 23:50:41 -07:00
if ( $env:POSH_GIT_ENABLED -eq $true -and ( 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-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 {
2021-05-21 10:56:31 -07:00
$subscription = Get-AzContext | Select-Object -ExpandProperty " Subscription " | Select-Object " Name " , " Id " , " Account "
2021-03-27 06:52:27 -07:00
if ( $null -ne $subscription ) {
$env:AZ_SUBSCRIPTION_NAME = $subscription . Name
$env:AZ_SUBSCRIPTION_ID = $subscription . Id
2021-05-21 10:56:31 -07:00
$env:AZ_SUBSCRIPTION_ACCOUNT = $subscription . Account
2021-03-27 06:52:27 -07:00
}
}
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
2021-04-17 11:35:20 -07:00
Initialize-ModuleSupport
2020-12-22 10:31:20 -08:00
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-14 05:29:31 -07:00
# 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))
$stackCount = ( Get-Location -stack ) . Count
2021-04-13 15:15:32 -07:00
try {
2021-05-04 22:18:29 -07:00
if ( $global:omp_global_sessionstate -ne $null ) {
2021-04-14 05:29:31 -07:00
$stackCount = ( $global:omp_global_sessionstate ) . path . locationstack ( '' ) . count
}
2021-04-13 15:15:32 -07:00
}
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-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
2021-05-04 22:18:29 -07:00
<#
. SYNOPSIS
Exports the current oh-my -posh theme
. DESCRIPTION
By default the config is exported in json to the clipboard
. EXAMPLE
Export-PoshTheme
Current theme exported in json to clipboard
. EXAMPLE
Export-PoshTheme -Format toml
Current theme exported in toml to clipboard
. EXAMPLE
Export-PoshTheme c: \ temp \ theme . toml toml
Current theme exported in toml to c: \ temp \ theme . toml
. EXAMPLE
Export-PoshTheme ~ \ theme . toml toml
Current theme exported in toml to your home \ theme . toml
#>
2021-03-20 11:32:15 -07:00
function global: Export-PoshTheme {
param (
2021-05-04 22:18:29 -07:00
[ Parameter ( Mandatory = $false ) ]
2021-03-20 11:32:15 -07:00
[ string ]
2021-05-04 22:18:29 -07:00
# The file path where the theme will be exported. If not provided, the config is copied to the clipboard by default.
2021-03-20 11:32:15 -07:00
$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 ]
2021-05-04 22:18:29 -07:00
# The format of the theme
2021-03-20 11:32:15 -07:00
$Format = 'json'
)
$config = $global:PoshSettings . Theme
$omp = " ::OMP:: "
$configString = @ ( & $omp - -config = " $config " - -config -format = " $Format " - -print -config 2 > & 1 )
2021-05-04 22:18:29 -07:00
# if no path, copy to clipboard by default
if ( $FilePath -ne " " ) {
2021-05-27 21:59:24 -07:00
if ( $FilePath . StartsWith ( '~' ) ) {
$FilePath = $FilePath . Replace ( '~' , $HOME )
}
2021-05-27 21:24:51 -07:00
$FilePath = [ IO.Path ] :: GetFullPath ( $FilePath )
2021-05-04 22:18:29 -07:00
[ IO.File ] :: WriteAllLines ( $FilePath , $configString )
}
else {
Set-Clipboard $configString
Write-Output " Theme copied to clipboard "
}
2021-03-20 11:32:15 -07:00
}
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 "
}