Skip to content
Myr

Add new tiebreak: 'chunk'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Favors the line with shorter matched chunk. A chunk is a set of consecutive non-whitespace characters. Unlike the default `length`, this new scheme works well with tabular input. # length prefers item #1, because the whole line is shorter, # chunk prefers item #2, because the matched chunk ("foo") is shorter fzf --height=6 --header-lines=2 --tiebreak=chunk --reverse --query=fo << "EOF" N | Field1 | Field2 | Field3 - | ------ | ------ | ------ 1 | hello | foobar | baz 2 | world | foo | bazbaz EOF If the input does not contain any spaces, `chunk` is equivalent to `length`. But we're not going to set it as the default because it is computationally more expensive. Close #2285 Close #2537 - Not the exact solution to --tiebreak=length not taking --nth into account, but this should work. And the added benefit is that it works well even when --nth is not provided. - Adding a bonus point to the last character of a word didn't turn out great. The order of the result suddenly changes when you type in the last character in the word producing a jarring effect.

authored by Junegunn Choi · committed by Junegunn Choi · parent c3a7a24 · Browse files · 7 files+81 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+81 added−8 removed

  • tests:+14 added−0 removed
  • other:+67 added−8 removed

7 files+81 added−8 removed

modified:man/man1/fzf-tmux.1+1 added-1 removed
19 unmodified lines
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN22THE SOFTWARE.23..24.TH fzf-tmux 1 "Jul 2022" "fzf 0.31.0" "fzf-tmux - open fzf in tmux split pane"24.TH fzf-tmux 1 "Aug 2022" "fzf 0.32.0" "fzf-tmux - open fzf in tmux split pane"2526.SH NAME27fzf-tmux - open fzf in tmux split pane28
40 unmodified lines
modified:man/man1/fzf.1+3 added-1 removed
19 unmodified lines
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN22THE SOFTWARE.23..24.TH fzf 1 "Jul 2022" "fzf 0.31.0" "fzf - a command-line fuzzy finder"24.TH fzf 1 "Aug 2022" "fzf 0.32.0" "fzf - a command-line fuzzy finder"2526.SH NAME27fzf - a command-line fuzzy finder28
65 unmodified lines
9495.br96.BR length " Prefers line with shorter length"97.br98.BR chunk " Prefers line with shorter matched chunk (delimited by whitespaces)"99.br100.BR begin " Prefers line with matched substring closer to the beginning"101.br102.BR end " Prefers line with matched substring closer to the end"103.br
930 unmodified lines
modified:src/options.go+9 added-1 removed
33 unmodified lines
34 +s, --no-sort Do not sort the result35 --tac Reverse the order of the input36 --disabled Do not perform search37 --tiebreak=CRI[,..] Comma-separated list of sort criteria to apply38 when the scores are tied [length|begin|end|index]38 when the scores are tied [length|chunk|begin|end|index]39 (default: length)4041 Interface42 -m, --multi[=MAX] Enable multi-select with tab/shift-tab
81 unmodified lines
124type criterion int125126const (127 byScore criterion = iota128 byChunk129 byLength130 byBegin131 byEnd132)
478 unmodified lines
611612func parseTiebreak(str string) []criterion {613 criteria := []criterion{byScore}614 hasIndex := false615 hasChunk := false616 hasLength := false617 hasBegin := false618 hasEnd := false619 check := func(notExpected *bool, name string) {
8 unmodified lines
628 for _, str := range strings.Split(strings.ToLower(str), ",") {629 switch str {630 case "index":631 check(&hasIndex, "index")632 case "chunk":633 check(&hasChunk, "chunk")634 criteria = append(criteria, byChunk)635 case "length":636 check(&hasLength, "length")637 criteria = append(criteria, byLength)638 case "begin":
5 unmodified lines
644 default:645 errorExit("invalid sort criterion: " + str)646 }647 }648 if len(criteria) > 4 {649 errorExit("at most 3 tiebreaks are allowed: " + str)650 }651 return criteria652}653654func dupeTheme(theme *tui.ColorTheme) *tui.ColorTheme {
1129 unmodified lines
modified:src/result_test.go+20 added-3 removed
52 unmodified lines
53func TestResultRank(t *testing.T) {54 // FIXME global55 sortCriteria = []criterion{byScore, byLength}5657 strs := [][]rune{[]rune("foo"), []rune("foobar"), []rune("bar"), []rune("baz")}57 str := []rune("foo")58 item1 := buildResult(59 withIndex(&Item{text: util.RunesToChars(strs[0])}, 1), []Offset{}, 2)59 withIndex(&Item{text: util.RunesToChars(str)}, 1), []Offset{}, 2)60 if item1.points[3] != math.MaxUint16-2 || // Bonus61 item1.points[2] != 3 || // Length62 item1.points[1] != 0 || // Unused63 item1.points[0] != 0 || // Unused64 item1.item.Index() != 1 {65 t.Error(item1)66 }67 // Only differ in index68 item2 := buildResult(&Item{text: util.RunesToChars(strs[0])}, []Offset{}, 2)68 item2 := buildResult(&Item{text: util.RunesToChars(str)}, []Offset{}, 2)6970 items := []Result{item1, item2}71 sort.Sort(ByRelevance(items))72 if items[0] != item2 || items[1] != item1 {
24 unmodified lines
97 t.Error(items, item1, item2, item3, item4, item5, item6)98 }99}100101func TestChunkTiebreak(t *testing.T) {102 // FIXME global103 sortCriteria = []criterion{byScore, byChunk}104105 score := 100106 test := func(input string, offset Offset, chunk string) {107 item := buildResult(withIndex(&Item{text: util.RunesToChars([]rune(input))}, 1), []Offset{offset}, score)108 if !(item.points[3] == math.MaxUint16-uint16(score) && item.points[2] == uint16(len(chunk))) {109 t.Error(item.points)110 }111 }112 test("hello foobar goodbye", Offset{8, 9}, "foobar")113 test("hello foobar goodbye", Offset{7, 18}, "foobar goodbye")114 test("hello foobar goodbye", Offset{0, 1}, "hello")115 test("hello foobar goodbye", Offset{5, 7}, "hello foobar") // TBD116}117118func TestColorOffset(t *testing.T) {119 // ------------ 20 ---- -- ----120 // ++++++++ ++++++++++121 // --++++++++-- --++++++++++---
53 unmodified lines
modified:src/result.go+15 added
47 unmodified lines
48 switch criterion {49 case byScore:50 // Higher is better51 val = math.MaxUint16 - util.AsUint16(score)52 case byChunk:53 b := minBegin54 e := maxEnd55 l := item.text.Length()56 for ; b >= 1; b-- {57 if unicode.IsSpace(item.text.Get(b - 1)) {58 break59 }60 }61 for ; e < l; e++ {62 if unicode.IsSpace(item.text.Get(e)) {63 break64 }65 }66 val = util.AsUint16(e - b)67 case byLength:68 val = item.TrimLength()69 case byBegin, byEnd:70 if validOffsetFound {
188 unmodified lines
modified:test/test_go.rb+14 added
752 unmodified lines
753 # Since 0.16.8, --nth doesn't affect --tiebreak754 assert_equal output, `#{FZF} -fh -n2 -d: < #{tempname}`.lines(chomp: true)755 end756757 def test_tiebreak_chunk758 writelines(tempname, [759 '1 foobarbaz baz',760 '2 foobar baz',761 '3 foo barbaz'762 ])763764 assert_equal [765 '3 foo barbaz',766 '2 foobar baz',767 '1 foobarbaz baz'768 ], `#{FZF} -fo --tiebreak=chunk < #{tempname}`.lines(chomp: true)769 end770771 def test_invalid_cache772 tmux.send_keys "(echo d; echo D; echo x) | #{fzf('-q d')}", :Enter773 tmux.until { |lines| assert_equal ' 2/3', lines[-2] }774 tmux.send_keys :BSpace
1949 unmodified lines
modified:CHANGELOG.md+19 added-2 removed
7 unmodified lines
8 (listed higher to lower bonus point)9 - Word after whitespace characters or beginning of the string10 - Word after common delimiter characters (`/,:;|`)11 - Word after other non-word characters12 ````sh12 ```sh13 # foo/bar.sh` is preferred over `foo-bar.sh` on `bar`14 fzf --query bar --height 4 << EOF14 fzf --query=bar --height=4 << EOF15 foo-bar.sh16 foo/bar.sh17 EOF18 ```19- Added a new tiebreak `chunk`20 - Favors the line with shorter matched chunk. A chunk is a set of21 consecutive non-whitespace characters.22 - Unlike the default `length`, this scheme works well with tabular input23 ```sh24 # length prefers item #1, because the whole line is shorter,25 # chunk prefers item #2, because the matched chunk ("foo") is shorter26 fzf --height=6 --header-lines=2 --tiebreak=chunk --reverse --query=fo << "EOF"27 N | Field1 | Field2 | Field328 - | ------ | ------ | ------29 1 | hello | foobar | baz30 2 | world | foo | bazbaz31 EOF32 ```33 - If the input does not contain any spaces, `chunk` is equivalent to34 `length`. But we're not going to set it as the default because it is35 computationally more expensive.36- Bug fixes and improvements37380.31.039------
1236 unmodified lines