Fix ANSI processor to handle multi-line regions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent f71ea5f · Browse files · 4 files·+72 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+72 added−20 removed
- other:+72 added−20 removed
Provenance
- Commit
5e3cb3a- Tree
3205662- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
f71ea5f
4 files changed+72 added−20 removed
modified:src/ansi_test.go+58 added-13 removed
12 unmodified lines13 }14 }15 16 src := "hello world"17+ var state *ansiState18 clean := "\x1b[0m"18- check := func(assertion func(ansiOffsets []ansiOffset)) {19- output, ansiOffsets := extractColor(&src)19+ check := func(assertion func(ansiOffsets []ansiOffset, state *ansiState)) {20+ output, ansiOffsets, newState := extractColor(&src, state)21+ state = newState22 if *output != "hello world" {23 t.Errorf("Invalid output: {}", output)24 }25 fmt.Println(src, ansiOffsets, clean)24- assertion(ansiOffsets)26+ assertion(ansiOffsets, state)27 }28 27- check(func(offsets []ansiOffset) {29+ check(func(offsets []ansiOffset, state *ansiState) {30 if len(offsets) > 0 {31 t.Fail()32 }33 })34 35+ state = nil36 src = "\x1b[0mhello world"34- check(func(offsets []ansiOffset) {37+ check(func(offsets []ansiOffset, state *ansiState) {38 if len(offsets) > 0 {39 t.Fail()40 }41 })42 43+ state = nil44 src = "\x1b[1mhello world"41- check(func(offsets []ansiOffset) {45+ check(func(offsets []ansiOffset, state *ansiState) {46 if len(offsets) != 1 {47 t.Fail()48 }49 assert(offsets[0], 0, 11, -1, -1, true)50 })51 52+ state = nil53 src = "\x1b[1mhello \x1b[mworld"49- check(func(offsets []ansiOffset) {54+ check(func(offsets []ansiOffset, state *ansiState) {55 if len(offsets) != 1 {56 t.Fail()57 }58 assert(offsets[0], 0, 6, -1, -1, true)59 })60 61+ state = nil62 src = "\x1b[1mhello \x1b[Kworld"57- check(func(offsets []ansiOffset) {63+ check(func(offsets []ansiOffset, state *ansiState) {64 if len(offsets) != 1 {65 t.Fail()66 }67 assert(offsets[0], 0, 11, -1, -1, true)68 })69 70+ state = nil71 src = "hello \x1b[34;45;1mworld"65- check(func(offsets []ansiOffset) {72+ check(func(offsets []ansiOffset, state *ansiState) {73 if len(offsets) != 1 {74 t.Fail()75 }76 assert(offsets[0], 6, 11, 4, 5, true)77 })78 79+ state = nil80 src = "hello \x1b[34;45;1mwor\x1b[34;45;1mld"73- check(func(offsets []ansiOffset) {81+ check(func(offsets []ansiOffset, state *ansiState) {82 if len(offsets) != 1 {83 t.Fail()84 }85 assert(offsets[0], 6, 11, 4, 5, true)86 })87 88+ state = nil89 src = "hello \x1b[34;45;1mwor\x1b[0mld"81- check(func(offsets []ansiOffset) {90+ check(func(offsets []ansiOffset, state *ansiState) {91 if len(offsets) != 1 {92 t.Fail()93 }94 assert(offsets[0], 6, 9, 4, 5, true)95 })96 97+ state = nil98 src = "hello \x1b[34;48;5;233;1mwo\x1b[38;5;161mr\x1b[0ml\x1b[38;5;161md"89- check(func(offsets []ansiOffset) {99+ check(func(offsets []ansiOffset, state *ansiState) {100 if len(offsets) != 3 {101 t.Fail()102 }103 assert(offsets[0], 6, 8, 4, 233, true)1 unmodified line105 assert(offsets[2], 10, 11, 161, -1, false)106 })107 108 // {38,48};5;{38,48}109+ state = nil110 src = "hello \x1b[38;5;38;48;5;48;1mwor\x1b[38;5;48;48;5;38ml\x1b[0md"100- check(func(offsets []ansiOffset) {111+ check(func(offsets []ansiOffset, state *ansiState) {112 if len(offsets) != 2 {113 t.Fail()114 }115 assert(offsets[0], 6, 9, 38, 48, true)116 assert(offsets[1], 9, 10, 48, 38, true)117 })118+119+ src = "hello \x1b[32;1mworld"120+ check(func(offsets []ansiOffset, state *ansiState) {121+ if len(offsets) != 1 {122+ t.Fail()123+ }124+ if state.fg != 2 || state.bg != -1 || !state.bold {125+ t.Fail()126+ }127+ assert(offsets[0], 6, 11, 2, -1, true)128+ })129+130+ src = "hello world"131+ check(func(offsets []ansiOffset, state *ansiState) {132+ if len(offsets) != 1 {133+ t.Fail()134+ }135+ if state.fg != 2 || state.bg != -1 || !state.bold {136+ t.Fail()137+ }138+ assert(offsets[0], 0, 11, 2, -1, true)139+ })140+141+ src = "hello \x1b[0;38;5;200;48;5;100mworld"142+ check(func(offsets []ansiOffset, state *ansiState) {143+ if len(offsets) != 2 {144+ t.Fail()145+ }146+ if state.fg != 200 || state.bg != 100 || state.bold {147+ t.Fail()148+ }149+ assert(offsets[0], 0, 6, 2, -1, true)150+ assert(offsets[1], 6, 11, 200, 100, false)151+ })152 }modified:src/ansi.go+6 added-4 removed
34 unmodified lines35 func init() {36 ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*[mK]")37 }38 39-func extractColor(str *string) (*string, []ansiOffset) {39+func extractColor(str *string, state *ansiState) (*string, []ansiOffset, *ansiState) {40 var offsets []ansiOffset41-41 var output bytes.Buffer43- var state *ansiState42 43+ if state != nil {44+ offsets = append(offsets, ansiOffset{[2]int32{0, 0}, *state})45+ }46+47 idx := 048 for _, offset := range ansiRegex.FindAllStringIndex(*str, -1) {49 output.WriteString((*str)[idx:offset[0]])50 newState := interpretCode((*str)[offset[0]:offset[1]], state)26 unmodified lines77 (&offsets[len(offsets)-1]).offset[1] = int32(utf8.RuneCount(output.Bytes()))78 }79 }80 outputStr := output.String()79- return &outputStr, offsets81+ return &outputStr, offsets, state82 }83 84 func interpretCode(ansiCode string, prevState *ansiState) *ansiState {85 // State65 unmodified linesmodified:src/core.go+5 added-2 removed
67 unmodified lines68 return data, nil69 }70 if opts.Ansi {71 if opts.Theme != nil {72+ var state *ansiState73 ansiProcessor = func(data *string) (*string, []ansiOffset) {73- return extractColor(data)74+ trimmed, offsets, newState := extractColor(data, state)75+ state = newState76+ return trimmed, offsets77 }78 } else {79 // When color is disabled but ansi option is given,80 // we simply strip out ANSI codes from the input81 ansiProcessor = func(data *string) (*string, []ansiOffset) {79- trimmed, _ := extractColor(data)82+ trimmed, _, _ := extractColor(data, nil)83 return trimmed, nil84 }85 }86 }181 unmodified linesmodified:src/terminal.go+3 added-1 removed
376 unmodified lines377 if len(t.header) == 0 {378 return379 }380 max := C.MaxY()381+ var state *ansiState382 for idx, lineStr := range t.header {383 if !t.reverse {384 idx = len(t.header) - idx - 1385 }3 unmodified lines389 }390 if line >= max {391 break392 }392- trimmed, colors := extractColor(&lineStr)393+ trimmed, colors, newState := extractColor(&lineStr, state)394+ state = newState395 item := &Item{396 text: trimmed,397 index: 0,398 colors: colors,638 unmodified lines