~mpldr/go-ansi

dbffa8bb37e391821d912ef80cc78540bdf90989 — Moritz Poldrack 1 year, 6 months ago 84cfcde master
expanded strip documentation
3 files changed, 16 insertions(+), 3 deletions(-)

M length.go
M notifications.go
M strip.go
M length.go => length.go +2 -0
@@ 5,6 5,8 @@ import (
	"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, ""))

M notifications.go => notifications.go +6 -2
@@ 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)
}

M strip.go => strip.go +8 -1
@@ 7,6 7,9 @@ import (

// 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 StripString(str string) string {

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 @@ func stripNotify(bts []byte) []byte {
}

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