summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2023-03-25 10:23:05 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2023-03-25 10:41:19 +0900
commitd7daf5f72411f29eca3ab398c7a8a951ca230cad (patch)
tree4ac325a32da748f3ecf447972726cafdf502a0bb /src/util
parente5103d94290eb9d65807a9f1ecee673bd9ce7cc2 (diff)
Render CR and LF as ␍ and ␊
Close #2529
Diffstat (limited to 'src/util')
-rw-r--r--src/util/util.go10
1 files changed, 7 insertions, 3 deletions
diff --git a/src/util/util.go b/src/util/util.go
index cb211cbb..73f53daf 100644
--- a/src/util/util.go
+++ b/src/util/util.go
@@ -11,6 +11,11 @@ import (
"github.com/rivo/uniseg"
)
+// StringWidth returns string width where each CR/LF character takes 1 column
+func StringWidth(s string) int {
+ return runewidth.StringWidth(s) + strings.Count(s, "\n") + strings.Count(s, "\r")
+}
+
// RunesWidth returns runes width
func RunesWidth(runes []rune, prefixWidth int, tabstop int, limit int) (int, int) {
width := 0
@@ -22,8 +27,7 @@ func RunesWidth(runes []rune, prefixWidth int, tabstop int, limit int) (int, int
if len(rs) == 1 && rs[0] == '\t' {
w = tabstop - (prefixWidth+width)%tabstop
} else {
- s := string(rs)
- w = runewidth.StringWidth(s) + strings.Count(s, "\n")
+ w = StringWidth(string(rs))
}
width += w
if width > limit {
@@ -41,7 +45,7 @@ func Truncate(input string, limit int) ([]rune, int) {
gr := uniseg.NewGraphemes(input)
for gr.Next() {
rs := gr.Runes()
- w := runewidth.StringWidth(string(rs))
+ w := StringWidth(string(rs))
if width+w > limit {
return runes, width
}