Custom prompt - debug suspend

This topic contains 2 replies, has 2 voices, and was last updated by  Tony Pagliaro 2 years, 2 months ago.

  • Author
  • #37136

    Tony Pagliaro

    I have a custom prompt in my profile.

    function prompt {
      $p = Split-Path -leaf -path (Get-Location)
      "$p> "

    But when I am debugging a script, I noticed the prompt in suspended mode doesn't have the telltale '>>' I normally see on an non-customized session's prompt.

    PS C:\windows> function testtest () {
    >>     $DebugPreference = 'inquire'
    >>     Write-Debug "test"
    >> }
    PS C:\windows> testtest -debug
    DEBUG: test
    Continue with this operation?
    [Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): s
    PS C:\windows>> 

    How would I change my custom prompt to account for this (and any other) case where the prompt is altered for the user?

  • #37327

    Don Jones

    I think the key is to look at the default prompt code (gc function:prompt):

    "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel
    + 1)) "

    It's the $nestedPromptLevel bit that you're missing. See how it's multiplying ">" times the number of prompts, plus one?

  • #37710

    Tony Pagliaro

    I've used the following and it works as expected 🙂

    function prompt {
      $p = Split-Path -leaf -path (Get-Location)
      "PS $p$('>' * ($nestedPromptLevel + 1)) "

You must be logged in to reply to this topic.