Skip to content

Instantly share code, notes, and snippets.

@randomchance
Forked from kadet1090/prompt.ps1
Last active April 6, 2020 08:03
Show Gist options
  • Save randomchance/afcedadb28ba145fe494 to your computer and use it in GitHub Desktop.
Save randomchance/afcedadb28ba145fe494 to your computer and use it in GitHub Desktop.
PowerLine like prompt for PowerShell
$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 ' '
}
@TechnoTone
Copy link

I like what you've done here. Can you tell me what font you are using? I've tried various Powerline fonts but I can't find one that has all of the characters you have used for the change tracking indicators (ahead/behind/etc.).

@dl1ely
Copy link

dl1ely commented Oct 6, 2016

When running the script on Win7 with Powershell 5.0 i get:

At C:\Users\pfeiffer.stefan\Downloads\86023bfad2bdd84d8b1a-e0e76833d142fd6958f1d7f46ef7c2c1c3699288\prompt.ps1:33 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
At C:\Users\pfeiffer.stefan\Downloads\86023bfad2bdd84d8b1a-e0e76833d142fd6958f1d7f46ef7c2c1c3699288\prompt.ps1:36 char:87
+ ... ion).ToString().Replace($env:USERPROFILE, '~').Replace('\', '  ');
+                                                                     ~
Missing ')' in method call.
At C:\Users\pfeiffer.stefan\Downloads\86023bfad2bdd84d8b1a-e0e76833d142fd6958f1d7f46ef7c2c1c3699288\prompt.ps1:36 char:87
+ ... ion).ToString().Replace($env:USERPROFILE, '~').Replace('\', '  ');
+                                                                     ~
Unexpected token '±' in expression or statement.
At C:\Users\pfeiffer.stefan\Downloads\86023bfad2bdd84d8b1a-e0e76833d142fd6958f1d7f46ef7c2c1c3699288\prompt.ps1:93 char:14
+     return ' '
+              ~
The string is missing the terminator: '.
At C:\Users\pfeiffer.stefan\Downloads\86023bfad2bdd84d8b1a-e0e76833d142fd6958f1d7f46ef7c2c1c3699288\prompt.ps1:35 char:23
+ function Get-FancyDir {
+                       ~
Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

C:\Users\pfeiffer.stefan\Downloads\86023bfad2bdd84d8b1a-e0e76833d142fd6958f1d7f46ef7c2c1c3699288
λ cd ..
C:\Users\pfeiffer.stefan\Downloads
λ cd .\afcedadb28ba145fe494-3821b68f78d1c690335590d64a9471c505860b9d
C:\Users\pfeiffer.stefan\Downloads\afcedadb28ba145fe494-3821b68f78d1c690335590d64a9471c505860b9d
λ .\prompt.ps1
At C:\Users\pfeiffer.stefan\Downloads\afcedadb28ba145fe494-3821b68f78d1c690335590d64a9471c505860b9d\prompt.ps1:16 char:36
+ $BranchAheadStatusSymbol  = ''#''
+                                    ~~~
Unexpected token ''#'' in expression or statement.
At C:\Users\pfeiffer.stefan\Downloads\afcedadb28ba145fe494-3821b68f78d1c690335590d64a9471c505860b9d\prompt.ps1:16 char:39
+ $BranchAheadStatusSymbol  = ''#''
+                                       ~
Unexpected token 'î' in expression or statement.
At C:\Users\pfeiffer.stefan\Downloads\afcedadb28ba145fe494-3821b68f78d1c690335590d64a9471c505860b9d\prompt.ps1:18 char:47
+ ... ol = '' #"$BranchBehindStatusSymbol$BranchAheadStatusSymbol"
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '' #"$BranchBehindStatusSymbol$BranchAheadStatusSymbol"
$BranchIdenticalStatusToSymbol = '' in expression or statement.
At C:\Users\pfeiffer.stefan\Downloads\afcedadb28ba145fe494-3821b68f78d1c690335590d64a9471c505860b9d\prompt.ps1:19 char:35
+ $BranchIdenticalStatusToSymbol = 'î‚ '
+                                   ~~~~
Unexpected token 'î‚ '' in expression or statement.
At C:\Users\pfeiffer.stefan\Downloads\afcedadb28ba145fe494-3821b68f78d1c690335590d64a9471c505860b9d\prompt.ps1:48 char:5
+     }
+     ~
Unexpected token '}' in expression or statement.
At C:\Users\pfeiffer.stefan\Downloads\afcedadb28ba145fe494-3821b68f78d1c690335590d64a9471c505860b9d\prompt.ps1:53 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
At C:\Users\pfeiffer.stefan\Downloads\afcedadb28ba145fe494-3821b68f78d1c690335590d64a9471c505860b9d\prompt.ps1:56 char:87
+ ... ion).ToString().Replace($env:USERPROFILE, '~').Replace('\', '  ');
+                                                                     ~
Missing ')' in method call.
At C:\Users\pfeiffer.stefan\Downloads\afcedadb28ba145fe494-3821b68f78d1c690335590d64a9471c505860b9d\prompt.ps1:56 char:87
+ ... ion).ToString().Replace($env:USERPROFILE, '~').Replace('\', '  ');
+                                                                     ~
Unexpected token '±' in expression or statement.
At C:\Users\pfeiffer.stefan\Downloads\afcedadb28ba145fe494-3821b68f78d1c690335590d64a9471c505860b9d\prompt.ps1:155 char:14
+     return ' '
+              ~
The string is missing the terminator: '.
At C:\Users\pfeiffer.stefan\Downloads\afcedadb28ba145fe494-3821b68f78d1c690335590d64a9471c505860b9d\prompt.ps1:55 char:23
+ function Get-FancyDir {
+                       ~
Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

Any idea? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment