Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Last active August 20, 2019 07:25
Show Gist options
  • Save JustinGrote/592ca40265993335e153b3e4811e68df to your computer and use it in GitHub Desktop.
Save JustinGrote/592ca40265993335e153b3e4811e68df to your computer and use it in GitHub Desktop.
Powerline Prompt Builder
function Add-Prompt {
<#
.SYNOPSIS
Builds a powerline-style prompt from segments
.DESCRIPTION
This command is used to build a prompt from existing segments, and automagically will figure out the colors for the
separators as necessary. You can insert additional colors into your string with ANSI escape sequences (don't use reset)
#>
[CmdletBinding()]
param (
#The additional prompt segment to add
[Parameter(ValueFromPipeline)][String]$InputObject,
#The separator to use. Defaults to Powerline Separator.
[String]$Separator = [char]0xe0b0,
#The name of a background color if not specified a random color will be chosen
[String]$Background,
#The name of a foreground color. If not specified this function will automatically choose based on background
[String]$Foreground
)
begin {
#Set some "constants"
if ($psedition -eq 'core') {
$ansiesc = "`e"
} else {
$ansiesc = [char]0x1b
}
$resetcolor = "$ansiesc[39;49m"
$fgcode = "$ansiesc[38;2;"
$bgcode = "$ansiesc[48;2;"
function Test-IsLightColor ([Drawing.Color]$Color) {
#https://stackoverflow.com/a/36888120/5511129
[double]$luma = ((0.299 * $Color.R) + (0.587 * $Color.G) + (0.114 * $Color.B)) / 255
$luma -gt 0.5
}
}
process {
if ($Background) {
$bgColorItem = $Background
} else {
$bgColorItem = (Get-Random (([Drawing.Color] | Get-Member -static).where{$_.membertype -eq 'property'}.name))
}
$bgColor = [Drawing.Color]::$bgColorItem
write-host -fore Green "$bgColorItem Light: $(Test-IsLightColor $bgColor) "
if ($bgColor) {
if ($Foreground) {
$fgColor = [Drawing.Color]::$Foreground
} else {
if (Test-IsLightColor $bgColor) {
$fgColor = [Drawing.Color]::Black
} else {
$fgColor = [Drawing.Color]::White
}
}
$bgColorCode = "$bgcode{0};{1};{2}m" -f
$bgColor.r,
$bgColor.g,
$bgcolor.b
$separatorColorCode = "$fgcode{0};{1};{2}m" -f
$bgColor.r,
$bgColor.g,
$bgcolor.b
$fgColorCode = "$fgcode{0};{1};{2}m" -f
$fgColor.r,
$fgColor.g,
$fgColor.b
} else {
$bgColorCode = $resetColor
}
#This is used later to strip off the characters
[String]$PSPromptEnd = "${resetcolor}${separatorColorCode}${Separator}${resetcolor}"
[String]$PSPromptSegment = "${bgColorCode}${fgColorCode}${InputObject}${PSPromptEnd}"
if (-not $GLOBAL:MyPSPrompt) {
$GLOBAL:MyPSPrompt = $PSPromptSegment
} else {
#Strip the leading separator so we can replace it with one of the proper background lead-in. We leave the
#foreground as-is since it is the same color as the background was previously.
$GLOBAL:MyPSPrompt = $GLOBAL:MyPSPrompt.SubString(0,($GLOBAL:MyPSPrompt.length-$GLOBAL:MyPSPromptEnd.length))
$GLOBAL:MyPSPrompt += "${resetcolor}$($GLOBAL:MyPSPromptLastSeparatorFG)${bgColorCode}${Separator}${resetcolor}${$bgColorCode}"
$GLOBAL:MyPSPrompt += $PSPromptSegment
}
Write-Host -fore green $inputobject
$GLOBAL:MyPSPromptLastSeparatorFG = $separatorColorCode
$GLOBAL:MyPSPromptEnd = $PSPromptEnd
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment