Skip to content

Commit cc39937

Browse files
committed
Added write-morse-code.ps1
1 parent e2887fa commit cc39937

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ The following PowerShell scripts can be found in the [Scripts/](Scripts/) subfol
6262
* [write-big.ps1](Scripts/write-big.ps1) - writes the given text in big letters
6363
* [write-blue.ps1](Scripts/write-blue.ps1) - writes the given text in a blue foreground color
6464
* [write-green.ps1](Scripts/write-green.ps1) - writes the given text in a green foreground color
65+
* [write-morse-code.ps1](Scripts/write-morse-code.ps1) - writes the given text in Morse code
6566
* [write-MOTD.ps1](Scripts/write-MOTD.ps1) - writes the message of the day (MOTD)
6667
* [write-red.ps1](Scripts/write-red.ps1) - writes the given text in a red foreground color
6768
* [write-typewriter.ps1](Scripts/write-typewriter.ps1) - writes the given text with the typewriter effect

Scripts/write-morse-code.ps1

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)