Add underline style variants and underline color support
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Support double, curly, dotted, and dashed underline styles via --color (e.g. underline-curly) and ANSI passthrough (SGR 4:N, 58, 59) with --ansi. Close #4633 Close #4678 Thanks to @shtse8 for the test cases.
authored by Junegunn Choi · committed by Junegunn Choi · parent 49ab253 · Browse files · 11 files·+371 added−64 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+371 added−64 removed
- other:+371 added−64 removed
Provenance
- Commit
b56d614- Tree
6761da6- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
49ab253
11 files changed+371 added−64 removed
modified:man/man1/fzf.1+6 added-2 removed
324 unmodified lines325 \fB16 ~ 255 \fRANSI 256 colors326 \fB#rrggbb \fR24-bit colors327 328 .B ANSI ATTRIBUTES: (Only applies to foreground colors)329- \fBregular \fRClear previously set attributes; should precede the other ones330- \fBstrip \fRRemove colors329+ \fBregular \fRClear previously set attributes; should precede the other ones330+ \fBstrip \fRRemove colors331 \fBbold\fR332 \fBunderline\fR333+ \fBunderline-double\fR334+ \fBunderline-curly\fR335+ \fBunderline-dotted\fR336+ \fBunderline-dashed\fR337 \fBreverse\fR338 \fBdim\fR339 \fBitalic\fR340 \fBstrikethrough\fR1853 unmodified linesmodified:src/tui/light.go+37 added-7 removed
1321 unmodified lines1322 if (attr & Italic) > 0 {1323 codes = append(codes, "3")1324 }1325 if (attr & Underline) > 0 {1326+ switch attr.UnderlineStyle() {1327+ case UlStyleDouble:1328+ codes = append(codes, "4:2")1329+ case UlStyleCurly:1330+ codes = append(codes, "4:3")1331+ case UlStyleDotted:1332+ codes = append(codes, "4:4")1333+ case UlStyleDashed:1334+ codes = append(codes, "4:5")1335+ default:1326- codes = append(codes, "4")1336+ codes = append(codes, "4")1337+ }1338 }1339 if (attr & Blink) > 0 {1340 codes = append(codes, "5")1341 }29 unmodified lines1371 appendCode(bg, 10)1372 return codes1373 }1374 1375+func ulColorCode(c Color) string {1376+ if c == colDefault {1377+ return ""1378+ }1379+ if c.is24() {1380+ r := (c >> 16) & 0xff1381+ g := (c >> 8) & 0xff1382+ b := (c) & 0xff1383+ return fmt.Sprintf("58;2;%d;%d;%d", r, g, b)1384+ }1385+ if c >= 0 && c < 256 {1386+ return fmt.Sprintf("58;5;%d", c)1387+ }1388+ return ""1389+}1390+1364-func (w *LightWindow) csiColor(fg Color, bg Color, attr Attr) (bool, string) {1391+func (w *LightWindow) csiColor(fg Color, bg Color, ul Color, attr Attr) (bool, string) {1392 codes := append(attrCodes(attr), colorCodes(fg, bg)...)1393+ if ulCode := ulColorCode(ul); ulCode != "" {1394+ codes = append(codes, ulCode)1395+ }1396 code := w.csi(";" + strings.Join(codes, ";") + "m")1397 return len(codes) > 0, code1398 }1399 5 unmodified lines1405 return strings.ReplaceAll(str, "\x1b", "")1406 }1407 1408 func (w *LightWindow) CPrint(pair ColorPair, text string) {1379- _, code := w.csiColor(pair.Fg(), pair.Bg(), pair.Attr())1409+ _, code := w.csiColor(pair.Fg(), pair.Bg(), pair.Ul(), pair.Attr())1410 w.stderrInternal(cleanse(text), false, code)1411 w.csi("0m")1412 }1413 1414 func (w *LightWindow) cprint2(fg Color, bg Color, attr Attr, text string) {1385- hasColors, code := w.csiColor(fg, bg, attr)1415+ hasColors, code := w.csiColor(fg, bg, colDefault, attr)1416 if hasColors {1417 defer w.csi("0m")1418 }1419 w.stderrInternal(cleanse(text), false, code)81 unmodified lines1501 }1502 1503 func (w *LightWindow) setBg() string {1504 if w.bg != colDefault {1475- _, code := w.csiColor(colDefault, w.bg, AttrRegular)1505+ _, code := w.csiColor(colDefault, w.bg, colDefault, AttrRegular)1506 return code1507 }1508 // Should clear dim attribute after ␍ in the preview window1509 // e.g. printf "foo\rbar" | fzf --ansi --preview 'printf "foo\rbar"'13 unmodified lines1523 code := w.setBg()1524 return w.fill(text, code)1525 }1526 1497-func (w *LightWindow) CFill(fg Color, bg Color, attr Attr, text string) FillReturn {1527+func (w *LightWindow) CFill(fg Color, bg Color, ul Color, attr Attr, text string) FillReturn {1528 w.Move(w.posy, w.posx)1529 if fg == colDefault {1530 fg = w.fg1531 }1532 if bg == colDefault {1533 bg = w.bg1534 }1505- if hasColors, resetCode := w.csiColor(fg, bg, attr); hasColors {1535+ if hasColors, resetCode := w.csiColor(fg, bg, ul, attr); hasColors {1536 defer w.csi("0m")1537 return w.fill(text, resetCode)1538 }1539 return w.fill(text, w.setBg())36 unmodified linesmodified:src/tui/tcell.go+33 added-4 removed
823 unmodified lines824 }825 return style826 }827 828+func underlineStyleFromAttr(a Attr) tcell.UnderlineStyle {829+ switch a.UnderlineStyle() {830+ case UlStyleDouble:831+ return tcell.UnderlineStyleDouble832+ case UlStyleCurly:833+ return tcell.UnderlineStyleCurly834+ case UlStyleDotted:835+ return tcell.UnderlineStyleDotted836+ case UlStyleDashed:837+ return tcell.UnderlineStyleDashed838+ default:839+ return tcell.UnderlineStyleSolid840+ }841+}842+843 func (w *TcellWindow) printString(text string, pair ColorPair) {844 lx := 0845 a := pair.Attr()846 847 style := pair.style()848 if a&AttrClear == 0 {849 style = style.850 Reverse(a&Attr(tcell.AttrReverse) != 0).836- Underline(a&Attr(tcell.AttrUnderline) != 0).851 StrikeThrough(a&Attr(tcell.AttrStrikeThrough) != 0).852 Italic(a&Attr(tcell.AttrItalic) != 0).853 Blink(a&Attr(tcell.AttrBlink) != 0).854 Dim(a&Attr(tcell.AttrDim) != 0)855+ if a&Attr(tcell.AttrUnderline) != 0 {856+ style = style.Underline(underlineStyleFromAttr(a))857+ if pair.Ul() != colDefault {858+ style = style.Underline(asTcellColor(pair.Ul()))859+ }860+ } else {861+ style = style.Underline(false)862+ }863 }864 style = w.withUrl(style)865 866 gr := uniseg.NewGraphemes(text)41 unmodified lines908 Blink(a&Attr(tcell.AttrBlink) != 0).909 Bold(a&Attr(tcell.AttrBold) != 0 || a&BoldForce != 0).910 Dim(a&Attr(tcell.AttrDim) != 0).911 Reverse(a&Attr(tcell.AttrReverse) != 0).890- Underline(a&Attr(tcell.AttrUnderline) != 0).912 StrikeThrough(a&Attr(tcell.AttrStrikeThrough) != 0).913 Italic(a&Attr(tcell.AttrItalic) != 0)914+ if a&Attr(tcell.AttrUnderline) != 0 {915+ style = style.Underline(underlineStyleFromAttr(a))916+ if pair.Ul() != colDefault {917+ style = style.Underline(asTcellColor(pair.Ul()))918+ }919+ } else {920+ style = style.Underline(false)921+ }922 style = w.withUrl(style)923 924 gr := uniseg.NewGraphemes(text)925 Loop:69 unmodified lines995 w.uri = nil996 w.params = nil997 }998 970-func (w *TcellWindow) CFill(fg Color, bg Color, a Attr, str string) FillReturn {999+func (w *TcellWindow) CFill(fg Color, bg Color, ul Color, a Attr, str string) FillReturn {1000 if fg == colDefault {1001 fg = w.normal.Fg()1002 }1003 if bg == colDefault {1004 bg = w.normal.Bg()1005 }977- return w.fillString(str, NewColorPair(fg, bg, a))1006+ return w.fillString(str, NewColorPair(fg, bg, a).WithUl(ul))1007 }1008 1009 func (w *TcellWindow) DrawBorder() {1010 w.drawBorder(false)89 unmodified linesmodified:src/tui/tui.go+40 added-7 removed
15 unmodified lines16 AttrClear = Attr(1 << 9)17 BoldForce = Attr(1 << 10)18 FullBg = Attr(1 << 11)19 Strip = Attr(1 << 12)20+21+ // Underline style stored in bits 13-15 (3 bits, values 0-4)22+ // Only meaningful when the Underline attribute bit is also set.23+ // 0 = solid (default)24+ UnderlineStyleShift = 1325+ UnderlineStyleMask = Attr(0b111 << UnderlineStyleShift)26+ UlStyleDouble = Attr(0b001 << UnderlineStyleShift)27+ UlStyleCurly = Attr(0b010 << UnderlineStyleShift)28+ UlStyleDotted = Attr(0b011 << UnderlineStyleShift)29+ UlStyleDashed = Attr(0b100 << UnderlineStyleShift)30 )31 32+func (a Attr) UnderlineStyle() Attr {33+ return a & UnderlineStyleMask34+}35+36 func (a Attr) Merge(b Attr) Attr {37 if b&AttrRegular > 0 {38 // Only keep bold attribute set by the system39 return (b &^ AttrRegular) | (a & BoldForce)40 }41 28- return (a &^ AttrRegular) | b42+ merged := (a &^ AttrRegular) | b43+ // When b sets Underline, use b's underline style instead of OR'ing44+ if b&Underline > 0 {45+ merged = (merged &^ UnderlineStyleMask) | (b & UnderlineStyleMask)46+ }47+ return merged48 }49 50 // Types of user action51 //318 unmodified lines370 371 type ColorPair struct {372 fg Color373 bg Color374+ ul Color375 attr Attr376 }377 378 func HexToColor(rrggbb string) Color {3 unmodified lines382 return Color((1 << 24) + (r << 16) + (g << 8) + b)383 }384 385 func NewColorPair(fg Color, bg Color, attr Attr) ColorPair {366- return ColorPair{fg, bg, attr}386+ return ColorPair{fg, bg, colDefault, attr}387 }388 389 func NoColorPair() ColorPair {370- return ColorPair{-1, -1, 0}390+ return ColorPair{-1, -1, -1, 0}391 }392 393 func (p ColorPair) Fg() Color {394 return p.fg2 unmodified lines397 func (p ColorPair) Bg() Color {398 return p.bg399 }400 401+func (p ColorPair) Ul() Color {402+ return p.ul403+}404+405+func (p ColorPair) WithUl(ul Color) ColorPair {406+ dup := p407+ dup.ul = ul408+ return dup409+}410+411 func (p ColorPair) Attr() Attr {412 return p.attr413 }414 18 unmodified lines433 }434 if other.bg != except {435 dup.bg = other.bg436 }437+ if other.ul != except {438+ dup.ul = other.ul439+ }440 return dup441 }442 443 func (p ColorPair) WithAttr(attr Attr) ColorPair {3 unmodified lines447 }448 449 func (p ColorPair) WithFg(fg ColorAttr) ColorPair {450 dup := p418- fgPair := ColorPair{fg.Color, colUndefined, fg.Attr}451+ fgPair := ColorPair{fg.Color, colUndefined, colUndefined, fg.Attr}452 return dup.Merge(fgPair)453 }454 455 func (p ColorPair) WithBg(bg ColorAttr) ColorPair {456 dup := p424- bgPair := ColorPair{colUndefined, bg.Color, bg.Attr}457+ bgPair := ColorPair{colUndefined, bg.Color, colUndefined, bg.Attr}458 return dup.Merge(bgPair)459 }460 461 func (p ColorPair) MergeAttr(other ColorPair) ColorPair {353 unmodified lines815 MoveAndClear(y int, x int)816 Print(text string)817 CPrint(color ColorPair, text string)818 Fill(text string) FillReturn786- CFill(fg Color, bg Color, attr Attr, text string) FillReturn819+ CFill(fg Color, bg Color, ul Color, attr Attr, text string) FillReturn820 LinkBegin(uri string, params string)821 LinkEnd()822 Erase()823 EraseMaybe() bool479 unmodified lines1303 pair := func(fg, bg ColorAttr) ColorPair {1304 if fg.Color == colDefault && (fg.Attr&Reverse) > 0 {1305 bg.Color = colDefault1306 }1274- return ColorPair{fg.Color, bg.Color, fg.Attr}1307+ return ColorPair{fg.Color, bg.Color, colDefault, fg.Attr}1308 }1309 blank := theme.ListFg1310 blank.Attr = AttrRegular1311 51 unmodified linesmodified:src/ansi_test.go+123 added-17 removed
367 unmodified lines368 strings.ReplaceAll(state.ToString(), "\x1b[", "\\x1b["))369 }370 }371 assert("\x1b[m", nil, "")372- assert("\x1b[m", &ansiState{attr: tui.Blink, lbg: -1}, "")373- assert("\x1b[0m", &ansiState{fg: 4, bg: 4, lbg: -1}, "")374- assert("\x1b[;m", &ansiState{fg: 4, bg: 4, lbg: -1}, "")375- assert("\x1b[;;m", &ansiState{fg: 4, bg: 4, lbg: -1}, "")372+ assert("\x1b[m", &ansiState{attr: tui.Blink, ul: -1, lbg: -1}, "")373+ assert("\x1b[0m", &ansiState{fg: 4, bg: 4, ul: -1, lbg: -1}, "")374+ assert("\x1b[;m", &ansiState{fg: 4, bg: 4, ul: -1, lbg: -1}, "")375+ assert("\x1b[;;m", &ansiState{fg: 4, bg: 4, ul: -1, lbg: -1}, "")376 377 assert("\x1b[31m", nil, "\x1b[31;49m")378 assert("\x1b[41m", nil, "\x1b[39;41m")379 380 assert("\x1b[92m", nil, "\x1b[92;49m")381 assert("\x1b[102m", nil, "\x1b[39;102m")382 383- assert("\x1b[31m", &ansiState{fg: 4, bg: 4, lbg: -1}, "\x1b[31;44m")384- assert("\x1b[1;2;31m", &ansiState{fg: 2, bg: -1, attr: tui.Reverse, lbg: -1}, "\x1b[1;2;7;31;49m")383+ assert("\x1b[31m", &ansiState{fg: 4, bg: 4, ul: -1, lbg: -1}, "\x1b[31;44m")384+ assert("\x1b[1;2;31m", &ansiState{fg: 2, bg: -1, ul: -1, attr: tui.Reverse, lbg: -1}, "\x1b[1;2;7;31;49m")385 assert("\x1b[38;5;100;48;5;200m", nil, "\x1b[38;5;100;48;5;200m")386 assert("\x1b[38:5:100:48:5:200m", nil, "\x1b[38;5;100;48;5;200m")387 assert("\x1b[48;5;100;38;5;200m", nil, "\x1b[38;5;200;48;5;100m")388 assert("\x1b[48;5;100;38;2;10;20;30;1m", nil, "\x1b[1;38;2;10;20;30;48;5;100m")389 assert("\x1b[48;5;100;38;2;10;20;30;7m",390- &ansiState{attr: tui.Dim | tui.Italic, fg: 1, bg: 1},390+ &ansiState{attr: tui.Dim | tui.Italic, fg: 1, bg: 1, ul: -1},391 "\x1b[2;3;7;38;2;10;20;30;48;5;100m")392+393+ // Underline styles394+ assert("\x1b[4:3m", nil, "\x1b[4:3;39;49m")395+ assert("\x1b[4:2m", nil, "\x1b[4:2;39;49m")396+ assert("\x1b[4:4m", nil, "\x1b[4:4;39;49m")397+ assert("\x1b[4:5m", nil, "\x1b[4:5;39;49m")398+ assert("\x1b[4:1m", nil, "\x1b[4;39;49m")399+400+ // Underline color (256-color)401+ assert("\x1b[4;58;5;100m", nil, "\x1b[4;39;49;58;5;100m")402+ // Underline color (24-bit)403+ assert("\x1b[4;58;2;255;0;128m", nil, "\x1b[4;39;49;58;2;255;0;128m")404+ // Curly underline + underline color405+ assert("\x1b[4:3;58;2;255;0;0m", nil, "\x1b[4:3;39;49;58;2;255;0;0m")406+ // SGR 59 resets underline color407+ assert("\x1b[59m", &ansiState{fg: 1, bg: -1, ul: 100, lbg: -1}, "\x1b[31;49m")408 }409 410 func TestParseAnsiCode(t *testing.T) {411 tests := []struct {412+ In string396- In, Exp string397- N int413+ Exp string414+ N int415+ Sep byte416 }{399- {"123", "", 123},400- {"1a", "", -1},401- {"1a;12", "12", -1},402- {"12;a", "a", 12},403- {"-2", "", -1},417+ {"123", "", 123, 0},418+ {"1a", "", -1, 0},419+ {"1a;12", "12", -1, ';'},420+ {"12;a", "a", 12, ';'},421+ {"-2", "", -1, 0},422+ // Colon sub-parameters: earliest separator wins (@shtse8)423+ {"4:3", "3", 4, ':'},424+ {"4:3;31", "3;31", 4, ':'},425+ {"38:2:255:0:0", "2:255:0:0", 38, ':'},426+ {"58:5:200", "5:200", 58, ':'},427+ // Semicolon before colon428+ {"4;38:2:0:0:0", "38:2:0:0:0", 4, ';'},429 }430 for _, x := range tests {406- n, s := parseAnsiCode(x.In)407- if n != x.N || s != x.Exp {408- t.Fatalf("%q: got: (%d %q) want: (%d %q)", x.In, n, s, x.N, x.Exp)431+ n, sep, s := parseAnsiCode(x.In)432+ if n != x.N || s != x.Exp || sep != x.Sep {433+ t.Fatalf("%q: got: (%d %q %q) want: (%d %q %q)", x.In, n, s, string(sep), x.N, x.Exp, string(x.Sep))434 }435 }436 }437 438+// Test cases adapted from @shtse8 (PR #4678)439+func TestInterpretCodeUnderlineStyles(t *testing.T) {440+ // 4:0 = no underline441+ state := interpretCode("\x1b[4:0m", nil)442+ if state.attr&tui.Underline != 0 {443+ t.Error("4:0 should not set underline")444+ }445+446+ // 4:1 = single underline447+ state = interpretCode("\x1b[4:1m", nil)448+ if state.attr&tui.Underline == 0 {449+ t.Error("4:1 should set underline")450+ }451+452+ // 4:3 = curly underline453+ state = interpretCode("\x1b[4:3m", nil)454+ if state.attr&tui.Underline == 0 {455+ t.Error("4:3 should set underline")456+ }457+ if state.attr.UnderlineStyle() != tui.UlStyleCurly {458+ t.Error("4:3 should set curly underline style")459+ }460+461+ // 4:3 should NOT set italic (3 is a sub-param, not SGR 3)462+ if state.attr&tui.Italic != 0 {463+ t.Error("4:3 should not set italic")464+ }465+466+ // 4:2;31 = double underline + red fg467+ state = interpretCode("\x1b[4:2;31m", nil)468+ if state.attr&tui.Underline == 0 {469+ t.Error("4:2;31 should set underline")470+ }471+ if state.fg != 1 {472+ t.Errorf("4:2;31 should set fg to red (1), got %d", state.fg)473+ }474+ if state.attr&tui.Dim != 0 {475+ t.Error("4:2;31 should not set dim")476+ }477+478+ // Plain 4 still works479+ state = interpretCode("\x1b[4m", nil)480+ if state.attr&tui.Underline == 0 {481+ t.Error("4 should set underline")482+ }483+484+ // 4;2 (semicolon) = underline + dim485+ state = interpretCode("\x1b[4;2m", nil)486+ if state.attr&tui.Underline == 0 {487+ t.Error("4;2 should set underline")488+ }489+ if state.attr&tui.Dim == 0 {490+ t.Error("4;2 should set dim")491+ }492+}493+494+// Test cases adapted from @shtse8 (PR #4678)495+func TestInterpretCodeUnderlineColor(t *testing.T) {496+ // 58:2:R:G:B should not affect fg or bg497+ state := interpretCode("\x1b[58:2:255:0:0m", nil)498+ if state.fg != -1 || state.bg != -1 {499+ t.Errorf("58:2:R:G:B should not affect fg/bg, got fg=%d bg=%d", state.fg, state.bg)500+ }501+502+ // 58:5:200 should not affect fg or bg503+ state = interpretCode("\x1b[58:5:200m", nil)504+ if state.fg != -1 || state.bg != -1 {505+ t.Errorf("58:5:N should not affect fg/bg, got fg=%d bg=%d", state.fg, state.bg)506+ }507+508+ // 58:2:R:G:B combined with 38:2:R:G:B should only set fg509+ state = interpretCode("\x1b[58:2:255:0:0;38:2:0:255:0m", nil)510+ expectedFg := tui.Color(1<<24 | 0<<16 | 255<<8 | 0)511+ if state.fg != expectedFg {512+ t.Errorf("expected fg=%d, got %d", expectedFg, state.fg)513+ }514+ if state.bg != -1 {515+ t.Errorf("bg should be -1, got %d", state.bg)516+ }517+}518+519 // kernel/bpf/preload/iterators/README520 const ansiBenchmarkString = "\x1b[38;5;81m\x1b[01;31m\x1b[Kkernel/\x1b[0m\x1b[38:5:81mbpf/" +521 "\x1b[0m\x1b[38:5:81mpreload/\x1b[0m\x1b[38;5;81miterators/" +522 "\x1b[0m\x1b[38:5:149mMakefile\x1b[m\x1b[K\x1b[0m"37 unmodified linesmodified:src/ansi.go+81 added-17 removed
20 unmodified lines21 22 type ansiState struct {23 fg tui.Color24 bg tui.Color25+ ul tui.Color26 attr tui.Attr27 lbg tui.Color28 url *url29 }30 31 func (s *ansiState) colored() bool {31- return s.fg != -1 || s.bg != -1 || s.attr > 0 || s.lbg >= 0 || s.url != nil32+ return s.fg != -1 || s.bg != -1 || s.ul != -1 || s.attr > 0 || s.lbg >= 0 || s.url != nil33 }34 35 func (s *ansiState) equals(t *ansiState) bool {36 if t == nil {37 return !s.colored()38 }38- return s.fg == t.fg && s.bg == t.bg && s.attr == t.attr && s.lbg == t.lbg && s.url == t.url39+ return s.fg == t.fg && s.bg == t.bg && s.ul == t.ul && s.attr == t.attr && s.lbg == t.lbg && s.url == t.url40 }41 42 func (s *ansiState) ToString() string {43 if !s.colored() {10 unmodified lines54 if s.attr&tui.Italic > 0 {55 ret += "3;"56 }57 if s.attr&tui.Underline > 0 {58+ switch s.attr.UnderlineStyle() {59+ case tui.UlStyleDouble:60+ ret += "4:2;"61+ case tui.UlStyleCurly:62+ ret += "4:3;"63+ case tui.UlStyleDotted:64+ ret += "4:4;"65+ case tui.UlStyleDashed:66+ ret += "4:5;"67+ default:57- ret += "4;"68+ ret += "4;"69+ }70 }71 if s.attr&tui.Blink > 0 {72 ret += "5;"73 }3 unmodified lines77 if s.attr&tui.StrikeThrough > 0 {78 ret += "9;"79 }80 ret += toAnsiString(s.fg, 30) + toAnsiString(s.bg, 40)81+ if s.ul != -1 {82+ ret += toAnsiStringUl(s.ul)83+ }84 85 ret = "\x1b[" + strings.TrimSuffix(ret, ";") + "m"86 if s.url != nil {87 ret = fmt.Sprintf("\x1b]8;%s;%s\x1b\\%s\x1b]8;;\x1b", s.url.params, s.url.uri, ret)88 }89 return ret90 }91 92+func toAnsiStringUl(color tui.Color) string {93+ col := int(color)94+ if col < 0 {95+ return ""96+ }97+ if col >= (1 << 24) {98+ r := strconv.Itoa((col >> 16) & 0xff)99+ g := strconv.Itoa((col >> 8) & 0xff)100+ b := strconv.Itoa(col & 0xff)101+ return "58;2;" + r + ";" + g + ";" + b + ";"102+ }103+ return "58;5;" + strconv.Itoa(col) + ";"104+}105+106 func toAnsiString(color tui.Color, offset int) string {107 col := int(color)108 ret := ""109 if col == -1 {256 unmodified lines366 }367 return trimmed, nil, state368 }369 341-func parseAnsiCode(s string) (int, string) {370+func parseAnsiCode(s string) (int, byte, string) {371 var remaining string343- var i int344- // Faster than strings.IndexAny(";:")345- i = strings.IndexByte(s, ';')346- if i < 0 {347- i = strings.IndexByte(s, ':')372+ var sep byte373+ // Find the first separator (either ; or :)374+ i := -1375+ for j := 0; j < len(s); j++ {376+ if s[j] == ';' || s[j] == ':' {377+ i = j378+ break379+ }380 }381 if i >= 0 {382+ sep = s[i]383 remaining = s[i+1:]384 s = s[:i]385 }386 3 unmodified lines390 code := 0391 for _, ch := range stringBytes(s) {392 ch -= '0'393 if ch > 9 {361- return -1, remaining394+ return -1, sep, remaining395 }396 code = code*10 + int(ch)397 }365- return code, remaining398+ return code, sep, remaining399 }400 368- return -1, remaining401+ return -1, sep, remaining402 }403 404 func interpretCode(ansiCode string, prevState *ansiState) ansiState {405 if ansiCode == "\n" {406 if prevState != nil {407 return *prevState408 }376- return ansiState{-1, -1, 0, -1, nil}409+ return ansiState{-1, -1, -1, 0, -1, nil}410 }411 412 var state ansiState413 if prevState == nil {381- state = ansiState{-1, -1, 0, -1, nil}414+ state = ansiState{-1, -1, -1, 0, -1, nil}415 } else {383- state = ansiState{prevState.fg, prevState.bg, prevState.attr, prevState.lbg, prevState.url}416+ state = ansiState{prevState.fg, prevState.bg, prevState.ul, prevState.attr, prevState.lbg, prevState.url}417 }418 if ansiCode[0] != '\x1b' || ansiCode[1] != '[' || ansiCode[len(ansiCode)-1] != 'm' {419 if prevState != nil && (strings.HasSuffix(ansiCode, "0K") || strings.HasSuffix(ansiCode, "[K")) {420 state.lbg = prevState.bg16 unmodified lines437 438 reset := func() {439 state.fg = -1440 state.bg = -1441+ state.ul = -1442 state.attr = 0443 }444 445 if len(ansiCode) <= 3 {7 unmodified lines453 454 count := 0455 for len(ansiCode) != 0 {456 var num int457+ var sep byte423- if num, ansiCode = parseAnsiCode(ansiCode); num != -1 {458+ if num, sep, ansiCode = parseAnsiCode(ansiCode); num != -1 {459 count++460 switch state256 {461 case 0:462 switch num {2 unmodified lines465 state256++466 case 48:467 ptr = &state.bg468 state256++469+ case 58:470+ ptr = &state.ul471+ state256++472 case 39:473 state.fg = -1474 case 49:475 state.bg = -1476+ case 59:477+ state.ul = -1478 case 1:479 state.attr = state.attr | tui.Bold480 case 2:481 state.attr = state.attr | tui.Dim482 case 3:483 state.attr = state.attr | tui.Italic484 case 4:485+ if sep == ':' {486+ // SGR 4:N — underline style sub-parameter487+ var subNum int488+ subNum, _, ansiCode = parseAnsiCode(ansiCode)489+ state.attr = state.attr &^ tui.UnderlineStyleMask490+ switch subNum {491+ case 0:492+ state.attr = state.attr &^ tui.Underline493+ case 1:445- state.attr = state.attr | tui.Underline494+ state.attr = state.attr | tui.Underline495+ case 2:496+ state.attr = state.attr | tui.Underline | tui.UlStyleDouble497+ case 3:498+ state.attr = state.attr | tui.Underline | tui.UlStyleCurly499+ case 4:500+ state.attr = state.attr | tui.Underline | tui.UlStyleDotted501+ case 5:502+ state.attr = state.attr | tui.Underline | tui.UlStyleDashed503+ default:504+ state.attr = state.attr | tui.Underline505+ }506+ } else {507+ state.attr = state.attr | tui.Underline508+ }509 case 5:510 state.attr = state.attr | tui.Blink511 case 7:512 state.attr = state.attr | tui.Reverse5 unmodified lines518 case 23: // tput rmso519 state.attr = state.attr &^ tui.Italic520 case 24: // tput rmul521 state.attr = state.attr &^ tui.Underline522+ state.attr = state.attr &^ tui.UnderlineStyleMask523 case 25:524 state.attr = state.attr &^ tui.Blink525 case 27:526 state.attr = state.attr &^ tui.Reverse51 unmodified linesmodified:src/options.go+8 added
1405 unmodified lines1406 case "italic":1407 cattr.Attr |= tui.Italic1408 case "underline":1409 cattr.Attr |= tui.Underline1410+ case "underline-double":1411+ cattr.Attr |= tui.Underline | tui.UlStyleDouble1412+ case "underline-curly":1413+ cattr.Attr |= tui.Underline | tui.UlStyleCurly1414+ case "underline-dotted":1415+ cattr.Attr |= tui.Underline | tui.UlStyleDotted1416+ case "underline-dashed":1417+ cattr.Attr |= tui.Underline | tui.UlStyleDashed1418 case "blink":1419 cattr.Attr |= tui.Blink1420 case "reverse":1421 cattr.Attr |= tui.Reverse2416 unmodified linesmodified:src/result_test.go+4 added-4 removed
122 unmodified lines123 offsets := []Offset{{5, 15}, {10, 12}, {25, 35}}124 item := Result{125 item: &Item{126 colors: &[]ansiOffset{127- {[2]int32{0, 20}, ansiState{1, 5, 0, -1, nil}},128- {[2]int32{22, 27}, ansiState{2, 6, tui.Bold, -1, nil}},129- {[2]int32{30, 32}, ansiState{3, 7, 0, -1, nil}},130- {[2]int32{33, 40}, ansiState{4, 8, tui.Bold, -1, nil}}}}}127+ {[2]int32{0, 20}, ansiState{1, 5, -1, 0, -1, nil}},128+ {[2]int32{22, 27}, ansiState{2, 6, -1, tui.Bold, -1, nil}},129+ {[2]int32{30, 32}, ansiState{3, 7, -1, 0, -1, nil}},130+ {[2]int32{33, 40}, ansiState{4, 8, -1, tui.Bold, -1, nil}}}}}131 132 colBase := tui.NewColorPair(89, 189, tui.AttrUndefined)133 colMatch := tui.NewColorPair(99, 199, tui.AttrUndefined)134 colors := item.colorOffsets(offsets, nil, tui.Dark256, colBase, colMatch, tui.AttrUndefined, false)50 unmodified linesmodified:src/result.go+1 added-1 removed
204 unmodified lines205 }206 if bg == -1 {207 bg = colBase.Bg()208 }209- return tui.NewColorPair(fg, bg, ansi.color.attr).MergeAttr(base)209+ return tui.NewColorPair(fg, bg, ansi.color.attr).WithUl(ansi.color.ul).MergeAttr(base)210 }211 var colors []colorOffset212 add := func(idx int) {213 if curr.fbg >= 0 {123 unmodified linesmodified:src/terminal.go+5 added-5 removed
1547 unmodified lines1548 // ------- // Do not apply ANSI attributes to the trailing whitespaces1549 // // unless the part has a non-default ANSI state1550 loc := whiteSuffix.FindStringIndex(trimmed)1551 if loc != nil {1552- blankState := ansiOffset{[2]int32{int32(loc[0]), int32(loc[1])}, ansiState{tui.ColPrompt.Fg(), tui.ColPrompt.Bg(), tui.AttrClear, -1, nil}}1552+ blankState := ansiOffset{[2]int32{int32(loc[0]), int32(loc[1])}, ansiState{tui.ColPrompt.Fg(), tui.ColPrompt.Bg(), -1, tui.AttrClear, -1, nil}}1553 if item.colors != nil {1554 lastColor := (*item.colors)[len(*item.colors)-1]1555 if lastColor.offset[1] < int32(loc[1]) {1556 blankState.offset[0] = lastColor.offset[1]2586 unmodified lines4143 if requiredLines > 0 && y+requiredLines > height {4144 top := true4145 for ; y < height; y++ {4146 t.pwindow.MoveAndClear(y, 0)4147- t.pwindow.CFill(tui.ColPreview.Fg(), tui.ColPreview.Bg(), tui.AttrRegular, t.makeImageBorder(maxWidth, top))4147+ t.pwindow.CFill(tui.ColPreview.Fg(), tui.ColPreview.Bg(), -1, tui.AttrRegular, t.makeImageBorder(maxWidth, top))4148 top = false4149 }4150 wireframe = true4151 t.previewed.filled = true56 unmodified lines4208 if width > prefixWidth {4209 prefixWidth = width4210 colored := ansi != nil && ansi.colored()4211 if t.theme.Colored && colored {4212- fillRet = t.pwindow.CFill(ansi.fg, ansi.bg, ansi.attr, str)4212+ fillRet = t.pwindow.CFill(ansi.fg, ansi.bg, ansi.ul, ansi.attr, str)4213 } else {4214 attr := tui.AttrRegular4215 if colored {4216 attr = ansi.attr4217 }4218- fillRet = t.pwindow.CFill(tui.ColPreview.Fg(), tui.ColPreview.Bg(), attr, str)4218+ fillRet = t.pwindow.CFill(tui.ColPreview.Fg(), tui.ColPreview.Bg(), -1, attr, str)4219 }4220 }4221 return !isTrimmed &&4222 (fillRet == tui.FillContinue || t.activePreviewOpts.wrap && fillRet == tui.FillNextLine)11 unmodified lines4234 if unchanged && lineNo == 0 {4235 break4236 }4237 if t.theme.Colored && lbg >= 0 {4238- fillRet = t.pwindow.CFill(-1, lbg, tui.AttrRegular,4238+ fillRet = t.pwindow.CFill(-1, lbg, -1, tui.AttrRegular,4239 strings.Repeat(" ", t.pwindow.Width()-t.pwindow.X())+"\n")4240 } else {4241 fillRet = t.pwindow.Fill("\n")4242 }3199 unmodified linesmodified:CHANGELOG.md+33 added
1 CHANGELOG2 =========3 4+0.68.05+------6+- Added support for underline style variants in `--color`:7+ `underline-double`, `underline-curly`, `underline-dotted`, `underline-dashed`8+ ```sh9+ fzf --color 'fg:underline-curly,current-fg:underline-dashed'10+ ```11+- Added support for underline styles (`4:N`) and underline colors (SGR 58/59)12+ ```sh13+ # In the list section14+ printf '\e[4:3;58;2;255;0;0mRed curly underline\e[0m\n' | fzf --ansi15+16+ # In the preview window17+ fzf --preview "printf '\e[4:3;58;2;255;0;0mRed curly underline\e[0m\n'"18+ ```19+- Added `alt-gutter` color option (#4602) (@hedgieinsocks)20+- Added fish completion support (#4605) (@lalvarezt)21+- zsh: Handle multi-line history selection (#4595) (@LangLangBart)22+- Bug fixes23+ - Fixed symlinks to directories being returned as files (#4676) (@skk64)24+ - Fixed SIGHUP signal handling (#4668) (@LangLangBart)25+ - Fixed preview process not killed on exit26+ - Fixed coloring of items with zero-width characters27+ - Fixed `track-current` unset after a combined movement action28+ - Fixed `--accept-nth` being ignored in filter mode (#4636) (@charemma)29+ - Fixed display width calculation with `maxWidth` (#4596) (@LangLangBart)30+ - Fixed clearing of the rest of the current line on start31+ - Fixed `x-api-key` header not required for GET requests32+ - Fixed key reading not cancelled when `execute` triggered via a server request (#4653)33+ - Fixed rebind of readline command `redraw-current-line` (#4635) (@jameslazo)34+ - Fixed `fzf-tmux` `TERM` quoting and added `mktemp` usage (#4664) (@Goofygiraffe06)35+ - Do not allow very long queries in `FuzzyMatchV2`36+37 0.67.038 ------39 - Added `--freeze-left=N` option to keep the leftmost N columns always visible.40 ```sh3112 unmodified lines