-
-
Save randomchance/afcedadb28ba145fe494 to your computer and use it in GitHub Desktop.
PowerLine like prompt for PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$script:bg = [Console]::BackgroundColor; | |
$script:first = $true; | |
$script:last = 0; | |
Import-Module Posh-Git | |
$BranchBehindAndAheadStatusForegroundColor = [System.ConsoleColor]::Magenta | |
$BranchBehindStatusForegroundColor = [System.ConsoleColor]::Yellow | |
$BranchAheadStatusForegroundColor = [System.ConsoleColor]::Green | |
$BranchIdenticalStatusToForegroundColor = [System.ConsoleColor]::White | |
$workingAdded = '+' | |
$workingRemoved = '-' | |
$workingModified = '~' | |
$workingUnmerged = '' | |
$BranchUntrackedSymbol = '' | |
$BeforeText = '' | |
$BranchAheadStatusSymbol = ''#'' | |
$BranchBehindStatusSymbol = ''#'' | |
$BranchBehindAndAheadStatusSymbol = '' #"$BranchBehindStatusSymbol$BranchAheadStatusSymbol" | |
$BranchIdenticalStatusToSymbol = '' | |
function Write-PromptFancyEnd { | |
Write-Host -NoNewline -ForegroundColor $script:bg | |
$script:bg = [System.ConsoleColor]::Black | |
} | |
function Write-PromptSegment { | |
param( | |
[Parameter( | |
Position=0, | |
Mandatory=$true, | |
ValueFromPipeline=$true, | |
ValueFromPipelineByPropertyName=$true | |
)][string]$Text, | |
[Parameter(Position=1)][System.ConsoleColor] $Background = [Console]::BackgroundColor, | |
[Parameter(Position=2)][System.ConsoleColor] $Foreground = [System.ConsoleColor]::White, | |
[switch]$SameSegment | |
) | |
if(-not $SameSegment){ | |
if(!$script:first) { | |
Write-Host -NoNewline -BackgroundColor $Background -ForegroundColor $script:bg | |
} else { | |
$script:first = $false | |
} | |
} | |
Write-Host $text -NoNewline -BackgroundColor $Background -ForegroundColor $Foreground | |
$script:bg = $background; | |
} | |
function Get-FancyDir { | |
return $(Get-Location).ToString().Replace($env:USERPROFILE, '~').Replace('\', ' '); | |
} | |
function Write-PromptStatus { | |
if($script:last) { | |
Write-PromptSegment ' ✔ ' Green Black | |
} else { | |
Write-PromptSegment " ✖ $lastexitcode " Red White | |
} | |
} | |
function Write-PromptUser { | |
if($global:admin) { | |
Write-PromptSegment ' # ADMIN ' Magenta White; | |
} else { | |
Write-PromptSegment " $env:USERNAME " Yellow Black; | |
} | |
} | |
function Write-PromptVirtualEnv { | |
if($env:VIRTUAL_ENV) { | |
Write-PromptSegment " $(split-path $env:VIRTUAL_ENV -leaf) " Cyan Black | |
} | |
} | |
function Write-PromptDir { | |
Write-PromptSegment " $(Get-FancyDir) " DarkGray White | |
} | |
# Depends on posh-git | |
# Depends on posh-git | |
function Write-PromptGit { | |
$Private:Status = Get-GitStatus | |
if($Private:Status) { | |
$branchStatusSymbol = $null | |
$branchStatusBackgroundColor = [System.ConsoleColor]::Blue # [Console]::BackgroundColor, | |
$branchStatusForegroundColor = [System.ConsoleColor]::White | |
$branchForegroundColor = [System.ConsoleColor]::White | |
if (-not $Private:status.Upstream) { | |
$branchStatusSymbol = $s.BranchUntrackedSymbol | |
} elseif ($Private:status.BehindBy -eq 0 -and $status.AheadBy -eq 0) { | |
# We are aligned with remote | |
$branchStatusSymbol = $BranchIdenticalStatusToSymbol | |
#$branchStatusBackgroundColor = $BranchIdenticalStatusToBackgroundColor | |
$branchStatusForegroundColor = $BranchIdenticalStatusToForegroundColor | |
} elseif ($Private:status.BehindBy -ge 1 -and $status.AheadBy -ge 1) { | |
# We are both behind and ahead of remote | |
$branchStatusSymbol = $BranchBehindAndAheadStatusSymbol | |
#$branchStatusBackgroundColor = $s.BranchBehindAndAheadStatusBackgroundColor | |
$branchStatusForegroundColor = $BranchBehindAndAheadStatusForegroundColor | |
} elseif ($Private:status.BehindBy -ge 1) { | |
# We are behind remote | |
#$branchStatusSymbol = $BranchBehindStatusSymbol | |
#$branchStatusBackgroundColor = $s.BranchBehindStatusBackgroundColor | |
$branchStatusForegroundColor =$BranchBehindStatusForegroundColor | |
} elseif ($Private:status.AheadBy -ge 1) { | |
# We are ahead of remote | |
$branchStatusSymbol = $BranchAheadStatusSymbol | |
#$branchStatusBackgroundColor = $s.BranchAheadStatusBackgroundColor | |
$branchStatusForegroundColor = $BranchAheadStatusForegroundColor | |
} | |
$working = " " | |
if($Private:Status.HasWorking){ | |
if($Private:status.Working.Added){ | |
$working += "$($workingAdded )$($Private:status.Working.Added.Count)" | |
} | |
if($Private:status.Working.Modified){ | |
$working +="$($workingModified )$($Private:status.Working.Modified.Count)" | |
} | |
if($Private:status.Working.Removed){ | |
$working +="$($workingRemoved)$($Private:status.Working.Removed.Count)" | |
} | |
if($Private:status.Working.Unmerged){ | |
$working +="$($workingUnmerged)$($Private:status.Working.Unmerged.Count)" | |
} | |
} | |
$BranchStatus = ($BeforeText + $branchStatusSymbol) | |
Write-PromptSegment $BranchStatus $branchStatusBackgroundColor $branchStatusForegroundColor | |
Write-PromptSegment (" $($Private:Status.Branch)") $branchStatusBackgroundColor $branchForegroundColor -SameSegment | |
#Write-Host " $($Private:Status.Branch) $branchStatusSymbol " -BackgroundColor Blue $branchStatusForegroundColor | |
Write-PromptSegment $working $branchStatusBackgroundColor $branchStatusForegroundColor -SameSegment | |
} | |
} | |
function prompt { | |
$script:last = $?; | |
$script:first = $true; | |
Write-PromptStatus | |
Write-PromptUser | |
Write-PromptVirtualEnv | |
Write-PromptDir | |
Write-PromptGit | |
Write-PromptFancyEnd | |
return ' ' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When running the script on Win7 with Powershell 5.0 i get:
Any idea? Thanks