Last active
February 24, 2025 14:55
-
-
Save JasonKleban/142bc5a24659f80df2ab921e1b43488a to your computer and use it in GitHub Desktop.
posh-git add last command duration
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
# Might not work before Powershell 7 (`$PSVersionTable`) | |
# Might not work before Posh-Git v1 (https://github.com/dahlbyk/posh-git#installing-posh-git-via-powershellget-on-linux-macos-and-windows) | |
# | |
# Find this file's path by running `$Profile` | |
Import-Module posh-git | |
$begin = (Get-Date) | |
function Prefix { | |
function SignificantDigits3 ($value) { | |
$abs = [Math]::Abs($value) | |
if (10 -gt $abs) { | |
('{0:N2}' -f $value).TrimEnd('0') | |
} elseif (100 -gt $abs) { | |
('{0:N1}' -f $value).TrimEnd('0') | |
} else { | |
('{0:N0}' -f $value) | |
} | |
} | |
if ($null -eq (Get-History) -or $null -eq (Get-History)[-1]) { | |
$since = $begin - (Get-Date) | |
if ($since.TotalSeconds -gt -120) { | |
'{0,-5} ' -f ('{0:N0}s' -f $since.TotalSeconds) | |
} else { | |
'{0,-5} ' -f ('{0:N0}m' -f $since.TotalMinutes) | |
} | |
} else { | |
$cmd = (Get-History)[-1] | |
$lastduration = $cmd.EndExecutionTime - $cmd.StartExecutionTime | |
$since = $cmd.EndExecutionTime - (Get-Date) | |
if (($begin - (Get-Date)).TotalSeconds -gt -1) { # ignore quick injected commands | |
'{0,-5} ' -f '0s' | |
} elseif ($since.TotalMilliseconds -gt -250) { | |
'{0,-5} ' -f ('{0}s' -f (SignificantDigits3 $lastduration.TotalSeconds)) | |
if ($lastduration.TotalSeconds -gt 15) { | |
if ($?) { | |
Wait-Job (Start-Job { [console]::beep(784, 750) }), (Start-Job { [console]::beep(1046.5, 750) }) | Remove-Job | |
} else { | |
Wait-Job (Start-Job { [console]::beep(1046.5, 750) }), (Start-Job { [console]::beep(784, 750) }) | Remove-Job | |
} | |
} | |
} elseif ($since.TotalSeconds -gt -120) { | |
'{0,-5} ' -f ('{0:N0}s' -f $since.TotalSeconds) | |
} else { | |
'{0,-5} ' -f ('{0:N0}m' -f $since.TotalMinutes) | |
} | |
} | |
} | |
$GitPromptSettings.DefaultPromptPrefix.Text = '$(Prefix)' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment