summaryrefslogtreecommitdiffstats
path: root/src/item.go
blob: f2f105ac50a807f71e8976c5890012639c7e97e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package fzf

import (
	"math"

	"github.com/junegunn/fzf/src/curses"
)

// Offset holds three 32-bit integers denoting the offsets of a matched substring
type Offset [3]int32

type colorOffset struct {
	offset [2]int32
	color  int
	bold   bool
}

// Item represents each input line
type Item struct {
	text        []rune
	origText    *[]rune
	transformed []Token
	index       uint32
	offsets     []Offset
	colors      []ansiOffset
	rank        Rank
}

// Rank is used to sort the search result
type Rank struct {
	matchlen uint16
	tiebreak uint16
	index    uint32
}

// Tiebreak criterion to use. Never changes once fzf is started.
var rankTiebreak tiebreak

// Rank calculates rank of the Item
func (item *Item) Rank(cache bool) Rank {
	if cache && (item.rank.matchlen > 0 || item.rank.tiebreak > 0) {
		return item.rank
	}
	matchlen := 0
	prevEnd := 0
	lenSum := 0
	minBegin := math.MaxUint16
	for _, offset := range item.offsets {
		begin := int(offset[0])
		end := int(offset[1])
		trimLen := int(offset[2])
		lenSum += trimLen
		if prevEnd > begin {
			begin = prevEnd
		}
		if end > prevEnd {
			prevEnd = end
		}
		if end > begin {
			if begin < minBegin {
				minBegin = begin
			}
			matchlen += end - begin
		}
	}
	var tiebreak uint16
	switch rankTiebreak {
	case byLength:
		// It is guaranteed that .transformed in not null in normal execution
		if item.transformed != nil {
			// If offsets is empty, lenSum will be 0, but we don't care
			tiebreak = uint16(lenSum)
		} else {
			tiebreak = uint16(len(item.text))
		}
	case byBegin:
		// We can't just look at item.offsets[0][0] because it can be an inverse term
		tiebreak = uint16(minBegin)
	case byEnd:
		if prevEnd > 0 {
			tiebreak = uint16(1 + len(item.text) - prevEnd)
		} else {
			// Empty offsets due to inverse terms.
			tiebreak = 1
		}
	case byIndex:
		tiebreak = 1
	}
	rank := Rank{uint16(matchlen), tiebreak, item.index}
	if cache {
		item.rank = rank
	}
	return rank
}

// AsString returns the original string
func (item *Item) AsString(stripAnsi bool) string {
	return *item.StringPtr(stripAnsi)
}

// StringPtr returns the pointer to the original string
func (item *Item) StringPtr(stripAnsi bool) *string {
	if item.origText != nil {
		if stripAnsi {
			trimmed, _, _ := extractColor(string(*item.origText), nil)
			return &trimmed
		}
		orig := string(*item.origText)
		return &orig
	}
	str := string(item.text)
	return &str
}

func (item *Item) colorOffsets(color int, bold bool, current bool) []colorOffset {
	if len(item.colors) == 0 {
		var offsets []colorOffset
		for _, off := range item.offsets {

			offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: color, bold: bold})
		}
		return offsets
	}

	// Find max column
	var maxCol int32
	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
	var offsets []colorOffset
	add := func(idx int) {
		if curr != 0 && idx > start {
			if curr == -1 {
				offsets = append(offsets, colorOffset{
					offset: [2]int32{int32(start), int32(idx)}, color: color, bold: bold})
			} else {
				ansi := item.colors[curr-1]
				fg := ansi.color.fg
				if fg == -1 {
					if current {
						fg = curses.CurrentFG
					} else {
						fg = curses.FG
					}
				}
				bg := ansi.color.bg
				if bg == -1 {
					if current {
						bg = curses.DarkBG
					} else {
						bg = curses.BG
					}
				}
				offsets = append(offsets, colorOffset{
					offset: [2]int32{int32(start), int32(idx)},
					color:  curses.PairFor(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
}

// 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 []*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 {
	irank := a[i].Rank(true)
	jrank := a[j].Rank(true)

	return compareRanks(irank, jrank, false)
}

// 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 {
	if irank.matchlen < jrank.matchlen {
		return true
	} else if irank