From 130153c48b6143a972a5e0e0b55cc7107bcfc1b1 Mon Sep 17 00:00:00 2001 From: Steffen Brandsborg Date: Wed, 7 Nov 2018 14:07:24 +0100 Subject: [PATCH] Code cleanup --- Get-CountdownToBeer.ps1 | 87 ++++++++++++++++++++++++++++++++++------- 1 file changed, 72 insertions(+), 15 deletions(-) diff --git a/Get-CountdownToBeer.ps1 b/Get-CountdownToBeer.ps1 index 19474d4..b08ba8f 100644 --- a/Get-CountdownToBeer.ps1 +++ b/Get-CountdownToBeer.ps1 @@ -1,18 +1,75 @@ -function Get-CountdownToBeer { +function Get-CountdownToBeer +{ param ( - [Parameter(Mandatory = $True)] - $BeerOClock, - - [Parameter(Mandatory = $False)] - [Bool]$Speak + [Parameter(Mandatory = $True)] $BeerOClock, + [Parameter(Mandatory = $False)] [Switch]$Speech, + [Parameter(Mandatory = $False)] [Switch]$Ascii, + [Parameter(Mandatory = $False)] [Switch]$Now ) -do -{ - $Timeleft = New-TimeSpan -Start (Get-date) -End $BeerOClock - Start-Sleep -Seconds 1 - Clear-Host - Write-Output "Countdown til øl! - $(($Timeleft).Minutes):$(($Timeleft).Seconds)" - } until ($Timeleft.Seconds -eq "0" -and $Timeleft.Minutes -eq "0") - Write-Output "Nutid, Datid, ØlTid, Altid!" - if ($Speak -eq $true){Add-Type -AssemblyName System.speech;$Speech = New-Object System.Speech.Synthesis.SpeechSynthesizer;$Speech.Speak("Beer time!!!");Sleep -Seconds 2;$Speech.Speak("Beer time!!!");Sleep -Seconds 2;$Speech.Speak("Beer time!!!")} + function Invoke-Speech + { + Add-Type -AssemblyName System.speech; $Speech = New-Object System.Speech.Synthesis.SpeechSynthesizer; $Speech.Speak("Beer time!!!"); Sleep -Seconds 1; $Speech.Speak("Beer time!!!"); Sleep -Seconds 1; $Speech.Speak("Beer time!!!") + } + + function Invoke-Ascii + { + [void] [System.Reflection.Assembly]::LoadWithPartialName('System.drawing') + + $Colors = @{ + 'FF000000' = 'Black' + 'FFFFFFFF' = 'White' + } + + Function Get-ClosestConsoleColor($PixelColor) + { + ($(foreach ($item in $Colors.Keys) + { + [pscustomobject]@{ + 'Color' = $Item + 'Diff' = [math]::abs([convert]::ToInt32($Item, 16) - [convert]::ToInt32($PixelColor, 16)) + } + }) | Sort-Object Diff)[0].color + } + $BitMap = [System.Drawing.Bitmap]::FromFile((Get-Item ".\Beer.png")) + Foreach ($y in (1..($BitMap.Height - 1))) + { + Foreach ($x in (1..($BitMap.Width - 1))) + { + $Pixel = $BitMap.GetPixel($X, $Y) + $BackGround = $Colors.Item((Get-ClosestConsoleColor $Pixel.name)) + + + If ($ToASCII) + { + Write-Host "$([Char](Get-Random -Maximum 126 -Minimum 33))" -NoNewline -ForegroundColor $BackGround + } + else + { + Write-Host " " -NoNewline -BackgroundColor $BackGround + } + } + Write-Host '' + } + } + + if($Now){ + Write-Output "Nutid, Datid, ØlTid, Altid!" + if ($Speech) {Invoke-Speech} + if ($Ascii) {Invoke-Ascii} + } + else + { + do + { + $Timeleft = New-TimeSpan -Start (Get-date) -End $BeerOClock + Start-Sleep -Seconds 1 + Clear-Host + Write-Output "Countdown til øl! - $(($Timeleft).Minutes):$(($Timeleft).Seconds)" + } until ($Timeleft.Seconds -eq "0" -and $Timeleft.Minutes -eq "0") + + Write-Output "Nutid, Datid, ØlTid, Altid!" + + if ($Sound) {Invoke-Speech} + if ($Ascii) {Invoke-Ascii} + } }