-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (69 loc) · 1.91 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"github.com/zhangtianhao/SourceTree-Workflow/homedir"
"howett.net/plist"
"io/ioutil"
"path"
"reflect"
"strings"
"unicode"
)
// SourceTreePlist {'$version': 100000, '$archiver': 'NSKeyedArchiver', '$top': {'root': Uid(1)}}
type SourceTreePlist struct {
Version uint `plist:"$version"`
Archiver string `plist:"$archiver"`
Top map[string]interface{} `plist:"$top"`
Objects []interface{} `plist:"$objects"`
}
func splitString(r rune) bool {
return r == '_' || r == '-' || unicode.IsSpace(r)
}
// k8s-docker-desktop-for-mac
// k8s docker desktop for mac
// k8s-docker k8s-docker-desktop k8s-docker-desktop-for k8s-docker-desktop-for-mac
// docker-desktop docker-desktop-for docker-desktop-for-mac
// desktop-for desktop-for-mac
// for-mac
func splitMatch(title string) string {
fragments := strings.FieldsFunc(title, splitString)
length := len(fragments)
for i := 0; i < length; i++ {
temp := fragments[i]
for j := i + 1; j < length; j++ {
temp = temp + "-" + fragments[j]
fragments = append(fragments, temp)
}
}
return strings.Join(fragments, " ")
}
func getList() *AlfredList {
dir, _ := homedir.Dir()
bs, _ := ioutil.ReadFile(path.Join(dir, "/Library/Application Support/SourceTree/browser.plist"))
var stp SourceTreePlist
_, _ = plist.Unmarshal(bs, &stp)
alfredSlice := make([]AlfredItem, 0, len(stp.Objects))
var tempName string
for _, obj := range stp.Objects {
typeName := reflect.TypeOf(obj).Name()
if typeName == "string" {
item := reflect.ValueOf(obj).String()
if item[:1] == "/" {
match := splitMatch(tempName)
alfredSlice = append(alfredSlice, AlfredItem{
Title: tempName,
Subtitle: item,
Arg: item,
Match: match,
})
} else {
tempName = item
}
}
}
return &AlfredList{Items: alfredSlice}
}
func main() {
alfredList := getList()
fmt.Print(alfredList.ToJson())
}