2016-08-18 14:27:42 -04:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"sort"
|
2016-09-29 09:40:22 -04:00
|
|
|
"unicode"
|
2016-08-18 14:27:42 -04:00
|
|
|
|
|
|
|
"github.com/junegunn/fzf/src/curses"
|
|
|
|
"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
|
|
|
|
color int
|
2016-09-28 11:54:27 -04:00
|
|
|
attr curses.Attr
|
2016-08-18 14:27:42 -04:00
|
|
|
index int32
|
|
|
|
}
|
|
|
|
|
|
|
|
type rank struct {
|
2016-09-06 20:58:18 -04:00
|
|
|
points [4]uint16
|
2016-08-19 12:46:54 -04:00
|
|
|
index int32
|
2016-08-18 14:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Result struct {
|
2016-08-19 12:46:54 -04:00
|
|
|
item *Item
|
|
|
|
rank rank
|
2016-08-18 14:27:42 -04:00
|
|
|
}
|
|
|
|
|
2016-09-06 20:58:18 -04:00
|
|
|
func buildResult(item *Item, offsets []Offset, score int, trimLen int) *Result {
|
2016-08-18 14:27:42 -04:00
|
|
|
if len(offsets) > 1 {
|
|
|
|
sort.Sort(ByOrder(offsets))
|
|
|
|
}
|
|
|
|
|
2016-08-19 12:46:54 -04:00
|
|
|
result := Result{item: item, rank: rank{index: item.index}}
|
2016-08-18 14:27:42 -04:00
|
|
|
numChars := item.text.Length()
|
2016-09-06 20:58:18 -04:00
|
|
|
minBegin := math.MaxUint16
|
|
|
|
maxEnd := 0
|
|
|
|
validOffsetFound := false
|
2016-08-18 14:27:42 -04:00
|
|
|
for _, offset := range offsets {
|
2016-09-06 20:58:18 -04:00
|
|
|
b, e := int(offset[0]), int(offset[1])
|
|
|
|
if b < e {
|
|
|
|
minBegin = util.Min(b, minBegin)
|
|
|
|
maxEnd = util.Max(e, maxEnd)
|
|
|
|
validOffsetFound = true
|
2016-08-18 14:27:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, criterion := range sortCriteria {
|
2016-09-06 20:58:18 -04:00
|
|
|
val := uint16(math.MaxUint16)
|
2016-08-18 14:27:42 -04:00
|
|
|
switch criterion {
|
2016-09-06 20:58:18 -04:00
|
|
|
case byScore:
|
2016-08-18 14:27:42 -04:00
|
|
|
// Higher is better
|
2016-09-06 20:58:18 -04:00
|
|
|
val = math.MaxUint16 - util.AsUint16(score)
|
2016-08-18 14:27:42 -04:00
|
|
|
case byLength:
|
|
|
|
// If offsets is empty, trimLen will be 0, but we don't care
|
|
|
|
val = util.AsUint16(trimLen)
|
2016-10-17 12:11:04 -04:00
|
|
|
case byBegin, byEnd:
|
2016-09-06 20:58:18 -04:00
|
|
|
if validOffsetFound {
|
|
|
|
whitePrefixLen := 0
|
|
|
|
for idx := 0; idx < numChars; idx++ {
|
|
|
|
r := item.text.Get(idx)
|
|
|
|
whitePrefixLen = idx
|
2016-09-29 09:40:22 -04:00
|
|
|
if idx == minBegin || !unicode.IsSpace(r) {
|
2016-09-06 20:58:18 -04:00
|
|
|
break
|
|
|
|
}
|
2016-08-18 14:27:42 -04:00
|
|
|
}
|
2016-10-17 12:11:04 -04:00
|
|
|
if criterion == byBegin {
|
|
|
|
val = util.AsUint16(minBegin - whitePrefixLen)
|
|
|
|
} else {
|
|
|
|
val = util.AsUint16(math.MaxUint16 - math.MaxUint16*(maxEnd-whitePrefixLen)/trimLen)
|
|
|
|
}
|
2016-08-18 14:27:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result.rank.points[idx] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return result.item.index
|
|
|
|
}
|
|
|
|
|
|
|
|
func minRank() rank {
|
2016-09-06 20:58:18 -04:00
|
|
|
return rank{index: 0, points: [4]uint16{math.MaxUint16, 0, 0, 0}}
|
2016-08-18 14:27:42 -04:00
|
|
|
}
|
|
|
|
|
2016-10-21 06:35:59 -04:00
|
|
|
func (result *Result) colorOffsets(matchOffsets []Offset, theme *curses.ColorTheme, color int, attr curses.Attr, current bool) []colorOffset {
|
2016-08-18 14:27:42 -04:00
|
|
|
itemColors := result.item.Colors()
|
|
|
|
|
2016-10-21 06:35:59 -04:00
|
|
|
// No ANSI code, or --color=no
|
2016-08-18 14:27:42 -04:00
|
|
|
if len(itemColors) == 0 {
|
|
|
|
var offsets []colorOffset
|
2016-08-19 12:46:54 -04:00
|
|
|
for _, off := range matchOffsets {
|
2016-09-28 11:54:27 -04:00
|
|
|
offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: color, attr: attr})
|
2016-08-18 14:27:42 -04:00
|
|
|
}
|
|
|
|
return offsets
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find max column
|
|
|
|
var maxCol int32
|
2016-08-19 12:46:54 -04:00
|
|
|
for _, off := range matchOffsets {
|
2016-08-18 14:27:42 -04:00
|
|
|
if off[1] > maxCol {
|
|
|
|
maxCol = off[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, ansi := range itemColors {
|
|
|
|
if ansi.offset[1] > maxCol {
|
|
|
|
maxCol = ansi.offset[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cols := make([]int, maxCol)
|
|
|
|
|
|
|
|
for colorIndex, ansi := range itemColors {
|
|
|
|
for i := ansi.offset[0]; i < ansi.offset[1]; i++ {
|
|
|
|
cols[i] = colorIndex + 1 // XXX
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 12:46:54 -04:00
|
|
|
for _, off := range matchOffsets {
|
2016-08-18 14:27:42 -04:00
|
|
|
for i := off[0]; i < off[1]; i++ {
|
|
|
|
cols[i] = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort.Sort(ByOrder(offsets))
|
|
|
|
|
|
|
|
// Merge offsets
|
|
|
|
// ------------ ---- -- ----
|
|
|
|
// ++++++++ ++++++++++
|
|
|
|
// --++++++++-- --++++++++++---
|
|
|
|
curr := 0
|
|
|
|
start := 0
|
|
|
|
var colors []colorOffset
|
|
|
|
add := func(idx int) {
|
|
|
|
if curr != 0 && idx > start {
|
|
|
|
if curr == -1 {
|
|
|
|
colors = append(colors, colorOffset{
|
2016-09-28 11:54:27 -04:00
|
|
|
offset: [2]int32{int32(start), int32(idx)}, color: color, attr: attr})
|
2016-08-18 14:27:42 -04:00
|
|
|
} else {
|
|
|
|
ansi := itemColors[curr-1]
|
|
|
|
fg := ansi.color.fg
|
|
|
|
if fg == -1 {
|
|
|
|
if current {
|
2016-10-21 06:35:59 -04:00
|
|
|
fg = int(theme.Current)
|
2016-08-18 14:27:42 -04:00
|
|
|
} else {
|
2016-10-21 06:35:59 -04:00
|
|
|
fg = int(theme.Fg)
|
2016-08-18 14:27:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bg := ansi.color.bg
|
|
|
|
if bg == -1 {
|
|
|
|
if current {
|
2016-10-21 06:35:59 -04:00
|
|
|
bg = int(theme.DarkBg)
|
2016-08-18 14:27:42 -04:00
|
|
|
} else {
|
2016-10-21 06:35:59 -04:00
|
|
|
bg = int(theme.Bg)
|
2016-08-18 14:27:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
colors = append(colors, colorOffset{
|
|
|
|
offset: [2]int32{int32(start), int32(idx)},
|
|
|
|
color: curses.PairFor(fg, bg),
|
2016-09-28 11:54:27 -04:00
|
|
|
attr: ansi.color.attr | attr})
|
2016-08-18 14:27:42 -04: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
|
|
|
|
type ByRelevance []*Result
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return compareRanks((*a[i]).rank, (*a[j]).rank, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByRelevanceTac is for sorting Items
|
|
|
|
type ByRelevanceTac []*Result
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return compareRanks((*a[i]).rank, (*a[j]).rank, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareRanks(irank rank, jrank rank, tac bool) bool {
|
2016-09-06 20:58:18 -04:00
|
|
|
for idx := 0; idx < 4; idx++ {
|
2016-08-18 14:27:42 -04:00
|
|
|
left := irank.points[idx]
|
|
|
|
right := jrank.points[idx]
|
|
|
|
if left < right {
|
|
|
|
return true
|
|
|
|
} else if left > right {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (irank.index <= jrank.index) != tac
|
|
|
|
}
|