Add special 'strip' style attribute for stripping colors
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Test cases: fd --color always | fzf --ansi --delimiter / fd --color always | fzf --ansi --delimiter / --nth -1 --color fg:dim,nth:regular fd --color always | fzf --ansi --delimiter / --nth -1 --color fg:dim:strip,nth:regular fd --color always | fzf --ansi --delimiter / --nth -1 --color fg:dim:strip,nth:regular --raw fd --color always | fzf --ansi --delimiter / --nth -1 --color fg:dim:strip,nth:regular,hidden:strikethrough --raw fd --color always | fzf --ansi --delimiter / --nth -1 --color fg:dim:strip,nth:regular,hidden:strip:strikethrough --raw fd --color always | fzf --ansi --delimiter / --nth -1 --color fg:dim:strip,nth:regular,hidden:strip:dim:strikethrough --raw
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+40 added−23 removed
- other:+40 added−23 removed
Provenance
- Commit
91beacf- Tree
978a5cd- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
e6ad01f
7 files changed+40 added−23 removed
modified:man/man1/fzf.1+2 added-1 removed
323 unmodified lines324 \fB16 ~ 255 \fRANSI 256 colors325 \fB#rrggbb \fR24-bit colors326 327 .B ANSI ATTRIBUTES: (Only applies to foreground colors)328- \fBregular \fRClears previously set attributes; should precede the other ones328+ \fBregular \fRClear previously set attributes; should precede the other ones329+ \fBstrip \fRRemove colors330 \fBbold\fR331 \fBunderline\fR332 \fBreverse\fR333 \fBdim\fR1819 unmodified linesmodified:src/tui/tui.go+5 added
14 unmodified lines15 AttrRegular = Attr(1 << 8)16 AttrClear = Attr(1 << 9)17 BoldForce = Attr(1 << 10)18 FullBg = Attr(1 << 11)19+ Strip = Attr(1 << 12)20 )21 22 func (a Attr) Merge(b Attr) Attr {23 if b&AttrRegular > 0 {360 unmodified lines384 func (p ColorPair) IsFullBgMarker() bool {385 return p.attr&FullBg > 0386 }387 388+func (p ColorPair) ShouldStripColors() bool {389+ return p.attr&Strip > 0390+}391+392 func (p ColorPair) HasBg() bool {393 return p.attr&Reverse == 0 && p.bg != colDefault ||394 p.attr&Reverse > 0 && p.fg != colDefault395 }921 unmodified linesmodified:src/options.go+2 added
1364 unmodified lines1365 case "bold", "strong":1366 cattr.Attr |= tui.Bold1367 case "dim":1368 cattr.Attr |= tui.Dim1369+ case "strip":1370+ cattr.Attr |= tui.Strip1371 case "italic":1372 cattr.Attr |= tui.Italic1373 case "underline":1374 cattr.Attr |= tui.Underline2404 unmodified linesmodified:src/result_test.go+2 added-2 removed
129 unmodified lines130 {[2]int32{33, 40}, ansiState{4, 8, 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)134+ colors := item.colorOffsets(offsets, nil, tui.Dark256, colBase, colMatch, tui.AttrUndefined, false)135 assert := func(idx int, b int32, e int32, c tui.ColorPair) {136 o := colors[idx]137 if o.offset[0] != b || o.offset[1] != e || o.color != c {138 t.Error(o, b, e, c)18 unmodified lines157 colUnderline := tui.NewColorPair(-1, -1, tui.Underline)158 159 nthOffsets := []Offset{{37, 39}, {42, 45}}160 for _, attr := range []tui.Attr{tui.AttrRegular, tui.StrikeThrough} {161- colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, attr)161+ colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, attr, false)162 163 // [{[0 5] {1 5 0}} {[5 15] {1 5 8}} {[15 20] {1 5 0}}164 // {[22 25] {2 6 1}} {[25 27] {2 6 9}} {[27 30] {-1 -1 8}}165 // {[30 32] {3 7 8}} {[32 33] {-1 -1 8}} {[33 35] {4 8 9}}19 unmodified linesmodified:src/result.go+13 added-2 removed
121 unmodified lines122 func minRank() Result {123 return Result{item: &minItem, points: [4]uint16{math.MaxUint16, 0, 0, 0}}124 }125 126-func (result *Result) colorOffsets(matchOffsets []Offset, nthOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, attrNth tui.Attr) []colorOffset {126+func (result *Result) colorOffsets(matchOffsets []Offset, nthOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, attrNth tui.Attr, hidden bool) []colorOffset {127 itemColors := result.item.Colors()128 129 // No ANSI codes130 if len(itemColors) == 0 && len(nthOffsets) == 0 {62 unmodified lines193 ansiToColorPair := func(ansi ansiOffset, base tui.ColorPair) tui.ColorPair {194 if !theme.Colored {195 return tui.NewColorPair(-1, -1, ansi.color.attr).MergeAttr(base)196 }197+ // fd --color always | fzf --ansi --delimiter / --nth -1 --color fg:dim:strip,nth:regular198+ if base.ShouldStripColors() {199+ return base200+ }201 fg := ansi.color.fg202 bg := ansi.color.bg203 if fg == -1 {204 fg = colBase.Fg()49 unmodified lines254 base := colBase255 if curr.nth {256 base = base.WithAttr(attrNth)257 }258+ if hidden {259+ base = base.WithFg(theme.Hidden)260+ }261 color := ansiToColorPair(ansi, base)262 colors = append(colors, colorOffset{263 offset: [2]int32{int32(start), int32(idx)},264 color: color,265 match: false,266 url: ansi.color.url})267 } else {268+ color := colBase.WithAttr(attrNth)269+ if hidden {270+ color = color.WithFg(theme.Hidden)271+ }272 colors = append(colors, colorOffset{273 offset: [2]int32{int32(start), int32(idx)},263- color: colBase.WithAttr(attrNth),274+ color: color,275 match: false,276 url: nil})277 }278 }58 unmodified linesmodified:src/terminal.go+8 added-18 removed
1483 unmodified lines1484 var offsets []colorOffset1485 printFn := func(window tui.Window, limit int) {1486 if offsets == nil {1487 // tui.Col* are not initialized until renderer.Init()1488- offsets = result.colorOffsets(nil, nil, t.theme, *color, *color, t.nthAttr)1488+ offsets = result.colorOffsets(nil, nil, t.theme, *color, *color, t.nthAttr, false)1489 }1490 for limit > 0 {1491 if length > limit {1492 trimmedRunes, _ := t.trimRight(runes, limit)54 unmodified lines1547 preTask := func(markerClass) int {1548 return 11549 }1550 t.printHighlighted(1551- Result{item: item}, tui.ColPrompt, tui.ColPrompt, false, false, line, line, true, preTask, nil)1551+ Result{item: item}, tui.ColPrompt, tui.ColPrompt, false, false, false, line, line, true, preTask, nil)1552 })1553 t.wrap = wrap1554 }1555 _, promptLen := t.processTabs([]rune(trimmed), 0)1513 unmodified lines3069 text: util.ToChars([]byte(trimmed)),3070 colors: colors}3071 3072 t.printHighlighted(Result{item: item},3073- tui.ColFooter, tui.ColFooter, false, false, line, line, true,3073+ tui.ColFooter, tui.ColFooter, false, false, false, line, line, true,3074 func(markerClass) int {3075 t.footerWindow.Print(indent)3076 return indentSize3077 }, nil)63 unmodified lines3141 text: util.ToChars([]byte(trimmed)),3142 colors: colors}3143 3144 t.printHighlighted(Result{item: item},3145- tui.ColHeader, tui.ColHeader, false, false, line, line, true,3145+ tui.ColHeader, tui.ColHeader, false, false, false, line, line, true,3146 func(markerClass) int {3147 t.window.Print(indent)3148 return indentSize3149 }, nil)227 unmodified lines3377 t.window.CPrint(tui.ColCurrentSelectedEmpty, t.markerEmpty)3378 }3379 return indentSize3380 }3381- base := tui.ColCurrent3382- match := tui.ColCurrentMatch3383- if !matched {3384- base = base.WithFg(t.theme.Hidden)3385- match = match.WithFg(t.theme.Hidden)3386- }3387- finalLineNum = t.printHighlighted(result, base, match, true, true, line, maxLine, forceRedraw, preTask, postTask)3381+ finalLineNum = t.printHighlighted(result, tui.ColCurrent, tui.ColCurrentMatch, true, true, !matched, line, maxLine, forceRedraw, preTask, postTask)3382 } else {3383 preTask := func(marker markerClass) int {3384 w := t.window.Width() - t.pointerLen3385 if w < 0 {25 unmodified lines3411 if alt {3412 base = base.WithBg(altBg)3413 match = match.WithBg(altBg)3414 }3421- if !matched {3422- base = base.WithFg(t.theme.Hidden)3423- match = match.WithFg(t.theme.Hidden)3424- }3425- finalLineNum = t.printHighlighted(result, base, match, false, true, line, maxLine, forceRedraw, preTask, postTask)3415+ finalLineNum = t.printHighlighted(result, base, match, false, true, !matched, line, maxLine, forceRedraw, preTask, postTask)3416 }3417 for i := 0; i < t.gap && finalLineNum < maxLine; i++ {3418 finalLineNum++3419 t.renderGapLine(finalLineNum, barRange, i == t.gap-1)38 unmodified lines3458 func (t *Terminal) overflow(runes []rune, max int) bool {3459 return t.displayWidthWithLimit(runes, 0, max) > max3460 }3461 3472-func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMatch tui.ColorPair, current bool, match bool, lineNum int, maxLineNum int, forceRedraw bool, preTask func(markerClass) int, postTask func(int, int, bool, bool, tui.ColorPair)) int {3462+func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMatch tui.ColorPair, current bool, match bool, hidden bool, lineNum int, maxLineNum int, forceRedraw bool, preTask func(markerClass) int, postTask func(int, int, bool, bool, tui.ColorPair)) int {3463 var displayWidth int3464 item := result.item3465 matchOffsets := []Offset{}3466 var pos *[]int43 unmodified lines3510 }3511 sort.Sort(ByOrder(nthOffsets))3512 }3513 }3524- allOffsets := result.colorOffsets(charOffsets, nthOffsets, t.theme, colBase, colMatch, t.nthAttr)3514+ allOffsets := result.colorOffsets(charOffsets, nthOffsets, t.theme, colBase, colMatch, t.nthAttr, hidden)3515 3516 maxLines := 13517 if t.canSpanMultiLines() {3518 maxLines = maxLineNum - lineNum + 13712 unmodified linesmodified:CHANGELOG.md+8 added
48 unmodified lines49 fzf --raw --color hidden:red:strikethrough:dim50 fzf --raw --color hidden:red:strikethrough:dim:italic51 ```52 53+For colored input, dimming alone may not be enough, and you may prefer to remove54+colors entirely. For that case, a new special style attribute `strip` has been55+added.56+57+```sh58+fd --color always | fzf --ansi --raw --color hidden:dim:strip:strikethrough59+```60+61 #### Conditional actions for raw mode62 63 You may want to perform different actions depending on whether the current item64 is a match or not. For that, fzf now exports `$FZF_RAW` environment variable.2881 unmodified lines