Preserve the original color of each token when using --with-nth with --ansi
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #1500
authored by Junegunn Choi · committed by Junegunn Choi · parent b7c6838 · Browse files · 4 files·+107 added−3 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+107 added−3 removed
- other:+107 added−3 removed
Provenance
- Commit
ef577a6- Tree
54b4cb9- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
b7c6838
4 files changed+107 added−3 removed
modified:src/util/chars.go+9 added
169 unmodified lines170 dest[idx] = rune(b)171 }172 return173 }174+175+func (chars *Chars) Wrap(prefix string, suffix string) {176+ if runes := chars.optionalRunes(); runes != nil {177+ runes = append(append([]rune(prefix), runes...), []rune(suffix)...)178+ chars.slice = *(*[]byte)(unsafe.Pointer(&runes))179+ } else {180+ chars.slice = append(append([]byte(prefix), chars.slice...), []byte(suffix)...)181+ }182+}modified:src/ansi_test.go+29 added
1 package fzf2 3 import (4 "fmt"5+ "strings"6 "testing"7 8 "github.com/junegunn/fzf/src/tui"9 )146 unmodified lines156 assert((*offsets)[0], 0, 6, 2, -1, true)157 assert((*offsets)[1], 6, 11, 200, 100, false)158 })159 }160+161+func TestAnsiCodeStringConversion(t *testing.T) {162+ assert := func(code string, prevState *ansiState, expected string) {163+ state := interpretCode(code, prevState)164+ if expected != state.ToString() {165+ t.Errorf("expected: %s, actual: %s",166+ strings.Replace(expected, "\x1b[", "\\x1b[", -1),167+ strings.Replace(state.ToString(), "\x1b[", "\\x1b[", -1))168+ }169+ }170+ assert("\x1b[m", nil, "\x1b[m")171+ assert("\x1b[m", &ansiState{attr: tui.Blink}, "\x1b[m")172+173+ assert("\x1b[31m", nil, "\x1b[31;49m")174+ assert("\x1b[41m", nil, "\x1b[39;41m")175+176+ assert("\x1b[92m", nil, "\x1b[92;49m")177+ assert("\x1b[102m", nil, "\x1b[39;102m")178+179+ assert("\x1b[31m", &ansiState{fg: 4, bg: 4}, "\x1b[31;44m")180+ assert("\x1b[1;2;31m", &ansiState{fg: 2, bg: -1, attr: tui.Reverse}, "\x1b[1;2;7;31;49m")181+ assert("\x1b[38;5;100;48;5;200m", nil, "\x1b[38;5;100;48;5;200m")182+ assert("\x1b[48;5;100;38;5;200m", nil, "\x1b[38;5;200;48;5;100m")183+ assert("\x1b[48;5;100;38;2;10;20;30;1m", nil, "\x1b[1;38;2;10;20;30;48;5;100m")184+ assert("\x1b[48;5;100;38;2;10;20;30;7m",185+ &ansiState{attr: tui.Dim | tui.Italic, fg: 1, bg: 1},186+ "\x1b[2;3;7;38;2;10;20;30;48;5;100m")187+}modified:src/ansi.go+50 added
1 package fzf2 3 import (4 "bytes"5+ "fmt"6 "regexp"7 "strconv"8 "strings"9 "unicode/utf8"22 unmodified lines32 }33 return s.fg == t.fg && s.bg == t.bg && s.attr == t.attr34 }35 36+func (s *ansiState) ToString() string {37+ if !s.colored() {38+ return "\x1b[m"39+ }40+41+ ret := ""42+ if s.attr&tui.Bold > 0 {43+ ret += "1;"44+ }45+ if s.attr&tui.Dim > 0 {46+ ret += "2;"47+ }48+ if s.attr&tui.Italic > 0 {49+ ret += "3;"50+ }51+ if s.attr&tui.Underline > 0 {52+ ret += "4;"53+ }54+ if s.attr&tui.Blink > 0 {55+ ret += "5;"56+ }57+ if s.attr&tui.Reverse > 0 {58+ ret += "7;"59+ }60+ ret += toAnsiString(s.fg, 30) + toAnsiString(s.bg, 40)61+62+ return "\x1b[" + strings.TrimSuffix(ret, ";") + "m"63+}64+65+func toAnsiString(color tui.Color, offset int) string {66+ col := int(color)67+ ret := ""68+ if col == -1 {69+ ret += strconv.Itoa(offset + 9)70+ } else if col < 8 {71+ ret += strconv.Itoa(offset + col)72+ } else if col < 16 {73+ ret += strconv.Itoa(offset - 30 + 90 + col - 8)74+ } else if col < 256 {75+ ret += fmt.Sprintf("%d;5;%d", offset+8, col)76+ } else if col >= (1 << 24) {77+ r := (col >> 16) & 0xff78+ g := (col >> 8) & 0xff79+ b := col & 0xff80+ ret += fmt.Sprintf("%d;2;%d;%d;%d", offset+8, r, g, b)81+ }82+ return ret + ";"83+}84+85 var ansiRegex *regexp.Regexp86 87 func init() {88 /*200 unmodified linesmodified:src/core.go+19 added-3 removed
61 unmodified lines62 // ANSI code processor63 ansiProcessor := func(data []byte) (util.Chars, *[]ansiOffset) {64 return util.ToChars(data), nil65 }66+67+ var lineAnsiState, prevLineAnsiState *ansiState68 if opts.Ansi {69 if opts.Theme != nil {68- var state *ansiState70 ansiProcessor = func(data []byte) (util.Chars, *[]ansiOffset) {71+ prevLineAnsiState = lineAnsiState70- trimmed, offsets, newState := extractColor(string(data), state, nil)71- state = newState72+ trimmed, offsets, newState := extractColor(string(data), lineAnsiState, nil)73+ lineAnsiState = newState74 return util.ToChars([]byte(trimmed)), offsets75 }76 } else {77 // When color is disabled but ansi option is given,23 unmodified lines101 })102 } else {103 chunkList = NewChunkList(func(item *Item, data []byte) bool {104 tokens := Tokenize(string(data), opts.Delimiter)105+ if opts.Ansi && len(tokens) > 1 {106+ var ansiState *ansiState107+ if prevLineAnsiState != nil {108+ ansiStateDup := *prevLineAnsiState109+ ansiState = &ansiStateDup110+ }111+ for _, token := range tokens {112+ prevAnsiState := ansiState113+ _, _, ansiState = extractColor(token.text.ToString(), ansiState, nil)114+ if prevAnsiState != nil {115+ token.text.Wrap(prevAnsiState.ToString(), "\x1b[m")116+ }117+ }118+ }119 trans := Transform(tokens, opts.WithNth)120 transformed := joinTokens(trans)121 if len(header) < opts.HeaderLines {122 header = append(header, transformed)181 unmodified lines