Last active
June 6, 2023 23:19
-
-
Save robearlam/02f4b2b5d1abd3abf8fd94f11c45eea5 to your computer and use it in GitHub Desktop.
PowerShell profile definition
This file contains 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
#requires -Version 6 | |
function Get-BranchName | |
{ | |
$default = "n/a" | |
$currentPath = $ExecutionContext.SessionState.Path.CurrentLocation | |
while ($true) | |
{ | |
try | |
{ | |
if ([string]::IsNullOrEmpty($currentPath)) | |
{ | |
return $default | |
} | |
$headPath = (Join-Path $currentPath "\.git\HEAD") | |
if (Test-Path $headPath) | |
{ | |
$head = (Get-Content -Path $headPath | Out-String).Trim().Replace("ref: refs/heads/", "") | |
if ([string]::IsNullOrEmpty($head)) | |
{ | |
return $default | |
} | |
return $head | |
} | |
$currentPath = Split-Path $currentPath | |
} | |
catch | |
{ | |
Write-Warning "Get-BranchName failed..." | |
return $default | |
} | |
} | |
} | |
function ConvertTo-AnsiVT100 | |
{ | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory = $true)] | |
[ValidateNotNull()] | |
[array]$Segments | |
) | |
$esc = "$([char]27)" | |
$builder = [System.Text.StringBuilder]::new() | |
for ($i = 0; $i -lt $Segments.Length; $i++) | |
{ | |
# render segment | |
$current = $Segments[$i] | |
if ($null -ne $current.ForegroundColor -and $null -ne $current.BackgroundColor) | |
{ | |
[void]$builder.Append("$esc[38;5;$($current.ForegroundColor)m$esc[48;5;$($current.BackgroundColor)m$($current.Value)$esc[0m$esc[0m") | |
} | |
else | |
{ | |
[void]$builder.Append($current.Value) | |
} | |
# render seperator | |
if ([string]::IsNullOrEmpty($current.Seperator)) | |
{ | |
continue | |
} | |
$next = $null | |
if ($i -lt ($Segments.Count - 1)) | |
{ | |
$next = $Segments[$i + 1] | |
} | |
$seperatorForegroundColor = $current.BackgroundColor | |
if ($null -ne $next) | |
{ | |
$seperatorBackgroundColor = $next.BackgroundColor | |
} | |
else | |
{ | |
$seperatorBackgroundColor = 0 | |
} | |
[void]$builder.Append("$esc[38;5;$($seperatorForegroundColor)m$esc[48;5;$($seperatorBackgroundColor)m$($current.Seperator)$esc[0m$esc[0m") | |
} | |
return $builder.ToString() | |
} | |
# configure prompt | |
function Prompt | |
{ | |
# was the last command executed successful ? | |
[bool]$script:success = $? | |
# shorten and decorate current path | |
$pathMaxDepth = 3 | |
$pathArray = ($ExecutionContext.SessionState.Path.CurrentLocation) -split "\\" | |
if ($pathArray.Count -gt $pathMaxDepth) | |
{ | |
$driveSegement = $pathArray | Select-Object -First 1 | |
$pathSegment = ($pathArray | Select-Object -Last $pathMaxDepth) -join " `u{e0b1} " | |
$currentLocation = "$driveSegement `u{e0b1} $([char]0x2026) `u{e0b1} $pathSegment" | |
} | |
else | |
{ | |
$currentLocation = $pathArray -join " `u{e0b1} " | |
} | |
# measure how long the last command took to execute | |
$lastCommand = Get-History -Count 1 | |
if ($lastCommand) | |
{ | |
$elapsed = "{0:h\:mm\:ss\.ffff}" -f ($lastCommand.EndExecutionTime - $lastCommand.StartExecutionTime) | |
} | |
else | |
{ | |
$elapsed = "0:00:00.0000" | |
} | |
# write prompt | |
$stateColor = @{ $true = 253; $false = 9 }[$script:success] | |
$seperator = "`u{e0b0}" | |
$segments = @() | |
$segments += [PSCustomObject]@{ Value = "$seperator"; ForegroundColor = 0; BackgroundColor = 24; Seperator = $null; } | |
$segments += [PSCustomObject]@{ Value = " `u{26A1} "; ForegroundColor = 226; BackgroundColor = 24; Seperator = $seperator; } | |
$segments += [PSCustomObject]@{ Value = " $elapsed "; ForegroundColor = 253; BackgroundColor = 31; Seperator = $seperator; } | |
$segments += [PSCustomObject]@{ Value = (" {0:HH\:mm\:ss\.ffff} " -f (Get-Date)); ForegroundColor = 253; BackgroundColor = 236; Seperator = $seperator; } | |
$segments += [PSCustomObject]@{ Value = " `u{e0a0} $(Get-BranchName) "; ForegroundColor = 234; BackgroundColor = 36; Seperator = $seperator; } | |
$segments += [PSCustomObject]@{ Value = " $currentLocation "; ForegroundColor = 253; BackgroundColor = 237; Seperator = $seperator; } | |
$segments += [PSCustomObject]@{ Value = "`n"; ForegroundColor = $null; BackgroundColor = $null; Seperator = $null; } | |
$segments += [PSCustomObject]@{ Value = "$seperator"; ForegroundColor = 0; BackgroundColor = $stateColor; Seperator = $null; } | |
$segments += [PSCustomObject]@{ Value = " `u{2665} "; ForegroundColor = 160; BackgroundColor = $stateColor; Seperator = $seperator; } | |
$segments += [PSCustomObject]@{ Value = " PS "; ForegroundColor = 253; BackgroundColor = 237; Seperator = $seperator; } | |
return ConvertTo-AnsiVT100 -Segments $segments | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment