13 "github.com/mattn/go-runewidth"
14 homedir "github.com/mitchellh/go-homedir"
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 // Convert byte array to rune array
27 func toRunes(b []byte) []rune {
28 runes := make([]rune, 0, utf8.RuneCount(b))
31 r, size := utf8.DecodeRune(b)
32 runes = append(runes, r)
40 // NumOccurrences counts the number of occurrences of a byte in a string
41 func NumOccurrences(s string, c byte) int {
43 for i := 0; i < len(s); i++ {
51 // Spaces returns a string with n spaces
52 func Spaces(n int) string {
53 return strings.Repeat(" ", n)
56 // Min takes the min of two ints
57 func Min(a, b int) int {
64 // Max takes the max of two ints
65 func Max(a, b int) int {
72 // FSize gets the size of a file
73 func FSize(f *os.File) int64 {
79 // IsWordChar returns whether or not the string is a 'word character'
80 // If it is a unicode character, then it does not match
81 // Word characters are defined as [A-Za-z0-9_]
82 func IsWordChar(str string) bool {
88 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '_')
91 // IsWhitespace returns true if the given rune is a space, tab, or newline
92 func IsWhitespace(c rune) bool {
93 return c == ' ' || c == '\t' || c == '\n'
96 // IsStrWhitespace returns true if the given string is all whitespace
97 func IsStrWhitespace(str string) bool {
98 for _, c := range str {
106 // Contains returns whether or not a string array contains a given string
107 func Contains(list []string, a string) bool {
108 for _, b := range list {
116 // Insert makes a simple insert into a string at the given position
117 func Insert(str string, pos int, value string) string {
118 return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:])
121 // MakeRelative will attempt to make a relative path between path and base
122 func MakeRelative(path, base string) (string, error) {
124 rel, err := filepath.Rel(base, path)
133 // GetLeadingWhitespace returns the leading whitespace of the given string
134 func GetLeadingWhitespace(str string) string {
136 for _, c := range str {
137 if c == ' ' || c == '\t' {
146 // IsSpaces checks if a given string is only spaces
147 func IsSpaces(str string) bool {
148 for _, c := range str {
157 // IsSpacesOrTabs checks if a given string contains only spaces and tabs
158 func IsSpacesOrTabs(str string) bool {
159 for _, c := range str {
160 if c != ' ' && c != '\t' {
168 // ParseBool is almost exactly like strconv.ParseBool, except it also accepts 'on' and 'off'
169 // as 'true' and 'false' respectively
170 func ParseBool(str string) (bool, error) {
177 return strconv.ParseBool(str)
180 // EscapePath replaces every path separator in a given path with a %
181 func EscapePath(path string) string {
182 path = filepath.ToSlash(path)
183 return strings.Replace(path, "/", "%", -1)
186 // GetModTime returns the last modification time for a given file
187 // It also returns a boolean if there was a problem accessing the file
188 func GetModTime(path string) (time.Time, bool) {
189 info, err := os.Stat(path)
191 return time.Now(), false
193 return info.ModTime(), true
196 // StringWidth returns the width of a string where tabs count as `tabsize` width
197 func StringWidth(str string, tabsize int) int {
198 sw := runewidth.StringWidth(str)
200 for _, ch := range str {
203 ts := tabsize - (lineIdx % tabsize)
215 // WidthOfLargeRunes searches all the runes in a string and counts up all the widths of runes
216 // that have a width larger than 1 (this also counts tabs as `tabsize` width)
217 func WidthOfLargeRunes(str string, tabsize int) int {
220 for _, ch := range str {
223 w = tabsize - (lineIdx % tabsize)
225 w = runewidth.RuneWidth(ch)
239 // RunePos returns the rune index of a given byte index
240 // This could cause problems if the byte index is between code points
241 func runePos(p int, str string) int {
242 return utf8.RuneCountInString(str[:p])
245 func lcs(a, b string) string {
250 for i, r := range arunes {
251 if i >= len(brunes) {
263 // CommonSubstring gets a common substring among the inputs
264 func CommonSubstring(arr ...string) string {
267 for _, str := range arr[1:] {
268 commonStr = lcs(commonStr, str)
274 // Abs is a simple absolute value function for ints
275 func Abs(n int) int {
282 // FuncName returns the full name of a given function object
283 func FuncName(i interface{}) string {
284 return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
287 // ShortFuncName returns the name only of a given function object
288 func ShortFuncName(i interface{}) string {
289 return strings.TrimPrefix(runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name(), "main.(*View).")
292 // ReplaceHome takes a path as input and replaces ~ at the start of the path with the user's
293 // home directory. Does nothing if the path does not start with '~'.
294 func ReplaceHome(path string) string {
295 if !strings.HasPrefix(path, "~") {
299 home, err := homedir.Dir()
301 messenger.Error("Could not find home directory: ", err)
304 return strings.Replace(path, "~", home, 1)