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 // 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 // FSize gets the size of a file
59 func FSize(f *os.File) int64 {
65 // IsWordChar returns whether or not the string is a 'word character'
66 // If it is a unicode character, then it does not match
67 // Word characters are defined as [A-Za-z0-9_]
68 func IsWordChar(str string) bool {
74 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '_')
77 // IsWhitespace returns true if the given rune is a space, tab, or newline
78 func IsWhitespace(c rune) bool {
79 return c == ' ' || c == '\t' || c == '\n'
82 // IsStrWhitespace returns true if the given string is all whitespace
83 func IsStrWhitespace(str string) bool {
84 for _, c := range str {
92 // Contains returns whether or not a string array contains a given string
93 func Contains(list []string, a string) bool {
94 for _, b := range list {
102 // Insert makes a simple insert into a string at the given position
103 func Insert(str string, pos int, value string) string {
104 return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:])
107 // MakeRelative will attempt to make a relative path between path and base
108 func MakeRelative(path, base string) (string, error) {
110 rel, err := filepath.Rel(base, path)
119 // GetLeadingWhitespace returns the leading whitespace of the given string
120 func GetLeadingWhitespace(str string) string {
122 for _, c := range str {
123 if c == ' ' || c == '\t' {
132 // IsSpaces checks if a given string is only spaces
133 func IsSpaces(str string) bool {
134 for _, c := range str {
143 // IsSpacesOrTabs checks if a given string contains only spaces and tabs
144 func IsSpacesOrTabs(str string) bool {
145 for _, c := range str {
146 if c != ' ' && c != '\t' {
154 // ParseBool is almost exactly like strconv.ParseBool, except it also accepts 'on' and 'off'
155 // as 'true' and 'false' respectively
156 func ParseBool(str string) (bool, error) {
163 return strconv.ParseBool(str)
166 // EscapePath replaces every path separator in a given path with a %
167 func EscapePath(path string) string {
168 path = filepath.ToSlash(path)
169 return strings.Replace(path, "/", "%", -1)
172 // GetModTime returns the last modification time for a given file
173 // It also returns a boolean if there was a problem accessing the file
174 func GetModTime(path string) (time.Time, bool) {
175 info, err := os.Stat(path)
177 return time.Now(), false
179 return info.ModTime(), true
182 // StringWidth returns the width of a string where tabs count as `tabsize` width
183 func StringWidth(str string, tabsize int) int {
184 sw := runewidth.StringWidth(str)
186 for _, ch := range str {
189 ts := tabsize - (lineIdx % tabsize)
201 // WidthOfLargeRunes searches all the runes in a string and counts up all the widths of runes
202 // that have a width larger than 1 (this also counts tabs as `tabsize` width)
203 func WidthOfLargeRunes(str string, tabsize int) int {
206 for _, ch := range str {
209 w = tabsize - (lineIdx % tabsize)
211 w = runewidth.RuneWidth(ch)
225 // RunePos returns the rune index of a given byte index
226 // This could cause problems if the byte index is between code points
227 func runePos(p int, str string) int {
228 return utf8.RuneCountInString(str[:p])
231 func lcs(a, b string) string {
236 for i, r := range arunes {
237 if i >= len(brunes) {
249 // CommonSubstring gets a common substring among the inputs
250 func CommonSubstring(arr ...string) string {
253 for _, str := range arr[1:] {
254 commonStr = lcs(commonStr, str)
260 // Abs is a simple absolute value function for ints
261 func Abs(n int) int {
268 // FuncName returns the full name of a given function object
269 func FuncName(i interface{}) string {
270 return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
273 // ShortFuncName returns the name only of a given function object
274 func ShortFuncName(i interface{}) string {
275 return strings.TrimPrefix(runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name(), "main.(*View).")
278 // ReplaceHome takes a path as input and replaces ~ at the start of the path with the user's
279 // home directory. Does nothing if the path does not start with '~'.
280 func ReplaceHome(path string) string {
281 if !strings.HasPrefix(path, "~") {
285 home, err := homedir.Dir()
287 messenger.Error("Could not find home directory: ", err)
290 return strings.Replace(path, "~", home, 1)