]> git.lizzy.rs Git - micro.git/blobdiff - internal/util/util.go
Update documentation to include Material colorscheme (#1279)
[micro.git] / internal / util / util.go
index 0d23028e7db21654655a25f8cd97eb4e05988867..485d4f55276d691fef24383320c4c7c77a7d5c33 100644 (file)
@@ -2,18 +2,52 @@ package util
 
 import (
        "errors"
+       "fmt"
        "os"
        "os/user"
        "path/filepath"
        "regexp"
+       "runtime"
        "strconv"
        "strings"
        "time"
+       "unicode"
        "unicode/utf8"
 
+       "github.com/blang/semver"
        runewidth "github.com/mattn/go-runewidth"
 )
 
+var (
+       // These variables should be set by the linker when compiling
+
+       // Version is the version number or commit hash
+       Version = "0.0.0-unknown"
+       // Semantic version
+       SemVersion semver.Version
+       // CommitHash is the commit this version was built on
+       CommitHash = "Unknown"
+       // CompileDate is the date this binary was compiled on
+       CompileDate = "Unknown"
+       // Debug logging
+       Debug = "ON"
+       // FakeCursor is used to disable the terminal cursor and have micro
+       // draw its own (enabled for windows consoles where the cursor is slow)
+       FakeCursor = false
+)
+
+func init() {
+       var err error
+       SemVersion, err = semver.Make(Version)
+       if err != nil {
+               fmt.Println("Invalid version: ", Version, err)
+       }
+
+       if runtime.GOOS == "windows" {
+               FakeCursor = true
+       }
+}
+
 // SliceEnd returns a byte slice where the index is a rune index
 // Slices off the start of the slice
 func SliceEnd(slc []byte, index int) []byte {
@@ -176,10 +210,9 @@ func FSize(f *os.File) int64 {
 }
 
 // IsWordChar returns whether or not the string is a 'word character'
-// If it is a unicode character, then it does not match
-// Word characters are defined as [A-Za-z0-9_]
+// Word characters are defined as numbers, letters, or '_'
 func IsWordChar(r rune) bool {
-       return (r >= '0' && r <= '9') || (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r == '_')
+       return unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_'
 }
 
 // Spaces returns a string with n spaces
@@ -211,14 +244,13 @@ func IsSpacesOrTabs(str []byte) bool {
 
 // IsWhitespace returns true if the given rune is a space, tab, or newline
 func IsWhitespace(c rune) bool {
-       return c == ' ' || c == '\t' || c == '\n'
+       return unicode.IsSpace(c)
 }
 
-// IsStrWhitespace returns true if the given string is all whitespace
-func IsStrWhitespace(str string) bool {
-       // Range loop for unicode correctness
-       for _, c := range str {
-               if !IsWhitespace(c) {
+// IsBytesWhitespace returns true if the given bytes are all whitespace
+func IsBytesWhitespace(b []byte) bool {
+       for _, c := range b {
+               if !IsWhitespace(rune(c)) {
                        return false
                }
        }
@@ -243,7 +275,6 @@ func MakeRelative(path, base string) (string, error) {
        return path, nil
 }
 
-// TODO: consider changing because of snap segfault
 // ReplaceHome takes a path as input and replaces ~ at the start of the path with the user's
 // home directory. Does nothing if the path does not start with '~'.
 func ReplaceHome(path string) (string, error) {
@@ -329,7 +360,6 @@ func IntOpt(opt interface{}) int {
 // coordinate (this is necessary because tabs are 1 char but
 // 4 visual spaces)
 func GetCharPosInLine(b []byte, visualPos int, tabsize int) int {
-
        // Scan rune by rune until we exceed the visual width that we are
        // looking for. Then we can return the character position we have found
        i := 0     // char pos
@@ -379,3 +409,16 @@ func Clamp(val, min, max int) int {
        }
        return val
 }
+
+func IsNonAlphaNumeric(c rune) bool {
+       return !unicode.IsLetter(c) && !unicode.IsNumber(c)
+}
+
+func ParseSpecial(s string) string {
+       return strings.Replace(s, "\\t", "\t", -1)
+}
+
+// String converts a byte array to a string (for lua plugins)
+func String(s []byte) string {
+       return string(s)
+}