File: ansi.go

package info (click to toggle)
golang-github-jedib0t-go-pretty 6.5.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,700 kB
  • sloc: makefile: 28; sh: 14
file content (57 lines) | stat: -rw-r--r-- 1,849 bytes parent folder | download
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
package text

import "strings"

// ANSICodesSupported will be true on consoles where ANSI Escape Codes/Sequences
// are supported.
var ANSICodesSupported = areANSICodesSupported()

// Escape encodes the string with the ANSI Escape Sequence.
// For ex.:
//
//	Escape("Ghost", "") == "Ghost"
//	Escape("Ghost", "\x1b[91m") == "\x1b[91mGhost\x1b[0m"
//	Escape("\x1b[94mGhost\x1b[0mLady", "\x1b[91m") == "\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m"
//	Escape("Nymeria\x1b[94mGhost\x1b[0mLady", "\x1b[91m") == "\x1b[91mNymeria\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m"
//	Escape("Nymeria \x1b[94mGhost\x1b[0m Lady", "\x1b[91m") == "\x1b[91mNymeria \x1b[94mGhost\x1b[0m\x1b[91m Lady\x1b[0m"
func Escape(str string, escapeSeq string) string {
	out := ""
	if !strings.HasPrefix(str, EscapeStart) {
		out += escapeSeq
	}
	out += strings.Replace(str, EscapeReset, EscapeReset+escapeSeq, -1)
	if !strings.HasSuffix(out, EscapeReset) {
		out += EscapeReset
	}
	if strings.Contains(out, escapeSeq+EscapeReset) {
		out = strings.Replace(out, escapeSeq+EscapeReset, "", -1)
	}
	return out
}

// StripEscape strips all ANSI Escape Sequence from the string.
// For ex.:
//
//	StripEscape("Ghost") == "Ghost"
//	StripEscape("\x1b[91mGhost\x1b[0m") == "Ghost"
//	StripEscape("\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m") == "GhostLady"
//	StripEscape("\x1b[91mNymeria\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m") == "NymeriaGhostLady"
//	StripEscape("\x1b[91mNymeria \x1b[94mGhost\x1b[0m\x1b[91m Lady\x1b[0m") == "Nymeria Ghost Lady"
func StripEscape(str string) string {
	var out strings.Builder
	out.Grow(RuneWidthWithoutEscSequences(str))

	isEscSeq := false
	for _, sChr := range str {
		if sChr == EscapeStartRune {
			isEscSeq = true
		}
		if !isEscSeq {
			out.WriteRune(sChr)
		}
		if isEscSeq && sChr == EscapeStopRune {
			isEscSeq = false
		}
	}
	return out.String()
}