diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2016-06-14 21:52:47 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2016-06-14 21:52:47 +0900 |
commit | 24e1fabf2e805ad28901618fafe5fd389b599b4e (patch) | |
tree | 81d4317090cc2d0c88e3d00de60c18bc467e0cfe /src/ansi.go | |
parent | c39c039e155c50bcf8fecc5956c4bdd3b5c6bea1 (diff) |
Do not process ANSI codes in --preview output at once
Close #598
Diffstat (limited to 'src/ansi.go')
-rw-r--r-- | src/ansi.go | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/ansi.go b/src/ansi.go index 876229f1..debba474 100644 --- a/src/ansi.go +++ b/src/ansi.go @@ -36,7 +36,7 @@ func init() { ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*[mK]") } -func extractColor(str string, state *ansiState) (string, []ansiOffset, *ansiState) { +func extractColor(str string, state *ansiState, proc func(string, *ansiState) bool) (string, []ansiOffset, *ansiState) { var offsets []ansiOffset var output bytes.Buffer @@ -46,7 +46,11 @@ func extractColor(str string, state *ansiState) (string, []ansiOffset, *ansiStat idx := 0 for _, offset := range ansiRegex.FindAllStringIndex(str, -1) { - output.WriteString(str[idx:offset[0]]) + prev := str[idx:offset[0]] + output.WriteString(prev) + if proc != nil && !proc(prev, state) { + break + } newState := interpretCode(str[offset[0]:offset[1]], state) if !newState.equals(state) { @@ -77,6 +81,9 @@ func extractColor(str string, state *ansiState) (string, []ansiOffset, *ansiStat (&offsets[len(offsets)-1]).offset[1] = int32(utf8.RuneCount(output.Bytes())) } } + if proc != nil { + proc(rest, state) + } return output.String(), offsets, state } |