$Global:__LastHistoryId = -1 function Global:__Terminal-Get-LastExitCode { if ($? -eq $True) { return 0 } # TODO: Should we just return a string instead? # return -1 if ("$LastExitCode" -ne "") { return $LastExitCode } return -1 } function prompt { $gle = $(__Terminal-Get-LastExitCode); $LastHistoryEntry = $(Get-History -Count 1) # Skip finishing the command if the first command has not yet started if ($Global:__LastHistoryId -ne -1) { if ($LastHistoryEntry.Id -eq $Global:__LastHistoryId) { # Don't provide a command line or exit code if there was no history entry (eg. ctrl+c, enter on no command) $out += "`e]133;D`a" } else { # Command finished exit code # OSC 133 ; D [; ] ST $out += "`e]133;D;$gle`a" } } $loc = $($executionContext.SessionState.Path.CurrentLocation); # IMPORTANT: Make sure there's a printable charater _last_ in the prompt. # Otherwise, PSReadline is gonna use the terminating `\` here and colorize # that if it detects a syntax error $out += "`e]133;A$([char]07)"; # Prompt started $out += "`e]9;9;`"$loc`"$([char]07)"; # CWD $out += "PWSH $loc$('>' * ($nestedPromptLevel + 1)) "; # { your prompt here } $out += "`e]133;B$([char]07)"; # Prompt ended == Command started $Global:__LastHistoryId = $LastHistoryEntry.Id return $out }