Change how hl:-1 or hl+:-1 is applied to text with background color

This commit is contained in:
Junegunn Choi 2020-12-04 19:27:43 +09:00
parent 052d17e66a
commit d2af3ff98d
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 22 additions and 1 deletions

View File

@ -160,8 +160,20 @@ func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme,
color := colMatch color := colMatch
if curr < -1 && theme.Colored { if curr < -1 && theme.Colored {
origColor := ansiToColorPair(itemColors[-curr-2], colMatch) origColor := ansiToColorPair(itemColors[-curr-2], colMatch)
// hl or hl+ only sets the foreground color, so colMatch is the
// combination of either [hl and bg] or [hl+ and bg+].
//
// If the original text already has background color, and the
// forground color of colMatch is -1, we shouldn't only apply the
// background color of colMatch.
// e.g. echo -e "\x1b[32;7mfoo\x1b[mbar" | fzf --ansi --color bg+:1,hl+:-1:underline
// echo -e "\x1b[42mfoo\x1b[mbar" | fzf --ansi --color bg+:1,hl+:-1:underline
if color.Fg().IsDefault() && origColor.HasBg() {
color = origColor
} else {
color = origColor.MergeNonDefault(color) color = origColor.MergeNonDefault(color)
} }
}
colors = append(colors, colorOffset{ colors = append(colors, colorOffset{
offset: [2]int32{int32(start), int32(idx)}, color: color}) offset: [2]int32{int32(start), int32(idx)}, color: color})
} else { } else {

View File

@ -124,6 +124,10 @@ const (
type Color int32 type Color int32
func (c Color) IsDefault() bool {
return c == colDefault
}
func (c Color) is24() bool { func (c Color) is24() bool {
return c > 0 && (c&(1<<24)) > 0 return c > 0 && (c&(1<<24)) > 0
} }
@ -190,6 +194,11 @@ func (p ColorPair) Attr() Attr {
return p.attr return p.attr
} }
func (p ColorPair) HasBg() bool {
return p.attr&Reverse == 0 && p.bg != colDefault ||
p.attr&Reverse > 0 && p.fg != colDefault
}
func (p ColorPair) merge(other ColorPair, except Color) ColorPair { func (p ColorPair) merge(other ColorPair, except Color) ColorPair {
dup := p dup := p
dup.attr = dup.attr.Merge(other.attr) dup.attr = dup.attr.Merge(other.attr)