Skip to content

Commit

Permalink
Use ANSI color codes if SupportsVirtualTerminal
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlbyk committed Sep 15, 2016
1 parent c39da78 commit ff6c66b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 20 deletions.
94 changes: 76 additions & 18 deletions GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,66 @@ if (Get-Module NuGet) {
$WindowTitleSupported = $false
}

# Color codes from https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx
function Get-ForegroundVirtualTerminalSequence($Color) {
$e = [char]27 + "["
switch ($Color) {
([ConsoleColor]::Black) { "${e}30m" }
([ConsoleColor]::DarkRed) { "${e}31m" }
([ConsoleColor]::DarkGreen) { "${e}32m" }
([ConsoleColor]::DarkYellow) { "${e}33m" }
([ConsoleColor]::DarkBlue) { "${e}34m" }
([ConsoleColor]::DarkMagenta) { "${e}35m" }
([ConsoleColor]::DarkCyan) { "${e}36m" }
([ConsoleColor]::Gray) { "${e}37m" }
([ConsoleColor]::DarkGray) { "${e}90m" }
([ConsoleColor]::Red) { "${e}91m" }
([ConsoleColor]::Green) { "${e}92m" }
([ConsoleColor]::Yellow) { "${e}93m" }
([ConsoleColor]::Blue) { "${e}94m" }
([ConsoleColor]::Magenta) { "${e}95m" }
([ConsoleColor]::Cyan) { "${e}96m" }
([ConsoleColor]::White) { "${e}97m" }
default { "${e}39m" }
}
}

function Get-BackgroundVirtualTerminalSequence($Color) {
$e = [char]27 + "["
switch ($Color) {
([ConsoleColor]::Black) { "${e}40m" }
([ConsoleColor]::DarkRed) { "${e}41m" }
([ConsoleColor]::DarkGreen) { "${e}42m" }
([ConsoleColor]::DarkYellow) { "${e}43m" }
([ConsoleColor]::DarkBlue) { "${e}44m" }
([ConsoleColor]::DarkMagenta) { "${e}45m" }
([ConsoleColor]::DarkCyan) { "${e}46m" }
([ConsoleColor]::Gray) { "${e}47m" }
([ConsoleColor]::DarkGray) { "${e}100m" }
([ConsoleColor]::Red) { "${e}101m" }
([ConsoleColor]::Green) { "${e}102m" }
([ConsoleColor]::Yellow) { "${e}103m" }
([ConsoleColor]::Blue) { "${e}104m" }
([ConsoleColor]::Magenta) { "${e}105m" }
([ConsoleColor]::Cyan) { "${e}106m" }
([ConsoleColor]::White) { "${e}107m" }
default { "${e}49m" }
}
}

function Write-Prompt($Object, $ForegroundColor, $BackgroundColor = -1) {
if ($Host.UI.SupportsVirtualTerminal) {
$e = [char]27 + "["
$f = Get-ForegroundVirtualTerminalSequence $ForegroundColor
$b = Get-BackgroundVirtualTerminalSequence $BackgroundColor
return "${f}${b}${Object}${e}0m"
}
if ($BackgroundColor -lt 0) {
Write-Host $Object -NoNewLine -ForegroundColor $ForegroundColor
} else {
Write-Host $Object -NoNewLine -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor
}
return ""
}

