]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/util.go
Fix draw ordering
[micro.git] / cmd / micro / util.go
index 63b0a040942d00c6a268c7deb548a9f36cddcabd..f5d77abc16c5aaedb9a2868eecc514d3e2de5454 100644 (file)
@@ -1,10 +1,14 @@
 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
@@ -126,6 +130,51 @@ func EscapePath(path string) string {
        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
+}