Skip to content

Instantly share code, notes, and snippets.

@toburger
Forked from kadet1090/prompt.ps1
Last active March 16, 2019 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toburger/09dee769a45718a36ccc65326e3a49c0 to your computer and use it in GitHub Desktop.
Save toburger/09dee769a45718a36ccc65326e3a49c0 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 Get-GitStatusInfo {
$info = Get-GitStatus
$message = ''
if ($info.HasIndex) { $message += "$($info.Index.Count)" }
if ($info.HasWorking -or $info.HasUntracked) { $message += "$($info.Working.Count)" }
if ($info.BehindBy -gt 0) { $message += "$($info.BehindBy)" }
if ($info.AheadBy -gt 0) { $message += "$($info.AheadBy)" }
if ($info.StashCount -gt 0) { $message += "$($info.StashCount)" }
$message
}
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)$(Get-GitStatusInfo) " Blue White
}
}
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