Skip to content
Myr

Update ANSI processor to handle more VT-100 escape sequences

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

The updated regular expression should include not all but most of the frequently used ANSI sequences. Close #735.

authored by Junegunn Choi · committed by Junegunn Choi · parent a2beb15 · Browse files · 2 files+13 added−4 removed
This commit

State

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

Diff stats

2 files+13 added−4 removed

  • other:+13 added−4 removed

2 files+13 added−4 removed

modified:src/ansi_test.go+2 added-2 removed
24 unmodified lines
25 check := func(assertion func(ansiOffsets *[]ansiOffset, state *ansiState)) {26 output, ansiOffsets, newState := extractColor(src, state, nil)27 state = newState28 if output != "hello world" {29 t.Errorf("Invalid output: {}", output)29 t.Errorf("Invalid output: %s %s", output, []rune(output))30 }31 fmt.Println(src, ansiOffsets, clean)32 assertion(ansiOffsets, state)33 }
21 unmodified lines
55 assert((*offsets)[0], 0, 11, -1, -1, true)56 })5758 state = nil59 src = "\x1b[1mhello \x1b[mworld"59 src = "\x1b[1mhello \x1b[mw\x1b7o\x1b8r\x1b(Bl\x1b[2@d"60 check(func(offsets *[]ansiOffset, state *ansiState) {61 if len(*offsets) != 1 {62 t.Fail()63 }
95 unmodified lines
modified:src/ansi.go+11 added-2 removed
33 unmodified lines
3435var ansiRegex *regexp.Regexp3637func init() {38 /*39 References:40 - https://github.com/gnachman/iTerm241 - http://ascii-table.com/ansi-escape-sequences.php42 - http://ascii-table.com/ansi-escape-sequences-vt-100.php43 - http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html44 */45 // The following regular expression will include not all but most of the46 // frequently used ANSI sequences38 ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*.|[\x0e\x0f]")47 ansiRegex = regexp.MustCompile("\x1b[\\[()][0-9;]*[a-zA-Z@]|\x1b.|[\x08\x0e\x0f]")48}4950func extractColor(str string, state *ansiState, proc func(string, *ansiState) bool) (string, *[]ansiOffset, *ansiState) {51 var offsets []ansiOffset
56 unmodified lines
108 state = &ansiState{-1, -1, 0}109 } else {110 state = &ansiState{prevState.fg, prevState.bg, prevState.attr}111 }103 if ansiCode[0] != '\x1b' || ansiCode[len(ansiCode)-1] != 'm' {112 if ansiCode[0] != '\x1b' || ansiCode[1] != '[' || ansiCode[len(ansiCode)-1] != 'm' {113 return state114 }115116 ptr := &state.fg
66 unmodified lines