X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=cmd%2Fmicro%2Futil.go;h=24a331844feb5b657c73836ae9d38878f9688d98;hb=a517ea45bd60770a46b8072b02ef8d7cc54a8379;hp=38d6ef72afaef2756cfd2d3d193d56f1d72ddcfe;hpb=191fd5e495b645946b28f22233a2ac70fb0bb26e;p=micro.git diff --git a/cmd/micro/util.go b/cmd/micro/util.go index 38d6ef72..24a33184 100644 --- a/cmd/micro/util.go +++ b/cmd/micro/util.go @@ -23,7 +23,7 @@ func Count(s string) int { return utf8.RuneCountInString(s) } -// NumOccurrences counts the number of occurences of a byte in a string +// NumOccurrences counts the number of occurrences of a byte in a string func NumOccurrences(s string, c byte) int { var n int for i := 0; i < len(s); i++ { @@ -91,6 +91,18 @@ func Insert(str string, pos int, value string) string { return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:]) } +// MakeRelative will attempt to make a relative path between path and base +func MakeRelative(path, base string) (string, error) { + if len(path) > 0 { + rel, err := filepath.Rel(base, path) + if err != nil { + return path, err + } + return rel, nil + } + return path, nil +} + // GetLeadingWhitespace returns the leading whitespace of the given string func GetLeadingWhitespace(str string) string { ws := "" @@ -162,7 +174,8 @@ func StringWidth(str string, tabsize int) int { switch ch { case '\t': ts := tabsize - (lineIdx % tabsize) - sw += ts - 1 + sw += ts + lineIdx += ts case '\n': lineIdx = 0 default: @@ -190,7 +203,7 @@ func WidthOfLargeRunes(str string, tabsize int) int { if ch == '\n' { lineIdx = 0 } else { - lineIdx++ + lineIdx += w } } return count @@ -243,11 +256,11 @@ func FuncName(i interface{}) string { return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() } -// SplitCommandArgs seperates multiple command arguments which may be quoted. +// SplitCommandArgs separates multiple command arguments which may be quoted. // The returned slice contains at least one string func SplitCommandArgs(input string) []string { var result []string - var curQuote *bytes.Buffer = nil + var curQuote *bytes.Buffer curArg := new(bytes.Buffer) escape := false