Commit dbffa8bb37e391821d912ef80cc78540bdf90989

Parents: 84cfcdee5491e1772a3400007d6e58477b5bee92

From: Moritz Poldrack <git@moritz.sh>
Date: Sun Oct 16 15:30:58 2022 +0700

expanded strip documentation

		

Stats

length.go +2/-0
notifications.go +6/-2
strip.go +8/-1

Changeset

 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
diff --git a/length.go b/length.go
index 5c963b1e2487e5789e9731a2dcc68701f05a8894..c61cbe19ef96fdd88dd96ca7f7a804c635ebe83b 100644
--- a/length.go
+++ b/length.go
@@ -5,6 +5,8 @@ 	"regexp"
 	"unicode/utf8"
 )
 
+// GetLengthWithoutCodes counts the characters after all Escape-sequences have
+// been removed.
 func GetLengthWithoutCodes(content string) int {
 	re := regexp.MustCompile(`(?m)\x1b(\[([\d;]+m|[\d;]+(H|A|B|C|D|J|K|S|T)|s|u)|\](8;;[^\x1b]*\x1b\\|777;notify;[^;]*;[^\a]*\a))`)
 	return utf8.RuneCountInString(re.ReplaceAllString(content, ""))
diff --git a/notifications.go b/notifications.go
index 5304ded7cf8ff6d1019bfa4aeab3e5c7407506be..fd6125d9c9c16f3474673224df5d55f43f8c585e 100644
--- a/notifications.go
+++ b/notifications.go
@@ -4,8 +4,12 @@ import (
 	"fmt"
 )
 
-// Link creates a terminal Hyperlink that can be clicked, if the terminal
-// emulator supports it.
+// SendNotification instructs the terminal emulator to send a notification to
+// the user. This will also work via SSH connections. Support for this code is
+// not ubiquitous and may lead to messed up output.
+//
+// The provided title may not contain semi-colon. Everything after the first
+// semicolon will be part of the body.
 func SendNotification(title, body string) string {
 	return fmt.Sprintf(notification, title, body)
 }
diff --git a/strip.go b/strip.go
index 1317c0fd43b4fd88c603bbd40d2e822944f3d9e6..36203bf645c7b03880eae21bf58fc5b08b8aa9eb 100644
--- a/strip.go
+++ b/strip.go
@@ -7,6 +7,9 @@ )
 
 // StripString removes all ANSI-Escape sequences from the given string and
 // returns the cleaned version
+//
+// Links are changed so that only the URL remains; Notifications are removed
+// completely.
 func StripString(str string) string {
 	bts := []byte(str)
 	bts = stripStandard(bts)
@@ -18,15 +21,18 @@ }
 
 func stripNotify(bts []byte) []byte {
 	for {
+		// find the start of the notification
 		index := bytes.Index(bts, []byte{0x1b, ']', '7', '7', '7', ';'})
 		if index == -1 {
 			break
 		}
+		// find the end of the notification
 		removeUntil := bytes.Index(bts[index+1:], []byte{0x07})
 		if removeUntil == -1 {
 			break
 		}
 
+		// remove everything between start and end of the sequence
 		bts = append(bts[:index], bts[index+removeUntil+2:]...)
 	}
 
@@ -34,7 +40,8 @@ 	return bts
 }
 
 func stripLink(bts []byte) []byte {
-	for { // stop if the last run did not match anything
+	for {
+		// find the start of the url
 		index := bytes.Index(bts, []byte{0x1b, '\\'})
 		if index == -1 {
 			break