1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
diff --git a/strip.go b/strip.go
index b89c38c8536cf92c0ca9eebd53f3e57601946360..1317c0fd43b4fd88c603bbd40d2e822944f3d9e6 100644
--- a/strip.go
+++ b/strip.go
@@ -11,8 +11,26 @@ func StripString(str string) string {
bts := []byte(str)
bts = stripStandard(bts)
bts = stripLink(bts)
+ bts = stripNotify(bts)
return string(bts)
+}
+
+func stripNotify(bts []byte) []byte {
+ for {
+ index := bytes.Index(bts, []byte{0x1b, ']', '7', '7', '7', ';'})
+ if index == -1 {
+ break
+ }
+ removeUntil := bytes.Index(bts[index+1:], []byte{0x07})
+ if removeUntil == -1 {
+ break
+ }
+
+ bts = append(bts[:index], bts[index+removeUntil+2:]...)
+ }
+
+ return bts
}
func stripLink(bts []byte) []byte {
diff --git a/strip_test.go b/strip_test.go
index abd7e16a10ed927f0baf9df7bc1f2abae400b6de..8ac340aa1c25b5e4ea8ca48a570bb30580956dbd 100644
--- a/strip_test.go
+++ b/strip_test.go
@@ -24,13 +24,36 @@ })
}
func TestStrip(t *testing.T) {
- str := fmt.Sprint(Black("black text"), "some kept text", ReverseVideo("inverted text"), Green("green text"), Bold("bold text"))
- if StripString(str) != "black textsome kept textinverted textgreen textbold text" {
- t.Errorf("strip not successful!\ngot: %s\n%v\nexpected: black textsome kept textinverted textgreen textbold text\n%v", StripString(str), []byte(StripString(str)), []byte("black textsome kept textinverted textgreen textbold text"))
+ tests := []struct {
+ name string
+ input string
+ expected string
+ }{
+ {
+ name: "formatting",
+ input: fmt.Sprint(Black("black text"), "some kept text", ReverseVideo("inverted text"), Green("green text"), Bold("bold text")),
+ expected: "black textsome kept textinverted textgreen textbold text",
+ },
+ {
+ name: "link",
+ input: fmt.Sprintf("Link to my homepage: %s", LinkString("https://moritz.sh", "Linktext")),
+ expected: "Link to my homepage: https://moritz.sh",
+ },
+ {
+ name: "notification",
+ input: fmt.Sprintf("Send a notification %sto me", SendNotification("title", "body")),
+ expected: "Send a notification to me",
+ },
}
- str = fmt.Sprintf("Link to my homepage: %s", LinkString("https://moritz.sh", "Linktext"))
- if StripString(str) != "Link to my homepage: https://moritz.sh" {
- t.Errorf("strip not successful!\ngot: %s\n%v\nexpected: Link to my homepage: https://moritz.sh\n%v", StripString(str), []byte(StripString(str)), []byte("Link to my homepage: https://moritz.sh"))
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ if StripString(test.input) != test.expected {
+ t.Logf("got: %s\n", StripString(test.input))
+ t.Logf("expected: %s\n", test.expected)
+ t.Log([]byte(StripString(test.input)))
+ t.Log([]byte(test.expected))
+ }
+ })
}
}
|