14 "github.com/mattn/go-runewidth"
17 // Util.go is a collection of utility functions that are used throughout
20 // Count returns the length of a string in runes
21 // This is exactly equivalent to utf8.RuneCountInString(), just less characters
22 func Count(s string) int {
23 return utf8.RuneCountInString(s)
26 // NumOccurrences counts the number of occurrences of a byte in a string
27 func NumOccurrences(s string, c byte) int {
29 for i := 0; i < len(s); i++ {
37 // Spaces returns a string with n spaces
38 func Spaces(n int) string {
39 return strings.Repeat(" ", n)
42 // Min takes the min of two ints
43 func Min(a, b int) int {
50 // Max takes the max of two ints
51 func Max(a, b int) int {
58 func FSize(f *os.File) int64 {
64 // IsWordChar returns whether or not the string is a 'word character'
65 // If it is a unicode character, then it does not match
66 // Word characters are defined as [A-Za-z0-9_]
67 func IsWordChar(str string) bool {
73 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '_')
76 // IsWhitespace returns true if the given rune is a space, tab, or newline
77 func IsWhitespace(c rune) bool {
78 return c == ' ' || c == '\t' || c == '\n'
81 // IsStrWhitespace returns true if the given string is all whitespace
82 func IsStrWhitespace(str string) bool {
83 for _, c := range str {
91 // Contains returns whether or not a string array contains a given string
92 func Contains(list []string, a string) bool {
93 for _, b := range list {
101 // Insert makes a simple insert into a string at the given position
102 func Insert(str string, pos int, value string) string {
103 return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:])
106 // MakeRelative will attempt to make a relative path between path and base
107 func MakeRelative(path, base string) (string, error) {
109 rel, err := filepath.Rel(base, path)
118 // GetLeadingWhitespace returns the leading whitespace of the given string
119 func GetLeadingWhitespace(str string) string {
121 for _, c := range str {
122 if c == ' ' || c == '\t' {
131 // IsSpaces checks if a given string is only spaces
132 func IsSpaces(str string) bool {
133 for _, c := range str {
142 // IsSpacesOrTabs checks if a given string contains only spaces and tabs
143 func IsSpacesOrTabs(str string) bool {
144 for _, c := range str {
145 if c != ' ' && c != '\t' {
153 // ParseBool is almost exactly like strconv.ParseBool, except it also accepts 'on' and 'off'
154 // as 'true' and 'false' respectively
155 func ParseBool(str string) (bool, error) {
162 return strconv.ParseBool(str)
165 // EscapePath replaces every path separator in a given path with a %
166 func EscapePath(path string) string {
167 path = filepath.ToSlash(path)
168 return strings.Replace(path, "/", "%", -1)
171 // GetModTime returns the last modification time for a given file
172 // It also returns a boolean if there was a problem accessing the file
173 func GetModTime(path string) (time.Time, bool) {
174 info, err := os.Stat(path)
176 return time.Now(), false
178 return info.ModTime(), true
181 // StringWidth returns the width of a string where tabs count as `tabsize` width
182 func StringWidth(str string, tabsize int) int {
183 sw := runewidth.StringWidth(str)
185 for _, ch := range str {
188 ts := tabsize - (lineIdx % tabsize)
200 // WidthOfLargeRunes searches all the runes in a string and counts up all the widths of runes
201 // that have a width larger than 1 (this also counts tabs as `tabsize` width)
202 func WidthOfLargeRunes(str string, tabsize int) int {
205 for _, ch := range str {
208 w = tabsize - (lineIdx % tabsize)
210 w = runewidth.RuneWidth(ch)
224 // RunePos returns the rune index of a given byte index
225 // This could cause problems if the byte index is between code points
226 func runePos(p int, str string) int {
227 return utf8.RuneCountInString(str[:p])
230 func lcs(a, b string) string {
235 for i, r := range arunes {
236 if i >= len(brunes) {
248 func CommonSubstring(arr ...string) string {
251 for _, str := range arr[1:] {
252 commonStr = lcs(commonStr, str)
258 // Abs is a simple absolute value function for ints
259 func Abs(n int) int {
266 // FuncName returns the full name of a given function object
267 func FuncName(i interface{}) string {
268 return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
271 // ShortFuncName returns the name only of a given function object
272 func ShortFuncName(i interface{}) string {
273 return strings.TrimPrefix(runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name(), "main.(*View).")
276 // SplitCommandArgs separates multiple command arguments which may be quoted.
277 // The returned slice contains at least one string
278 func SplitCommandArgs(input string) []string {
280 var curQuote *bytes.Buffer
282 curArg := new(bytes.Buffer)
285 finishQuote := func() {
289 str := curQuote.String()
290 if unquoted, err := strconv.Unquote(str); err == nil {
293 curArg.WriteString(str)
297 appendResult := func() {
301 str := curArg.String()
302 result = append(result, str)
306 for _, r := range input {
307 if r == ' ' && curQuote == nil {
311 appendRuneToBuff := func() {
313 curQuote.WriteRune(r)
320 if r == '"' && curQuote == nil {
321 curQuote = new(bytes.Buffer)
324 if curQuote != nil && !escape {
328 } else if r == '\\' {
346 // JoinCommandArgs joins multiple command arguments and quote the strings if needed.
347 func JoinCommandArgs(args ...string) string {
348 buf := new(bytes.Buffer)
350 for _, arg := range args {
356 quoted := strconv.Quote(arg)
357 if quoted[1:len(quoted)-1] != arg || strings.ContainsRune(arg, ' ') {
358 buf.WriteString(quoted)