2016-08-18 18:27:42 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"sort"
|
2016-09-29 13:40:22 +00:00
|
|
|
"unicode"
|
2016-08-18 18:27:42 +00:00
|
|
|
|
2016-10-24 00:44:56 +00:00
|
|
|
"github.com/junegunn/fzf/src/tui"
|
2016-08-18 18:27:42 +00:00
|
|
|
"github.com/junegunn/fzf/src/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Offset holds two 32-bit integers denoting the offsets of a matched substring
|
|
|
|
type Offset [2]int32
|
|
|
|
|
|
|
|
type colorOffset struct {
|
|
|
|
offset [2]int32
|
2016-10-24 00:44:56 +00:00
|
|
|
color tui.ColorPair
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Result struct {
|
2017-07-17 18:10:49 +00:00
|
|
|
item *Item
|
|
|
|
points [4]uint16
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 18:10:49 +00:00
|
|
|
func buildResult(item *Item, offsets []Offset, score int) Result {
|
2016-08-18 18:27:42 +00:00
|
|
|
if len(offsets) > 1 {
|
|
|
|
sort.Sort(ByOrder(offsets))
|
|
|
|
}
|
|
|
|
|
2017-07-17 18:10:49 +00:00
|
|
|
result := Result{item: item}
|
2016-08-18 18:27:42 +00:00
|
|
|
numChars := item.text.Length()
|
2016-09-07 00:58:18 +00:00
|
|
|
minBegin := math.MaxUint16
|
2017-02-02 04:46:46 +00:00
|
|
|
minEnd := math.MaxUint16
|
2016-09-07 00:58:18 +00:00
|
|
|
maxEnd := 0
|
|
|
|
validOffsetFound := false
|
2016-08-18 18:27:42 +00:00
|
|
|
for _, offset := range offsets {
|
2016-09-07 00:58:18 +00:00
|
|
|
b, e := int(offset[0]), int(offset[1])
|
|
|
|
if b < e {
|
|
|
|
minBegin = util.Min(b, minBegin)
|
2017-02-02 04:46:46 +00:00
|
|
|
minEnd = util.Min(e, minEnd)
|
2016-09-07 00:58:18 +00:00
|
|
|
maxEnd = util.Max(e, maxEnd)
|
|
|
|
validOffsetFound = true
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, criterion := range sortCriteria {
|
2016-09-07 00:58:18 +00:00
|
|
|
val := uint16(math.MaxUint16)
|
2016-08-18 18:27:42 +00:00
|
|
|
switch criterion {
|
2016-09-07 00:58:18 +00:00
|
|
|
case byScore:
|
2016-08-18 18:27:42 +00:00
|
|
|
// Higher is better
|
2016-09-07 00:58:18 +00:00
|
|
|
val = math.MaxUint16 - util.AsUint16(score)
|
2022-08-02 04:44:55 +00:00
|
|
|
case byChunk:
|
2022-11-18 07:18:11 +00:00
|
|
|
if validOffsetFound {
|
|
|
|
b := minBegin
|
|
|
|
e := maxEnd
|
|
|
|
for ; b >= 1; b-- {
|
|
|
|
if unicode.IsSpace(item.text.Get(b - 1)) {
|
|
|
|
break
|
|
|
|
}
|
2022-08-02 04:44:55 +00:00
|
|
|
}
|
2022-11-18 07:18:11 +00:00
|
|
|
for ; e < numChars; e++ {
|
|
|
|
if unicode.IsSpace(item.text.Get(e)) {
|
|
|
|
break
|
|
|
|
}
|
2022-08-02 04:44:55 +00:00
|
|
|
}
|
2022-11-18 07:18:11 +00:00
|
|
|
val = util.AsUint16(e - b)
|
2022-08-02 04:44:55 +00:00
|
|
|
}
|
2016-08-18 18:27:42 +00:00
|
|
|
case byLength:
|
2017-07-16 14:31:19 +00:00
|
|
|
val = item.TrimLength()
|
2016-10-17 16:11:04 +00:00
|
|
|
case byBegin, byEnd:
|
2016-09-07 00:58:18 +00:00
|
|
|
if validOffsetFound {
|
|
|
|
whitePrefixLen := 0
|
|
|
|
for idx := 0; idx < numChars; idx++ {
|
|
|
|
r := item.text.Get(idx)
|
|
|
|
whitePrefixLen = idx
|
2016-09-29 13:40:22 +00:00
|
|
|
if idx == minBegin || !unicode.IsSpace(r) {
|
2016-09-07 00:58:18 +00:00
|
|
|
break
|
|
|
|
}
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
2016-10-17 16:11:04 +00:00
|
|
|
if criterion == byBegin {
|
2017-02-02 04:46:46 +00:00
|
|
|
val = util.AsUint16(minEnd - whitePrefixLen)
|
2016-10-17 16:11:04 +00:00
|
|
|
} else {
|
2023-12-26 14:40:20 +00:00
|
|
|
val = util.AsUint16(math.MaxUint16 - math.MaxUint16*(maxEnd-whitePrefixLen)/int(item.TrimLength()+1))
|
2016-10-17 16:11:04 +00:00
|
|
|
}
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-26 12:58:18 +00:00
|
|
|
result.points[3-idx] = val
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 18:10:49 +00:00
|
|
|
return result
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort criteria to use. Never changes once fzf is started.
|
|
|
|
var sortCriteria []criterion
|
|
|
|
|
|
|
|
// Index returns ordinal index of the Item
|
|
|
|
func (result *Result) Index() int32 {
|
2017-07-16 14:31:19 +00:00
|
|
|
return result.item.Index()
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 18:10:49 +00:00
|
|
|
func minRank() Result {
|
2017-08-14 16:10:41 +00:00
|
|
|
return Result{item: &minItem, points: [4]uint16{math.MaxUint16, 0, 0, 0}}
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 10:29:37 +00:00
|
|
|
func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, current bool) []colorOffset {
|
2016-08-18 18:27:42 +00:00
|
|
|
itemColors := result.item.Colors()
|
|
|
|
|
2020-10-25 10:29:37 +00:00
|
|
|
// No ANSI codes
|
2016-08-18 18:27:42 +00:00
|
|
|
if len(itemColors) == 0 {
|
|
|
|
var offsets []colorOffset
|
2016-08-19 16:46:54 +00:00
|
|
|
for _, off := range matchOffsets {
|
2020-10-25 10:29:37 +00:00
|
|
|
offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: colMatch})
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
return offsets
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find max column
|
|
|
|
var maxCol int32
|
2016-08-19 16:46:54 +00:00
|
|
|
for _, off := range matchOffsets {
|
2016-08-18 18:27:42 +00:00
|
|
|
if off[1] > maxCol {
|
|
|
|
maxCol = off[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, ansi := range itemColors {
|
|
|
|
if ansi.offset[1] > maxCol {
|
|
|
|
maxCol = ansi.offset[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 10:29:37 +00:00
|
|
|
cols := make([]int, maxCol)
|
2016-08-18 18:27:42 +00:00
|
|
|
for colorIndex, ansi := range itemColors {
|
|
|
|
for i := ansi.offset[0]; i < ansi.offset[1]; i++ {
|
2020-10-25 10:29:37 +00:00
|
|
|
cols[i] = colorIndex + 1 // 1-based index of itemColors
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 16:46:54 +00:00
|
|
|
for _, off := range matchOffsets {
|
2016-08-18 18:27:42 +00:00
|
|
|
for i := off[0]; i < off[1]; i++ {
|
2020-10-25 10:29:37 +00:00
|
|
|
// Negative of 1-based index of itemColors
|
|
|
|
// - The extra -1 means highlighted
|
2024-01-23 03:17:40 +00:00
|
|
|
if cols[i] >= 0 {
|
|
|
|
cols[i] = cols[i]*-1 - 1
|
|
|
|
}
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort.Sort(ByOrder(offsets))
|
|
|
|
|
|
|
|
// Merge offsets
|
|
|
|
// ------------ ---- -- ----
|
|
|
|
// ++++++++ ++++++++++
|
|
|
|
// --++++++++-- --++++++++++---
|
|
|
|
curr := 0
|
|
|
|
start := 0
|
2020-10-25 10:29:37 +00:00
|
|
|
ansiToColorPair := func(ansi ansiOffset, base tui.ColorPair) tui.ColorPair {
|
|
|
|
fg := ansi.color.fg
|
|
|
|
bg := ansi.color.bg
|
|
|
|
if fg == -1 {
|
|
|
|
if current {
|
|
|
|
fg = theme.Current.Color
|
|
|
|
} else {
|
|
|
|
fg = theme.Fg.Color
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bg == -1 {
|
|
|
|
if current {
|
|
|
|
bg = theme.DarkBg.Color
|
|
|
|
} else {
|
|
|
|
bg = theme.Bg.Color
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tui.NewColorPair(fg, bg, ansi.color.attr).MergeAttr(base)
|
|
|
|
}
|
2016-08-18 18:27:42 +00:00
|
|
|
var colors []colorOffset
|
|
|
|
add := func(idx int) {
|
|
|
|
if curr != 0 && idx > start {
|
2020-10-25 10:29:37 +00:00
|
|
|
if curr < 0 {
|
|
|
|
color := colMatch
|
|
|
|
if curr < -1 && theme.Colored {
|
|
|
|
origColor := ansiToColorPair(itemColors[-curr-2], colMatch)
|
2020-12-04 10:27:43 +00:00
|
|
|
// 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
|
2021-01-15 01:10:09 +00:00
|
|
|
// foreground color of colMatch is -1, we shouldn't only apply the
|
2020-12-04 10:27:43 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2020-10-25 10:29:37 +00:00
|
|
|
}
|
2016-08-18 18:27:42 +00:00
|
|
|
colors = append(colors, colorOffset{
|
2020-10-25 10:29:37 +00:00
|
|
|
offset: [2]int32{int32(start), int32(idx)}, color: color})
|
2016-08-18 18:27:42 +00:00
|
|
|
} else {
|
|
|
|
ansi := itemColors[curr-1]
|
|
|
|
colors = append(colors, colorOffset{
|
|
|
|
offset: [2]int32{int32(start), int32(idx)},
|
2020-10-25 10:29:37 +00:00
|
|
|
color: ansiToColorPair(ansi, colBase)})
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for idx, col := range cols {
|
|
|
|
if col != curr {
|
|
|
|
add(idx)
|
|
|
|
start = idx
|
|
|
|
curr = col
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add(int(maxCol))
|
|
|
|
return colors
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByOrder is for sorting substring offsets
|
|
|
|
type ByOrder []Offset
|
|
|
|
|
|
|
|
func (a ByOrder) Len() int {
|
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a ByOrder) Swap(i, j int) {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a ByOrder) Less(i, j int) bool {
|
|
|
|
ioff := a[i]
|
|
|
|
joff := a[j]
|
|
|
|
return (ioff[0] < joff[0]) || (ioff[0] == joff[0]) && (ioff[1] <= joff[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByRelevance is for sorting Items
|
2017-07-17 18:10:49 +00:00
|
|
|
type ByRelevance []Result
|
2016-08-18 18:27:42 +00:00
|
|
|
|
|
|
|
func (a ByRelevance) Len() int {
|
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a ByRelevance) Swap(i, j int) {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a ByRelevance) Less(i, j int) bool {
|
2017-07-17 18:10:49 +00:00
|
|
|
return compareRanks(a[i], a[j], false)
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ByRelevanceTac is for sorting Items
|
2017-07-17 18:10:49 +00:00
|
|
|
type ByRelevanceTac []Result
|
2016-08-18 18:27:42 +00:00
|
|
|
|
|
|
|
func (a ByRelevanceTac) Len() int {
|
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a ByRelevanceTac) Swap(i, j int) {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a ByRelevanceTac) Less(i, j int) bool {
|
2017-07-17 18:10:49 +00:00
|
|
|
return compareRanks(a[i], a[j], true)
|
2016-08-18 18:27:42 +00:00
|
|
|
}
|