function Format-BranchName($branchName){
Expand All @@ -126,8 +180,9 @@ function Format-BranchName($branchName){

function Write-GitStatus($status) {
$s = $global:GitPromptSettings
$p = ''
if ($status -and $s) {
Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor
$p += Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor

$branchStatusSymbol = $null
$branchStatusBackgroundColor = $s.BranchBackgroundColor
Expand Down Expand Up @@ -160,47 +215,47 @@ function Write-GitStatus($status) {
$branchStatusSymbol = "?"
}

Write-Prompt (Format-BranchName($status.Branch)) -BackgroundColor $branchStatusBackgroundColor -ForegroundColor $branchStatusForegroundColor
$p += Write-Prompt (Format-BranchName($status.Branch)) -BackgroundColor $branchStatusBackgroundColor -ForegroundColor $branchStatusForegroundColor

if ($branchStatusSymbol) {
Write-Prompt (" {0}" -f $branchStatusSymbol) -BackgroundColor $branchStatusBackgroundColor -ForegroundColor $branchStatusForegroundColor
$p += Write-Prompt (" {0}" -f $branchStatusSymbol) -BackgroundColor $branchStatusBackgroundColor -ForegroundColor $branchStatusForegroundColor
}

if($s.EnableFileStatus -and $status.HasIndex) {
Write-Prompt $s.BeforeIndexText -BackgroundColor $s.BeforeIndexBackgroundColor -ForegroundColor $s.BeforeIndexForegroundColor
$p += Write-Prompt $s.BeforeIndexText -BackgroundColor $s.BeforeIndexBackgroundColor -ForegroundColor $s.BeforeIndexForegroundColor

if($s.ShowStatusWhenZero -or $status.Index.Added) {
Write-Prompt (" $($s.FileAddedText)$($status.Index.Added.Count)") -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
$p += Write-Prompt (" $($s.FileAddedText)$($status.Index.Added.Count)") -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
}
if($s.ShowStatusWhenZero -or $status.Index.Modified) {
Write-Prompt (" $($s.FileModifiedText)$($status.Index.Modified.Count)") -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
$p += Write-Prompt (" $($s.FileModifiedText)$($status.Index.Modified.Count)") -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
}
if($s.ShowStatusWhenZero -or $status.Index.Deleted) {
Write-Prompt (" $($s.FileRemovedText)$($status.Index.Deleted.Count)") -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
$p += Write-Prompt (" $($s.FileRemovedText)$($status.Index.Deleted.Count)") -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
}

if ($status.Index.Unmerged) {
Write-Prompt (" $($s.FileConflictedText)$($status.Index.Unmerged.Count)") -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
$p += Write-Prompt (" $($s.FileConflictedText)$($status.Index.Unmerged.Count)") -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
}

if($status.HasWorking) {
Write-Prompt $s.DelimText -BackgroundColor $s.DelimBackgroundColor -ForegroundColor $s.DelimForegroundColor
$p += Write-Prompt $s.DelimText -BackgroundColor $s.DelimBackgroundColor -ForegroundColor $s.DelimForegroundColor
}
}

if($s.EnableFileStatus -and $status.HasWorking) {
if($s.ShowStatusWhenZero -or $status.Working.Added) {
Write-Prompt (" $($s.FileAddedText)$($status.Working.Added.Count)") -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
$p += Write-Prompt (" $($s.FileAddedText)$($status.Working.Added.Count)") -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
}
if($s.ShowStatusWhenZero -or $status.Working.Modified) {
Write-Prompt (" $($s.FileModifiedText)$($status.Working.Modified.Count)") -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
$p += Write-Prompt (" $($s.FileModifiedText)$($status.Working.Modified.Count)") -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
}
if($s.ShowStatusWhenZero -or $status.Working.Deleted) {
Write-Prompt (" $($s.FileRemovedText)$($status.Working.Deleted.Count)") -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
$p += Write-Prompt (" $($s.FileRemovedText)$($status.Working.Deleted.Count)") -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
}

if ($status.Working.Unmerged) {
Write-Prompt (" $($s.FileConflictedText)$($status.Working.Unmerged.Count)") -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
$p += Write-Prompt (" $($s.FileConflictedText)$($status.Working.Unmerged.Count)") -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
}
}

Expand All @@ -222,16 +277,16 @@ function Write-GitStatus($status) {
}

if ($localStatusSymbol) {
Write-Prompt (" {0}" -f $localStatusSymbol) -BackgroundColor $localStatusBackgroundColor -ForegroundColor $localStatusForegroundColor
$p += Write-Prompt (" {0}" -f $localStatusSymbol) -BackgroundColor $localStatusBackgroundColor -ForegroundColor $localStatusForegroundColor
}

if ($s.EnableStashStatus -and ($status.StashCount -gt 0)) {
Write-Prompt $s.BeforeStashText -BackgroundColor $s.BeforeStashBackgroundColor -ForegroundColor $s.BeforeStashForegroundColor
Write-Prompt $status.StashCount -BackgroundColor $s.StashBackgroundColor -ForegroundColor $s.StashForegroundColor
Write-Prompt $s.AfterStashText -BackgroundColor $s.AfterStashBackgroundColor -ForegroundColor $s.AfterStashForegroundColor
$p += Write-Prompt $s.BeforeStashText -BackgroundColor $s.BeforeStashBackgroundColor -ForegroundColor $s.BeforeStashForegroundColor
$p += Write-Prompt $status.StashCount -BackgroundColor $s.StashBackgroundColor -ForegroundColor $s.StashForegroundColor
$p += Write-Prompt $s.AfterStashText -BackgroundColor $s.AfterStashBackgroundColor -ForegroundColor $s.AfterStashForegroundColor
}

Write-Prompt $s.AfterText -BackgroundColor $s.AfterBackgroundColor -ForegroundColor $s.AfterForegroundColor
$p += Write-Prompt $s.AfterText -BackgroundColor $s.AfterBackgroundColor -ForegroundColor $s.AfterForegroundColor

if ($WindowTitleSupported -and $s.EnableWindowTitle) {
if( -not $Global:PreviousWindowTitle ) {
Expand All @@ -241,8 +296,11 @@ function Write-GitStatus($status) {
$prefix = if ($s.EnableWindowTitle -is [string]) { $s.EnableWindowTitle } else { '' }
$Host.UI.RawUI.WindowTitle = "$script:adminHeader$prefix$repoName [$($status.Branch)]"
}

return $p
} elseif ( $Global:PreviousWindowTitle ) {
$Host.UI.RawUI.WindowTitle = $Global:PreviousWindowTitle
return ""
}
}

Expand Down
4 changes: 2 additions & 2 deletions profile.example.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ function global:prompt {

Write-Host($pwd.ProviderPath) -nonewline

Write-VcsStatus
$p = Write-VcsStatus

$global:LASTEXITCODE = $realLASTEXITCODE
return "> "
return $p + "> "
}

Pop-Location
Expand Down

0 comments on commit ff6c66b

Please sign in to comment.