Skip to content
Myr

Do not process ANSI codes in --preview output at once

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Close #598

authored by Junegunn Choi · committed by Junegunn Choi · parent c39c039 · Browse files · 5 files+19 added−24 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+19 added−24 removed

  • other:+19 added−24 removed

5 files+19 added−24 removed

modified:src/ansi_test.go+1 added-1 removed
15 unmodified lines
16 src := "hello world"17 var state *ansiState18 clean := "\x1b[0m"19 check := func(assertion func(ansiOffsets []ansiOffset, state *ansiState)) {20 output, ansiOffsets, newState := extractColor(src, state)20 output, ansiOffsets, newState := extractColor(src, state, nil)21 state = newState22 if output != "hello world" {23 t.Errorf("Invalid output: {}", output)24 }
128 unmodified lines
modified:src/ansi.go+9 added-2 removed
34 unmodified lines
35func init() {36 ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*[mK]")37}3839func extractColor(str string, state *ansiState) (string, []ansiOffset, *ansiState) {39func extractColor(str string, state *ansiState, proc func(string, *ansiState) bool) (string, []ansiOffset, *ansiState) {40 var offsets []ansiOffset41 var output bytes.Buffer4243 if state != nil {
1 unmodified line
45 }4647 idx := 048 for _, offset := range ansiRegex.FindAllStringIndex(str, -1) {49 prev := str[idx:offset[0]]49 output.WriteString(str[idx:offset[0]])50 output.WriteString(prev)51 if proc != nil && !proc(prev, state) {52 break53 }54 newState := interpretCode(str[offset[0]:offset[1]], state)5556 if !newState.equals(state) {57 if state != nil {
22 unmodified lines
80 // Update last offset81 (&offsets[len(offsets)-1]).offset[1] = int32(utf8.RuneCount(output.Bytes()))82 }83 }84 if proc != nil {85 proc(rest, state)86 }87 return output.String(), offsets, state88}8990func interpretCode(ansiCode string, prevState *ansiState) *ansiState {
66 unmodified lines
modified:src/core.go+2 added-2 removed
71 unmodified lines
72 if opts.Ansi {73 if opts.Theme != nil {74 var state *ansiState75 ansiProcessor = func(data []byte) ([]rune, []ansiOffset) {76 trimmed, offsets, newState := extractColor(string(data), state)76 trimmed, offsets, newState := extractColor(string(data), state, nil)77 state = newState78 return []rune(trimmed), offsets79 }80 } else {81 // When color is disabled but ansi option is given,82 // we simply strip out ANSI codes from the input83 ansiProcessor = func(data []byte) ([]rune, []ansiOffset) {84 trimmed, _, _ := extractColor(string(data), nil)84 trimmed, _, _ := extractColor(string(data), nil, nil)85 return []rune(trimmed), nil86 }87 }88 ansiProcessorRunes = func(data []rune) ([]rune, []ansiOffset) {
203 unmodified lines
modified:src/item.go+1 added-1 removed
126 unmodified lines
127// StringPtr returns the pointer to the original string128func (item *Item) StringPtr(stripAnsi bool) *string {129 if item.origText != nil {130 if stripAnsi {131 trimmed, _, _ := extractColor(string(*item.origText), nil)131 trimmed, _, _ := extractColor(string(*item.origText), nil, nil)132 return &trimmed133 }134 orig := string(*item.origText)135 return &orig
156 unmodified lines
modified:src/terminal.go+6 added-18 removed
554 unmodified lines
555 }556 if line >= max {557 continue558 }559 trimmed, colors, newState := extractColor(lineStr, state)559 trimmed, colors, newState := extractColor(lineStr, state, nil)560 state = newState561 item := &Item{562 text: []rune(trimmed),563 colors: colors,
165 unmodified lines
729 }730}731732func (t *Terminal) printPreview() {733 trimmed, ansiOffsets, _ := extractColor(t.previewTxt, nil)734 var index int32733 t.pwindow.Erase()736 for _, o := range ansiOffsets {737 b := o.offset[0]738 e := o.offset[1]739 if b > index {740 if !t.pwindow.Fill(trimmed[index:b]) {741 return742 }734 extractColor(t.previewTxt, nil, func(str string, ansi *ansiState) bool {735 if ansi != nil && ansi.colored() {736 return t.pwindow.CFill(str, ansi.fg, ansi.bg, ansi.bold)737 }744 if !t.pwindow.CFill(trimmed[b:e], o.color.fg, o.color.bg, o.color.bold) {745 return746 }747 index = e748 }749 if int(index) < len(trimmed) {750 t.pwindow.Fill(trimmed[index:])751 }738 return t.pwindow.Fill(str)739 })740}741742func processTabs(runes []rune, prefixWidth int) (string, int) {743 var strbuf bytes.Buffer
631 unmodified lines