|
| 1 | +#!/snap/bin/powershell |
| 2 | + |
| 3 | +# Syntax: ./write-morse-code.ps1 [<text>] |
| 4 | +# Description: writes the given text in Morse code |
| 5 | +# Author: Markus Fleschutz |
| 6 | +# Source: github.com/fleschutz/PowerShell |
| 7 | +# License: CC0 |
| 8 | + |
| 9 | +param([String]$Text) |
| 10 | + |
| 11 | +function dot() { |
| 12 | + write-host "." -nonewline |
| 13 | + start-sleep -milliseconds 100 |
| 14 | +} |
| 15 | + |
| 16 | +function dash() { |
| 17 | + write-host "-" -nonewline |
| 18 | + start-sleep -milliseconds 300 |
| 19 | +} |
| 20 | + |
| 21 | +function pause() { |
| 22 | + write-host " " -nonewline |
| 23 | + start-sleep -milliseconds 400 |
| 24 | +} |
| 25 | + |
| 26 | +function Char2MorseCode() { param([String]$Char) |
| 27 | + switch($Char) { |
| 28 | + 'A' { dot; dash } |
| 29 | + 'B' { dash; dot; dot; dot } |
| 30 | + 'C' { dash; dot; dash; dot } |
| 31 | + 'D' { dash; dot; dot } |
| 32 | + 'E' { dot } |
| 33 | + 'F' { dot; dot; dash; dot } |
| 34 | + 'G' { dash; dash; dot } |
| 35 | + 'H' { dot; dot; dot; dot } |
| 36 | + 'I' { dot; dot } |
| 37 | + 'J' { dot; dash; dash; dash } |
| 38 | + 'K' { dash; dot; dash } |
| 39 | + 'L' { dot; dash; dot; dot } |
| 40 | + 'M' { dash; dash } |
| 41 | + 'N' { dash; dot } |
| 42 | + 'O' { dash; dash; dash } |
| 43 | + 'P' { dot; dash; dash; dot } |
| 44 | + 'Q' { dash; dash; dot; dash } |
| 45 | + 'R' { dot; dash; dot } |
| 46 | + 'S' { dot; dot; dot } |
| 47 | + 'T' { dash } |
| 48 | + 'U' { dot; dot; dash } |
| 49 | + 'V' { dot; dot; dot; dash } |
| 50 | + 'W' { dot; dash; dash } |
| 51 | + 'X' { dash; dot; dot; dash } |
| 52 | + 'Y' { dash; dot; dash; dash } |
| 53 | + 'Z' { dash; dash; dot; dot } |
| 54 | + '1' { dot; dash; dash; dash; dash } |
| 55 | + '2' { dot; dot; dash; dash; dash } |
| 56 | + '3' { dot; dot; dot; dash; dash } |
| 57 | + '4' { dot; dot; dot; dot; dash } |
| 58 | + '5' { dot; dot; dot; dot; dot } |
| 59 | + '6' { dash; dot; dot; dot; dot } |
| 60 | + '7' { dash; dash; dot; dot; dot } |
| 61 | + '8' { dash; dash; dash; dot; dot } |
| 62 | + '9' { dash; dash; dash; dash; dot } |
| 63 | + '0' { dash; dash; dash; dash; dash } |
| 64 | + default { pause } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +try { |
| 69 | + if ($Text -eq "" ) { |
| 70 | + [String]$Text = read-host "Enter text to write" |
| 71 | + } |
| 72 | + [char[]]$ArrayOfChars = $Text.ToUpper() |
| 73 | + foreach($Char in $ArrayOfChars) { |
| 74 | + Char2MorseCode $Char |
| 75 | + } |
| 76 | + write-host "" |
| 77 | + exit 0 |
| 78 | +} catch { |
| 79 | + write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" |
| 80 | + exit 1 |
| 81 | +} |
0 commit comments