From d7daf5f72411f29eca3ab398c7a8a951ca230cad Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 25 Mar 2023 10:23:05 +0900 Subject: =?UTF-8?q?Render=20CR=20and=20LF=20as=20=E2=90=8D=20and=20?= =?UTF-8?q?=E2=90=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Close #2529 --- src/util/util.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/util') 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 } -- cgit v1.2.3