FatalBulletHit

Enhanced Host Functions

May 14th, 2018 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.  
  3. Function List:
  4.     Adjust-Host
  5.     Animate-Host
  6.     Break-Line
  7.     Choice-AdjustHost
  8.  
  9. Superuser:
  10. https://superuser.com/a/1322721/868077
  11.  
  12. Preview:
  13. https://imgur.com/a/B3FNaej
  14.  
  15. Requires an array list at the beginning of the script:
  16. $HostLogArrayList = New-Object System.Collections.ArrayList
  17.  
  18. Break-Line also supports seperators:
  19. !ui_seperator_big!      : __________________________________________________
  20.                           __________________________________________________
  21.  
  22. !ui_seperator_normal!   : __________________________________________________
  23.  
  24. !ui_seperator_thin!     : --------------------------------------------------
  25.  
  26. And default leading spaces:
  27. $DefaultLeadingSpaces = 1
  28.  
  29. #>
  30.  
  31. <#
  32. .SYNOPSIS
  33. Adjusts UI to window width.
  34.  
  35. .DESCRIPTION
  36. Adjusts the UI to the window width as long as a monitored process is running or for n iterations.
  37. Also allows a simple dot animation based on Animate-Host.
  38.  
  39. .PARAMETER MonitoredProcessId
  40. The Process ID (PID) of the process to be monitored.
  41. The loop will run as long as the monitored process is running.
  42.  
  43. .PARAMETER Iteration
  44. Number of iterations for the loop.
  45.  
  46. .PARAMETER Duration
  47. Duration in milliseconds for the loop.
  48.  
  49. .PARAMETER BufferWidth
  50. Minimum buffer width (default is 50).
  51.  
  52. .PARAMETER TempMessage
  53. A normal message which will not get logged.
  54.  
  55. .PARAMETER TempLeadingSpaces
  56. Leading spaces to be displayed in front of the normal message.
  57.  
  58. .PARAMETER TempColor
  59. The color of the normal message.
  60.  
  61. .PARAMETER TempNoNewline
  62.  
  63. .PARAMETER AnimateHostMessage
  64. The message to be displayed in front of the dots.
  65.  
  66. .PARAMETER AnimateHostLeadingSpaces
  67. Leading spaces to be displayed in front of the animated message.
  68.  
  69. .PARAMETER AnimateHostColor
  70. The color of the animated message.
  71.  
  72. .PARAMETER AnimateHostTitleMain
  73. The console window title in front of the dots.
  74.  
  75. .PARAMETER AnimateHostTitleSub
  76. The console window title after the dots.
  77.  
  78. .PARAMETER BufferOnly
  79. Only adjusts the buffer size.
  80.  
  81. .PARAMETER NoLog
  82. Does not add the $AnimateHostMessage to the $HostLogArrayList array.
  83.  
  84. .EXAMPLE
  85. C:\PS> Adjust-Host -MonitoredProcessId 6376
  86.  
  87. .EXAMPLE
  88. C:\PS> Adjust-Host 6379 -Duration 3000 -BufferOnly
  89.  
  90. .EXAMPLE
  91. C:\PS> Adjust-Host -MonitoredProcessId $SomeProcess.Id -AnimateHostMessage "Downloading" -AnimateHostLeadingSpaces 1 -AnimateHostColor Yellow
  92. #>
  93. function Adjust-Host {
  94.  
  95.     Param (
  96.    
  97.         [Parameter(ParameterSetName = 'Monitoring', Position = 0)]
  98.         [Int32[]] $MonitoredProcessId,
  99.         [Parameter(ParameterSetName = 'Iterating', Position = 0)]
  100.         [Int32] $Iteration,
  101.         [Parameter(ParameterSetName = 'Duration', Position = 0)]
  102.         [Int32] $Duration,
  103.         [AllowNull()]
  104.         [Int32] $BufferWidth,
  105.         [String] $TempMessage,
  106.         [Int32] $TempLeadingSpaces,
  107.         [ConsoleColor] $TempColor,
  108.         [Switch] $TempNoNewline,
  109.         [String] $AnimateHostMessage,
  110.         [Int32] $AnimateHostLeadingSpaces,
  111.         [ConsoleColor] $AnimateHostColor,
  112.         [String] $AnimateHostTitleMain,
  113.         [String] $AnimateHostTitleSub,
  114.         [Switch] $BufferOnly,
  115.         [Switch] $NoLog
  116.    
  117.     )
  118.  
  119.     $IterationCounter = 0
  120.     $AdjustHost_main_start = (Get-Date)
  121.     $window_width = $Host.UI.RawUI.WindowSize.Width
  122.  
  123.     if ($MonitoredProcessId -eq $null -or $MonitoredProcessId -eq '') {
  124.    
  125.         $MonitoredProcessId = [Int32]::MaxValue
  126.    
  127.     }
  128.    
  129.     if ($AnimateHostColor -eq $null -or $AnimateHostColor -eq '') {
  130.  
  131.         $AnimateHostColor = $Host.UI.RawUI.ForegroundColor
  132.  
  133.     }
  134.    
  135.     if ($BufferWidth -eq $null -or $BufferWidth -eq '') {
  136.    
  137.         $BufferWidth = 50
  138.    
  139.     }
  140.    
  141.     while ((Get-Process -Id $MonitoredProcessId 2>$null) -ne $null -or $IterationCounter -lt $Iteration -or ((Get-Date) - $AdjustHost_main_start).TotalMilliseconds -lt $Duration) {
  142.    
  143.         if ($window_width -ne $Host.UI.RawUI.WindowSize.Width -or $Iteration -eq 1 -and !$BufferOnly) {
  144.  
  145.             if ($Host.UI.RawUI.WindowSize.Width -lt $BufferWidth) {
  146.  
  147.                 $Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size($BufferWidth, $Host.UI.RawUI.BufferSize.Height)
  148.  
  149.             }
  150.  
  151.             Clear-Host
  152.            
  153.             for ($x=0; $x -le $HostLogArrayList.Count; $x += 5) {
  154.            
  155.                 $prev_message = $cur_message
  156.                 $prev_leading_spaces = $cur_leading_spaces
  157.                 $prev_color = $cur_color
  158.                 $prev_new_line = $cur_new_line
  159.                 $prev_start_spacer = $cur_start_spacer
  160.                
  161.                 $cur_message = $HostLogArrayList[$x]
  162.                 $cur_leading_spaces = $HostLogArrayList[$x+1]
  163.                 $cur_color = $HostLogArrayList[$x+2]
  164.                 $cur_new_line = $HostLogArrayList[$x+3]
  165.                 $cur_start_spacer = $HostLogArrayList[$x+4]
  166.                
  167.                 if ($cur_color -eq $prev_color -and $cur_leading_spaces -eq $prev_leading_spaces -and ($prev_start_spacer -eq 'False' -or $prev_start_spacer -eq $false)) {
  168.                
  169.                     if ($prev_new_line -eq 'True' -or $prev_new_line -eq $true) {
  170.                    
  171.                         $cur_message = $prev_message + $cur_message
  172.                    
  173.                     } else {
  174.                    
  175.                         $cur_message = $prev_message + "`n" + $cur_message
  176.                    
  177.                     }
  178.                
  179.                 } else {
  180.                
  181.                     if ($prev_color -eq '' -or $prev_color -eq $null) {
  182.                    
  183.                         $prev_color = $Host.UI.RawUI.ForegroundColor
  184.                    
  185.                     }
  186.                                        
  187.                     if ($prev_start_spacer -eq 'True' -or $prev_start_spacer -eq $true) {
  188.                    
  189.                         if (($prev_new_line -eq 'True' -or $prev_new_line -eq $true) -and $prev_message -ne $null) {
  190.                            
  191.                             Break-Line $prev_message -Color $prev_color -LeadingSpaces $prev_leading_spaces -NoNewline -NoStartSpacer -NoLog
  192.                        
  193.                         } elseif ($prev_message -ne $null) {
  194.                            
  195.                             Break-Line $prev_message -Color $prev_color -LeadingSpaces $prev_leading_spaces -NoStartSpacer -NoLog
  196.                        
  197.                         }
  198.                    
  199.                     } else {
  200.                    
  201.                         if (($prev_new_line -eq 'True' -or $prev_new_line -eq $true) -and $prev_message -ne $null) {
  202.                            
  203.                             Break-Line $prev_message -Color $prev_color -LeadingSpaces $prev_leading_spaces -NoNewline -NoLog
  204.                        
  205.                         } elseif ($prev_message -ne $null) {
  206.                            
  207.                             Break-Line $prev_message -Color $prev_color -LeadingSpaces $prev_leading_spaces -NoLog
  208.                        
  209.                         }
  210.  
  211.                    
  212.                     }
  213.                 }
  214.             }
  215.  
  216.             if ($TempMessage -ne $null -and $TempMessage -ne '' -and $TempNoNewline) {
  217.            
  218.                 Break-Line $TempMessage -LeadingSpaces $TempLeadingSpaces -Color $TempColor -NoLog -NoNewline
  219.            
  220.             } elseif ($TempMessage -ne $null -and $TempMessage -ne '') {
  221.            
  222.                 Break-Line $TempMessage -LeadingSpaces $TempLeadingSpaces -Color $TempColor -NoLog
  223.            
  224.             }
  225.            
  226.             $window_width = $Host.UI.RawUI.WindowSize.Width
  227.        
  228.         } else {
  229.        
  230.             if ($Host.UI.RawUI.WindowSize.Width -lt $BufferWidth) {
  231.  
  232.                 $Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size($BufferWidth, $Host.UI.RawUI.BufferSize.Height)
  233.                 $window_width = $Host.UI.RawUI.WindowSize.Width
  234.            
  235.             }
  236.  
  237.             if ($AnimateHostMessage -ne $null -and $AnimateHostMessage -ne '') {
  238.                
  239.                 Animate-Host -MonitoredProcessId $MonitoredProcessId -Message $AnimateHostMessage -LeadingSpaces $AnimateHostLeadingSpaces -Color $AnimateHostColor -OnAdjustHost -TitleMain $AnimateHostTitleMain -TitleSub $AnimateHostTitleSub
  240.  
  241.             }
  242.         }
  243.        
  244.         $IterationCounter++
  245.         Sleep -Milliseconds 10
  246.        
  247.     }
  248.    
  249.     if ($AnimateHostMessage -ne $null -and $AnimateHostMessage -ne '' -and !$NoLog) {
  250.    
  251.         Break-Line "$AnimateHostMessage..." -LeadingSpaces $AnimateHostLeadingSpaces -Color $AnimateHostColor -NoNewline
  252.         Adjust-Host -Iteration 1
  253.    
  254.     }
  255. }
  256.  
  257.  
  258.  
  259. <#
  260. .SYNOPSIS
  261. Simple dot animation.
  262.  
  263. .DESCRIPTION
  264. Animates dots while a monitored process is running.
  265.  
  266. .PARAMETER MonitoredProcessId
  267. The Process ID (PID) of the process to be monitored.
  268. The loop will run as long as the monitored process is running.
  269.  
  270. .PARAMETER Iteration
  271. Number of iterations for the loop (one iteration takes slightly more than 1 second).
  272.  
  273. .PARAMETER Duration
  274. Duration in seconds for the loop (one iteration takes slightly more than 1 second).
  275.  
  276. .PARAMETER Message
  277. The message to be displayed in front of the dots.
  278.  
  279. .PARAMETER LeadingSpaces
  280. Leading spaces to be displayed in front of the message.
  281.  
  282. .PARAMETER Color
  283. The color of the message.
  284.  
  285. .PARAMETER TitleMain
  286. The console window title in front of the dots.
  287.  
  288. .PARAMETER TitleSub
  289. The console window title after the dots.
  290.  
  291. .PARAMETER OnAdjustHost
  292. Breaks the loop when the window width was changed.
  293. Only used by Adjust-Host.
  294.  
  295. .EXAMPLE
  296. C:\PS> Animate-Host -MonitoredProcessId 1112 -Message "Doing stuff" -LeadingSpaces 1 -Color Green
  297.  
  298. .EXAMPLE
  299. C:\PS> Animate-Host -Iteration 3 "Doing some more stuff" -TitleMain "MyProgramm - Doing some more stuff" -TitleSub "(Step 2/3)"
  300. #>
  301. function Animate-Host {
  302.  
  303.     Param (
  304.    
  305.         [Parameter(ParameterSetName = 'MonitoredProcess', Position = 0)]
  306.         [Int32[]] $MonitoredProcessId,
  307.         [Parameter(ParameterSetName = 'Iteration', Position = 0)]
  308.         [Int32] $Iteration,
  309.         [Parameter(ParameterSetName = 'Duration', Position = 0)]
  310.         [Int32] $Duration,
  311.         [Parameter(Mandatory = $true, Position = 1)]
  312.         [String] $Message,
  313.         [Int32] $LeadingSpaces,
  314.         [ConsoleColor] $Color,
  315.         [String] $TitleMain,
  316.         [AllowNull()]
  317.         [String] $TitleSub,
  318.         [Switch] $OnAdjustHost
  319.  
  320.     )
  321.    
  322.     $IterationCounter = 0
  323.     $AnimateHost_main_start = (Get-Date)
  324.     if ($MonitoredProcessId -eq $null -or $MonitoredProcessId -eq '') {
  325.    
  326.         $MonitoredProcessId = [Int32]::MaxValue
  327.    
  328.     }
  329.    
  330.     if (($LeadingSpaces -eq $null -or $LeadingSpaces -eq '') -and $DefaultLeadingSpaces -is [int]) {
  331.        
  332.         $LeadingSpaces = $DefaultLeadingSpaces
  333.    
  334.     }
  335.    
  336.     if ($Color -eq '' -or $Color -eq $null) {
  337.    
  338.         $Color = $Host.UI.RawUI.ForegroundColor
  339.  
  340.     }
  341.    
  342.     Break-Line "$Message   " -LeadingSpaces $LeadingSpaces -Color $Color -Width ($Host.UI.RawUI.WindowSize.Width + 3) -NoNewline -NoLog
  343.     $window_width = $Host.UI.RawUI.WindowSize.Width
  344.    
  345.     $AnimateHost_loop = {
  346.    
  347.         $AnimateHost_loop_start = (Get-Date)
  348.  
  349.         while (((Get-Date) - $AnimateHost_loop_start).TotalMilliseconds -lt 250) {
  350.        
  351.             if ($OnAdjustHost -and $window_width -ne $Host.UI.RawUI.WindowSize.Width) {
  352.  
  353.                 Break main
  354.  
  355.             } elseif ($OnAdjustHost -and $Host.UI.RawUI.WindowSize.Width -lt 50 -and $Host.UI.RawUI.BufferSize.Width -ne 50) {
  356.  
  357.                 $Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(50, $Host.UI.RawUI.BufferSize.Height)
  358.  
  359.             }
  360.            
  361.             Sleep -Milliseconds 1
  362.  
  363.         }
  364.     }
  365.    
  366.     :main while ((Get-Process -Id $MonitoredProcessId 2>$null) -ne $null -or $IterationCounter -lt $Iteration -or ((Get-Date) - $AnimateHost_main_start).TotalMilliseconds -lt $Duration * 1000) {
  367.        
  368.         Write-Host "`b`b`b   " -ForegroundColor $Color -NoNewline
  369.        
  370.         if ($TitleMain -ne '' -and $TitleMain -ne $null) {
  371.        
  372.             $Host.UI.RawUI.WindowTitle = "$TitleMain    $TitleSub"
  373.            
  374.         }
  375.  
  376.         & $AnimateHost_loop
  377.  
  378.         Write-Host "`b`b`b.  " -ForegroundColor $Color -NoNewline
  379.        
  380.         if ($TitleMain -ne '' -and $TitleMain -ne $null) {
  381.        
  382.             $Host.UI.RawUI.WindowTitle = "$TitleMain.   $TitleSub"
  383.            
  384.         }
  385.  
  386.         & $AnimateHost_loop
  387.        
  388.         Write-Host "`b`b`b.. " -ForegroundColor $Color -NoNewline
  389.        
  390.         if ($TitleMain -ne '' -and $TitleMain -ne $null) {
  391.        
  392.             $Host.UI.RawUI.WindowTitle = "$TitleMain..  $TitleSub"
  393.            
  394.         }
  395.  
  396.         & $AnimateHost_loop
  397.  
  398.         Write-Host "`b`b`b..." -ForegroundColor $Color -NoNewline
  399.        
  400.         if ($TitleMain -ne '' -and $TitleMain -ne $null) {
  401.        
  402.             $Host.UI.RawUI.WindowTitle = "$TitleMain... $TitleSub"
  403.            
  404.         }
  405.  
  406.         $IterationCounter++
  407.         & $AnimateHost_loop
  408.        
  409.     }
  410.    
  411.     if (!$OnAdjustHost) {
  412.    
  413.         Break-Line "$Message..." -LeadingSpaces $LeadingSpaces -Color $Color -NoNewline -LogOnly
  414.  
  415.     }
  416. }
  417.  
  418.  
  419.  
  420. <#
  421. .SYNOPSIS
  422. Prevents words from getting split due to line breaks.
  423.  
  424. .DESCRIPTION
  425. Splits a message at spaces based on a user defined Width or the window width.
  426.  
  427. .PARAMETER Message
  428. The message to be split.
  429.  
  430. .PARAMETER LeadingSpaces
  431. Leading spaces to be displayed in front of the message.
  432.  
  433. .PARAMETER Color
  434. The color of the message.
  435.  
  436. .PARAMETER Width
  437. The maximum Width per line.
  438.  
  439. .PARAMETER WindowWidth
  440. Uses the window width instead of the Width.
  441.  
  442. .PARAMETER NoNewline
  443. Does not add a new line ('`n') at the end of the message.
  444.  
  445. .PARAMETER NoStartSpacer
  446. Does not add the spacer at the beginning of the first line.
  447.  
  448. .PARAMETER NoLog
  449. Does not add the message to the $HostLogArrayList array.
  450.  
  451. .PARAMETER LogOnly
  452. Only adds the message to the $HostLogArrayList array.
  453.  
  454. .PARAMETER ReplaceLastMessage
  455. Replace the last message.
  456.  
  457. .PARAMETER DontAddToLogFile
  458. Does not add the output to the log file.
  459. Output will still be added to the $HostLogArrayList!
  460.  
  461. .EXAMPLE
  462. C:\PS> Break-Line "This is a very long message that would normally get split in the middle of a word" -LeadingSpaces 1 -WindowWidth
  463.  
  464. .EXAMPLE
  465. C:\PS> Break-Line $MyMessage -Color Green -Width 50 -NoNewline -NoLog
  466.  
  467. .EXAMPLE
  468. C:\PS> Break-Line "A short message" -WindowWidth -LogOnly
  469.  
  470. .EXAMPLE
  471. C:\PS> Break-Line -ReplaceLastMessage -Message "My new message" -LeadingSpaces 0 -NoNewline
  472. #>
  473. function Break-Line {
  474.  
  475.     Param (
  476.    
  477.         [Parameter(Mandatory = $true, ParameterSetName = 'ReplaceLastMessage')]
  478.         [Switch] $ReplaceLastMessage,
  479.         [Parameter(Mandatory = $true, ParameterSetName = 'ReplaceLastMessage', Position = 1)]
  480.         [Parameter(Mandatory = $true, ParameterSetName = 'Default', Position = 0)]
  481.         [String] $Message,
  482.         [Int32] $LeadingSpaces,
  483.         [AllowNull()]
  484.         [ConsoleColor] $Color,
  485.         [Int32] $Width,
  486.         [Switch] $WindowWidth,
  487.         [Switch] $NoNewline,
  488.         [Switch] $NoStartSpacer,
  489.         [Switch] $NoLog,
  490.         [Switch] $LogOnly,
  491.         [Switch] $DontAddToLogFile
  492.  
  493.     )
  494.  
  495.     if ($ReplaceLastMessage) {
  496.  
  497.         if ($Message -ne '' -and $Message -ne $null) {
  498.  
  499.             $HostLogArrayList.Item($HostLogArrayList.Count - 5) = $Message
  500.        
  501.         }
  502.  
  503.         if ($LeadingSpaces -ne '' -and $LeadingSpaces -ne $null) {
  504.  
  505.             $HostLogArrayList.Item($HostLogArrayList.Count - 4) = $LeadingSpaces
  506.        
  507.         }
  508.  
  509.         if ($Color -ne '' -and $Color -ne $null) {
  510.  
  511.             $HostLogArrayList.Item($HostLogArrayList.Count - 3) = $Color
  512.        
  513.         }
  514.        
  515.         if ($NoNewline -ne '' -and $NoNewline -ne $null) {
  516.  
  517.             $HostLogArrayList.Item($HostLogArrayList.Count - 2) = $NoNewline
  518.  
  519.         }
  520.        
  521.         if ($NoStartSpacer -ne '' -and $NoStartSpacer -ne $null) {
  522.        
  523.             $HostLogArrayList.Item($HostLogArrayList.Count - 1) = $NoStartSpacer
  524.        
  525.         }
  526.        
  527.         Adjust-Host -Iteration 1
  528.  
  529.     } else {
  530.    
  531.         if ($WindowWidth -or $Width -eq '' -or $Width -eq $null) {
  532.        
  533.             $Width = $Host.UI.RawUI.BufferSize.Width
  534.            
  535.             if ($Width -lt 50) {
  536.            
  537.                 $Width = 50
  538.            
  539.             }
  540.         }
  541.        
  542.         if (($LeadingSpaces -eq $null -or $LeadingSpaces -eq '') -and $DefaultLeadingSpaces -is [int]) {
  543.        
  544.             $LeadingSpaces = $DefaultLeadingSpaces
  545.        
  546.         }
  547.        
  548.         if ($Color -eq '' -or $Color -eq $null) {
  549.        
  550.             $Color = $Host.UI.RawUI.ForegroundColor
  551.  
  552.         }  
  553.  
  554.         if (!$NoLog -or $LogOnly) {
  555.        
  556.             $HostLogArrayList.Add($Message) 2>&1>$null
  557.             $HostLogArrayList.Add($LeadingSpaces) 2>&1>$null
  558.             $HostLogArrayList.Add($Color) 2>&1>$null
  559.             $HostLogArrayList.Add($NoNewline) 2>&1>$null
  560.             $HostLogArrayList.Add($NoStartSpacer) 2>&1>$null
  561.                
  562.         }
  563.  
  564.         if ($LeadingSpaces -gt 0 -and !$LogOnly) {
  565.        
  566.             $spacer = ' '*$LeadingSpaces
  567.            
  568.             if (!$NoStartSpacer) {
  569.            
  570.                 $output = $spacer
  571.            
  572.             }
  573.         }
  574.        
  575.         if ($LogFilePath -ne $null -and $LogFilePath -ne '' -and !$NoLog -and !$DontAddToLogFile) {
  576.        
  577.                 $MessageOutFile = $Message -replace ('!ui_seperator_thin!', ("`n" + '-' * 100 + "`n")) -replace ('!ui_seperator_normal!', ("`n" + '_' * 100 + "`n")) -replace ('!ui_seperator_big!', ("`n" + [string]('_' * 100 + "`n") * 2))
  578.            
  579.             if ($NoNewline) {
  580.            
  581.                 $MessageOutFile | Add-Content -Path $LogFilePath -Encoding Unicode -NoNewline
  582.                
  583.             } else {
  584.            
  585.                 $MessageOutFile | Add-Content -Path $LogFilePath -Encoding Unicode
  586.            
  587.             }
  588.         }
  589.        
  590.         $Message = $Message -replace ('!ui_seperator_thin!', ('-' * $Width + "`n")) -replace ('!ui_seperator_normal!', ('_' * $Width + "`n")) -replace ('!ui_seperator_big!', ([string]('_' * $Width) * 2 + "`n`n"))
  591.        
  592.         if (!$LogOnly) {
  593.  
  594.             $Message -split '(?<= )' -split "(?<=`n)" | ForEach-Object {
  595.  
  596.                 if ($_.Contains(('-' * $Width)) -or $_.Contains(('_' * $Width)) -or $_.Contains([string]('_' * $Width) * 2)) {
  597.                
  598.                     $output += "`n" + $_
  599.                
  600.                 } elseif ($_.Contains("`n")) {
  601.                
  602.                     if (($line_Width += $_.Length) -le $Width) {
  603.                
  604.                         $output += ($_ + $spacer)
  605.                    
  606.                     } else {
  607.                
  608.                         $output += ("`n" + $spacer + $_ + $spacer)
  609.                
  610.                     }
  611.                    
  612.                     $line_Width = $LeadingSpaces
  613.  
  614.                 } else {
  615.                
  616.                     if (($line_Width += $_.Length) -lt $Width) {
  617.                
  618.                         $output += $_
  619.                    
  620.                     } else {
  621.                
  622.                         $output += ("`n" + $spacer + $_)
  623.                         $line_Width = $LeadingSpaces + $_.Length
  624.                
  625.                     }
  626.                 }
  627.             }
  628.  
  629.         }
  630.        
  631.         if ($NoNewline -and !$LogOnly) {
  632.        
  633.             Write-Host -ForegroundColor $Color $output -NoNewline
  634.        
  635.         } elseif (!$LogOnly) {
  636.        
  637.             Write-Host -ForegroundColor $Color $output
  638.        
  639.         }
  640.     }
  641. }
  642.  
  643.  
  644.  
  645. <#
  646. .SYNOPSIS
  647. Waits for user input while adjusting host and returns it or sets a variable.
  648.  
  649. .DESCRIPTION
  650. Waits for user input and optionally adjusts host (function Adjust-Host required).
  651. If the $Options array contains the input it gets returned or a variable is set with either a specified value or the input itself.
  652. If it does not contain the input it repeats the loop.
  653.  
  654. .PARAMETER Options
  655. Array of keys for choice option.
  656.  
  657. .PARAMETER Name
  658. The name or an array of names of the variable(s) to be set.
  659.  
  660. .PARAMETER SetOnlyDefined
  661. Will only assign a value to a variable if the option contains an array of a key and a value.
  662.  
  663. .PARAMETER ChoiceMessage
  664. Array of short info for each choice option.
  665.  
  666. .PARAMETER ChoiceMessageLeadingSpaces
  667. Leading spaces for the short info.
  668.  
  669. .PARAMETER ChoiceMessageColor
  670. Color for the short info.
  671.  
  672. .PARAMETER NoLog
  673. Does not add the message to the $HostLogArrayList array.
  674.  
  675. .PARAMETER DontAdjust
  676. Does not adjust the host.
  677.  
  678. .PARAMETER BufferOnly
  679. Only adjusts the buffer size.
  680.  
  681. .PARAMETER Return
  682. Returns key even if a variable was defined.
  683.  
  684. .PARAMETER NoReturn
  685. Does not return key even if no variable was defined.
  686.  
  687. .OUTPUTS
  688. Pressed key.
  689.  
  690. .EXAMPLE
  691. C:\PS> Choice-AdjustHost -Options ('y', 'n') -ChoiceMessage ('Yes', 'No') -ChoiceMessageLeadingSpaces 1 -ChoiceMessageColor Red
  692.  
  693. .EXAMPLE
  694. C:\PS> Choice-AdjustHost (('a', 'valueA'), ('b', 'valueB'), 'c') -Name 'MyVariable_with_valueAorB' -SetOnlyDefined
  695.  
  696. .EXAMPLE
  697. C:\PS> Choice-AdjustHost $MyOptionsArray -Name ('MyVariable_1_KeyCharValue', 'MyVariable_2_KeyCharValue', 'MyVariable_n_KeyCharValue')
  698. #>
  699. function Choice-AdjustHost {
  700.  
  701.     Param (
  702.  
  703.         [Parameter(Mandatory = $true)]
  704.         [Array] $Options,
  705.         [String[]] $Name,
  706.         [Switch] $SetOnlyDefined,
  707.         [Int32] $AdjustHostBufferWidth,
  708.         [Array] $ChoiceMessage,
  709.         [Int32] $ChoiceMessageLeadingSpaces,
  710.         [ConsoleColor] $ChoiceMessageColor,
  711.         [Switch] $NoLog,
  712.         [Switch] $DontAdjust,
  713.         [Switch] $BufferOnly,
  714.         [Switch] $Return,
  715.         [Switch] $NoReturn
  716.        
  717.     )
  718.    
  719.     if ($ChoiceMessage.Count -gt 0) {
  720.    
  721.         $ChoiceMessage | ForEach-Object {
  722.  
  723.             $MessageOptions += (' / [' + $_.Substring(0, 1) + ']' + $_.Substring(1))
  724.  
  725.         }
  726.  
  727.         $MessageOptions = $MessageOptions.Substring(3)
  728.        
  729.         if ($ChoiceMessageLeadingSpaces -isnot [int] -and $DefaultLeadingSpaces -is [int]) {
  730.        
  731.             $ChoiceMessageLeadingSpaces = $DefaultLeadingSpaces
  732.        
  733.         }
  734.        
  735.         if ($ChoiceMessageColor -isnot [ConsoleColor]) {
  736.        
  737.             $ChoiceMessageColor = $Host.UI.RawUI.ForegroundColor
  738.  
  739.         }
  740.        
  741.         Break-Line $MessageOptions -LeadingSpaces $ChoiceMessageLeadingSpaces -Color $ChoiceMessageColor -NoLog -NoNewline
  742.        
  743.     }
  744.    
  745.     $OptionKeys = $null
  746.    
  747.     $Options | ForEach-Object {
  748.  
  749.         if ($_ -is [Array]) {
  750.  
  751.             $OptionKeys += @($_[0])
  752.  
  753.         } else {
  754.  
  755.             $OptionKeys += @($_)
  756.  
  757.         }
  758.     }
  759.    
  760.     $env:OptionKeys = $OptionKeys
  761.    
  762.     $ChoiceProcess = Start-Process PowerShell {
  763.    
  764.         $env:OptionKeys.Split(' ') | ForEach-Object {
  765.        
  766.             $OptionKeys += @($_)
  767.        
  768.         }
  769.        
  770.         $KeyChar = $null
  771.        
  772.         while (!$OptionKeys.Contains($KeyChar)) {
  773.  
  774.             $KeyChar = [string]([System.Console]::ReadKey('NoEcho')).KeyChar
  775.            
  776.         }
  777.        
  778.         $ChoiceCounter = 0
  779.        
  780.         while ($OptionKeys[$ChoiceCounter] -ne $KeyChar) {
  781.  
  782.             $ChoiceCounter++
  783.  
  784.         }
  785.        
  786.         return $ChoiceCounter
  787.        
  788.     } -PassThru -NoNewWindow -RedirectStandardOutput "$env:TEMP\r6s_extractor_choice_option_index.temp"
  789.        
  790.     if (!$DontAdjust -and $BufferOnly -and $ChoiceMessage.Count -gt 0) {
  791.        
  792.         Adjust-Host -MonitoredProcessId $ChoiceProcess.Id -BufferWidth $AdjustHostBufferWidth -BufferOnly -TempMessage $MessageOptions -TempLeadingSpaces $ChoiceMessageLeadingSpaces -TempColor $ChoiceMessageColor -TempNoNewline
  793.        
  794.     } elseif (!$DontAdjust -and $ChoiceMessage.Count -gt 0) {
  795.        
  796.         Adjust-Host -MonitoredProcessId $ChoiceProcess.Id -BufferWidth $AdjustHostBufferWidth -TempMessage $MessageOptions -TempLeadingSpaces $ChoiceMessageLeadingSpaces -TempColor $ChoiceMessageColor -TempNoNewline
  797.        
  798.     } else {
  799.    
  800.         while ((Get-Process -Id $ChoiceProcess.Id 2>$null) -ne $null) {
  801.        
  802.             Sleep -Milliseconds 10
  803.        
  804.         }
  805.     }
  806.    
  807.     [Int32]$ChoiceCounter = (Get-Content "$env:TEMP\r6s_extractor_choice_option_index.temp")
  808.    
  809.     if ($ChoiceMessage.Count -eq 1) {
  810.    
  811.         Write-Host "`r" -NoNewline
  812.        
  813.         if ($NoLog) {
  814.    
  815.             Break-Line $MessageOptions -Color Yellow -NoLog
  816.            
  817.         } else {
  818.        
  819.             Break-Line $MessageOptions -Color Yellow
  820.        
  821.         }
  822.    
  823.     } elseif ($ChoiceMessage.Count -gt 1 -and $NoLog) {
  824.    
  825.         $SelectedMessage = ('[' + $ChoiceMessage[$ChoiceCounter].Substring(0, 1) + ']' + $ChoiceMessage[$ChoiceCounter].Substring(1))
  826.  
  827.         if ($ChoiceCounter -gt 0) {
  828.        
  829.             Break-Line ("`r" + $MessageOptions -split ($SelectedMessage).Replace('[', '\['))[0] -NoNewline -NoLog
  830.  
  831.         } else {
  832.  
  833.             Break-Line $SelectedMessage -Color Yellow -NoNewline -NoLog
  834.  
  835.         }
  836.        
  837.         if ($ChoiceCounter -lt $ChoiceMessage.Count - 1) {
  838.  
  839.             if ($ChoiceCounter -gt 0) {
  840.            
  841.                 Write-Host -ForegroundColor Yellow (' ' * $DefaultLeadingSpaces + $SelectedMessage) -NoNewline
  842.  
  843.             }
  844.  
  845.             Break-Line ($MessageOptions -split ($SelectedMessage).Replace('[', '\['))[1] -NoLog
  846.  
  847.         } else {
  848.  
  849.             Write-Host -ForegroundColor Yellow (' ' * $DefaultLeadingSpaces + $SelectedMessage)
  850.  
  851.         }
  852.        
  853.     } elseif ($ChoiceMessage.Count -gt 1) {
  854.    
  855.         $SelectedMessage = ('[' + $ChoiceMessage[$ChoiceCounter].Substring(0, 1) + ']' + $ChoiceMessage[$ChoiceCounter].Substring(1))
  856.  
  857.         if ($ChoiceCounter -gt 0) {
  858.        
  859.             Write-Host "`r" -NoNewline
  860.             Break-Line ($MessageOptions -split ($SelectedMessage).Replace('[', '\['))[0] -NoNewline
  861.             Write-Host "`b" -NoNewline
  862.  
  863.         } else {
  864.  
  865.             Write-Host "`r" -NoNewline
  866.             Break-Line $SelectedMessage -Color Yellow -NoNewline
  867.  
  868.         }
  869.        
  870.         if ($ChoiceCounter -lt $ChoiceMessage.Count - 1) {
  871.  
  872.             if ($ChoiceCounter -gt 0) {
  873.            
  874.                 Break-Line $SelectedMessage -Color Yellow -NoNewline -NoStartSpacer -LogOnly
  875.                 Write-Host -ForegroundColor Yellow (' ' * $DefaultLeadingSpaces + $SelectedMessage) -NoNewline
  876.  
  877.             }
  878.  
  879.             Break-Line (($MessageOptions -split ($SelectedMessage).Replace('[', '\['))[1]).Substring(1)
  880.  
  881.         } else {
  882.  
  883.             Break-Line $SelectedMessage -Color Yellow -NoStartSpacer -LogOnly
  884.             Write-Host -ForegroundColor Yellow (' ' * $DefaultLeadingSpaces + $SelectedMessage)
  885.  
  886.         }
  887.     }
  888.    
  889.     if ($Name -ne $null -and $Name -ne '') {
  890.    
  891.         if ($Options[$ChoiceCounter] -is [Array]) {
  892.  
  893.             $Value = ($Options[$ChoiceCounter])[1]
  894.  
  895.         } elseif (!$SetOnlyDefined) {
  896.  
  897.             $Value = $Options[$ChoiceCounter]
  898.  
  899.         }
  900.        
  901.         if ($Value -ne $null -and [string]$Value -ne '') {
  902.        
  903.             if ($Name -is [Array]) {
  904.            
  905.                 $Name | ForEach-Object {
  906.                
  907.                     Set-Variable -Name $_ -Value $Value -Scope 1
  908.                
  909.                 }
  910.            
  911.             } else {
  912.            
  913.                 Set-Variable -Name $Name -Value $Value -Scope 1
  914.  
  915.             }
  916.            
  917.         } elseif (!$NoReturn) {
  918.        
  919.             return $OptionKeys[$ChoiceCounter]
  920.        
  921.         }
  922.  
  923.     }
  924.    
  925.     if (($Name -eq $null -or $Name -eq '' -or $Return) -and !$NoReturn) {
  926.    
  927.         return $OptionKeys[$ChoiceCounter]
  928.    
  929.     }
  930. }
Advertisement
Add Comment
Please, Sign In to add comment