Last active
October 18, 2017 14:55
-
-
Save waf/8268a61ab0a240ed46b3cec6d9608a04 to your computer and use it in GitHub Desktop.
plank countdown script in more of a functional style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# main entry point | |
function Main | |
{ | |
$total = 60 * 2 + 58 | |
$timings = Generate-Timings -Total $total | |
Speak-Timings $timings | |
} | |
# int -> string[] | |
# generates an array of strings representing what should be said at each second. | |
function Generate-Timings($Total) | |
{ | |
$startCountdown = $total - 10 | |
# count up every 30 seconds | |
$countup = 1..($startCountdown - 1) ` | |
| ForEach-Object { | |
$seconds = $_ | |
switch ($seconds) { | |
30 { "30 seconds passed" } | |
60 { "1 minute passed" } | |
default { | |
[string]$minutes = [Math]::Floor($seconds / 60) | |
switch ($seconds % 60) { | |
0 { "$minutes minutes passed" } | |
30 { "$minutes 30 passed" } | |
default { "" } | |
} | |
} | |
} | |
} | |
# count down from 10 to 1 | |
$countdown = $startCountdown..$total ` | |
| ForEach-Object { | |
$total - $_ | |
} | |
$countup + $countdown | |
} | |
# string[] -> IO | |
function Speak-Timings($timings) | |
{ | |
Add-Type -AssemblyName System.Speech; | |
$speaker = (New-Object System.Speech.Synthesis.SpeechSynthesizer) | |
$speaker.SelectVoice("Microsoft Zira Desktop") | |
$speaker.Speak("Hi everyone, ready? Start planking in 3, 2, 1!") | |
$timings | ForEach-Object { | |
if($_) { | |
Speak-Async $_ | |
write-host $_ | |
} | |
sleep 1 | |
} | |
sleep 1 | |
$speaker.Speak("Good job everyone. I knew you could do it") | |
} | |
# speak the text, but don't block the current thread | |
function Speak-Async($text) | |
{ | |
# not using $synth.SpeakAsync because execution is slow when we do the countdown | |
Start-Process powershell -NoNewWindow -argument @" | |
Add-Type -AssemblyName System.Speech; | |
`$synth = (New-Object System.Speech.Synthesis.SpeechSynthesizer) | |
`$synth.SelectVoice('Microsoft Zira Desktop') | |
`$synth.Speak('$text') | |
"@ | |
} | |
Main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment