2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
2015-03-18 16:59:14 +00:00
|
|
|
import (
|
|
|
|
"github.com/junegunn/fzf/src/curses"
|
|
|
|
)
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Offset holds two 32-bit integers denoting the offsets of a matched substring
|
2015-01-08 17:37:08 +00:00
|
|
|
type Offset [2]int32
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2015-03-18 16:59:14 +00:00
|
|
|
type ColorOffset struct {
|
|
|
|
offset [2]int32
|
|
|
|
color int
|
|
|
|
bold bool
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Item represents each input line
|
2015-01-01 19:49:30 +00:00
|
|
|
type Item struct {
|
|
|
|
text *string
|
|
|
|
origText *string
|
2015-01-10 16:47:46 +00:00
|
|
|
transformed *Transformed
|
2015-01-11 14:49:12 +00:00
|
|
|
index uint32
|
2015-01-01 19:49:30 +00:00
|
|
|
offsets []Offset
|
2015-03-18 16:59:14 +00:00
|
|
|
colors []AnsiOffset
|
2015-01-01 19:49:30 +00:00
|
|
|
rank Rank
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Rank is used to sort the search result
|
2015-01-08 17:37:08 +00:00
|
|
|
type Rank struct {
|
|
|
|
matchlen uint16
|
|
|
|
strlen uint16
|
|
|
|
index uint32
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Rank calculates rank of the Item
|
2015-01-11 14:49:12 +00:00
|
|
|
func (i *Item) Rank(cache bool) Rank {
|
|
|
|
if cache && (i.rank.matchlen > 0 || i.rank.strlen > 0) {
|
2015-01-01 19:49:30 +00:00
|
|
|
return i.rank
|
|
|
|
}
|
|
|
|
matchlen := 0
|
|
|
|
prevEnd := 0
|
|
|
|
for _, offset := range i.offsets {
|
2015-01-08 17:37:08 +00:00
|
|
|
begin := int(offset[0])
|
|
|
|
end := int(offset[1])
|
2015-01-01 19:49:30 +00:00
|
|
|
if prevEnd > begin {
|
|
|
|
begin = prevEnd
|
|
|
|
}
|
|
|
|
if end > prevEnd {
|
|
|
|
prevEnd = end
|
|
|
|
}
|
|
|
|
if end > begin {
|
|
|
|
matchlen += end - begin
|
|
|
|
}
|
|
|
|
}
|
2015-01-11 14:49:12 +00:00
|
|
|
rank := Rank{uint16(matchlen), uint16(len(*i.text)), i.index}
|
|
|
|
if cache {
|
|
|
|
i.rank = rank
|
|
|
|
}
|
|
|
|
return rank
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// AsString returns the original string
|
|
|
|
func (i *Item) AsString() string {
|
2015-01-01 19:49:30 +00:00
|
|
|
if i.origText != nil {
|
2015-01-11 18:01:24 +00:00
|
|
|
return *i.origText
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-01-11 18:01:24 +00:00
|
|
|
return *i.text
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 16:59:14 +00:00
|
|
|
func (item *Item) ColorOffsets(color int, bold bool, current bool) []ColorOffset {
|
|
|
|
if len(item.colors) == 0 {
|
|
|
|
offsets := make([]ColorOffset, 0)
|
|
|
|
for _, off := range item.offsets {
|
|
|
|
offsets = append(offsets, ColorOffset{offset: off, color: color, bold: bold})
|
|
|
|
}
|
|
|
|
return offsets
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find max column
|
|
|
|
var maxCol int32 = 0
|
|
|
|
for _, off := range item.offsets {
|
|
|
|
if off[1] > maxCol {
|
|
|
|
maxCol = off[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, ansi := range item.colors {
|
|
|
|
if ansi.offset[1] > maxCol {
|
|
|
|
maxCol = ansi.offset[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cols := make([]int, maxCol)
|
|
|
|
|
|
|
|
for colorIndex, ansi := range item.colors {
|
|
|
|
for i := ansi.offset[0]; i < ansi.offset[1]; i++ {
|
|
|
|
cols[i] = colorIndex + 1 // XXX
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, off := range item.offsets {
|
|
|
|
for i := off[0]; i < off[1]; i++ {
|
|
|
|
cols[i] = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort.Sort(ByOrder(offsets))
|
|
|
|
|
|
|
|
// Merge offsets
|
|
|
|
// ------------ ---- -- ----
|
|
|
|
// ++++++++ ++++++++++
|
|
|
|
// --++++++++-- --++++++++++---
|
|
|
|
curr := 0
|
|
|
|
start := 0
|
|
|
|
offsets := make([]ColorOffset, 0)
|
|
|
|
add := func(idx int) {
|
|
|
|
if curr != 0 && idx > start {
|
|
|
|
if curr == -1 {
|
|
|
|
offsets = append(offsets, ColorOffset{
|
|
|
|
offset: Offset{int32(start), int32(idx)}, color: color, bold: bold})
|
|
|
|
} else {
|
|
|
|
ansi := item.colors[curr-1]
|
|
|
|
bg := ansi.color.bg
|
|
|
|
if current {
|
|
|
|
bg = int(curses.DarkBG)
|
|
|
|
}
|
|
|
|
offsets = append(offsets, ColorOffset{
|
|
|
|
offset: Offset{int32(start), int32(idx)},
|
|
|
|
color: curses.PairFor(ansi.color.fg, bg),
|
|
|
|
bold: ansi.color.bold || bold})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for idx, col := range cols {
|
|
|
|
if col != curr {
|
|
|
|
add(idx)
|
|
|
|
start = idx
|
|
|
|
curr = col
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add(int(maxCol))
|
|
|
|
return offsets
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// ByOrder is for sorting substring offsets
|
2015-01-01 19:49:30 +00:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// ByRelevance is for sorting Items
|
2015-01-01 19:49:30 +00:00
|
|
|
type ByRelevance []*Item
|
|
|
|
|
|
|
|
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 {
|
2015-01-11 14:49:12 +00:00
|
|
|
irank := a[i].Rank(true)
|
|
|
|
jrank := a[j].Rank(true)
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2015-02-25 16:42:15 +00:00
|
|
|
return compareRanks(irank, jrank, false)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 16:42:15 +00:00
|
|
|
// ByRelevanceTac is for sorting Items
|
|
|
|
type ByRelevanceTac []*Item
|
|
|
|
|
|
|
|
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 {
|
|
|
|
irank := a[i].Rank(true)
|
|
|
|
jrank := a[j].Rank(true)
|
|
|
|
|
|
|
|
return compareRanks(irank, jrank, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareRanks(irank Rank, jrank Rank, tac bool) bool {
|
2015-01-08 17:37:08 +00:00
|
|
|
if irank.matchlen < jrank.matchlen {
|
|
|
|
return true
|
|
|
|
} else if irank.matchlen > jrank.matchlen {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if irank.strlen < jrank.strlen {
|
|
|
|
return true
|
|
|
|
} else if irank.strlen > jrank.strlen {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-02-25 16:42:15 +00:00
|
|
|
return (irank.index <= jrank.index) != tac
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|