Skip to content

Instantly share code, notes, and snippets.

@Iaotle
Created August 19, 2025 09:55
Show Gist options
  • Select an option

  • Save Iaotle/352d911c33a8687cf91bd04c1f700386 to your computer and use it in GitHub Desktop.

Select an option

Save Iaotle/352d911c33a8687cf91bd04c1f700386 to your computer and use it in GitHub Desktop.
# --- Timing state ---
$global:__lastStart = $null
$global:LastCommandDuration = [timespan]::Zero
$global:LastExitStatus = $null
# Record start time each time Enter is pressed
Set-PSReadLineKeyHandler -Key Enter -BriefDescription 'AcceptLineWithTimer' -ScriptBlock {
$global:__lastStart = Get-Date
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
} | Out-Null
function Format-DurationCompact([TimeSpan]$ts) {
if ($ts -lt [TimeSpan]::FromSeconds(60)) {
$secs = [math]::Round($ts.TotalSeconds, 1)
return "/{0}s" -f $secs.ToString("0.0", [System.Globalization.CultureInfo]::InvariantCulture)
}
elseif ($ts -lt [TimeSpan]::FromHours(1)) {
return "/{0}m{1:D2}s" -f [int][math]::Floor($ts.TotalMinutes), $ts.Seconds
}
else {
return "/{0}h{1:D2}m{2:D2}s" -f [int][math]::Floor($ts.TotalHours), $ts.Minutes, $ts.Seconds
}
}
function Write-RightAligned([string]$text) {
try {
$raw = $Host.UI.RawUI
$width = $raw.WindowSize.Width
$len = ($text | Measure-Object -Character).Characters
$cur = $raw.CursorPosition
$target = $cur
$target.X = [Math]::Max(0, $width - $len)
$raw.CursorPosition = $target
Write-Host $text -NoNewline
$cur.X = 0
$raw.CursorPosition = $cur
} catch {
Write-Host $text
}
}
function global:prompt {
# --- capture last command status immediately (before doing anything else) ---
$lastSuccess = $?
$lastExitCode = $LASTEXITCODE
# --- finish timing for the last command ---
if ($global:__lastStart) {
$global:LastCommandDuration = (Get-Date) - $global:__lastStart
$global:__lastStart = $null
}
# --- persist exit status for display (best-practice: use $? for PS errors, $LASTEXITCODE for native) ---
if ($lastSuccess) {
$global:LastExitStatus = 0
} else {
if ($null -ne $lastExitCode -and $lastExitCode -ne 0) {
$global:LastExitStatus = $lastExitCode
} else {
# PowerShell pipeline failure without a native exit code (e.g., command-not-found)
$global:LastExitStatus = 'PS'
}
}
# --- draw a horizontal rule ---
try {
$width = $Host.UI.RawUI.WindowSize.Width
$rule = ($PSStyle.Foreground.BrightBlack + ("─" * $width) + $PSStyle.Reset)
Write-Host $rule
} catch {
Write-Host ("-" * 60)
}
# --- build left prompt (cwd + ») ---
$cwd = (Get-Location).Path
$left = "$($PSStyle.Foreground.BrightCyan)$cwd$($PSStyle.Reset) $([char]0x00BB) "
# --- build right side (time + exit code if failed) ---
$ts = $global:LastCommandDuration
$durationText = if ($ts -and $ts -gt [TimeSpan]::Zero) { "$(Format-DurationCompact $ts)" } else { "/0.0s" }
if ($global:LastExitStatus -ne 0) {
if ($global:LastExitStatus -is [int]) {
$rightText = "$($PSStyle.Foreground.Red)✘ $($global:LastExitStatus)$($PSStyle.Reset) $($PSStyle.Foreground.BrightBlack)$durationText$($PSStyle.Reset)"
} else {
# 'PS' indicates a PowerShell error without a native exit code
$rightText = "$($PSStyle.Foreground.Red)✘$($PSStyle.Reset) $($PSStyle.Foreground.BrightBlack)$durationText$($PSStyle.Reset)"
}
} else {
$rightText = "$($PSStyle.Foreground.BrightBlack)$durationText$($PSStyle.Reset)"
}
Write-RightAligned $rightText
return $left
}
Set-StrictMode -Version Latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment