Skip to content
Myr

Support colon delimiter in ANSI escape sequences

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

# Both should work printf "\e[38;5;208mOption 1\e[m\nOption 2" | fzf --ansi printf "\e[38:5:208mOption 1\e[m\nOption 2" | fzf --ansi This change makes ANSI parsing slightly slower. cpu: Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz Before: BenchmarkNextAnsiEscapeSequence-12 992.22 MB/s BenchmarkExtractColor-12 174.35 MB/s After: BenchmarkNextAnsiEscapeSequence-12 925.05 MB/s BenchmarkExtractColor-12 163.33 MB/s Fix #2913

authored by Junegunn Choi · committed by Junegunn Choi · parent f4fd532 · Browse files · 3 files+39 added−18 removed
This commit

State

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

Diff stats

3 files+39 added−18 removed

  • other:+39 added−18 removed

3 files+39 added−18 removed

modified:src/ansi_test.go+5 added-4 removed
356 unmodified lines
357358 assert("\x1b[31m", &ansiState{fg: 4, bg: 4, lbg: -1}, "\x1b[31;44m")359 assert("\x1b[1;2;31m", &ansiState{fg: 2, bg: -1, attr: tui.Reverse, lbg: -1}, "\x1b[1;2;7;31;49m")360 assert("\x1b[38;5;100;48;5;200m", nil, "\x1b[38;5;100;48;5;200m")361 assert("\x1b[38:5:100:48:5:200m", nil, "\x1b[38;5;100;48;5;200m")362 assert("\x1b[48;5;100;38;5;200m", nil, "\x1b[38;5;200;48;5;100m")363 assert("\x1b[48;5;100;38;2;10;20;30;1m", nil, "\x1b[1;38;2;10;20;30;48;5;100m")364 assert("\x1b[48;5;100;38;2;10;20;30;7m",365 &ansiState{attr: tui.Dim | tui.Italic, fg: 1, bg: 1},
11 unmodified lines
377 {"12;a", "a", 12},378 {"-2", "", -1},379 }380 for _, x := range tests {380 n, s := parseAnsiCode(x.In)381 n, _, s := parseAnsiCode(x.In, 0)382 if n != x.N || s != x.Exp {383 t.Fatalf("%q: got: (%d %q) want: (%d %q)", x.In, n, s, x.N, x.Exp)384 }385 }386}387388// kernel/bpf/preload/iterators/README388const ansiBenchmarkString = "\x1b[38;5;81m\x1b[01;31m\x1b[Kkernel/\x1b[0m\x1b[38;5;81mbpf/" +389 "\x1b[0m\x1b[38;5;81mpreload/\x1b[0m\x1b[38;5;81miterators/" +390 "\x1b[0m\x1b[38;5;149mMakefile\x1b[m\x1b[K\x1b[0m"389const ansiBenchmarkString = "\x1b[38;5;81m\x1b[01;31m\x1b[Kkernel/\x1b[0m\x1b[38:5:81mbpf/" +390 "\x1b[0m\x1b[38:5:81mpreload/\x1b[0m\x1b[38;5;81miterators/" +391 "\x1b[0m\x1b[38:5:149mMakefile\x1b[m\x1b[K\x1b[0m"392393func BenchmarkNextAnsiEscapeSequence(b *testing.B) {394 b.SetBytes(int64(len(ansiBenchmarkString)))395 for i := 0; i < b.N; i++ {
33 unmodified lines
modified:src/ansi.go+26 added-14 removed
83 unmodified lines
84 return '\x20' <= c && c <= '\x7e'85}8687func matchOperatingSystemCommand(s string) int {88 // `\x1b][0-9];[[:print:]]+(?:\x1b\\\\|\x07)`88 // `\x1b][0-9][;:][[:print:]]+(?:\x1b\\\\|\x07)`89 // ^ match starting here90 //91 i := 5 // prefix matched in nextAnsiEscapeSequence()92 for ; i < len(s) && isPrint(s[i]); i++ {
9 unmodified lines
102 return -1103}104105func matchControlSequence(s string) int {106 // `\x1b[\\[()][0-9;?]*[a-zA-Z@]`107 // ^ match starting here106 // `\x1b[\\[()][0-9;:?]*[a-zA-Z@]`107 // ^ match starting here108 //109 i := 2 // prefix matched in nextAnsiEscapeSequence()110 for ; i < len(s) && (isNumeric(s[i]) || s[i] == ';' || s[i] == '?'); i++ {110 for ; i < len(s) && (isNumeric(s[i]) || s[i] == ';' || s[i] == ':' || s[i] == '?'); i++ {111 }112 if i < len(s) {113 c := s[i]114 if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '@' {
9 unmodified lines
124125// nextAnsiEscapeSequence returns the ANSI escape sequence and is equivalent to126// calling FindStringIndex() on the below regex (which was originally used):127//128// "(?:\x1b[\\[()][0-9;?]*[a-zA-Z@]|\x1b][0-9];[[:print:]]+(?:\x1b\\\\|\x07)|\x1b.|[\x0e\x0f]|.\x08)"128// "(?:\x1b[\\[()][0-9;:?]*[a-zA-Z@]|\x1b][0-9][;:][[:print:]]+(?:\x1b\\\\|\x07)|\x1b.|[\x0e\x0f]|.\x08)"129func nextAnsiEscapeSequence(s string) (int, int) {130 // fast check for ANSI escape sequences131 i := 0132 for ; i < len(s); i++ {
19 unmodified lines
152 _, n := utf8.DecodeLastRuneInString(s[:i])153 return i - n, i + 1154 }155 case '\x1b':156 // match: `\x1b[\\[()][0-9;?]*[a-zA-Z@]`156 // match: `\x1b[\\[()][0-9;:?]*[a-zA-Z@]`157 if i+2 < len(s) && isCtrlSeqStart(s[i+1]) {158 if j := matchControlSequence(s[i:]); j != -1 {159 return i, i + j160 }161 }162163 // match: `\x1b][0-9];[[:print:]]+(?:\x1b\\\\|\x07)`163 // match: `\x1b][0-9][;:][[:print:]]+(?:\x1b\\\\|\x07)`164 if i+5 < len(s) && s[i+1] == ']' && isNumeric(s[i+2]) &&165 s[i+3] == ';' && isPrint(s[i+4]) {165 (s[i+3] == ';' || s[i+3] == ':') && isPrint(s[i+4]) {166167 if j := matchOperatingSystemCommand(s[i:]); j != -1 {168 return i, i + j169 }
108 unmodified lines
278 }279 return trimmed, nil, state280}281282func parseAnsiCode(s string) (int, string) {282func parseAnsiCode(s string, delimiter byte) (int, byte, string) {283 var remaining string284 if i := strings.IndexByte(s, ';'); i >= 0 {284 i := -1285 if delimiter == 0 {286 // Faster than strings.IndexAny(";:")287 i = strings.IndexByte(s, ';')288 if i < 0 {289 i = strings.IndexByte(s, ':')290 }291 } else {292 i = strings.IndexByte(s, delimiter)293 }294 if i >= 0 {295 delimiter = s[i]296 remaining = s[i+1:]297 s = s[:i]298 }299
3 unmodified lines
303 code := 0304 for _, ch := range []byte(s) {305 ch -= '0'306 if ch > 9 {296 return -1, remaining307 return -1, delimiter, remaining308 }309 code = code*10 + int(ch)310 }300 return code, remaining311 return code, delimiter, remaining312 }313303 return -1, remaining314 return -1, delimiter, remaining315}316317func interpretCode(ansiCode string, prevState *ansiState) ansiState {318 var state ansiState
19 unmodified lines
338339 state256 := 0340 ptr := &state.fg341342 var delimiter byte = 0343 for len(ansiCode) != 0 {344 var num int333 if num, ansiCode = parseAnsiCode(ansiCode); num != -1 {345 if num, delimiter, ansiCode = parseAnsiCode(ansiCode, delimiter); num != -1 {346 switch state256 {347 case 0:348 switch num {349 case 38:
71 unmodified lines
modified:CHANGELOG.md+8 added
1CHANGELOG2=========340.32.25------6- ANSI color sequences with colon delimiters are now supported.7 ```sh8 printf "\e[38;5;208mOption 1\e[m\nOption 2" | fzf --ansi9 printf "\e[38:5:208mOption 1\e[m\nOption 2" | fzf --ansi10 ```11120.32.113------14- Fixed incorrect ordering of `--tiebreak=chunk`15- fzf-tmux will show fzf border instead of tmux popup border (requires tmux 3.3)
1282 unmodified lines