powershell custom prompt
xxxxxxxxxx
<#
.Description
Custom Prompt() function with 3 features:
1. Show execution times for commands
2. Reduce path length to show only 2 parent folders
3. Show git status if the folder is under source control
.Kudos
As mentioned in the post, I mixed the script based on existing scripts by
@ryanyates1990, @texmandie, @fatherjack, @FredWeinmann, @psdbatools, @cl
#>
function Prompt {
try {
$history = Get-History -ErrorAction Ignore -Count 1
if ($history) {
Write-Host "[" -NoNewline
$ts = New-TimeSpan $history.StartExecutionTime $history.EndExecutionTime
switch ($ts) {
{$_.TotalSeconds -lt 1} {
[int]$d = $_.TotalMilliseconds
'{0}ms' -f ($d) | Write-Host -ForegroundColor Black -NoNewline -BackgroundColor DarkGreen
break
}
{$_.totalminutes -lt 1} {
[int]$d = $_.TotalSeconds
'{0}s' -f ($d) | Write-Host -ForegroundColor Black -NoNewline -BackgroundColor DarkYellow
break
}
{$_.totalminutes -ge 1} {
"{0:HH:mm:ss}" -f ([datetime]$ts.Ticks) | Write-Host -ForegroundColor Gray -NoNewline -BackgroundColor Red
break
}
}
Write-Host "] " -NoNewline
}
if(Get-Module Posh-git) {
Write-VcsStatus
Write-Host " " -NoNewline
}
}
catch { }
# New line
Write-Host ""
# show the drive and then last 2 directories of current path
if (($pwd.Path.Split('\').count -gt 3)){
write-host "$($pwd.path.split('\')[0], '...', $pwd.path.split('\')[-2], $pwd.path.split('\')[-1] -join ('\'))" -NoNewline
}
else{
Write-Host "$($pwd.path)" -NoNewline
}
"> "
}
#Adjust your post-git path! I installed it with chocolatey!
Import-Module 'C:\tools\poshgit\dahlbyk-posh-git-9bda399\src\posh-git.psd1'