Custom prompt - debug suspend

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Tony Pagliaro Tony Pagliaro 5 months, 2 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #37136
    Profile photo of Tony Pagliaro
    Tony Pagliaro
    Participant

    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
    Confirm
    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
    Profile photo of Tony Pagliaro
    Tony Pagliaro
    Participant

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

    function prompt {
      $p = Split-Path -leaf -path (Get-Location)
      "PS $p$('>' * ($nestedPromptLevel + 1)) "
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.