Skip to content
Myr

result.go

This file

Path

src/result.go

231 lines · 5.0 KiB · Go

Last changed by

Add support for text styling using --colorJunegunn Choibefore adoption11841f6

History of this file

Take it

The walk stopped after 200 commits: lines older than a1bcdc2 point at it.

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 1–88why, one step back
1package fzf
23import (4 "math"5 "sort"6 "unicode"78 "github.com/junegunn/fzf/src/tui"9 "github.com/junegunn/fzf/src/util"10)1112// Offset holds two 32-bit integers denoting the offsets of a matched substring13type Offset [2]int321415type colorOffset struct {16 offset [2]int3217 color tui.ColorPair18}1920type Result struct {21 item *Item22 points [4]uint1623}2425func buildResult(item *Item, offsets []Offset, score int) Result {26 if len(offsets) > 1 {27 sort.Sort(ByOrder(offsets))28 }2930 result := Result{item: item}31 numChars := item.text.Length()32 minBegin := math.MaxUint1633 minEnd := math.MaxUint1634 maxEnd := 035 validOffsetFound := false36 for _, offset := range offsets {37 b, e := int(offset[0]), int(offset[1])38 if b < e {39 minBegin = util.Min(b, minBegin)40 minEnd = util.Min(e, minEnd)41 maxEnd = util.Max(e, maxEnd)42 validOffsetFound = true43 }44 }4546 for idx, criterion := range sortCriteria {47 val := uint16(math.MaxUint16)48 switch criterion {49 case byScore:50 // Higher is better51 val = math.MaxUint16 - util.AsUint16(score)52 case byLength:53 val = item.TrimLength()54 case byBegin, byEnd:55 if validOffsetFound {56 whitePrefixLen := 057 for idx := 0; idx < numChars; idx++ {58 r := item.text.Get(idx)59 whitePrefixLen = idx60 if idx == minBegin || !unicode.IsSpace(r) {61 break62 }63 }64 if criterion == byBegin {65 val = util.AsUint16(minEnd - whitePrefixLen)66 } else {67 val = util.AsUint16(math.MaxUint16 - math.MaxUint16*(maxEnd-whitePrefixLen)/int(item.TrimLength()))68 }69 }70 }71 result.points[3-idx] = val72 }7374 return result75}7677// Sort criteria to use. Never changes once fzf is started.78var sortCriteria []criterion7980// Index returns ordinal index of the Item81func (result *Result) Index() int32 {82 return result.item.Index()83}8485func minRank() Result {86 return Result{item: &minItem, points: [4]uint16{math.MaxUint16, 0, 0, 0}}87}88

Add support for text styling using --colorJunegunn Choibefore adoption11841f6

line 89why, one step back
89func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, current bool) []colorOffset {

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 90–91why, one step back
90 itemColors := result.item.Colors()
91

Add support for text styling using --colorJunegunn Choibefore adoption11841f6

line 92why, one step back
92 // No ANSI codes

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 93–95why, one step back
93 if len(itemColors) == 0 {
94 var offsets []colorOffset95 for _, off := range matchOffsets {

Add support for text styling using --colorJunegunn Choibefore adoption11841f6

line 96why, one step back
96 offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: colMatch})

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 97–113why, one step back
97 }
98 return offsets99 }100101 // Find max column102 var maxCol int32103 for _, off := range matchOffsets {104 if off[1] > maxCol {105 maxCol = off[1]106 }107 }108 for _, ansi := range itemColors {109 if ansi.offset[1] > maxCol {110 maxCol = ansi.offset[1]111 }112 }113

Add support for text styling using --colorJunegunn Choibefore adoption11841f6

