powershell indentation

后端 未结 6 1617
小蘑菇
小蘑菇 2021-02-14 12:05

I\'m writing a large script that deploys an application. This script is based on several nested function calls.

Is there any way to \"ident\" the output based on the dep

6条回答
  •  清酒与你
    2021-02-14 12:52

    You can use Console.CursorLeft to set its position. Be aware that write-output will reset any custom location, so you need to reset it after each output. Here is a sample:

        $i = 0
        function Indent() {
            [console]::CursorLeft += 2
            $i = [console]::CursorLeft
            $i
        }
    
        function UnIndent() {
            if($i -gt 0) { $i -= 2 }
            [console]::CursorLeft = $i
            $i
        }
    
        function WriteIndent([string]$s) {
            [console]::CursorLeft += $i
            write-host $s
            # Reset indent, as write-host will set cursor to indent 0
            [console]::CursorLeft += $i
        }
    
        function myFnNested() {     
          $i = Indent     
          WriteIndent "Start of myFnNested"     
          WriteIndent "End of myFnNested"     
          $i = UnIndent 
        }
    
        function myFn() {     
          $i = Indent   
          WriteIndent "Start of myfn"     
          myFnNested
          WriteIndent "End of myfn"     
          $i = UnIndent 
        } 
    
        WriteIndent "Start of myscript"
        myFn
        WriteIndent "End of myscript"
    

    Output:

        PS C:\scripting> .\Indent-Output.ps1
        Start of myscript
            Start of myfn
                Start of myFnNested
                End of myFnNested
            End of myfn 
        End of myscript
    

提交回复
热议问题