Private/Console.psm1
|
using namespace System using module .\Colors.psm1 class ConsoleCoordinate { [int]$X [int]$Y ConsoleCoordinate([int]$X, [int]$Y) { $this.X = $X; $this.Y = $Y } } class ConsoleReader : System.IO.TextReader { ConsoleReader() : base() { } [int] Read() { $key = [Console]::ReadKey($true) return [int][char]$key.KeyChar } [int] Read([char[]]$buffer, [int]$index, [int]$count) { if ($null -eq $buffer) { throw [ArgumentNullException]::new("buffer") } if ($index -lt 0 -or $count -lt 0) { throw [ArgumentOutOfRangeException]::new("Index and count must be non-negative") } if ($buffer.Length - $index -lt $count) { throw [ArgumentException]::new("Buffer too small") } $charsRead = 0 while ($charsRead -lt $count -and [Console]::KeyAvailable) { $key = [Console]::ReadKey($true) $buffer[$index + $charsRead] = $key.KeyChar $charsRead++ } return $charsRead } [string] ReadLine() { return [Console]::ReadLine() } } class ConsoleWriter : System.IO.TextWriter { static hidden [string] $LeadPreffix static hidden [bool] $UseTypingEffect = $true static hidden [bool] $UseLeadPreffix = ![string]::IsNullOrWhiteSpace([ConsoleWriter]::LeadPreffix) static hidden [string[]] $Colors = [ConsoleWriter]::get_ColorNames() static hidden [ValidateNotNull()][scriptblock]$ValidateScript = { param($arg) if ([String]::IsNullOrEmpty($arg)) { throw [ArgumentNullException]::new('text', 'Cannot be Null Or Empty') } } static [string] write([string]$text) { return [ConsoleWriter]::Write($text, 20, 1200) } static [string] Write([string]$text, [bool]$AddPreffix) { return [ConsoleWriter]::Write($text, 20, 1200, $AddPreffix) } static [string] Write([string]$text, [int]$Speed, [int]$Duration) { return [ConsoleWriter]::Write($text, 20, 1200, [ConsoleWriter]::UseLeadPreffix) } static [string] write([string]$text, [ConsoleColor]$color) { return [ConsoleWriter]::Write($text, $color, [ConsoleWriter]::UseTypingEffect) } static [string] write([string]$text, [ConsoleColor]$color, [bool]$Animate) { return [ConsoleWriter]::Write($text, [ConsoleWriter]::LeadPreffix, 20, 1200, $color, $Animate, [ConsoleWriter]::LeadPreffix) } static [string] write([string]$text, [int]$Speed, [int]$Duration, [bool]$AddPreffix) { return [ConsoleWriter]::Write($text, [ConsoleWriter]::LeadPreffix, $Speed, $Duration, [ConsoleColor]::White, [ConsoleWriter]::UseTypingEffect, $AddPreffix) } static [string] write([string]$text, [ConsoleColor]$color, [bool]$Animate, [bool]$AddPreffix) { return [ConsoleWriter]::Write($text, [ConsoleWriter]::LeadPreffix, 20, 1200, $color, $Animate, $AddPreffix) } static [string] write([string]$text, [string]$Preffix, [System.ConsoleColor]$color) { return [ConsoleWriter]::Write($text, $Preffix, $color, [ConsoleWriter]::UseTypingEffect) } static [string] write([string]$text, [string]$Preffix, [System.ConsoleColor]$color, [bool]$Animate) { return [ConsoleWriter]::Write($text, $Preffix, 20, 1200, $color, $Animate, [ConsoleWriter]::LeadPreffix) } static [string] write([string]$text, [string]$Preffix, [int]$Speed, [int]$Duration, [bool]$AddPreffix) { return [ConsoleWriter]::Write($text, $Preffix, $Speed, $Duration, [ConsoleColor]::White, [ConsoleWriter]::UseTypingEffect, $AddPreffix) } static [string] write([string]$text, [string]$Preffix, [int]$Speed, [int]$Duration, [ConsoleColor]$color, [bool]$Animate, [bool]$AddPreffix) { return [ConsoleWriter]::Write($text, $Preffix, $Speed, $Duration, $color, $Animate, $AddPreffix, [ConsoleWriter]::ValidateScript) } static [string] write([string]$text, [string]$Preffix, [int]$Speed, [int]$Duration, [ConsoleColor]$color, [bool]$Animate, [bool]$AddPreffix, [scriptblock]$ValidateScript) { if ($null -ne $ValidateScript) { [void]$ValidateScript.Invoke($text) } elseif ([string]::IsNullOrWhiteSpace($text)) { return $text } [int]$length = $text.Length; $delay = 0 # Check if delay time is required: $delayIsRequired = if ($length -lt 50) { $false } else { $delay = $Duration - $length * $Speed; $delay -gt 0 } if ($AddPreffix -and ![string]::IsNullOrEmpty($Preffix)) { [void][ConsoleWriter]::Write($Preffix, [string]::Empty, 1, 100, [ConsoleColor]::Green, $false, $false); } $FgColr = [Console]::ForegroundColor [Console]::ForegroundColor = $color if ($Animate) { for ($i = 0; $i -lt $length; $i++) { [void][Console]::Write($text[$i]); Start-Sleep -Milliseconds $Speed; } } else { [void][Console]::Write($text); } if ($delayIsRequired) { Start-Sleep -Milliseconds $delay } [Console]::ForegroundColor = $FgColr return $text } static [byte[]] Encode([string]$text) { return [System.Text.Encoding]::UTF8.GetBytes($text) } static [string[]] get_ColorNames() { return [color].GetMethods().Where({ $_.IsStatic -and $_.Name -like "Get_*" }).Name.Substring(4) } static [int] get_ConsoleWidth() { # Force a refresh of the console information [System.Console]::SetCursorPosition([System.Console]::CursorLeft, [System.Console]::CursorTop) return [System.Console]::WindowWidth } hidden [System.Text.Encoding] get_Encoding() { return [System.Text.Encoding]::UTF8 } static [void] Clear() { [System.Console]::Clear() } static [void] SetCursorPosition([int]$X, [int]$Y) { [System.Console]::SetCursorPosition($X, $Y) } static [void] SetCursorPosition([ConsoleCoordinate]$Coordinate) { [System.Console]::SetCursorPosition($Coordinate.X, $Coordinate.Y) } static [void] ResetColor() { [System.Console]::ResetColor() } static [void] WriteLine([string]$text) { [void][ConsoleWriter]::Write($text) [System.Console]::WriteLine() } static [void] WriteLine([string]$text, [ConsoleColor]$color) { [void][ConsoleWriter]::Write($text, $color) [System.Console]::WriteLine() } } |