diff options
| author | Junegunn Choi <junegunn.c@gmail.com> | 2024-05-20 01:33:33 +0900 |
|---|---|---|
| committer | Junegunn Choi <junegunn.c@gmail.com> | 2024-05-20 09:25:30 +0900 |
| commit | 04db44067dd4d9bf7f85ae4d704e740d7420f957 (patch) | |
| tree | ccb739f25b4e79d8d03ec076042ad65cd62efe4e /src/util/chars.go | |
| parent | 5b204c54f9d16accdf66bb24477e9eff4fc3a21a (diff) | |
| download | fzf-04db44067dd4d9bf7f85ae4d704e740d7420f957.tar.gz | |
Implement multi-line display of multi-line items
Diffstat (limited to 'src/util/chars.go')
| -rw-r--r-- | src/util/chars.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/util/chars.go b/src/util/chars.go index f84d365f..7c706ae8 100644 --- a/src/util/chars.go +++ b/src/util/chars.go @@ -1,6 +1,7 @@ package util import ( + "bytes" "fmt" "unicode" "unicode/utf8" @@ -74,6 +75,35 @@ func (chars *Chars) Bytes() []byte { return chars.slice } +func (chars *Chars) NumLines(atMost int) int { + lines := 1 + if runes := chars.optionalRunes(); runes != nil { + for _, r := range runes { + if r == '\n' { + lines++ + } + if lines >= atMost { + return atMost + } + } + return lines + } + + for idx := 0; idx < len(chars.slice); idx++ { + found := bytes.IndexByte(chars.slice[idx:], '\n') + if found < 0 { + break + } + + idx += found + lines++ + if lines >= atMost { + return atMost + } + } + return lines +} + func (chars *Chars) optionalRunes() []rune { if chars.inBytes { return nil |
