Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Param (
- [Parameter(Mandatory=$false,
- Position=0)]
- [switch] $DisableUpdateCheck,
- [Parameter(Mandatory=$false,
- Position=1)]
- [switch] $DontForce
- )
- <# https://pastebin.com/9RSusrvC
- NOTE: Save this with UTF-8-BOM encoding!
- The SimpleUI module improves user friendliness and readability of the PowerShell console and comes with a few gimmicks which contribute to an overall better console experience.
- Amongst other things it features a stable buffer width, correct word splitting, leading spaces, a simple three dot animation and a neat looking user prompt with some additional features.
- For a preview paste the following lines into...
- ... PowerShell:
- # This will download the SimpleUI module as well as a preview script and then start the latter (make sure script execution is allowed)
- Set-Location $HOME\Downloads
- (Invoke-WebRequest -UseBasicParsing -Uri 'https://pastebin.com/raw/9RSusrvC').Content | Add-Content -Path .\SimpleUI.psm1 -Encoding UTF8
- (Invoke-WebRequest -UseBasicParsing -Uri 'https://pastebin.com/raw/e0n3mWy0').Content | Add-Content -Path .\SimpleUIPreview.ps1 -Encoding UTF8
- & .\SimpleUIPreview.ps1
- ... cmd:
- :: This will download the SimpleUI module as well as a preview script and then start the latter
- PowerShell -ExecutionPolicy Unrestricted -NoProfile -Command "& { Set-Location $HOME\Downloads; (Invoke-WebRequest -UseBasicParsing -Uri 'https://pastebin.com/raw/9RSusrvC').Content | Add-Content -Path .\SimpleUI.psm1 -Encoding UTF8; (Invoke-WebRequest -UseBasicParsing -Uri 'https://pastebin.com/raw/e0n3mWy0').Content | Add-Content -Path .\SimpleUIPreview.ps1 -Encoding UTF8; & .\SimpleUIPreview.ps1 }"
- Feel free to use and modify this module as you desire.
- You can report issues, give feedback, suggest features or ask for help by sending an email to: FatalBulletHit@gmail.com
- #>
- # Specifies the location of the log file. If set to an empty string, no log file will be created. The default value is: '.\logs\'
- # Affected cmdlets: Animate-Console, Read-Console, Write-Console
- [string] $LogFilePath = ''
- # Specifies the default width. The default value is: 120
- # Affected cmdlets: Set-Console, Animate-Console, Monitor-Console, Read-Console, Write-Console
- [int] $DefaultWidth = 120
- # Specifies the default amount of leading spaces. The default value is: 1
- # Affected cmdlets: Animate-Console, Read-Console, Write-Console
- [int] $DefaultLeadingSpaces = 1
- # Specifies that all text will be written with typewriter effect. The default value is: $false
- # Affected cmdlets: Animate-Console, Read-Console, Write-Console
- [bool] $DefaultTypewriter = $false
- # Specifies debug behaviour. If set to $true, the StandardError of sub processes will be kept and moved into a folder at the modules location. If set to $false, the StandardError of sub processes will be deleted. The default value is: $true
- # Affected cmdlets: Read-Console
- [bool] $Env:Debug = $true
- # Specifies the default color for a selected option. The default value is: 'Yellow'
- # Affected cmdlets: Read-Console
- [System.ConsoleColor] $DefaultSelectColor = 'Yellow'
- # Specifies the default prompt style. Accepted values are 'Inline' or 'Vertical'. The default value is: 'Inline'
- # Affected cmdlets: Read-Console
- [string] $DefaultStyle = 'Inline'
- # Specifies the scope in which variables will be set. Default value is: 2
- # Affected cmdlets: Read-Console
- [int] $Scope = 2
- # Searching for updates
- if (!$DisableUpdateCheck -and (Test-Connection -Computer 'google.com' -Count 1 -Quiet) -and ($Version = ((Invoke-WebRequest -UseBasicParsing -Uri 'https://pastebin.com/raw/UjBSWHbN').Content)) -ne '1.0.3') {
- if (!$DontForce) {
- Add-Content -Value ((Invoke-WebRequest -UseBasicParsing -Uri 'https://pastebin.com/raw/9RSusrvC').Content) -LiteralPath ('{0}\SimpleUI.psm1' -f $Env:TEMP) -NoNewline -Encoding UTF8
- Move-Item -LiteralPath ('{0}\SimpleUI.psm1' -f $Env:TEMP) -Destination $PSScriptRoot -Force
- } else {
- Write-Warning -Message ("SimpleUI`nThere is a new version available! You can download it from here: 'https://pastebin.com/9RSusrvC'`nCurrent version : 1.0.3`nAvailable version: {0}" -f $Version)
- }
- }
- # Monitoring error output
- Add-Content -Value ('{0},' -f $PID) -LiteralPath ('{0}\SimpleUI_pids' -f $Env:TEMP) -NoNewline
- $DebugRunning = $false
- if (Test-Path ('{0}\SimpleUI_error0' -f $Env:TEMP)) {
- try {
- Remove-Item -LiteralPath ('{0}\SimpleUI_error0' -f $Env:TEMP) -ErrorAction 'Stop'
- } catch {
- $DebugRunning = $true
- }
- }
- if (!$DebugRunning) {
- Start-Process PowerShell {
- $PID
- Wait-Process -Id (Get-Content -LiteralPath ('{0}\SimpleUI_pids' -f $Env:TEMP) -Delimiter ',')
- $Time = (Get-Date).ToString('yyyy\-MM\-dd\_HH\-mm\-ss')
- Get-ChildItem -Filter 'SimpleUI*' -LiteralPath $Env:TEMP -File | Where-Object {$_.Name -notmatch 'error0'} | ForEach-Object {
- if ([bool]$Env:Debug -and $_.Length -gt 0 -and $_.BaseName -match 'SimpleUI[0-9]*_error[0-9]*') {
- if (!(Test-Path ('.\SimpleUI_error\{0}' -f $Time))) {
- New-Item -Path ('.\SimpleUI_error\{0}' -f $Time) -ItemType 'Directory'
- }
- Move-Item -LiteralPath $_.FullName -Destination ('.\SimpleUI_error\{0}\{1}' -f $Time, $_.Name)
- } else {
- Remove-Item -LiteralPath $_.FullName -Force
- }
- }
- } -RedirectStandardOutput ('{0}\SimpleUI_error0' -f $Env:TEMP) -WindowStyle 'Hidden'
- }
- <#
- .SYNOPSIS
- Simple loading animation in form of 3 dots.
- .Description
- The Animate-Console cmdlet offers a simple loading animation in form of 3 dots. It can be set to animate as long as a process is running or for a certain amount of iterations. It can also animate the console window title.
- .PARAMETER AndLogic
- Specifies that all given conditions need to be true for the animation to continue.
- .PARAMETER ProcessId
- Specifies the process id of the process to wait for. The default value is [int]::MaxValue.
- .PARAMETER Iteration
- Specifies the amount of iterations. 1 iteration equals approximately 1 second. The default value is 1.
- .PARAMETER Message
- Specifies the message to display in front of the dot animation.
- .PARAMETER WindowTitle
- Specifies the window title. Can be an array of 2 strings where the 2nd object will be displayed behind the dot animation.
- .PARAMETER BackgroundColor
- Specifies the background color. The acceptable values for this parameter are:
- - Black
- - DarkBlue
- - DarkGreen
- - DarkCyan
- - DarkRed
- - DarkMagenta
- - DarkYellow
- - Gray
- - DarkGray
- - Blue
- - Green
- - Cyan
- - Red
- - Magenta
- - Yellow
- - White
- The default value is $Host.UI.RawUI.BackgroundColor.
- .PARAMETER ForegroundColor
- Specifies the foreground color. The acceptable values for this parameter are:
- - Black
- - DarkBlue
- - DarkGreen
- - DarkCyan
- - DarkRed
- - DarkMagenta
- - DarkYellow
- - Gray
- - DarkGray
- - Blue
- - Green
- - Cyan
- - Red
- - Magenta
- - Yellow
- - White
- The default value is $Host.UI.RawUI.ForegroundColor.
- .PARAMETER NoNewline
- Specifies that the content displayed in the console does not end with a newline character.
- .PARAMETER Width
- Specifies the maximum width before a newline character will be inserted. The default value is set by the variable $DefaultWidth.
- .PARAMETER LeadingSpaces
- Specifies the amount of leading spaces. The default value is set by the variable $DefaultLeadingSpaces.
- .PARAMETER NoFirstLineLeadingSpaces
- Specifies that no leading spaces will be inserted at the beginning of the first line.
- .PARAMETER Typewriter
- Specifies that the text will be displayed with a typewriter animation.
- Delays are generated randomly. Each alphanumeric character and dot has a delay of 25ms - 75ms, all other characters have a delay of 50ms - 100ms. Use '`p(x)' or "``p(x)" to insert an additional delay of x ms.
- The default value is set by the variable $DefaultTypewriter.
- .PARAMETER Log
- Specifies log behaviour. The default value is ''.
- .INPUTS
- None
- .OUTPUTS
- The Animate-Console cmdlet uses the Write-Console cmdlet which uses the Write-Host cmdlet to send the message to the host. It does not return any message. However, the host might display the message that Write-Host sends to it.
- .EXAMPLE
- PS> Animate-Console -Iteration 5 -Message 'Some string' -BackgroundColor DarkYellow -ForegroundColor Red -LeadingSpaces 4
- Some string...
- Inserts 4 spaces (equals 1 tab) in front of the animated string and repeats the dot animation 5 times (approximately 5 seconds).
- The background color is white and the foreground color red.
- .EXAMPLE
- PS> Animate-Console -AndLogic -ProcessId 123 -Iteration 60 -Message 'A string which is quite long and therefore has new line characters inserted so the specified width is not exceeded' -Width 40 -NoFirstLineLeadingSpaces -Typewriter
- A string which is quite long and
- therefore has new line characters
- inserted so the specified width is not
- exceeded...
- Displays the string with a typewriter effect and with no leading spaces and a maximum width of 40. The 3 dot animation runs for as long as the process with the PID '123' is running but for no longer than 60 iterations (approximately 60 seconds).
- .EXAMPLE
- PS> Animate-Console -ProcessId 123 -Iteration 5 -Message 'Doing something' -WindowTitle ('Doing something', '(Step 1/3)') -NoNewline -Log 'Nothing'
- Some string...
- Animates a string and the window title as long as the process with the PID '123' is running but for at least 5 iterations (approximately 5 seconds).
- Nothing is logged and no new line character is inserted at the end of the line.
- .LINK
- https://pastebin.com/9RSusrvC
- Write-Console
- #>
- function Animate-Console {
- Param (
- [Parameter(Mandatory=$false)]
- [Alias('And')]
- [switch] $AndLogic,
- [Parameter(Mandatory=$false)]
- [Alias('PID', 'Id')]
- [ValidateScript({[int]$_ -ge 0})]
- [int] $ProcessId = [int]::MaxValue,
- [Parameter(Mandatory=$false)]
- [Alias('It')]
- [ValidateScript({[int]$_ -gt 0})]
- [int] $Iteration = 1,
- [Parameter(Mandatory=$false,
- Position=0)]
- [Alias('Msg','Object')]
- [string[]] $Message,
- [Parameter(Mandatory=$false)]
- [Alias('Title')]
- [ValidateCount(1,2)]
- [string[]] $WindowTitle,
- [Parameter(Mandatory=$false)]
- [Alias('BgC','Background')]
- [System.ConsoleColor] $BackgroundColor = $Host.UI.RawUI.BackgroundColor,
- [Parameter(Mandatory=$false)]
- [Alias('FgC','Foreground')]
- [System.ConsoleColor] $ForegroundColor = $Host.UI.RawUI.ForegroundColor,
- [Parameter(Mandatory=$false)]
- [switch] $NoNewline,
- [Parameter(Mandatory=$false)]
- [ValidateScript({[int]$_ -gt ($Message -split " |`n" | Sort-Object -Property Length -Descending | Select-Object -First 1 -ExpandProperty Length) + 7})]
- [int] $Width = $DefaultWidth,
- [Parameter(Mandatory=$false)]
- [Alias('LS')]
- [ValidateScript({if ([int]$Width -eq 0) {[int]$_ -ge 0 -and [int]$_ -lt $DefaultWidth} else {[int]$_ -ge 0 -and [int]$_ -lt $Width}})]
- [int] $LeadingSpaces = $DefaultLeadingSpaces,
- [Parameter(Mandatory=$false)]
- [Alias('NoLS')]
- [switch] $NoFirstLineLeadingSpaces,
- [Parameter(Mandatory=$false)]
- [switch] $Typewriter = $DefaultTypewriter,
- [Parameter(Mandatory=$false)]
- [ValidateSet('Only', 'o', 'Nothing', 'n', '')]
- [string] $Log = ''
- )
- ($MainStopWatch = New-Object System.Diagnostics.Stopwatch).Start()
- $Condition = '$IterationCounter++ -lt $Iteration'
- if ($ProcessId -ne [int]::MaxValue) {
- if ($AndLogic) {
- $Condition += ' -and ((Get-Process -Id $ProcessId 2>$null) -ne $null)'
- } else {
- $Condition += ' -or ((Get-Process -Id $ProcessId 2>$null) -ne $null)'
- }
- }
- while (Invoke-Expression $Condition) {
- if ($null -ne $Message -and $IterationCounter -eq 1) {
- Write-Console -Message $Message -BackgroundColor $BackgroundColor -ForegroundColor $ForegroundColor -NoNewline -Width $Width -LeadingSpaces $LeadingSpaces -Typewriter:$Typewriter -Log $Log
- Write-Console -Message '... ' -BackgroundColor $BackgroundColor -ForegroundColor $ForegroundColor -NoNewline -Width $Width -LeadingSpaces 0 -Log $Log
- }
- for ($AnimateCounter = 0; $AnimateCounter -lt 4; $AnimateCounter++) {
- if ($null -ne $Message -or $WindowTitle.Count -eq 0) {
- Write-Host -Object ("`b" * 4 + "{0,-4}" -f ('.' * $AnimateCounter)) -BackgroundColor $BackgroundColor -ForegroundColor $ForegroundColor -NoNewline
- }
- if ($WindowTitle.Count -ne 0) {
- $Host.UI.RawUI.WindowTitle = ($WindowTitle[0] + "{0,-4}" -f ('.' * $AnimateCounter) + $WindowTitle[1])
- }
- Start-Sleep -Milliseconds 250
- }
- }
- $MainStopWatch.Stop()
- if ($null -ne $Message -and !$NoNewline) {
- Write-Host -Object ''
- if ($Log[0] -ne 'n' -and [string]$LogFilePath -ne '') {
- Add-Content -Value '' -LiteralPath $LogFilePath -Encoding UTF8
- }
- }
- }
- New-Alias -Name animate -Value Animate-Console
- <#
- .SYNOPSIS
- Monitors the buffer width of the console window.
- .Description
- The Monitor-Console cmdlet monitors the buffer width of the console window and prevents it from falling below the specified width.
- .PARAMETER Stop
- Stops the MonitorProcess.
- .PARAMETER Width
- Specifies the window buffer width. The default value is set by the variable $DefaultWidth.
- If a monitoring process is already running, it will be stopped and a new one will be started with the specified width.
- .INPUTS
- None
- .OUTPUTS
- None
- .EXAMPLE
- PS> Monitor-Console
- Starts monitoring the console window with the $DefaultWidth.
- .EXAMPLE
- PS> Monitor-Console -Width 50
- Starts monitoring the console window with a width of 50.
- .EXAMPLE
- PS> Monitor-Console -Stop
- Stops monitoring of the console window.
- .LINK
- https://pastebin.com/9RSusrvC
- Get-Host
- #>
- function Monitor-Console {
- Param (
- [Parameter(Mandatory=$false)]
- [switch] $Stop,
- [Parameter(Mandatory=$false,
- Position=0)]
- [ValidateScript({[int]$_ -gt 0})]
- [int] $Width = $DefaultWidth
- )
- if ($null -ne ($MonitorProcessId = (Get-Variable -Name 'MonitorProcessId' -Scope $Scope -ValueOnly 2>$null))) {
- Stop-Process -Id $MonitorProcessId 2>$null
- }
- if (!$Stop) {
- $Env:Width = $Width
- $MonitorProcess = Start-Process PowerShell {
- while ($true) {
- if ($Host.UI.RawUI.BufferSize.Width -lt $Env:Width) {
- $Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size($Env:Width, $Host.UI.RawUI.BufferSize.Height)
- } elseif ($Host.UI.RawUI.WindowSize.Width -ge $Env:Width -and $Host.UI.RawUI.BufferSize.Width -gt $Host.UI.RawUI.WindowSize.Width) {
- $Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size($Host.UI.RawUI.WindowSize.Width, $Host.UI.RawUI.BufferSize.Height)
- }
- Start-Sleep -Milliseconds 1
- }
- } -NoNewWindow -PassThru
- Set-Variable -Name 'MonitorProcessId' -Value $MonitorProcess.Id -Scope $Scope
- }
- }
- New-Alias -Name monitor -Value Monitor-Console
- <#
- .SYNOPSIS
- Moves the cursor to a specific position.
- .Description
- Moves the cursor to a specific position. You can retrieve the cursor position from $Host.UI.RawUI.CursorPosition.
- .PARAMETER X
- Specifies the X value. The default value is 0.
- .PARAMETER Y
- Specifies the Y value.
- .INPUTS
- None
- .OUTPUTS
- None
- .EXAMPLE
- PS> Move-CursorPosition 10 25
- Moves the cursor to the coordinates X10,Y25
- .EXAMPLE
- PS> Move-CursorPosition -Y 25
- Moves the cursor to the coordinates X0,Y25
- .LINK
- https://pastebin.com/9RSusrvC
- https://docs.microsoft.com/en-us/dotnet/api/system.console.setcursorposition?view=netframework-4.8
- Get-Host
- #>
- function Move-CursorPosition {
- Param(
- [Parameter(Mandatory=$false,
- Position=0)]
- [int]$X = 0,
- [Parameter(Mandatory=$true,
- Position=1)]
- [int]$Y
- )
- $Host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates ($X,$Y)
- }
- New-Alias -Name movecp -Value Move-CursorPosition
- <#
- .SYNOPSIS
- Waits for user input and returns it.
- .Description
- The Read-Console cmdlet waits for the user to hit one of the keys defined in the $Options array. It offers a choice menu in two different styles (either a list or a single line) and gives visual feedback based on the option chosen. The Read-Console cmdlet can return the pressed key, a specified default key if the condition became false without an option being chosen or [int]::MaxValue if the latter happens without a default key being specified. You can also set the pressed key (or [int]::MaxValue) directly as value for one or multiple variables. Additionally you can specify a value by adding an array of key and value to the $Options array.
- .PARAMETER AndLogic
- Specifies that only one given condition needs to be false for the cmdlet to continue without user input.
- .PARAMETER ProcessId
- Specifies the process id. The default value is [int]::MaxValue.
- .PARAMETER Duration
- Specifies the duration in milliseconds. The default value is 0.
- .PARAMETER Attempts
- Specifies the maximum amount of attempts. The default value is [int]::MaxValue.
- .PARAMETER Options
- Specifies the keys that can be pressed to proceed and optionally also holds the value for the variables to be set.
- .PARAMETER DefaultOptionsIndex
- Specifies which option should be selected if the condition becomes false by the index of the $Options array. The default value is [int]::MaxValue.
- .PARAMETER Type
- Specifies if the Key or the KeyChar is returned.
- .PARAMETER Message
- Specifies the message to display for each option in the $Options array.
- .PARAMETER BackgroundColor
- Specifies the background color. The acceptable values for this parameter are:
- - Black
- - DarkBlue
- - DarkGreen
- - DarkCyan
- - DarkRed
- - DarkMagenta
- - DarkYellow
- - Gray
- - DarkGray
- - Blue
- - Green
- - Cyan
- - Red
- - Magenta
- - Yellow
- - White
- The default value is $Host.UI.RawUI.BackgroundColor.
- .PARAMETER ForegroundColor
- Specifies the foreground color. The acceptable values for this parameter are:
- - Black
- - DarkBlue
- - DarkGreen
- - DarkCyan
- - DarkRed
- - DarkMagenta
- - DarkYellow
- - Gray
- - DarkGray
- - Blue
- - Green
- - Cyan
- - Red
- - Magenta
- - Yellow
- - White
- The default value is $Host.UI.RawUI.ForegroundColor.
- .PARAMETER SelectColor
- Specifies the foreground color of the selected option. The acceptable values for this parameter are:
- - Black
- - DarkBlue
- - DarkGreen
- - DarkCyan
- - DarkRed
- - DarkMagenta
- - DarkYellow
- - Gray
- - DarkGray
- - Blue
- - Green
- - Cyan
- - Red
- - Magenta
- - Yellow
- - White
- The default value is set by the variable $DefaultSelectColor.
- .PARAMETER Style
- Specifies the way the text for the options is displayed. The default value is set by the variable $DefaultStyle.
- .PARAMETER NoNewline
- Specifies that the content displayed in the console does not end with a newline character.
- .PARAMETER Width
- Specifies the maximum width before a newline character will be inserted. The default value is set by the variable $DefaultWidth.
- .PARAMETER LeadingSpaces
- Specifies the amount of leading spaces. The default value is set by the variable $DefaultLeadingSpaces.
- .PARAMETER NoFirstLineLeadingSpaces
- Specifies that no leading spaces will be inserted at the beginning of the first line.
- .PARAMETER Typewriter
- Specifies that the text will be displayed with a typewriter animation.
- Delays are generated randomly. Each alphanumeric character and dot has a delay of 25ms - 75ms, all other characters have a delay of 50ms - 100ms. Use '`p(x)' or "``p(x)" to insert an additional delay of x ms.
- The default value is set by by the variable $DefaultTypewriter
- .PARAMETER VariableName
- Specifies the name(s) of the variable(s) you want to assign values to based on the option chosen.
- .PARAMETER MessageAsValue
- Specifies that the value of the variable(s) is the corrosponding message rather than the $Options index. Value will be [int]::MaxValue if the the condition became false.
- .PARAMETER DefinedOnly
- Specifies to not set the pressed key as value of the variable(s) if the $Options array does not have a value set for the pressed key.
- .PARAMETER ClearUndefined
- Specifies wether to use the Clear-Variable cmdlet on the variable(s) if no value is specified.
- .PARAMETER Return
- Specifies wether to return the pressed key.
- .PARAMETER Log
- Specifies log behaviour. Default value is ''.
- .INPUTS
- None
- .OUTPUTS
- The Read-Console cmdlet uses the Write-Console cmdlet which uses the Write-Host cmdlet to send the message to the host. It does not return any message. However, the host might display the message that Write-Host sends to it and it may return the pressed key or a default key or the [int]::MaxValue if the condition became false and $DefaultOptionsIndex was not specified.
- .EXAMPLE
- PS> Read-Console -Options ('y', 'n') -Message ('Yes', 'No') -Style 'Inline' -LeadingSpaces 5 -Return
- [Y]es / [N]o
- Waits for the user to press one of the keys and returns it.
- .EXAMPLE
- PS> Read-Console -Duration 15000 -Options (('D1', $true), ('D0', $false)) -DefaultOptionsIndex 0 -Type Key -Message ('Yes', 'No') -VariableName 'SomeVariable' -MessageAsValue -Return -Log Nothing
- [Y]es / [N]o
- D1
- Returns 'D1' after 15 seconds when neither 'D1' nor 'D0' were pressed. $SomeVariable has the value [int]::MaxValue. Nothing is logged.
- If one of the options were chosen, the value of $SomeVariable would've been the corrosponding message.
- Note: When using -Type Key it is important to use the Key and not the KeyChar: if the options were 1 and 0, the user input would be ignored!
- .EXAMPLE
- PS> Read-Console -AndLogic -Duration 10000 -Attempts 1 -Options ((0, 'null'), (1, 'one'), (2, 'two'), 'c') -Message (0, 1, 2, 'Cancle') -Style 'Vertical' -VariableName 'SomeVariable' -DefinedOnly -ClearUndefined -Return
- [0]
- [1]
- [2]
- [C]ancle
- [C]ancle
- c
- The text is alligned vertically and the prompt will allow 1 attempt. After 10 seconds without user input or if none of the keys in the $Options array were pressed, [int]::MaxValue is returend.
- In this case, the user pressed 'c', so 'c' is returned and $SomeVariable will be cleared as the option has no variable defined.
- .LINK
- https://pastebin.com/9RSusrvC
- https://docs.microsoft.com/en-us/dotnet/api/system.console.readkey?view=netframework-4.8
- https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.keys?view=netframework-4.8
- Write-Console
- about_Scopes
- #>
- function Read-Console {
- Param (
- [Parameter(Mandatory=$false,
- ParameterSetName='Condition')]
- [Alias('And')]
- [switch] $AndLogic,
- [Parameter(Mandatory=$false,
- ParameterSetName='Condition')]
- [Alias('PID', 'Id')]
- [ValidateScript({[int]$_ -ge 0})]
- [int] $ProcessId = [int]::MaxValue,
- [Parameter(Mandatory=$false,
- ParameterSetName='Condition')]
- [Alias('Dur','T')]
- [ValidateScript({[int]$_ -ge 0})]
- [int] $Duration = 0,
- [Parameter(Mandatory=$false,
- ParameterSetName='Condition')]
- [Alias('Att','It')]
- [ValidateScript({[int]$_ -ge 0})]
- [int] $Attempts = [int]::MaxValue,
- [Parameter(Mandatory=$true,
- Position=0)]
- [Alias('Key','Keys','Option')]
- [array] $Options,
- [Parameter(Mandatory=$false)]
- [Alias('StdIndex')]
- [ValidateScript({[int]$_ -lt $Options.Count})]
- [int] $DefaultOptionsIndex = [int]::MaxValue,
- [Parameter(Mandatory=$false)]
- [ValidateSet('Key','KeyChar')]
- [string] $Type = 'KeyChar',
- [Parameter(Mandatory=$false,
- Position=1)]
- [Alias('Msg','Object')]
- [string[]] $Message,
- [Parameter(Mandatory=$false)]
- [Alias('BgC','Background')]
- [System.ConsoleColor] $BackgroundColor = $Host.UI.RawUI.BackgroundColor,
- [Parameter(Mandatory=$false)]
- [Alias('FgC','Foreground')]
- [System.ConsoleColor] $ForegroundColor = $Host.UI.RawUI.ForegroundColor,
- [Parameter(Mandatory=$false)]
- [Alias('SC')]
- [System.ConsoleColor] $SelectColor = $DefaultSelectColor,
- [Parameter(Mandatory=$false)]
- [ValidateSet('Inline', 'i', 'Vertical', 'v')]
- [string] $Style = $DefaultStyle,
- [Parameter(Mandatory=$false)]
- [switch] $NoNewline,
- [Parameter(Mandatory=$false)]
- [ValidateScript({[int]$_ -gt 0})]
- [int] $Width = $DefaultWidth,
- [Parameter(Mandatory=$false)]
- [Alias('LS')]
- [ValidateScript({if ([int]$Width -eq 0) {[int]$_ -ge 0 -and [int]$_ -lt $DefaultWidth} else {[int]$_ -ge 0 -and [int]$_ -lt $Width}})]
- [int] $LeadingSpaces = $DefaultLeadingSpaces,
- [Parameter(Mandatory=$false)]
- [Alias('NoLS')]
- [switch] $NoFirstLineLeadingSpaces,
- [Parameter(Mandatory=$false)]
- [switch] $Typewriter = $DefaultTypewriter,
- [Parameter(Mandatory=$false)]
- [Alias('Var')]
- [string[]] $VariableName,
- [Parameter(Mandatory=$false)]
- [ValidateScript({[string]$VariableName -ne ''})]
- [switch] $MessageAsValue,
- [Parameter(Mandatory=$false)]
- [ValidateScript({[string]$VariableName -ne ''})]
- [switch] $DefinedOnly,
- [Parameter(Mandatory=$false)]
- [ValidateScript({[string]$VariableName -ne ''})]
- [switch] $ClearUndefined,
- [Parameter(Mandatory=$false)]
- [switch] $Return,
- [Parameter(Mandatory=$false)]
- [ValidateSet('Only', 'o', 'Nothing', 'n', '')]
- [string] $Log = ''
- )
- if ($Message.Count -gt 0) {
- $Message | ForEach-Object {
- $OptionsText += (' / [' + $_.Substring(0, 1) + ']' + $_.Substring(1))
- }
- if ($Style[0] -eq 'v') {
- $OptionsText = ('..' + $OptionsText.Replace(' / ', "`n") + "`n")
- }
- $OptionsText = $OptionsText.Substring(3)
- Write-Console -Message $OptionsText -BackgroundColor $BackgroundColor -ForegroundColor $ForegroundColor -NoNewline -Width $Width -LeadingSpaces $LeadingSpaces -Typewriter:$Typewriter -Log 'Nothing'
- }
- $Options | ForEach-Object {
- if ($_ -is [array]) {
- $OptionKeys += @($_[0])
- } else {
- $OptionKeys += @($_)
- }
- }
- $Env:OptionKeys = $OptionKeys
- $Env:Attempts = $Attempts
- $Env:Type = $Type
- $ReadProcess = Start-Process PowerShell {
- $Env:OptionKeys.Split(' ') | ForEach-Object {
- $OptionKeys += @($_)
- }
- $AttemptsCounter = 0
- while (!$OptionKeys.Contains($PressedKey) -and $AttemptsCounter++ -lt $Env:Attempts) {
- $PressedKey = [string]([System.Console]::ReadKey('NoEcho')).$Env:Type
- }
- $OptionCounter = 0
- if ($AttemptsCounter -gt $Env:Attempts) {
- $OptionCounter = [int]::MaxValue
- } else {
- while ($OptionKeys[$OptionCounter] -ne $PressedKey) {
- $OptionCounter++
- }
- }
- return $OptionCounter
- } -PassThru -NoNewWindow -RedirectStandardError ('{0}\SimpleUI{1}_error1' -f $Env:TEMP, $PID) -RedirectStandardOutput ('{0}\SimpleUI{1}_read' -f $Env:TEMP, $PID)
- if (($Attempts -eq [int]::MaxValue -or ($Attempts -ne [int]::MaxValue -and $AndLogic)) -and ([int]($Env:ProcessId = $ProcessId) -ne [int]::MaxValue -or [int]($Env:Duration = $Duration) -ne 0)) {
- $Env:AndLogic = $AndLogic
- $Env:ReadProcessId = $ReadProcess.Id
- $MonitorReadProcess = Start-Process PowerShell {
- ($StopWatch = New-Object System.Diagnostics.Stopwatch).Start()
- $Condition = '$StopWatch.Elapsed.TotalMilliseconds -lt $Env:Duration'
- if ($Env:ProcessId -ne [int]::MaxValue) {
- if ($Env:AndLogic) {
- if ($Env:Duration -eq 0) {
- $Env:Duration = [int]::MaxValue
- }
- $Condition += ' -and ((Get-Process -Id $Env:ProcessId 2>$null) -ne $null)'
- } else {
- $Logic += ' -or ((Get-Process -Id $Env:ProcessId 2>$null) -ne $null)'
- }
- }
- while (Invoke-Expression $Condition) {
- if ((Get-Process -Id $Env:ReadProcessId 2>$null) -eq $null) {
- Start-Process -Id $PID
- }
- Start-Sleep -Milliseconds 1
- }
- $StopWatch.Stop()
- Stop-Process -Id $Env:ReadProcessId
- } -PassThru -NoNewWindow -RedirectStandardError ('{0}\SimpleUI{1}_error2' -f $Env:TEMP, $PID)
- Wait-Process -Id $ReadProcess.Id
- Stop-Process -Id $MonitorReadProcess.Id
- } else {
- Wait-Process -Id $ReadProcess.Id
- }
- if ((Get-Item -LiteralPath ('{0}\SimpleUI{1}_read' -f $Env:TEMP, $PID)).Length -eq 0) {
- [int]$OptionCounter = $DefaultOptionsIndex
- $Default = $true
- } else {
- [int]$OptionCounter = (Get-Content ('{0}\SimpleUI{1}_read' -f $Env:TEMP, $PID))
- $Default = $false
- }
- if ($Message.Count -gt 0 -and $OptionCounter -ne [int]::MaxValue) {
- $SelectedMessage = ('[' + $Message[$OptionCounter].Substring(0, 1) + ']' + $Message[$OptionCounter].Substring(1))
- Write-Host -Object "`r" -NoNewline
- if ($Style[0] -eq 'v') {
- if ($Log[0] -ne 'n') {
- Write-Console -Message $OptionsText.Replace('[', '').Replace(']', '') -NoNewline:$NoNewline -Width $Width -LeadingSpaces $LeadingSpaces -Log 'Only'
- }
- Write-Console -Message ("`n" + $SelectedMessage) -BackgroundColor $BackgroundColor -ForegroundColor $SelectColor -NoNewline:$NoNewline -Width $Width -LeadingSpaces $LeadingSpaces -Log $Log
- } elseif ($Message.Count -eq 1) {
- Write-Console -Message $OptionsText -BackgroundColor $BackgroundColor -ForegroundColor $SelectColor -NoNewline:$NoNewline -Width $Width -LeadingSpaces $LeadingSpaces -Log $Log
- } elseif ($Message.Count -gt 1) {
- if ($OptionCounter -gt 0) {
- Write-Console -Message ($OptionsText -split ($SelectedMessage).Replace('[', '\['))[0] -BackgroundColor $BackgroundColor -ForegroundColor $ForegroundColor -NoNewline -Width $Width -Log 'Nothing'
- if ($Log[0] -ne 'n') {
- Write-Console -Message (($OptionsText -split ($SelectedMessage).Replace('[', '\['))[0]).Replace('[', '').Replace(']', '') -NoNewline -Width $Width -Log 'Only'
- }
- if ($OptionCounter -lt $Message.Count - 1) {
- Write-Console -Message $SelectedMessage -BackgroundColor $BackgroundColor -ForegroundColor $SelectColor -NoNewline -Width $Width -NoFirstLineLeadingSpaces -Log $Log
- Write-Console -Message (($OptionsText -split ($SelectedMessage).Replace('[', '\['))[1]) -BackgroundColor $BackgroundColor -ForegroundColor $ForegroundColor -NoNewline:$NoNewline -Width $Width -NoFirstLineLeadingSpaces -Log 'Nothing'
- if ($Log[0] -ne 'n') {
- Write-Console -Message (($OptionsText -split ($SelectedMessage).Replace('[', '\['))[1]).Replace('[', '').Replace(']', '') -NoNewline:$NoNewline -Width $Width -NoFirstLineLeadingSpaces -Log 'Only'
- }
- } else {
- Write-Console -Message $SelectedMessage -BackgroundColor $BackgroundColor -ForegroundColor $SelectColor -NoNewline:$NoNewline -Width $Width -NoFirstLineLeadingSpaces -Log $Log
- }
- } else {
- Write-Console -Message $SelectedMessage -BackgroundColor $BackgroundColor -ForegroundColor $SelectColor -NoNewline -Width $Width -Log $Log
- Write-Console -Message (($OptionsText -split ($SelectedMessage).Replace('[', '\['))[1]) -BackgroundColor $BackgroundColor -ForegroundColor $ForegroundColor -NoNewline:$NoNewline -Width $Width -NoFirstLineLeadingSpaces -Log 'Nothing'
- if ($Log[0] -ne 'n') {
- Write-Console -Message (($OptionsText -split ($SelectedMessage).Replace('[', '\['))[1]).Replace('[', '').Replace(']', '') -NoNewline:$NoNewline -Width $Width -NoFirstLineLeadingSpaces -Log 'Only'
- }
- }
- }
- }
- if ([string]$VariableName -ne '') {
- if ($DefinedOnly -and $OptionCounter -ne [int]::MaxValue -and $Options[$OptionCounter] -isnot [array]) {
- if ($ClearUndefined) {
- Clear-Variable -Name $VariableName -Scope $Scope 2>$null
- }
- } else {
- if ($Default) {
- $Value = [int]::MaxValue
- } elseif ($MessageAsValue) {
- $Value = $Message[$OptionCounter]
- } elseif ($Options[$OptionCounter] -is [array]) {
- $Value = ($Options[$OptionCounter])[1]
- } else {
- $Value = $Options[$OptionCounter]
- }
- if ([string]$Value -ne '') {
- if ($VariableName -is [array]) {
- $VariableName | ForEach-Object {
- Set-Variable -Name $_ -Value $Value -Scope $Scope
- }
- } else {
- Set-Variable -Name $VariableName -Value $Value -Scope $Scope
- }
- }
- }
- }
- if ($Return) {
- if ($OptionCounter -eq [int]::MaxValue) {
- return [int]::MaxValue
- } else {
- return $OptionKeys[$OptionCounter]
- }
- }
- }
- New-Alias -Name read -Value Read-Console
- <#
- .SYNOPSIS
- Adjusts the console and the console window.
- .Description
- The Set-Console cmdlet adjusts the console and the console window. You can specifie width, buffer width, title, foreground color and background color.
- .PARAMETER WindowSize
- Specifies the console window size. Accepts either an array of width and height or an integer with the width only. Each need to be greater than 0.
- .PARAMETER BufferSize
- Specifies the console window buffer size. Accepts either an array of width and height or an integer with the width only. Each need to be greater than the console window size.
- .PARAMETER WindowTitle
- Specifies the console window title.
- .PARAMETER BackgroundColor
- Specifies the console background color. The acceptable values for this parameter are:
- - Black
- - DarkBlue
- - DarkGreen
- - DarkCyan
- - DarkRed
- - DarkMagenta
- - DarkYellow
- - Gray
- - DarkGray
- - Blue
- - Green
- - Cyan
- - Red
- - Magenta
- - Yellow
- - White
- Only works properly during script execution.
- .PARAMETER ForegroundColor
- Specifies the console foreground color. The acceptable values for this parameter are:
- - Black
- - DarkBlue
- - DarkGreen
- - DarkCyan
- - DarkRed
- - DarkMagenta
- - DarkYellow
- - Gray
- - DarkGray
- - Blue
- - Green
- - Cyan
- - Red
- - Magenta
- - Yellow
- - White
- Only works properly during script execution.
- .PARAMETER ClearHost
- Executes the Clear-Host cmdlet. Needed to effectively change background and foreground color of the console.
- .INPUTS
- None
- .OUTPUTS
- None
- .EXAMPLE
- PS> Set-Console -WindowSize 50
- Sets the console window width.
- .EXAMPLE
- PS> Set-Console -BufferSize (120, 9000)
- Sets the console window buffer width and height.
- .EXAMPLE
- PS> Set-Console -WindowTitle 'Some title'
- Sets the console window title.
- .EXAMPLE
- PS> Set-Console -BackgroundColor 'DarkMagenta' -ForegroundColor 'DarkYellow' -ClearHost
- Sets the background and foreground color of the console and calls the Clear-Host cmdlet afterwards.
- .LINK
- https://pastebin.com/9RSusrvC
- Clear-Host
- Get-Host
- #>
- function Set-Console {
- Param (
- [Parameter(Mandatory=$false)]
- [ValidateScript({($_.Count -in 1..2) -and ($_[0] -gt 0 -and ($null -eq $_[1] -or $_[1] -gt 0))})]
- [int[]] $WindowSize,
- [Parameter(Mandatory=$false)]
- [ValidateScript({($_.Count -in 1..2) -and ($_[0] -ge $Host.UI.RawUI.WindowSize.Width -and ($null -eq $_[1] -or $_[1] -ge $Host.UI.RawUI.WindowSize.Height))})]
- [int[]] $BufferSize,
- [Parameter(Mandatory=$false)]
- [Alias('Title')]
- [string] $WindowTitle,
- [Parameter(Mandatory=$false)]
- [Alias('BgC','Background')]
- [System.ConsoleColor] $BackgroundColor,
- [Parameter(Mandatory=$false)]
- [Alias('FC','Fackground')]
- [System.ConsoleColor] $ForegroundColor,
- [Parameter(Mandatory=$false)]
- [Alias('Clear')]
- [switch] $ClearHost
- )
- if ($WindowSize.Count -eq 1) {
- $Host.UI.RawUI.WindowSize = New-Object System.Management.Automation.Host.Size($WindowSize[0], $Host.UI.RawUI.WindowSize.Height)
- } elseif ($WindowSize.Count -eq 2) {
- $Host.UI.RawUI.WindowSize = New-Object System.Management.Automation.Host.Size($WindowSize[0], $WindowSize[1])
- }
- if ($BufferSize.Count -eq 1) {
- $Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size($BufferSize[0], $Host.UI.RawUI.BufferSize.Height)
- } elseif ($BufferSize.Count -eq 2) {
- $Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size($BufferSize[0], $BufferSize[1])
- }
- if ([string]$WindowTitle -ne '') {
- $Host.UI.RawUI.WindowTitle = $WindowTitle
- }
- if ([string]$ForegroundColor -ne '') {
- $Host.UI.RawUI.ForegroundColor = $ForegroundColor
- }
- if ([string]$BackgroundColor -ne '') {
- $Host.UI.RawUI.BackgroundColor = $BackgroundColor
- }
- if ($ClearHost) {
- Clear-Host
- }
- }
- New-Alias -Name adjust -Value Set-Console
- <#
- .SYNOPSIS
- Writes customized output to the console.
- .Description
- The Write-Console cmdlet customizes output similiar to the Write-Host cmdlet, but it inserts new line characters in between words if the string to be displayed exceeds the buffer width of the console window.
- It also comes with a typewriter effect and delays.
- .PARAMETER Message
- Specifies the message to display in the console.
- .PARAMETER BackgroundColor
- Specifies the background color. The acceptable values for this parameter are:
- - Black
- - DarkBlue
- - DarkGreen
- - DarkCyan
- - DarkRed
- - DarkMagenta
- - DarkYellow
- - Gray
- - DarkGray
- - Blue
- - Green
- - Cyan
- - Red
- - Magenta
- - Yellow
- - White
- The default value is $Host.UI.RawUI.BackgroundColor.
- .PARAMETER ForegroundColor
- Specifies the foreground color. The acceptable values for this parameter are:
- - Black
- - DarkBlue
- - DarkGreen
- - DarkCyan
- - DarkRed
- - DarkMagenta
- - DarkYellow
- - Gray
- - DarkGray
- - Blue
- - Green
- - Cyan
- - Red
- - Magenta
- - Yellow
- - White
- The default value is $Host.UI.RawUI.ForegroundColor.
- .PARAMETER NoNewline
- Specifies that the content displayed in the console does not end with a newline character.
- .PARAMETER Seperator
- Inserts either a small, normal or big horizontal seperator.
- .PARAMETER Width
- Specifies the window buffer width. The default value is set by the variable $DefaultWidth.
- .PARAMETER LeadingSpaces
- Specifies the amount of leading spaces. The default value is set by the variable $DefaultLeadingSpaces.
- .PARAMETER NoFirstLineLeadingSpaces
- Specifies that no leading spaces will be inserted at the beginning of the first line.
- .PARAMETER Typewriter
- Specifies that the text will be displayed with a typewriter animation.
- Delays are generated randomly. Each alphanumeric character and dot has a delay of 25ms - 75ms, all other characters have a delay of 50ms - 100ms. Use '`p(x)' or "``p(x)" to insert an additional delay of x ms.
- The default value is set by the variable $DefaultTypewriter.
- .PARAMETER Log
- Specifies log behaviour. The default value is ''.
- .INPUTS
- None
- .OUTPUTS
- Write-Console uses the Write-Host cmdlet to send the message to the host. It does not return any message. However, the host might display the message that Write-Host sends to it.
- .EXAMPLE
- PS> Write-Console -Message 'Some string' -NoNewline -LeadingSpaces 4 -Log 'Nothing'
- Some string
- Displays the message with 4 spaces (or 1 tab) inserted in front of the string and every following line if the string exceeds the maximum width. At the end of the string there is no newline character being added, so the next will be added at the end of the string on the same line.
- .EXAMPLE
- PS> Write-Console -Message 'A string which is quite long and therefore has new line characters inserted so the specified width is not exceeded' -Width 40 -NoFirstLineLeadingSpaces
- A string which is quite long and
- therefore has new line characters
- inserted so the specified width is not
- exceeded
- Displays the message and splits it so no line exceeds the width of 40 characters.
- There are no leading spaces at the start of the string.
- .EXAMPLE
- PS> Write-Console -Seperator 'Big'
- ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
- .EXAMPLE
- PS> Write-Console -Message 'Some string' -BackgroundColor Black -ForegroundColor Cyan -Typewriter
- Some string
- Displays the message with a typewriter effect. The background color is black and the foreground color is cyan.
- .LINK
- https://pastebin.com/9RSusrvC
- Write-Host
- #>
- function Write-Console {
- Param (
- [Parameter(Mandatory=$true,
- Position=0,
- ParameterSetName='Write')]
- [Alias('Msg','Object')]
- [AllowNull()]
- [AllowEmptyString()]
- [string[]] $Message,
- [Parameter(Mandatory=$false)]
- [Alias('BgC','Background')]
- [System.ConsoleColor] $BackgroundColor = $Host.UI.RawUI.BackgroundColor,
- [Parameter(Mandatory=$false)]
- [Alias('FgC','Foreground')]
- [System.ConsoleColor] $ForegroundColor = $Host.UI.RawUI.ForegroundColor,
- [Parameter(Mandatory=$false)]
- [switch] $NoNewline,
- [Parameter(Mandatory=$true,
- ParameterSetName='WriteSeperator')]
- [ValidateSet('Small', 's', 'Normal', 'n', 'Big', 'b')]
- [string] $Seperator = '',
- [Parameter(Mandatory=$false)]
- [ValidateScript({[int]$_ -gt ($Message -split " |`n" | Sort-Object -Property Length -Descending | Select-Object -First 1 -ExpandProperty Length) + 4})]
- [int] $Width = $DefaultWidth,
- [Parameter(Mandatory=$false,
- ParameterSetName='Write')]
- [Alias('LS')]
- [ValidateScript({if ([int]$Width -eq 0) {[int]$_ -ge 0 -and [int]$_ -lt $DefaultWidth} else {[int]$_ -ge 0 -and [int]$_ -lt $Width}})]
- [int] $LeadingSpaces = $DefaultLeadingSpaces,
- [Parameter(Mandatory=$false,
- ParameterSetName='Write')]
- [Alias('NoLS')]
- [switch] $NoFirstLineLeadingSpaces,
- [Parameter(Mandatory=$false,
- ParameterSetName='Write')]
- [switch] $Typewriter = $DefaultTypewriter,
- [Parameter(Mandatory=$false)]
- [ValidateSet('Only', 'o', 'Nothing', 'n', '')]
- [string] $Log = ''
- )
- if ([string]$Seperator -ne '') {
- if ($Seperator[0] -eq 's') {
- $Output = ('─' * $Width)
- } elseif ($Seperator[0] -eq 'n') {
- $Output = ('═' * $Width)
- } else {
- $Output = ('▄' * $Width + "`n")
- }
- } else {
- $Output = (' ' * $LeadingSpaces)
- $CursorPosX = $Host.UI.RawUI.CursorPosition.X
- $Message -split "`n" | ForEach-Object {
- $CurObj = $_
- if ($Counter1++ -gt 0) {
- $Output += ("`n" + ' ' * $LeadingSpaces)
- $CursorPosX = $LeadingSpaces
- }
- while (($CurObj -replace '`p\([0-9]+?\)').Length -ge $Width - ($Output.Length - [Math]::Abs($Output.LastIndexOf("`n"))) - $CursorPosX) {
- if ($Counter2++ -gt 0) {
- $Output += ("`n" + ' ' * $LeadingSpaces)
- $CursorPosX = $LeadingSpaces
- }
- $pLength = -1
- Select-String -Pattern '`p\([0-9]+?\)' -AllMatches -InputObject $CurObj | Select-Object -ExpandProperty Matches | ForEach-Object {
- if ($CurObj.Length -ge ($Width + $pLength + $_.Length - ($Output.Length - [Math]::Abs($Output.LastIndexOf("`n"))) - $CursorPosX) -and $_.Index -le ($CurObj.Substring(0, $Width + $pLength + $_.Length - ($Output.Length - [Math]::Abs($Output.LastIndexOf("`n"))) - $CursorPosX).LastIndexOf(' '))) {
- $pLength += $_.Length
- }
- }
- if ($CurObj.Length -ge ($End = $Width + $pLength - ($Output.Length - [Math]::Abs($Output.LastIndexOf("`n"))) - $CursorPosX)) {
- if ($CurObj.Contains(' ')) {
- if ($CurObj.Substring(0, $End).Contains(' ')) {
- $Output += $CurObj.Substring(0, ($SpaceIndex = $CurObj.Substring(0, $End).LastIndexOf(' ')))
- $CurObj = $CurObj.Substring($SpaceIndex + 1)
- } else {
- $Output += $CurObj.Substring(0, ($SpaceIndex = $CurObj.LastIndexOf(' ')))
- $CurObj = $CurObj.Substring($SpaceIndex + 1)
- }
- } else {
- $Output += $CurObj
- $CurObj = ''
- }
- }
- }
- if ($Counter2 -eq 1) {
- $Output += ("`n" + ' ' * $LeadingSpaces)
- }
- $Output += $CurObj
- }
- if ($NoFirstLineLeadingSpaces) {
- $Output = $Output.Substring($LeadingSpaces)
- }
- }
- if ($Log[0] -ne 'n' -and [string]$LogFilePath -ne '') {
- Add-Content -Value $Output -LiteralPath $LogFilePath -NoNewline:$NoNewline -Encoding UTF8
- }
- if ($Log[0] -ne 'o') {
- $Output -split '(`p\([0-9]+?\))' | ForEach-Object {
- if ($_ -match '`p\([0-9]+?\)') {
- Start-Sleep -Milliseconds ([int]($Matches.0).Trim('`p()'))
- } elseif ($Typewriter) {
- $_ -split '.*?' | ForEach-Object {
- Write-Host -Object $_ -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor -NoNewline
- if ($_ -notmatch '[A-z0-9]|') {
- Start-Sleep -Milliseconds (Get-Random -Minimum 75 -Maximum 150)
- } else {
- Start-Sleep -Milliseconds (Get-Random -Minimum 25 -Maximum 75)
- }
- }
- } else {
- Write-Host -Object $_ -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor -NoNewline
- }
- }
- if (!$NoNewline) {
- Write-Host -Object ''
- }
- }
- }
- New-Alias -Name wc -Value Write-Console
- Export-ModuleMember -Alias * -Function *
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement