diff --git a/src/ansi.go b/src/ansi.go index b7d3a9e..427f7f4 100644 --- a/src/ansi.go +++ b/src/ansi.go @@ -35,7 +35,16 @@ func (s *ansiState) equals(t *ansiState) bool { var ansiRegex *regexp.Regexp func init() { - ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*.|[\x0e\x0f]") + /* + References: + - https://github.com/gnachman/iTerm2 + - http://ascii-table.com/ansi-escape-sequences.php + - http://ascii-table.com/ansi-escape-sequences-vt-100.php + - http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html + */ + // The following regular expression will include not all but most of the + // frequently used ANSI sequences + ansiRegex = regexp.MustCompile("\x1b[\\[()][0-9;]*[a-zA-Z@]|\x1b.|[\x08\x0e\x0f]") } func extractColor(str string, state *ansiState, proc func(string, *ansiState) bool) (string, *[]ansiOffset, *ansiState) { @@ -100,7 +109,7 @@ func interpretCode(ansiCode string, prevState *ansiState) *ansiState { } else { state = &ansiState{prevState.fg, prevState.bg, prevState.attr} } - if ansiCode[0] != '\x1b' || ansiCode[len(ansiCode)-1] != 'm' { + if ansiCode[0] != '\x1b' || ansiCode[1] != '[' || ansiCode[len(ansiCode)-1] != 'm' { return state } diff --git a/src/ansi_test.go b/src/ansi_test.go index 3afcc6c..a536677 100644 --- a/src/ansi_test.go +++ b/src/ansi_test.go @@ -26,7 +26,7 @@ func TestExtractColor(t *testing.T) { output, ansiOffsets, newState := extractColor(src, state, nil) state = newState if output != "hello world" { - t.Errorf("Invalid output: {}", output) + t.Errorf("Invalid output: %s %s", output, []rune(output)) } fmt.Println(src, ansiOffsets, clean) assertion(ansiOffsets, state) @@ -56,7 +56,7 @@ func TestExtractColor(t *testing.T) { }) state = nil - src = "\x1b[1mhello \x1b[mworld" + src = "\x1b[1mhello \x1b[mw\x1b7o\x1b8r\x1b(Bl\x1b[2@d" check(func(offsets *[]ansiOffset, state *ansiState) { if len(*offsets) != 1 { t.Fail()