Skip to content

Instantly share code, notes, and snippets.

@kadet1090
Created March 21, 2016 17:33
Show Gist options
  • 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 ' '
}
@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