-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
43 lines (36 loc) · 1.08 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"errors"
"regexp"
"strings"
)
var safeFilenameRegex = regexp.MustCompile(`[^\p{L}\p{N}-_+=]+`)
var repeatedHyphens = regexp.MustCompile(`--+`)
var extraRegex = regexp.MustCompile(`%![(]EXTRA string=.*$`)
func convertApplicationName(input string) string {
output := extraRegex.ReplaceAllString(input, "")
output = strings.ToLower(output)
output = safeFilenameRegex.ReplaceAllString(output, "-")
output = repeatedHyphens.ReplaceAllString(output, "-")
return strings.Trim(output, "-")
}
func getVarFromXdotool(output []byte, variable string) (string, error) {
re := regexp.MustCompile(variable + "=([0-9]+)\n")
matches := re.FindSubmatch(output)
if matches == nil {
return "",
errors.New("Variable " + variable + " not found in xdotool output")
}
return string(matches[1]), nil
}
// Naive linear search, fine for small numbers of items
// User would have to add tens of thousands of items to their configs for this
// to be noticeable
func contains(ss []string, s string) bool {
for _, st := range ss {
if st == s {
return true
}
}
return false
}