Update write-morse-code.ps1

This commit is contained in:
Markus Fleschutz 2023-01-06 20:14:05 +01:00
parent 3876614b62
commit b2424da287

View File

@ -1,14 +1,15 @@
<# <#
.SYNOPSIS .SYNOPSIS
Writes text in Morse code Writes Morse code
.DESCRIPTION .DESCRIPTION
This PowerShell script writes text in Morse code. This PowerShell script writes the given text in Morse code.
.PARAMETER text .PARAMETER text
Specifies the text to write Specifies the text to write
.PARAMETER speed .PARAMETER speed
Specifies the speed of one time unit (100 ms per default) Specifies the speed of one time unit (100 ms per default)
.EXAMPLE .EXAMPLE
PS> ./write-morse-code "Hello World" PS> ./write-morse-code ABC
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -18,20 +19,20 @@
param([string]$text = "", [int]$speed = 100) # one time unit in milliseconds param([string]$text = "", [int]$speed = 100) # one time unit in milliseconds
function gap { param([int]$Length) function gap { param([int]$Length)
for ([int]$i = 1; $i -lt $Length; $i++) { for ([int]$i = 0; $i -lt $Length; $i++) {
write-host " " -nonewline Write-Host " " -noNewline
} }
start-sleep -milliseconds ($Length * $speed) Start-Sleep -milliseconds ($Length * $speed)
} }
function dot { function dot {
write-host "." -nonewline Write-Host "" -noNewline
start-sleep -milliseconds $speed # signal Start-Sleep -milliseconds $speed # signal
} }
function dash { function dash {
write-host "_" -nonewline Write-Host "" -noNewline
start-sleep -milliseconds (3 * $speed) # signal Start-Sleep -milliseconds (3 * $speed) # signal
} }
function Char2MorseCode { param([string]$Char) function Char2MorseCode { param([string]$Char)
@ -77,16 +78,14 @@ function Char2MorseCode { param([string]$Char)
} }
try { try {
if ($text -eq "" ) { [string]$text = read-host "Enter text to write" } if ($text -eq "" ) { [string]$text = Read-Host "Enter text to write" }
[char[]]$ArrayOfChars = $text.ToUpper() [char[]]$ArrayOfChars = $text.ToUpper()
foreach($Char in $ArrayOfChars) { foreach($Char in $ArrayOfChars) {
Char2MorseCode $Char Char2MorseCode $Char
} }
write-host ""
write-host ""
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1 exit 1
} }