diff options
| author | Junegunn Choi <junegunn.c@gmail.com> | 2022-03-29 21:35:36 +0900 |
|---|---|---|
| committer | Junegunn Choi <junegunn.c@gmail.com> | 2022-03-29 21:35:36 +0900 |
| commit | ef67a45702c01ff93e0ea99a51594c8160f66cc1 (patch) | |
| tree | f7339a0133e7328dd8fbafdf56d8fc5e0dadfabd /src/util/util.go | |
| parent | b88eb72ac29b92c82a0d7c7f8d7b65380720b02c (diff) | |
| download | fzf-ef67a45702c01ff93e0ea99a51594c8160f66cc1.tar.gz | |
Add --ellipsis=.. option
Close #2432
Also see
- #1769
- https://github.com/junegunn/fzf/pull/1844#issuecomment-586663660
Diffstat (limited to 'src/util/util.go')
| -rw-r--r-- | src/util/util.go | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/util/util.go b/src/util/util.go index c3995bfd..a1c37f7a 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -34,6 +34,23 @@ func RunesWidth(runes []rune, prefixWidth int, tabstop int, limit int) (int, int return width, -1 } +// Truncate returns the truncated runes and its width +func Truncate(input string, limit int) ([]rune, int) { + runes := []rune{} + width := 0 + gr := uniseg.NewGraphemes(input) + for gr.Next() { + rs := gr.Runes() + w := runewidth.StringWidth(string(rs)) + if width+w > limit { + return runes, width + } + width += w + runes = append(runes, rs...) + } + return runes, width +} + // Max returns the largest integer func Max(first int, second int) int { if first >= second { |