line 114why, one step back
114 cols := make([]int, maxCol)

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 115–116why, one step back
115 for colorIndex, ansi := range itemColors {
116 for i := ansi.offset[0]; i < ansi.offset[1]; i++ {

Add support for text styling using --colorJunegunn Choibefore adoption11841f6

line 117why, one step back
117 cols[i] = colorIndex + 1 // 1-based index of itemColors

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 118–122why, one step back
118 }
119 }120121 for _, off := range matchOffsets {122 for i := off[0]; i < off[1]; i++ {

Add support for text styling using --colorJunegunn Choibefore adoption11841f6

lines 123–125why, one step back
123 // Negative of 1-based index of itemColors
124 // - The extra -1 means highlighted125 cols[i] = cols[i]*-1 - 1

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 126–136why, one step back
126 }
127 }128129 // sort.Sort(ByOrder(offsets))130131 // Merge offsets132 // ------------ ---- -- ----133 // ++++++++ ++++++++++134 // --++++++++-- --++++++++++---135 curr := 0136 start := 0

Add support for text styling using --colorJunegunn Choibefore adoption11841f6

lines 137–155why, one step back
137 ansiToColorPair := func(ansi ansiOffset, base tui.ColorPair) tui.ColorPair {
138 fg := ansi.color.fg139 bg := ansi.color.bg140 if fg == -1 {141 if current {142 fg = theme.Current.Color143 } else {144 fg = theme.Fg.Color145 }146 }147 if bg == -1 {148 if current {149 bg = theme.DarkBg.Color150 } else {151 bg = theme.Bg.Color152 }153 }154 return tui.NewColorPair(fg, bg, ansi.color.attr).MergeAttr(base)155 }

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 156–158why, one step back
156 var colors []colorOffset
157 add := func(idx int) {158 if curr != 0 && idx > start {

Add support for text styling using --colorJunegunn Choibefore adoption11841f6

lines 159–164why, one step back
159 if curr < 0 {
160 color := colMatch161 if curr < -1 && theme.Colored {162 origColor := ansiToColorPair(itemColors[-curr-2], colMatch)163 color = origColor.MergeNonDefault(color)164 }

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

line 165why, one step back
165 colors = append(colors, colorOffset{

Add support for text styling using --colorJunegunn Choibefore adoption11841f6

line 166why, one step back
166 offset: [2]int32{int32(start), int32(idx)}, color: color})

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 167–170why, one step back
167 } else {
168 ansi := itemColors[curr-1]169 colors = append(colors, colorOffset{170 offset: [2]int32{int32(start), int32(idx)},

Add support for text styling using --colorJunegunn Choibefore adoption11841f6

line 171why, one step back
171 color: ansiToColorPair(ansi, colBase)})

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 172–231why, one step back
172 }
173 }174 }175 for idx, col := range cols {176 if col != curr {177 add(idx)178 start = idx179 curr = col180 }181 }182 add(int(maxCol))183 return colors184}185186// ByOrder is for sorting substring offsets187type ByOrder []Offset188189func (a ByOrder) Len() int {190 return len(a)191}192193func (a ByOrder) Swap(i, j int) {194 a[i], a[j] = a[j], a[i]195}196197func (a ByOrder) Less(i, j int) bool {198 ioff := a[i]199 joff := a[j]200 return (ioff[0] < joff[0]) || (ioff[0] == joff[0]) && (ioff[1] <= joff[1])201}202203// ByRelevance is for sorting Items204type ByRelevance []Result205206func (a ByRelevance) Len() int {207 return len(a)208}209210func (a ByRelevance) Swap(i, j int) {211 a[i], a[j] = a[j], a[i]212}213214func (a ByRelevance) Less(i, j int) bool {215 return compareRanks(a[i], a[j], false)216}217218// ByRelevanceTac is for sorting Items219type ByRelevanceTac []Result220221func (a ByRelevanceTac) Len() int {222 return len(a)223}224225func (a ByRelevanceTac) Swap(i, j int) {226 a[i], a[j] = a[j], a[i]227}228229func (a ByRelevanceTac) Less(i, j int) bool {230 return compareRanks(a[i], a[j], true)231}