Use trimmed length when --nth is used with --tiebreak=length

This change improves sort ordering for aligned tabular input.
Given the following input:

    apple   juice   100
    apple   pie     200

fzf --nth=2 will now prefer the one with pie. Before this change fzf
compared "juice   " and "pie     ", both of which have the same length.
This commit is contained in:
Junegunn Choi
2015-10-02 18:40:20 +09:00
parent 7c7a30c472
commit 92a75c9563
7 changed files with 124 additions and 28 deletions

View File

@@ -20,6 +20,7 @@ type Range struct {
type Token struct {
text []rune
prefixLength int
trimLength int
}
// Delimiter for tokenizing the input
@@ -81,7 +82,7 @@ func withPrefixLengths(tokens [][]rune, begin int) []Token {
for idx, token := range tokens {
// Need to define a new local variable instead of the reused token to take
// the pointer to it
ret[idx] = Token{text: token, prefixLength: prefixLength}
ret[idx] = Token{token, prefixLength, util.TrimLen(token)}
prefixLength += len(token)
}
return ret
@@ -233,7 +234,7 @@ func Transform(tokens []Token, withNth []Range) []Token {
} else {
prefixLength = 0
}
transTokens[idx] = Token{part, prefixLength}
transTokens[idx] = Token{part, prefixLength, util.TrimLen(part)}
}
return transTokens
}