자바스크립트를 활성화 해주세요

d024 Debug중일때 Prompt바꾸기, Psdbcontext이용합니다

 ·  ☕ 1 min read

    vscode에서 f5를 눌러 debug중인 경우, 프롬프트를 변경할 수 있습니다. 예를 들어 변경한 화면은 다음과 같습니다.

    d024_vscode_debug.png

    테스트 코드

    test.ps1

    1
    2
    3
    4
    5
    
    "Hello, World!" > hello_world.txt
    $content = Get-Content hello_world.txt
    $content = $content -replace "World","PowrShell"
    $content
    $content > hello_world.txt
    

    $profile안에서 prompt 함수를 정의해 두면 됩니다. 이 예제에서 사용하는 코드는 다음과 같습니다.

    profile 코드

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    $env:LC_ALL='C.UTF-8'
    $PSDefaultParameterValues['*:Encoding'] = 'utf8'
    
    # start starship
    Invoke-Expression (&starship init powershell)
    
    $starshipPrompt = (Get-Item Function:\prompt).ScriptBlock
    
    function prompt {
        $out = [system.Text.StringBuilder]::new()
    
        # debug?
        if (Test-Path variable:/PSDebugContext) {
            if ($host.Name -like "Visual Studio Code*") {
                $out.Append("[DBG]: ") > $null
            } else {
                $out.AppendFormat("`e[38;5;202m{0}`e[0m", "[DBG]: ") > $null
            }
        }
    
        # starship
        $out.Append((& $starshipPrompt)) > $null
    
        $out.ToString()
    }
    

    조금 코드가 길어지더라도 SDK Native class를 사용하는 것이 퍼포먼스에 도움이 됩니다.

    포스트가 점점 짧아지고 있습니다. 이미지만 포스트하던 예전 버릇이 되살아나면 곤란하니 주의해야겠습니다.

    레퍼런스

    공유하기

    tkim
    글쓴이
    tkim
    Software Engineer