package main
import (
+ "os"
+ "path/filepath"
"strconv"
+ "strings"
+ "time"
"unicode/utf8"
+
+ "github.com/mattn/go-runewidth"
)
// Util.go is a collection of utility functions that are used throughout
}
return strconv.ParseBool(str)
}
+
+// EscapePath replaces every path separator in a given path with a %
+func EscapePath(path string) string {
+ path = filepath.ToSlash(path)
+ return strings.Replace(path, "/", "%", -1)
+}
+
+// GetModTime returns the last modification time for a given file
+// It also returns a boolean if there was a problem accessing the file
+func GetModTime(path string) (time.Time, bool) {
+ info, err := os.Stat(path)
+ if err != nil {
+ return time.Now(), false
+ }
+ return info.ModTime(), true
+}
+
+// StringWidth returns the width of a string where tabs count as `tabsize` width
+func StringWidth(str string) int {
+ sw := runewidth.StringWidth(str)
+ sw += NumOccurences(str, '\t') * (int(settings["tabsize"].(float64)) - 1)
+ return sw
+}
+
+// WidthOfLargeRunes searches all the runes in a string and counts up all the widths of runes
+// that have a width larger than 1 (this also counts tabs as `tabsize` width)
+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
+}
+
+// RunePos returns the rune index of a given byte index
+// This could cause problems if the byte index is between code points
+func runePos(p int, str string) int {
+ return utf8.RuneCountInString(str[:p])
+}
+
+// Abs is a simple absolute value function for ints
+func Abs(n int) int {
+ if n < 0 {
+ return -n
+ }
+ return n
+}