Proper support for double width characters

Fixes #99
This commit is contained in:
Zachary Yedidia
2016-06-04 16:25:11 -04:00
parent 852bd2c904
commit d6307b2718
3 changed files with 42 additions and 12 deletions

View File

@@ -7,6 +7,8 @@ import (
"strings"
"time"
"unicode/utf8"
"github.com/mattn/go-runewidth"
)
// Util.go is a collection of utility functions that are used throughout
@@ -138,6 +140,28 @@ func GetModTime(path string) (time.Time, bool) {
return info.ModTime(), true
}
func StringWidth(str string) int {
sw := runewidth.StringWidth(str)
sw += NumOccurences(str, '\t') * (int(settings["tabsize"].(float64)) - 1)
return sw
}
func WidthOfLargeRunes(str string) int {
count := 0
for _, ch := range str {
var w int
if ch == '\t' {
w = int(settings["tabsize"].(float64))
} else {
w = runewidth.RuneWidth(ch)
}
if w > 1 {
count += (w - 1)
}
}
return count
}
func runePos(p int, str string) int {
return utf8.RuneCountInString(str[:p])
}