Skip to content

Commit

Permalink
convert to ascii best we can (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-grace committed Sep 28, 2022
1 parent d5cd207 commit b2b535b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ require (
gopkg.in/yaml.v2 v2.4.0
)

require golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
require (
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/text v0.3.7
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
19 changes: 19 additions & 0 deletions pkg/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package common
import (
"fmt"
"strings"
"unicode"

"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)

func SplitMessageToLength(msg string, length int) []string {
Expand All @@ -27,3 +32,17 @@ func SplitMessageToLength(msg string, length int) []string {
splits = append(splits, subMsg)
return splits
}

func ToAscii(str string) (string, error) {
result, _, err := transform.String(
transform.Chain(
norm.NFD,
runes.Remove(runes.In(unicode.Mn))),
str)

if err != nil {
return "", err
}

return result, nil
}
50 changes: 50 additions & 0 deletions pkg/common/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package common

import "testing"

type testCase struct {
input string
expected string
}

func TestToAscii(t *testing.T) {
tests := []testCase{
{
input: "Zoë",
expected: "Zoe",
},
{
input: "Café",
expected: "Cafe",
},
{
input: "Cortège",
expected: "Cortege",
},
{
input: "Naïve",
expected: "Naive",
},
{
input: "Entrepôt",
expected: "Entrepot",
},
{
input: "Façade",
expected: "Facade",
},
{
input: "Jalapeño",
expected: "Jalapeno",
},
}

for _, c := range tests {
if result, err := ToAscii(c.input); err != nil {
t.Errorf(err.Error())
if result != c.expected {
t.Errorf("%v became %v not %v", c.input, result, c.expected)
}
}
}
}
18 changes: 17 additions & 1 deletion pkg/data/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,32 @@ type RadiotextSession struct {
}

func (s *RadiotextSession) OutputRadioTextMessage(msg string, highPriority bool) {
var fullyOutputed bool

defer func() {
if !fullyOutputed {
time.Sleep(time.Duration(s.SSHSession.Config.WaitTime) * time.Second)
}
}()

if !highPriority {
// if we're in a high priority write lock, and we get
// a low priority message, we'll just store it so we
// can refer to it later once the high priority message is over
s.OutputMessage = msg

if s.PriorityWriteLock {
time.Sleep(time.Duration(s.SSHSession.Config.WaitTime) * time.Second)
return
}
}

m, err := common.ToAscii(msg)
if err != nil {
fmt.Println(err)
return
}
msg = m

for _, output := range common.SplitMessageToLength(msg, s.SSHSession.Config.MaxTextLength) {
log.Println(output)
_, err := s.SSHSession.Stdin.Write([]byte(
Expand All @@ -54,4 +68,6 @@ func (s *RadiotextSession) OutputRadioTextMessage(msg string, highPriority bool)

time.Sleep(time.Duration(s.SSHSession.Config.WaitTime) * time.Second)
}

fullyOutputed = true
}

0 comments on commit b2b535b

Please sign in to comment.