@@ -97,6 +97,8 @@ $global:GitPromptSettings = [pscustomobject]@{
9797
9898 EnableWindowTitle = ' posh~git ~ '
9999
100+ AnsiConsole = $Host.UI.SupportsVirtualTerminal -or ($Env: ConEmuANSI -eq " ON" )
101+
100102 DefaultPromptPrefix = ' '
101103 DefaultPromptSuffix = ' $('' >'' * ($nestedPromptLevel + 1)) '
102104 DefaultPromptDebugSuffix = ' [DBG]$('' >'' * ($nestedPromptLevel + 1)) '
@@ -127,12 +129,35 @@ if (Get-Module NuGet) {
127129 $WindowTitleSupported = $false
128130}
129131
130- function Write-Prompt ($Object , $ForegroundColor , $BackgroundColor = -1 ) {
132+ function Write-Prompt {
133+ param (
134+ [parameter (Mandatory = $true )]
135+ $Object ,
136+ [parameter (Mandatory = $true )]
137+ $ForegroundColor ,
138+ [parameter ()]
139+ $BackgroundColor = $null ,
140+ [parameter (ValueFromPipeline = $true )]
141+ [Text.StringBuilder ]
142+ $Builder )
143+ if ($GitPromptSettings.AnsiConsole ) {
144+ $e = [char ]27 + " ["
145+ $f = Get-ForegroundVirtualTerminalSequence $ForegroundColor
146+ $b = Get-BackgroundVirtualTerminalSequence $BackgroundColor
147+ if ($Builder ) {
148+ return $Builder.Append ($f ).Append($b ).Append($Object ).Append($e ).Append(" 0m" )
149+ }
150+ return " ${f}${b}${Object}${e} 0m"
151+ }
131152 if ($BackgroundColor -lt 0 ) {
132153 Write-Host $Object - NoNewLine - ForegroundColor $ForegroundColor
133154 } else {
134155 Write-Host $Object - NoNewLine - ForegroundColor $ForegroundColor - BackgroundColor $BackgroundColor
135156 }
157+ if ($Builder ) {
158+ return $Builder
159+ }
160+ return " "
136161}
137162
138163function Format-BranchName ($branchName ){
@@ -148,8 +173,9 @@ function Format-BranchName($branchName){
148173
149174function Write-GitStatus ($status ) {
150175 $s = $global :GitPromptSettings
176+ $sb = New-Object System.Text.StringBuilder
151177 if ($status -and $s ) {
152- Write-Prompt $s.BeforeText - BackgroundColor $s.BeforeBackgroundColor - ForegroundColor $s.BeforeForegroundColor
178+ $sb | Write-Prompt $s.BeforeText - BackgroundColor $s.BeforeBackgroundColor - ForegroundColor $s.BeforeForegroundColor | Out-Null
153179
154180 $branchStatusText = $null
155181 $branchStatusBackgroundColor = $s.BranchBackgroundColor
@@ -201,47 +227,47 @@ function Write-GitStatus($status) {
201227 $branchStatusText = " ?"
202228 }
203229
204- Write-Prompt (Format-BranchName ($status.Branch )) - BackgroundColor $branchStatusBackgroundColor - ForegroundColor $branchStatusForegroundColor
230+ $sb | Write-Prompt (Format-BranchName ($status.Branch )) - BackgroundColor $branchStatusBackgroundColor - ForegroundColor $branchStatusForegroundColor | Out-Null
205231
206232 if ($branchStatusText ) {
207- Write-Prompt (" {0}" -f $branchStatusText ) - BackgroundColor $branchStatusBackgroundColor - ForegroundColor $branchStatusForegroundColor
233+ $sb | Write-Prompt (" {0}" -f $branchStatusText ) - BackgroundColor $branchStatusBackgroundColor - ForegroundColor $branchStatusForegroundColor | Out-Null
208234 }
209235
210236 if ($s.EnableFileStatus -and $status.HasIndex ) {
211- Write-Prompt $s.BeforeIndexText - BackgroundColor $s.BeforeIndexBackgroundColor - ForegroundColor $s.BeforeIndexForegroundColor
237+ $sb | Write-Prompt $s.BeforeIndexText - BackgroundColor $s.BeforeIndexBackgroundColor - ForegroundColor $s.BeforeIndexForegroundColor | Out-Null
212238
213239 if ($s.ShowStatusWhenZero -or $status.Index.Added ) {
214- Write-Prompt (" $ ( $s.FileAddedText ) $ ( $status.Index.Added.Count ) " ) - BackgroundColor $s.IndexBackgroundColor - ForegroundColor $s.IndexForegroundColor
240+ $sb | Write-Prompt (" $ ( $s.FileAddedText ) $ ( $status.Index.Added.Count ) " ) - BackgroundColor $s.IndexBackgroundColor - ForegroundColor $s.IndexForegroundColor | Out-Null
215241 }
216242 if ($s.ShowStatusWhenZero -or $status.Index.Modified ) {
217- Write-Prompt (" $ ( $s.FileModifiedText ) $ ( $status.Index.Modified.Count ) " ) - BackgroundColor $s.IndexBackgroundColor - ForegroundColor $s.IndexForegroundColor
243+ $sb | Write-Prompt (" $ ( $s.FileModifiedText ) $ ( $status.Index.Modified.Count ) " ) - BackgroundColor $s.IndexBackgroundColor - ForegroundColor $s.IndexForegroundColor | Out-Null
218244 }
219245 if ($s.ShowStatusWhenZero -or $status.Index.Deleted ) {
220- Write-Prompt (" $ ( $s.FileRemovedText ) $ ( $status.Index.Deleted.Count ) " ) - BackgroundColor $s.IndexBackgroundColor - ForegroundColor $s.IndexForegroundColor
246+ $sb | Write-Prompt (" $ ( $s.FileRemovedText ) $ ( $status.Index.Deleted.Count ) " ) - BackgroundColor $s.IndexBackgroundColor - ForegroundColor $s.IndexForegroundColor | Out-Null
221247 }
222248
223249 if ($status.Index.Unmerged ) {
224- Write-Prompt (" $ ( $s.FileConflictedText ) $ ( $status.Index.Unmerged.Count ) " ) - BackgroundColor $s.IndexBackgroundColor - ForegroundColor $s.IndexForegroundColor
250+ $sb | Write-Prompt (" $ ( $s.FileConflictedText ) $ ( $status.Index.Unmerged.Count ) " ) - BackgroundColor $s.IndexBackgroundColor - ForegroundColor $s.IndexForegroundColor | Out-Null
225251 }
226252
227253 if ($status.HasWorking ) {
228- Write-Prompt $s.DelimText - BackgroundColor $s.DelimBackgroundColor - ForegroundColor $s.DelimForegroundColor
254+ $sb | Write-Prompt $s.DelimText - BackgroundColor $s.DelimBackgroundColor - ForegroundColor $s.DelimForegroundColor | Out-Null
229255 }
230256 }
231257
232258 if ($s.EnableFileStatus -and $status.HasWorking ) {
233259 if ($s.ShowStatusWhenZero -or $status.Working.Added ) {
234- Write-Prompt (" $ ( $s.FileAddedText ) $ ( $status.Working.Added.Count ) " ) - BackgroundColor $s.WorkingBackgroundColor - ForegroundColor $s.WorkingForegroundColor
260+ $sb | Write-Prompt (" $ ( $s.FileAddedText ) $ ( $status.Working.Added.Count ) " ) - BackgroundColor $s.WorkingBackgroundColor - ForegroundColor $s.WorkingForegroundColor | Out-Null
235261 }
236262 if ($s.ShowStatusWhenZero -or $status.Working.Modified ) {
237- Write-Prompt (" $ ( $s.FileModifiedText ) $ ( $status.Working.Modified.Count ) " ) - BackgroundColor $s.WorkingBackgroundColor - ForegroundColor $s.WorkingForegroundColor
263+ $sb | Write-Prompt (" $ ( $s.FileModifiedText ) $ ( $status.Working.Modified.Count ) " ) - BackgroundColor $s.WorkingBackgroundColor - ForegroundColor $s.WorkingForegroundColor | Out-Null
238264 }
239265 if ($s.ShowStatusWhenZero -or $status.Working.Deleted ) {
240- Write-Prompt (" $ ( $s.FileRemovedText ) $ ( $status.Working.Deleted.Count ) " ) - BackgroundColor $s.WorkingBackgroundColor - ForegroundColor $s.WorkingForegroundColor
266+ $sb | Write-Prompt (" $ ( $s.FileRemovedText ) $ ( $status.Working.Deleted.Count ) " ) - BackgroundColor $s.WorkingBackgroundColor - ForegroundColor $s.WorkingForegroundColor | Out-Null
241267 }
242268
243269 if ($status.Working.Unmerged ) {
244- Write-Prompt (" $ ( $s.FileConflictedText ) $ ( $status.Working.Unmerged.Count ) " ) - BackgroundColor $s.WorkingBackgroundColor - ForegroundColor $s.WorkingForegroundColor
270+ $sb | Write-Prompt (" $ ( $s.FileConflictedText ) $ ( $status.Working.Unmerged.Count ) " ) - BackgroundColor $s.WorkingBackgroundColor - ForegroundColor $s.WorkingForegroundColor | Out-Null
245271 }
246272 }
247273
@@ -263,16 +289,16 @@ function Write-GitStatus($status) {
263289 }
264290
265291 if ($localStatusSymbol ) {
266- Write-Prompt (" {0}" -f $localStatusSymbol ) - BackgroundColor $localStatusBackgroundColor - ForegroundColor $localStatusForegroundColor
292+ $sb | Write-Prompt (" {0}" -f $localStatusSymbol ) - BackgroundColor $localStatusBackgroundColor - ForegroundColor $localStatusForegroundColor | Out-Null
267293 }
268294
269295 if ($s.EnableStashStatus -and ($status.StashCount -gt 0 )) {
270- Write-Prompt $s.BeforeStashText - BackgroundColor $s.BeforeStashBackgroundColor - ForegroundColor $s.BeforeStashForegroundColor
271- Write-Prompt $status.StashCount - BackgroundColor $s.StashBackgroundColor - ForegroundColor $s.StashForegroundColor
272- Write-Prompt $s.AfterStashText - BackgroundColor $s.AfterStashBackgroundColor - ForegroundColor $s.AfterStashForegroundColor
296+ $sb | Write-Prompt $s.BeforeStashText - BackgroundColor $s.BeforeStashBackgroundColor - ForegroundColor $s.BeforeStashForegroundColor | Out-Null
297+ $sb | Write-Prompt $status.StashCount - BackgroundColor $s.StashBackgroundColor - ForegroundColor $s.StashForegroundColor | Out-Null
298+ $sb | Write-Prompt $s.AfterStashText - BackgroundColor $s.AfterStashBackgroundColor - ForegroundColor $s.AfterStashForegroundColor | Out-Null
273299 }
274300
275- Write-Prompt $s.AfterText - BackgroundColor $s.AfterBackgroundColor - ForegroundColor $s.AfterForegroundColor
301+ $sb | Write-Prompt $s.AfterText - BackgroundColor $s.AfterBackgroundColor - ForegroundColor $s.AfterForegroundColor | Out-Null
276302
277303 if ($WindowTitleSupported -and $s.EnableWindowTitle ) {
278304 if ( -not $Global :PreviousWindowTitle ) {
@@ -282,8 +308,11 @@ function Write-GitStatus($status) {
282308 $prefix = if ($s.EnableWindowTitle -is [string ]) { $s.EnableWindowTitle } else { ' ' }
283309 $Host.UI.RawUI.WindowTitle = " $script :adminHeader$prefix$repoName [$ ( $status.Branch ) ]"
284310 }
311+
312+ return $sb.ToString ()
285313 } elseif ( $Global :PreviousWindowTitle ) {
286314 $Host.UI.RawUI.WindowTitle = $Global :PreviousWindowTitle
315+ return " "
287316 }
288317}
289318
@@ -304,6 +333,7 @@ if ($Host.UI.RawUI.BackgroundColor -eq [ConsoleColor]::DarkMagenta) {
304333}
305334
306335function Global :Write-VcsStatus {
336+ Set-ConsoleMode - ANSI
307337 $Global :VcsPromptStatuses | ForEach-Object { & $_ }
308338}
309339
0 commit comments