|
| 1 | +function Prompt { |
| 2 | +# A custom PowerShell prompt that includes command history ID, previous command duration, and current working directory. |
| 3 | +# Uses the Format-TimeSpan function to format the duration. |
| 4 | + function Format-TimeSpan { |
| 5 | + [CmdletBinding()] |
| 6 | + [OutputType([string])] |
| 7 | + param ( |
| 8 | + # The TimeSpan object to format. |
| 9 | + [Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = 'The TimeSpan object to format.')] |
| 10 | + [TimeSpan]$TimeSpan, |
| 11 | + |
| 12 | + # Optional switch to abbreviate the unit labels in the output. |
| 13 | + [Parameter(Mandatory = $false, HelpMessage = 'If specified, unit labels will be abbreviated (e.g., "ms" instead of "milliseconds").')] |
| 14 | + [switch] $Abbreviate |
| 15 | + ) |
| 16 | + |
| 17 | + begin { |
| 18 | + if ($Abbreviate) { |
| 19 | + $MicrosecondLabel = 'µs' |
| 20 | + $MillisecondLabel = 'ms' |
| 21 | + $SecondLabel = 's' |
| 22 | + $MinuteLabel = 'm' |
| 23 | + $HourLabel = 'h' |
| 24 | + $DayLabel = 'd' |
| 25 | + } else { |
| 26 | + $MicrosecondLabel = ' microseconds' |
| 27 | + $MillisecondLabel = ' milliseconds' |
| 28 | + $SecondLabel = ' seconds' |
| 29 | + $MinuteLabel = ' minutes' |
| 30 | + $HourLabel = ' hours' |
| 31 | + $DayLabel = ' days' |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + process { |
| 36 | + $TotalMilliseconds = $TimeSpan.TotalMilliseconds |
| 37 | + |
| 38 | + # Handle zero timespan |
| 39 | + if ($TotalMilliseconds -eq 0) { |
| 40 | + "0$SecondLabel" |
| 41 | + } elseif ($TotalMilliseconds -lt 1) { |
| 42 | + "{0:N2}$MicrosecondLabel" -f ($TotalMilliseconds * 1000) |
| 43 | + } elseif ($TotalMilliseconds -lt 1000) { |
| 44 | + "{0:N2}$MillisecondLabel" -f $TotalMilliseconds |
| 45 | + } elseif ($TimeSpan.TotalSeconds -lt 60) { |
| 46 | + "{0:N2}$SecondLabel" -f $TimeSpan.TotalSeconds |
| 47 | + } elseif ($TimeSpan.TotalMinutes -lt 60) { |
| 48 | + # Build output with non-zero components only |
| 49 | + $Output = "{0:N0}$MinuteLabel" -f $TimeSpan.Minutes |
| 50 | + if ($TimeSpan.Seconds -gt 0) { |
| 51 | + $Output += " {0:N0}$SecondLabel" -f $TimeSpan.Seconds |
| 52 | + } |
| 53 | + $Output |
| 54 | + } elseif ($TimeSpan.TotalHours -lt 24) { |
| 55 | + # Build output with non-zero components only |
| 56 | + $Output = "{0:N0}$HourLabel" -f $TimeSpan.Hours |
| 57 | + if ($TimeSpan.Minutes -gt 0) { |
| 58 | + $Output += " {0:N0}$MinuteLabel" -f $TimeSpan.Minutes |
| 59 | + } |
| 60 | + if ($TimeSpan.Seconds -gt 0) { |
| 61 | + $Output += " {0:N0}$SecondLabel" -f $TimeSpan.Seconds |
| 62 | + } |
| 63 | + $Output |
| 64 | + } else { |
| 65 | + # Build output with non-zero components only |
| 66 | + $Output = "{0:N0}$DayLabel" -f $TimeSpan.Days |
| 67 | + if ($TimeSpan.Hours -gt 0) { |
| 68 | + $Output += " {0:N0}$HourLabel" -f $TimeSpan.Hours |
| 69 | + } |
| 70 | + if ($TimeSpan.Minutes -gt 0) { |
| 71 | + $Output += " {0:N0}$MinuteLabel" -f $TimeSpan.Minutes |
| 72 | + } |
| 73 | + if ($TimeSpan.Seconds -gt 0) { |
| 74 | + $Output += " {0:N0}$SecondLabel" -f $TimeSpan.Seconds |
| 75 | + } |
| 76 | + $Output |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + # The actual prompt output with history ID, previous command duration, and current path. |
| 82 | + Write-Host "[$((Get-History)[-1].Id)] " -NoNewline -ForegroundColor Cyan |
| 83 | + Write-Host "$(Format-TimeSpan -TimeSpan ((Get-History)[-1].Duration) -Abbreviate) " -NoNewline -ForegroundColor Yellow |
| 84 | + Write-Host "$($PWD.ToString().Replace($HOME,'~'))" -ForegroundColor Cyan |
| 85 | + Write-Host '>' -NoNewline -ForegroundColor White |
| 86 | + return ' ' |
| 87 | +} |
0 commit comments