Skip to content

Instantly share code, notes, and snippets.

@kadet1090
Created March 21, 2016 17:33
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save kadet1090/86023bfad2bdd84d8b1a to your computer and use it in GitHub Desktop.
Save kadet1090/86023bfad2bdd84d8b1a to your computer and use it in GitHub Desktop.
PowerLine like prompt for PowerShell
$script:bg = [Console]::BackgroundColor;
$script:first = $true;
$script:last = 0;
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
)
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 Get-GitBranch {
$HEAD = Get-Content $(Join-Path $(Get-GitDirectory) HEAD)
if($HEAD -like 'ref: refs/heads/*') {
return $HEAD -replace 'ref: refs/heads/(.*?)', "$1";
} else {
return $HEAD.Substring(0, 8);
}
}
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
function Write-PromptGit {
if(Get-GitDirectory) {
Write-PromptSegment "  $(Get-GitBranch) " Blue White
}
}
function prompt {
$script:last = $?;
$script:first = $true;
Write-PromptStatus
Write-PromptUser
Write-PromptVirtualEnv
Write-PromptDir
Write-PromptGit
Write-PromptFancyEnd
return ' '
}
@kadet1090
Copy link
Author

It looks like this
Screenshot

It's quite modular so you can easily add/remove/rearrange segments, see lines 85-89

You need to use PowerLine enabled font therefore ConEmu is highly recommended because of color schemes and font handling.

@dl1ely
Copy link

dl1ely commented Oct 6, 2016

When exectuting the prompt script (Win7, Powershell 5), i get unexpected tokens:

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

Any idea? Thanks

@mhartington
Copy link

Same here, not really sure what the issue is. @dl1ely did you ever figure it out?

@Steven-Harris
Copy link

Same

@Snaptags
Copy link

Snaptags commented Feb 7, 2017

When downloading the ZIP the file comes encoded as "UTF-8 without BOM" which Windows does not seem to like very much.
Open the file in Notepad++ (or another unicode enabled editor) and convert it to "UTF-8". This does solve the issue for me.

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