Created
June 24, 2019 08:47
-
-
Save Vivelin/3a854110adee83bc9e638103fc887794 to your computer and use it in GitHub Desktop.
Basic Powerline-like PowerShell prompt. Save to `$PROFILE.AllUsersAllHosts`.
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
# Note: requires a Powerline-patched font to display properly | |
# See https://github.com/powerline/fonts | |
$CURRENT_BG = $null | |
$SEGMENT_DELIMETER_GLYPH = "" | |
$GIT_BRANCH_GLYPH = "" | |
function prompt { | |
Write-PowerlineUserContext | |
Write-PowerlineLocation | |
Write-PowerlineGit | |
Write-PowerlineSegmentEnd | |
# Required to prevent the default "PS>" since we use Write-Host to get colors | |
return " " | |
} | |
Function Write-PowerlineSegment($bg = 'Black', $fg = 'Blue', $str) { | |
if ($CURRENT_BG -and $CURRENT_BG -ne $bg) { | |
Write-Host "$SEGMENT_DELIMETER_GLYPH" -ForegroundColor $CURRENT_BG -BackgroundColor $bg -NoNewline | |
} | |
$Global:CURRENT_BG = $bg | |
Write-Host " $str " -ForegroundColor $fg -BackgroundColor $bg -NoNewline | |
} | |
Function Write-PowerlineSegmentEnd() { | |
Write-Host "$SEGMENT_DELIMETER_GLYPH" -ForegroundColor $CURRENT_BG -BackgroundColor Black -NoNewline | |
$Global:CURRENT_BG = $null | |
} | |
Function Write-PowerlineUserContext() { | |
if (Test-Administrator) { | |
Write-PowerlineSegment -bg Red -fg Black "${env:UserName}" | |
} | |
else { | |
Write-PowerlineSegment -bg Green -fg Black "${env:UserName}" | |
} | |
} | |
Function Write-PowerlineLocation() { | |
Write-PowerlineSegment -bg Blue -fg Black -str "$pwd" | |
} | |
Function Write-PowerlineGit() { | |
if (Get-Command git -ErrorAction SilentlyContinue) { | |
if ($(git rev-parse --is-inside-work-tree 2>$null)) { | |
$branch = $(git symbolic-ref HEAD 2>$null).ToString().Substring(11) | |
Write-PowerlineSegment -bg Yellow -fg Black -str "$GIT_BRANCH_GLYPH $branch" | |
} | |
} | |
} | |
Function Test-Administrator { | |
$user = [Security.Principal.WindowsIdentity]::GetCurrent(); | |
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) | |
} | |
Function Initialize-Host { | |
$bg = $Host.UI.RawUI.BackgroundColor | |
$Host.PrivateData.ErrorBackgroundColor = $bg | |
$Host.PrivateData.WarningBackgroundColor = $bg | |
$Host.PrivateData.DebugBackgroundColor = $bg | |
$Host.PrivateData.VerboseBackgroundColor = $bg | |
$Host.UI.RawUI.WindowTitle = "Windows PowerShell v$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)" | |
} | |
Initialize-Host |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment