diff options
| author | Junegunn Choi <junegunn.c@gmail.com> | 2015-10-02 18:40:20 +0900 |
|---|---|---|
| committer | Junegunn Choi <junegunn.c@gmail.com> | 2015-10-02 18:40:20 +0900 |
| commit | 92a75c9563600a174e9ee8334853f99ed560492a (patch) | |
| tree | c65a17633ee57dbfbbafa4b351c41bbbfffa3f9f /src/util | |
| parent | 7c7a30c472463e0115adcf8bc2a792b48c03bf08 (diff) | |
| download | fzf-92a75c9563600a174e9ee8334853f99ed560492a.tar.gz | |
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.
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/util.go | 26 | ||||
| -rw-r--r-- | src/util/util_test.go | 20 |
2 files changed, 46 insertions, 0 deletions
diff --git a/src/util/util.go b/src/util/util.go index aa5f227c..e7e4f313 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -75,6 +75,7 @@ func IsTty() bool { return int(C.isatty(C.int(os.Stdin.Fd()))) != 0 } +// TrimRight returns rune array with trailing white spaces cut off func TrimRight(runes []rune) []rune { var i int for i = len(runes) - 1; i >= 0; i-- { @@ -86,6 +87,7 @@ func TrimRight(runes []rune) []rune { return runes[0 : i+1] } +// BytesToRunes converts byte array into rune array func BytesToRunes(bytea []byte) []rune { runes := make([]rune, 0, len(bytea)) for i := 0; i < len(bytea); { @@ -100,3 +102,27 @@ func BytesToRunes(bytea []byte) []rune { } return runes } + +// TrimLen returns the length of trimmed rune array +func TrimLen(runes []rune) int { + var i int + for i = len(runes) - 1; i >= 0; i-- { + char := runes[i] + if char != ' ' && char != '\t' { + break + } + } + // Completely empty + if i < 0 { + return 0 + } + + var j int + for j = 0; j < len(runes); j++ { + char := runes[j] + if char != ' ' && char != '\t' { + break + } + } + return i - j + 1 +} diff --git a/src/util/util_test.go b/src/util/util_test.go index 06cfd4f2..8aeaeac5 100644 --- a/src/util/util_test.go +++ b/src/util/util_test.go @@ -20,3 +20,23 @@ func TestContrain(t *testing.T) { t.Error("Expected", 3) } } + +func TestTrimLen(t *testing.T) { + check := func(str string, exp int) { + trimmed := TrimLen([]rune(str)) + if trimmed != exp { + t.Errorf("Invalid TrimLen result for '%s': %d (expected %d)", + str, trimmed, exp) + } + } + check("hello", 5) + check("hello ", 5) + check("hello ", 5) + check(" hello", 5) + check(" hello", 5) + check(" hello ", 5) + check(" hello ", 5) + check("h o", 5) + check(" h o ", 5) + check(" ", 0) +} |
