|
#requires -Version 2 -Modules posh-git |
|
|
|
function Write-Theme { |
|
param( |
|
[bool] |
|
$lastCommandFailed, |
|
[string] |
|
$with |
|
) |
|
|
|
# save exit code for use within this prompt, then clear it |
|
# this is because LASTEXITCODE is only changed by executable commands, not cmdlets, |
|
# so displaying it is misleading because it is not necessarily be the actual last thing run |
|
$actualLastExitCode = $LASTEXITCODE |
|
$global:LASTEXITCODE = $null |
|
|
|
$lastColor = $sl.Colors.PromptBackgroundColor |
|
$prompt = Write-Prompt -Object $sl.PromptSymbols.StartSymbol -ForegroundColor $sl.Colors.PromptForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor |
|
|
|
#check the last command state and indicate if failed |
|
If ($lastCommandFailed -or ($null -ne $actualLastExitCode -and $actualLastExitCode -ne 0)) { |
|
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.FailedCommandSymbol)$actualLastExitCode " -ForegroundColor $sl.Colors.CommandFailedIconForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor |
|
} |
|
|
|
#check for elevated prompt |
|
If (Test-Administrator) { |
|
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.ElevatedSymbol) " -ForegroundColor $sl.Colors.AdminIconForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor |
|
} |
|
|
|
$user = $sl.CurrentUser |
|
#$computer = [System.Environment]::MachineName |
|
$path = Get-FullPath -dir $pwd |
|
|
|
if (Test-NotDefaultUser($user)) { |
|
$prompt += Write-Prompt -Object "$user " -ForegroundColor $sl.Colors.SessionInfoForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor |
|
} |
|
|
|
if (Test-VirtualEnv) { |
|
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.VirtualEnvBackgroundColor |
|
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.VirtualEnvSymbol) $(Get-VirtualEnvName) " -ForegroundColor $sl.Colors.VirtualEnvForegroundColor -BackgroundColor $sl.Colors.VirtualEnvBackgroundColor |
|
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.VirtualEnvBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor |
|
} |
|
else { |
|
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor |
|
} |
|
|
|
# Writes the drive portion |
|
$pathSplit = $path.Split($sl.PromptSymbols.PathSeparator) |
|
if ($pathSplit.length -gt 6) { |
|
$compactPath = ($pathSplit | select -first 2) + @([char]::ConvertFromUtf32(0x2026)) + ($pathSplit | select -last 3) -join $sl.PromptSymbols.PathSeparator |
|
} else { |
|
$compactPath = $path; |
|
} |
|
$prompt += Write-Prompt -Object "$compactPath " -ForegroundColor $sl.Colors.PromptForegroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor |
|
|
|
$vcsStatus = Get-VCSStatus |
|
if ($vcsStatus) { |
|
$themeInfo = Get-VcsInfo -status ($vcsStatus) |
|
$lastColor = $themeInfo.BackgroundColor |
|
$prompt += Write-Prompt -Object $($sl.PromptSymbols.SegmentForwardSymbol) -ForegroundColor $sl.Colors.PromptBackgroundColor -BackgroundColor $lastColor |
|
$prompt += Write-Prompt -Object " $($themeInfo.VcInfo) " -BackgroundColor $lastColor -ForegroundColor $sl.Colors.GitForegroundColor |
|
} |
|
|
|
# Writes the postfix to the prompt |
|
$prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $lastColor |
|
|
|
$timestamp = "[$(Get-Date -UFormat %T)]" |
|
|
|
$prompt += Set-CursorForRightBlockWrite -textLength ($timestamp.Length + 1) |
|
$prompt += Write-Prompt $timeStamp -ForegroundColor $sl.Colors.PromptForegroundColor |
|
|
|
$prompt += Set-Newline |
|
if ($with) { |
|
$prompt += Write-Prompt -Object "$($with.ToUpper()) " -BackgroundColor $sl.Colors.WithBackgroundColor -ForegroundColor $sl.Colors.WithForegroundColor |
|
} |
|
$prompt += Write-Prompt -Object ($sl.PromptSymbols.PromptIndicator) -ForegroundColor $sl.Colors.PromptBackgroundColor |
|
$prompt += ' ' |
|
|
|
# set title |
|
$currentDir = Get-Item $pwd |
|
if ($vcsStatus) { |
|
$host.ui.rawui.WindowTitle = "$($vcsStatus.Branch) ($($currentDir.FullName))" |
|
} else { |
|
$host.ui.rawui.WindowTitle = "$($currentDir.Name) ($($currentDir.Parent.FullName))" |
|
} |
|
|
|
$prompt |
|
} |
|
|
|
$sl = $global:ThemeSettings #local settings |
|
$sl.PromptSymbols.StartSymbol = '' |
|
$sl.PromptSymbols.SegmentForwardSymbol = [char]::ConvertFromUtf32(0xE0B0) |
|
$sl.Colors.PromptForegroundColor = [ConsoleColor]::White |
|
$sl.Colors.PromptSymbolColor = [ConsoleColor]::White |
|
$sl.Colors.PromptHighlightColor = [ConsoleColor]::DarkBlue |
|
$sl.Colors.GitForegroundColor = [ConsoleColor]::Black |
|
$sl.Colors.WithForegroundColor = [ConsoleColor]::DarkRed |
|
$sl.Colors.WithBackgroundColor = [ConsoleColor]::Magenta |
|
$sl.Colors.VirtualEnvBackgroundColor = [System.ConsoleColor]::Red |
|
$sl.Colors.VirtualEnvForegroundColor = [System.ConsoleColor]::White |
|
|
|
$sl.PromptSymbols.PromptIndicator = [char]::ConvertFromUtf32(0x03BB) # Lambda |
|
$sl.PromptSymbols.FailedCommandSymbol = " $([char]::ConvertFromUtf32(0x20E0)) " # Circled slash |
|
|
|
$sl.Options.ConsoleTitle = $false # we do this ourselves |
|
$sl.Options.PreserveLastExitCode = $false # note: for >=2.0.465, requires https://github.com/JanDeDobbeleer/oh-my-posh/pull/307 |
|
|