Skip to content

Instantly share code, notes, and snippets.

@JasonKleban
Last active March 8, 2021 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JasonKleban/142bc5a24659f80df2ab921e1b43488a to your computer and use it in GitHub Desktop.
Save JasonKleban/142bc5a24659f80df2ab921e1b43488a to your computer and use it in GitHub Desktop.
posh-git add last command duration
# 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))
} 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