WriteHost.psm1

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
      for($l=0; $l -lt $blocks.Length; $l++) {
         $block = $blocks[$l]
         if($block -is [string]) {
            if($block -eq "`n") {
               $lineCount++
               $leftLength = 0
               $rightLength = 0
               Write-Host
            } elseif($block -eq "`t") {
               # the length of the rest of the line
               $rightLength = ($(for($r=$l+1; $r -lt $blocks.Length -and $blocks[$r] -is [hashtable]; $r++) {
                  $blocks[$r].length + 1
               }) | Measure-Object -Sum).Sum
               $space = $width - $rightLength - $leftLength

               if($leftLength) {
                  $last = $blocks[($l-1)]
                  $c = @{}
                  if($last.bg) { $c.ForegroundColor = $last.bg }
                  if($last.fg) { $c.BackgroundColor = $last.fg }
                  Write-Host -NoNewLine $RIGHT @c
               }

               Write-Host -NoNewLine (" " * ${space})

               if($rightLength) {
                  $next = $blocks[($l+1)]
                  $c = @{}
                  if($next.bg) { $c.ForegroundColor = $next.bg }
                  if($next.fg) { $c.BackgroundColor = $next.fg }
                  Write-Host -NoNewLine $LEFT @c
               }
            }
         } else {
            if($leftLength -eq 0 -and $rightLength -eq 0) {
               # On a new line, recalculate the length of the "left-aligned" line
               $leftLength = ($(for($r=$l; $r -lt $blocks.Length -and $blocks[$r] -is [hashtable]; $r++) {
                  $blocks[$r].length + 1
               }) | Measure-Object -Sum).Sum
            }

            $c = @{}
            if($block.fg) { $c.ForegroundColor = $block.fg }
            if($block.bg) { $c.BackgroundColor = $block.bg }
            Write-Host -NoNewLine  $block.Text @c
            if($blocks[($l+1)] -is [hashtable])
            {
               if($block.bg -eq $blocks[($l+1)].bg) {
                  Write-Host $GT -NoNewLine @c
               } else {
                  $c = @{}
                  $c.ForegroundColor = if($block.bg) { $block.bg } else { [PowerLineOutput]::EscapeCodes.bg.Default }
                  if($block.bg) { $c.BackgroundColor = $blocks[($l+1)].bg }
                  Write-Host -NoNewLine $RIGHT @c
               }
            }
         }
      }
      # if there's anything on the right and there's no ANSI VT support, put the prompt on the next line
      if($rightLength) {
         Write-Host "`n$RIGHT" -NoNewLine
      }