summaryrefslogtreecommitdiff
path: root/src/util/chars.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2024-05-20 01:33:33 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2024-05-20 09:25:30 +0900
commit04db44067dd4d9bf7f85ae4d704e740d7420f957 (patch)
treeccb739f25b4e79d8d03ec076042ad65cd62efe4e /src/util/chars.go
parent5b204c54f9d16accdf66bb24477e9eff4fc3a21a (diff)
downloadfzf-04db44067dd4d9bf7f85ae4d704e740d7420f957.tar.gz
Implement multi-line display of multi-line items
Diffstat (limited to 'src/util/chars.go')
-rw-r--r--src/util/chars.go30
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