package core import ( "regexp" "strings" ) // ansiCSI matches CSI sequences: ESC [ ... // Covers colors (SGR), cursor movement, erase, etc. var ansiCSI = regexp.MustCompile(`\x1b\[[0-9;?]*[ -/]*[@-~]`) // ansiOSC matches OSC sequences: ESC ] ... // Used for window titles, hyperlinks, etc. var ansiOSC = regexp.MustCompile(`\x1b\][^\x07\x1b]*(\x07|\x1b\\)`) // ansiEsc matches other two-character escape sequences: ESC // Covers ESC c (reset), ESC ( B, ESC ) 0, etc. var ansiEsc = regexp.MustCompile(`\x1b[@-Z\\-_]|\x1b[()][0-9A-Za-z]`) // StripANSI removes ANSI/VT100 terminal escape sequences from s and filters // non-printable control characters, preserving newlines (\n), tabs (\t) and // carriage returns (\r). func StripANSI(s string) string { s = ansiCSI.ReplaceAllString(s, "") s = ansiOSC.ReplaceAllString(s, "") s = ansiEsc.ReplaceAllString(s, "") return strings.Map(func(r rune) rune { // Preserve printable characters, \n (0x0A), \t (0x09), \r (0x0D). if r == '\n' || r == '\t' || r == '\r' { return r } // Drop C0 control characters (0x00-0x1F) and DEL (0x7F). if r < 0x20 || r == 0x7F { return -1 } return r }, s) }