Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ANSI 256-color and 24-bit color support #455

Merged
merged 8 commits into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/AnsiUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,28 @@ $ConsoleColorToAnsi = @(
$AnsiDefaultColor = 39
$AnsiEscape = [char]27 + "["

[Reflection.Assembly]::LoadWithPartialName('System.Drawing') > $null
$ColorTranslatorType = ([System.Management.Automation.PSTypeName]'System.Drawing.ColorTranslator').Type
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be

$ColorTranslatorType = 'System.Drawing.ColorTranslator' -as [Type]

$ColorType = ([System.Management.Automation.PSTypeName]'System.Drawing.Color').Type

function Get-VirtualTerminalSequence ($color, [int]$offset = 0) {
if (($color -is [ConsoleColor]) -and ($color -ge 0) -and ($color -le 15)) {
return "${AnsiEscape}$($ConsoleColorToAnsi[$color] + $offset)m"
}
if ($color -is [byte]) {
return "${AnsiEscape}$(38 + $offset);5;${color}m"
}
if ($ColorTranslatorType -and ($color -is [String])) {
try {
$color = $ColorTranslatorType::FromHtml($color)
}
catch {
Write-Debug $_
}
}
if ($ColorType -and ($color -is $ColorType)) {
return "${AnsiEscape}$(38 + $offset);2;$($color.R);$($color.G);$($color.B)m"
}
return "${AnsiEscape}$($AnsiDefaultColor + $offset)m"
}

Expand Down
61 changes: 36 additions & 25 deletions src/GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
# http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration

$global:GitPromptSettings = [pscustomobject]@{
DefaultForegroundColor = $Host.UI.RawUI.ForegroundColor
DefaultForegroundColor = $null

BeforeText = ' ['
BeforeForegroundColor = [ConsoleColor]::Yellow
BeforeBackgroundColor = $Host.UI.RawUI.BackgroundColor
BeforeBackgroundColor = $null

DelimText = ' |'
DelimForegroundColor = [ConsoleColor]::Yellow
DelimBackgroundColor = $Host.UI.RawUI.BackgroundColor
DelimBackgroundColor = $null

AfterText = ']'
AfterForegroundColor = [ConsoleColor]::Yellow
AfterBackgroundColor = $Host.UI.RawUI.BackgroundColor
AfterBackgroundColor = $null

FileAddedText = '+'
FileModifiedText = '~'
Expand All @@ -24,62 +24,62 @@ $global:GitPromptSettings = [pscustomobject]@{
LocalDefaultStatusSymbol = $null
LocalDefaultStatusForegroundColor = [ConsoleColor]::DarkGreen
LocalDefaultStatusForegroundBrightColor = [ConsoleColor]::Green
LocalDefaultStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
LocalDefaultStatusBackgroundColor = $null

LocalWorkingStatusSymbol = '!'
LocalWorkingStatusForegroundColor = [ConsoleColor]::DarkRed
LocalWorkingStatusForegroundBrightColor = [ConsoleColor]::Red
LocalWorkingStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
LocalWorkingStatusBackgroundColor = $null

LocalStagedStatusSymbol = '~'
LocalStagedStatusForegroundColor = [ConsoleColor]::Cyan
LocalStagedStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
LocalStagedStatusBackgroundColor = $null

BranchUntrackedSymbol = $null
BranchForegroundColor = [ConsoleColor]::Cyan
BranchBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchBackgroundColor = $null

BranchGoneStatusSymbol = [char]0x00D7 # × Multiplication sign
BranchGoneStatusForegroundColor = [ConsoleColor]::DarkCyan
BranchGoneStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchGoneStatusBackgroundColor = $null

BranchIdenticalStatusToSymbol = [char]0x2261 # ≡ Three horizontal lines
BranchIdenticalStatusToForegroundColor = [ConsoleColor]::Cyan
BranchIdenticalStatusToBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchIdenticalStatusToBackgroundColor = $null

BranchAheadStatusSymbol = [char]0x2191 # ↑ Up arrow
BranchAheadStatusForegroundColor = [ConsoleColor]::Green
BranchAheadStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchAheadStatusBackgroundColor = $null

BranchBehindStatusSymbol = [char]0x2193 # ↓ Down arrow
BranchBehindStatusForegroundColor = [ConsoleColor]::Red
BranchBehindStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchBehindStatusBackgroundColor = $null

BranchBehindAndAheadStatusSymbol = [char]0x2195 # ↕ Up & Down arrow
BranchBehindAndAheadStatusForegroundColor = [ConsoleColor]::Yellow
BranchBehindAndAheadStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchBehindAndAheadStatusBackgroundColor = $null

BeforeIndexText = ""
BeforeIndexForegroundColor = [ConsoleColor]::DarkGreen
BeforeIndexForegroundBrightColor = [ConsoleColor]::Green
BeforeIndexBackgroundColor = $Host.UI.RawUI.BackgroundColor
BeforeIndexBackgroundColor = $null

IndexForegroundColor = [ConsoleColor]::DarkGreen
IndexForegroundBrightColor = [ConsoleColor]::Green
IndexBackgroundColor = $Host.UI.RawUI.BackgroundColor
IndexBackgroundColor = $null

WorkingForegroundColor = [ConsoleColor]::DarkRed
WorkingForegroundBrightColor = [ConsoleColor]::Red
WorkingBackgroundColor = $Host.UI.RawUI.BackgroundColor
WorkingBackgroundColor = $null

EnableStashStatus = $false
BeforeStashText = ' ('
BeforeStashBackgroundColor = $Host.UI.RawUI.BackgroundColor
BeforeStashBackgroundColor = $null
BeforeStashForegroundColor = [ConsoleColor]::Red
AfterStashText = ')'
AfterStashBackgroundColor = $Host.UI.RawUI.BackgroundColor
AfterStashBackgroundColor = $null
AfterStashForegroundColor = [ConsoleColor]::Red
StashBackgroundColor = $Host.UI.RawUI.BackgroundColor
StashBackgroundColor = $null
StashForegroundColor = [ConsoleColor]::Red

ShowStatusWhenZero = $true
Expand Down Expand Up @@ -133,13 +133,18 @@ function Write-Prompt {
param(
[parameter(Mandatory = $true)]
$Object,
[parameter(Mandatory = $true)]
$ForegroundColor,
[parameter()]
$ForegroundColor = $null,
[parameter()]
$BackgroundColor = $null,
[parameter(ValueFromPipeline = $true)]
[Text.StringBuilder]
$Builder)
$s = $global:GitPromptSettings
if ($s -and !$ForegroundColor) {
$ForegroundColor = $s.DefaultForegroundColor
}

if ($GitPromptSettings.AnsiConsole) {
$e = [char]27 + "["
$f = Get-ForegroundVirtualTerminalSequence $ForegroundColor
Expand All @@ -149,11 +154,17 @@ function Write-Prompt {
}
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
$writeHostParams = @{
Object = $Object;
NoNewLine = $true;
}
if (($BackgroundColor -ge 0) -and ($BackgroundColor -le 15)) {
$writeHostParams.BackgroundColor = $BackgroundColor
}
if (($ForegroundColor -ge 0) -and ($ForegroundColor -le 15)) {
$writeHostParams.ForegroundColor = $ForegroundColor
}
Write-Host @writeHostParams
if ($Builder) {
return $Builder
}
Expand Down
6 changes: 3 additions & 3 deletions src/posh-git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ if ($ForcePoshGitPrompt -or !$currentPromptDef -or ($currentPromptDef -eq $defau
$defaultPromptPrefix = [string]$GitPromptSettings.DefaultPromptPrefix
if ($defaultPromptPrefix) {
$expandedDefaultPromptPrefix = $ExecutionContext.SessionState.InvokeCommand.ExpandString($defaultPromptPrefix)
$res += Write-Prompt $expandedDefaultPromptPrefix -ForegroundColor $GitPromptSettings.DefaultForegroundColor
$res += Write-Prompt $expandedDefaultPromptPrefix
}

# Write the abbreviated current path
$res += Write-Prompt $currentPath -ForegroundColor $GitPromptSettings.DefaultForegroundColor
$res += Write-Prompt $currentPath

# Write the Git status summary information
$res += Write-VcsStatus
Expand All @@ -103,7 +103,7 @@ if ($ForcePoshGitPrompt -or !$currentPromptDef -or ($currentPromptDef -eq $defau
if ($GitPromptSettings.DefaultPromptEnableTiming) {
$sw.Stop()
$elapsed = $sw.ElapsedMilliseconds
$res += Write-Prompt " ${elapsed}ms" -ForegroundColor $GitPromptSettings.DefaultForegroundColor
$res += Write-Prompt " ${elapsed}ms"
}

$global:LASTEXITCODE = $origLastExitCode
Expand Down